No Description

USART.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*--------------------------------------------------------------------------------------------------
  2. Name : USART.c
  3. Description : Utility functions for ATmega8 USART.
  4. Author : 2004-10-22 - Louis Frigon
  5. History : 2004-10-22 - First release (v0.1).
  6. --------------------------------------------------------------------------------------------------*/
  7. #include <avr/io.h>
  8. #include <avr/interrupt.h>
  9. #include <avr/pgmspace.h>
  10. #include <stdio.h>
  11. #include "USART.h"
  12. /*--------------------------------------------------------------------------------------------------
  13. Name : InitUSART
  14. Description : Performs USART initialization: 9600,N,8,1.
  15. Argument(s) : None.
  16. Return value : None.
  17. --------------------------------------------------------------------------------------------------*/
  18. void InitUSART ( void )
  19. {
  20. // Disable USART while setting baud rate.
  21. UCSR0B = 0x00;
  22. UCSR0A = 0x00;
  23. // 8 data bit, 1 stop, no parity.
  24. UCSR0C = _BV(EEAR7) | _BV(UCSZ01) | _BV(UCSZ00);
  25. // Set USART baud rate @ 9600. Divider is 103 @ 16 MHz.
  26. UBRR0L = 103;
  27. // Enable internal pull-up on Rx pin.
  28. PORTD |= _BV(PD0);
  29. // Enable Tx & Rx.
  30. UCSR0B = _BV(RXEN0) | _BV(TXEN0);
  31. }
  32. /*--------------------------------------------------------------------------------------------------
  33. Name : UsartIsChr
  34. Description : Return status of USART Rx buffer.
  35. Argument(s) : None.
  36. Return value : 0 if Rx buffer empty.
  37. --------------------------------------------------------------------------------------------------*/
  38. bool UsartIsChr ( void )
  39. {
  40. return UCSR0A & _BV(RXC0);
  41. }
  42. /*--------------------------------------------------------------------------------------------------
  43. Name : UsartGetChr
  44. Description : Return character USART Rx buffer. Blocking until Rx buffer not empty.
  45. Argument(s) : None.
  46. Return value : Character in Rx buffer.
  47. --------------------------------------------------------------------------------------------------*/
  48. char UsartGetChr ( void )
  49. {
  50. while ( !UsartIsChr() );
  51. return UDR0;
  52. }
  53. /*--------------------------------------------------------------------------------------------------
  54. Name : UsartPutChr
  55. Description : Send a character through the USART.
  56. Argument(s) : c -> char to send.
  57. Return value : None.
  58. --------------------------------------------------------------------------------------------------*/
  59. void UsartPutChr ( char c )
  60. {
  61. // Wait for transmit register to be empty.
  62. while ( !(UCSR0A & _BV(UDRE0)) );
  63. UDR0 = c;
  64. }
  65. /*--------------------------------------------------------------------------------------------------
  66. Name : UsartPutStr
  67. Description : Transmit a string on the serial port.
  68. Argument(s) : str -> pointer to string to send.
  69. Return value : None.
  70. --------------------------------------------------------------------------------------------------*/
  71. void UsartPutStr ( char *str )
  72. {
  73. while ( *str )
  74. {
  75. UsartPutChr( *str++ );
  76. }
  77. }
  78. /*--------------------------------------------------------------------------------------------------
  79. Name : UsartPutCStr
  80. Description : Transmit a string on the serial port.
  81. Argument(s) : str -> pointer to constant string to send (strings in Flash).
  82. Return value : None.
  83. --------------------------------------------------------------------------------------------------*/
  84. void UsartPutCStr ( const char *str )
  85. {
  86. char c;
  87. while ( (c = pgm_read_byte_near( str++ )) )
  88. {
  89. UsartPutChr( c );
  90. }
  91. }
  92. /*--------------------------------------------------------------------------------------------------
  93. End of file.
  94. --------------------------------------------------------------------------------------------------*/