uart.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* Simple routine to use the hardware UART. Designed to NOT use interrupts,
  2. this code is extreamly portable, and also extreamly simple.
  3. Created by: Colin O'Flynn
  4. Contact: c_oflynn@yahoo.com or coflynn@newae.com or username c_oflynn on
  5. www.avrfreaks.net
  6. These routines are released free of restrictions, but if anything bad happens
  7. (including but not limited to loss of your time, loss of profit, loss of life,
  8. injury, loss of money, loss of your dog) it is your OWN fault, NO ONE else
  9. can be held responsible*/
  10. //CPU clock speed in Hz
  11. #ifndef F_CPU
  12. #error "Define F_CPU"
  13. #else
  14. #define CPU_CLK_SPEED F_CPU
  15. #endif
  16. //Baud rate we want for UART0
  17. #define BAUD_RATE0 38400
  18. //Baud rate we want for UART1
  19. #define BAUD_RATE1 115200
  20. /* If the AVR you want to use isn't supported yet, it is very simple to add. If
  21. the AVR has the same register names as another already defined chip (say for
  22. example the AtMega8), then just add another line to the #if statement. So
  23. instead of being
  24. #if defined(__AVR_ATmega8__)
  25. //if chip is AtMega8
  26. #define BAUDREGS 2
  27. #define BAUD0H_REG UBRRH
  28. #define....
  29. it becomes
  30. #if defined(__AVR_ATmega8__) || \
  31. defined(__AVR_ATname__)
  32. //if chip is AtMega8 or ATname
  33. #define BAUDREGS 2
  34. #define BAUD0H_REG UBRRH
  35. If this isn't the case, then look through the data-sheet and map all the register
  36. names to the generals defines used in this program.
  37. */
  38. #if defined(__AVR_ATmega8__) || \
  39. defined(__AVR_ATmega16__) || \
  40. defined(__AVR_ATmega32__)
  41. #define NUM_OF_BAUDREGS 2
  42. #define BAUD0H_REG UBRRH
  43. #define BAUD0L_REG UBRRL
  44. #define NUM_OF_UARTS 1
  45. #define RXTXEN0_REG UCSRB
  46. #define STAT0RXTX_REG UCSRA
  47. #define UDR0 UDR
  48. #define RX0EN RXEN
  49. #define TX0EN TXEN
  50. #define RX0C RXC
  51. #define UDR0E UDRE
  52. #elif defined(__AVR_AT90S4433__)
  53. #define NUM_OF_BAUDREGS 1
  54. #define BAUD0L_REG UBRR
  55. #define NUM_OF_UARTS 1
  56. #define RXTXEN0_REG UCSRB
  57. #define STAT0RXTX_REG UCSRA
  58. #define UDR0 UDR
  59. #define RX0EN RXEN
  60. #define TX0EN TXEN
  61. #define RX0C RXC
  62. #define UDR0E UDRE
  63. #elif defined(__AVR_AT90S8515__) || \
  64. defined(__AVR_AT90S2313__) || \
  65. defined(__AVR_AT90S8535__) || \
  66. defined(__AVR_ATmega103__)
  67. #define BAUDREGS 1
  68. #define BAUD0L_REG UBRR
  69. #define NUM_OF_UARTS 1
  70. #define RXTXEN0_REG UCR
  71. #define STAT0RXTX_REG USR
  72. #define UDR0 UDR
  73. #define RX0EN RXEN
  74. #define TX0EN TXEN
  75. #define RX0C RXC
  76. #define UDR0E UDRE
  77. //if chip is AtMega128
  78. #elif defined(__AVR_ATmega128__) || \
  79. defined(__AVR_ATmega64__) || \
  80. defined(__AVR_ATmega128RFA1__) || \
  81. defined(__AVR_AT90CAN128__)
  82. #define NUM_OF_BAUDREGS 2
  83. #define BAUD0H_REG UBRR0H
  84. #define BAUD0L_REG UBRR0L
  85. #define NUM_OF_UARTS 2
  86. #define RXTXEN0_REG UCSR0B
  87. #define STAT0RXTX_REG UCSR0A
  88. // #define UDR0 UDR0 don't need to redefine it
  89. #define RX0EN RXEN0
  90. #define TX0EN TXEN0
  91. #define RX0C RXC0
  92. #define UDR0E UDRE0
  93. #define BAUD1H_REG UBRR1H
  94. #define BAUD1L_REG UBRR1L
  95. #define RXTXEN1_REG UCSR1B
  96. #define STAT1RXTX_REG UCSR1A
  97. // #define UDR1 UDR1 don't need to redefine it
  98. #define RX1EN RXEN1
  99. #define TX1EN TXEN1
  100. #define RX1C RXC1
  101. #define UDR1E UDRE1
  102. #elif defined(__AVR_ATmega48__) || \
  103. defined(__AVR_ATmega88__) || \
  104. defined(__AVR_ATmega168__) || \
  105. defined(__AVR_ATmega328__) || \
  106. defined(__AVR_ATmega48P__) || \
  107. defined(__AVR_ATmega88P__) || \
  108. defined(__AVR_ATmega168P__) || \
  109. defined(__AVR_ATmega328P__) || \
  110. defined(__AVR_ATmega48A__) || \
  111. defined(__AVR_ATmega88A__) || \
  112. defined(__AVR_ATmega168A__) || \
  113. defined(__AVR_ATmega328A__)
  114. #define NUM_OF_BAUDREGS 2
  115. #define BAUD0H_REG UBRR0H
  116. #define BAUD0L_REG UBRR0L
  117. #define NUM_OF_UARTS 1
  118. #define RXTXEN0_REG UCSR0B
  119. #define STAT0RXTX_REG UCSR0A
  120. // #define UDR0 UDR0 don't need to redefine it
  121. #define RX0EN RXEN0
  122. #define TX0EN TXEN0
  123. #define RX0C RXC0
  124. #define UDR0E UDRE0
  125. #else
  126. #define NUM_OF_BAUDREGS 2
  127. #define BAUD0H_REG UBRR1H
  128. #define BAUD0L_REG UBRR1L
  129. #define NUM_OF_UARTS 1
  130. #define RXTXEN0_REG UCSR1B
  131. #define STAT0RXTX_REG UCSR1A
  132. // #define UDR0 UDR0 don't need to redefine it
  133. #define RX0EN RXEN1
  134. #define TX0EN TXEN1
  135. #define RX0C RXC1
  136. #define UDR0E UDRE1
  137. #define BAUD1H_REG UBRR1H
  138. #define BAUD1L_REG UBRR1L
  139. #define RXTXEN1_REG UCSR1B
  140. #define STAT1RXTX_REG UCSR1A
  141. // #define UDR1 UDR1 don't need to redefine it
  142. #define RX1EN RXEN1
  143. #define TX1EN TXEN1
  144. #define RX1C RXC1
  145. #define UDR1E UDRE1
  146. //#else
  147. // #error "No supported chip type in use for UART.C"
  148. #endif
  149. //Init UART0, set to baud rate as defined in header file
  150. void init_uart0
  151. (
  152. void
  153. );
  154. //Input a char on UART0 and store it to data, however if no char is recieved
  155. //within timeout, then abort and return TIMEOUT, otherwise return BYTE_REC
  156. //(note: timeout is NOT a reliable value, as it uses a simple C loop that
  157. //will change with different compiler settings likely
  158. unsigned char input_ch_w_timeout_0
  159. (
  160. char * data,
  161. unsigned int timeout
  162. );
  163. //wait forever for a char on UART 0 and return it
  164. char input_ch_0
  165. (
  166. void
  167. );
  168. //output char data on UART0
  169. void output_ch_0
  170. (
  171. char data
  172. );
  173. #if (NUM_OF_UARTS == 2)
  174. //Init UART1, set to baud rate as defined in header file
  175. void init_uart1
  176. (
  177. void
  178. );
  179. //Input a char on UART1 and store it to data, however if no char is recieved
  180. //within timeout, then abort and return TIMEOUT, otherwise return BYTE_REC
  181. //(note: timeout is NOT a reliable value, as it uses a simple C loop that
  182. //will change with different compiler settings likely
  183. unsigned char input_ch_w_timeout_1
  184. (
  185. char * data,
  186. unsigned int timeout
  187. );
  188. //wait forever for a char on UART 1 and return it
  189. char input_ch_1
  190. (
  191. void
  192. );
  193. //output char data on UART1
  194. void output_ch_1
  195. (
  196. char data
  197. );
  198. #endif
  199. //error codes returned by functions
  200. #define BYTE_REC 0
  201. #define TIMEOUT 1