» Advertenties

Zo 20 Mei 2012, 07:54

C code -
  1. //*****************************************************************************
  2. //
  3. // File Name : uart.h
  4. // Title : Hardware UART
  5. // Author : Smoerijf.be (Orinal by Pascal Stang - edit by smoerijf)
  6. // Target MCU : Atmel AVR Series
  7. //
  8. //*****************************************************************************
  9. //@{
  10.  
  11. #ifndef UART_H
  12. #define UART_H
  13.  
  14. #define UART_TX           0x01
  15. #define UART_RX           0x02
  16. #define UART_FULL         0x03
  17.  
  18. // compatibility with most newer processors
  19. #ifdef UCSRB
  20.   #define UCR           UCSRB
  21. #endif
  22. // compatibility with old Mega processors
  23. #if defined(UBRR) && !defined(UBRRL)
  24.   #define   UBRRL         UBRR
  25. #endif
  26. // compatibility with megaXX8 processors
  27. #if   defined(__AVR_ATmega88__)   || \
  28.   defined(__AVR_ATmega168__)   || \
  29.   defined(__AVR_ATmega644__)
  30.   #define UDR           UDR0
  31.   #define UCR           UCSR0B
  32.   #define RXCIE         RXCIE0
  33.   #define TXCIE         TXCIE0
  34.   #define RXC           RXC0
  35.   #define TXC           TXC0
  36.   #define RXEN         RXEN0
  37.   #define TXEN         TXEN0
  38.   #define UBRRL         UBRR0L
  39.   #define UBRRH         UBRR0H
  40.   #define UCSRA         UCSR0A
  41.   #define UDRE         UDRE0
  42.   #define SIG_UART_TRANS     SIG_USART_TRANS
  43.   #define SIG_UART_RECV     SIG_USART_RECV
  44.   #define SIG_UART_DATA     SIG_USART_DATA
  45. #endif
  46. // compatibility with mega169 processors
  47. #if   defined(__AVR_ATmega169__)
  48.   #define SIG_UART_TRANS     SIG_USART_TRANS
  49.   #define SIG_UART_RECV     SIG_USART_RECV
  50.   #define SIG_UART_DATA     SIG_USART_DATA
  51. #endif
  52. // compatibility with dual-uart processors
  53. // (if you need to use both uarts, please use the uart2 library)
  54. #if defined(__AVR_ATmega161__)
  55.   #define UDR           UDR0
  56.   #define UCR           UCSR0B
  57.   #define UBRRL         UBRR0
  58.   #define SIG_UART_TRANS     SIG_UART0_TRANS
  59.   #define SIG_UART_RECV     SIG_UART0_RECV
  60.   #define SIG_UART_DATA     SIG_UART0_DATA
  61. #endif
  62. #if defined(__AVR_ATmega162__)   || \
  63.   defined(__AVR_ATmega128__)
  64. #ifdef UART_USE_UART1
  65.   #define UDR           UDR1
  66.   #define UCR           UCSR1B
  67.   #define UBRRL         UBRR1L
  68.   #define UBRRH         UBRR1H
  69.   #define SIG_UART_TRANS     SIG_UART1_TRANS
  70.   #define SIG_UART_RECV     SIG_UART1_RECV
  71.   #define SIG_UART_DATA     SIG_UART1_DATA
  72. #else
  73.   #define UDR           UDR0
  74.   #define UCR           UCSR0B
  75.   #define UBRRL         UBRR0L
  76.   #define UBRRH         UBRR0H
  77.   #define SIG_UART_TRANS     SIG_UART0_TRANS
  78.   #define SIG_UART_RECV     SIG_UART0_RECV
  79.   #define SIG_UART_DATA     SIG_UART0_DATA
  80. #endif
  81. #endif
  82.  
  83. // functions
  84.  
  85. //! Initializes uart.
  86. /// \note   After running this init function, the processor
  87. /// I/O pins that used for uart communications (RXD, TXD)
  88. /// are no long available for general purpose I/O.
  89. void uartInit(u08 mode, u32 baud);
  90.  
  91. //! Sets the uart baud rate.
  92. /// Argument should be in bits-per-second, like \c uartSetBaudRate(9600);
  93. void uartSetBaudRate(u32 baudrate);
  94.  
  95. //! Sends a single byte over the uart.
  96. /// \note This function waits for the uart to be ready,
  97. /// therefore, consecutive calls to uartSendByte() will
  98. /// go only as fast as the data can be sent over the
  99. /// serial port.
  100. void uartSendByte(u08 data);
  101.  
  102. #endif
  103. //@}
Laatste wijziging: Vr 10 April 2009, 10:44