uart.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include <avr/io.h>
  11. #include "uart.h"
  12. #define BAUD_RATE0_REG (unsigned int)(CPU_CLK_SPEED / (16 * BAUD_RATE0) ) - 1
  13. //Actual baud rate, can be used to calculate error
  14. #define ACTUAL_BAUD0 (unsigned int)(CPU_CLK_SPEED / (16 * BAUD_RATE0_REG + 1)
  15. #define BAUD_RATE1_REG (unsigned int)(CPU_CLK_SPEED / (16 * BAUD_RATE1) ) - 1
  16. //Actual baud rate, can be used to calculate error
  17. #define ACTUAL_BAUD1 (unsigned int)(CPU_CLK_SPEED / (16 * BAUD_RATE1_REG + 1)
  18. /*if you want to be able to change the baud rate "on the fly", just add in some
  19. code that calculates the proper baud rate register settings. The calculations
  20. are used above with #define BAUD_RATE0_REG. However, if you want to change
  21. baud rates BUT will only need a few, it would be easier to pre-calculate
  22. the baud register settings in pre-processor, and use a switch statement that
  23. lets you select between a few baud rates*/
  24. void init_uart0(void)
  25. {
  26. //turn on TX and RX
  27. RXTXEN0_REG = (1<<RX0EN) | (1<<TX0EN);
  28. //set up baud rate
  29. #if (BAUDREGS == 2)
  30. BAUD0H_REG = (unsigned char)(BAUD_RATE0_REG >> 8);
  31. BAUD0L_REG = (unsigned char)BAUD_RATE0_REG;
  32. #else
  33. BAUD0L_REG = (unsigned char)BAUD_RATE0_REG;
  34. #endif
  35. return;
  36. }
  37. unsigned char input_ch_w_timeout_0(char *data, volatile unsigned int timeout)
  38. {
  39. unsigned int timeout_counter = 0;
  40. //check if a byte has been recieved or if the timeout has been excedded
  41. while (timeout_counter != timeout) {
  42. if ((STAT0RXTX_REG & (1<<RX0C)) == (1<<RX0C)) {
  43. *data = UDR0;
  44. return BYTE_REC;
  45. }
  46. timeout_counter++;
  47. }
  48. return TIMEOUT;
  49. }
  50. char input_ch_0(void)
  51. {
  52. //check if a byte has been recieved or if the timeout has been excedded
  53. while ((STAT0RXTX_REG & (1<<RX0C)) != (1<<RX0C)) {
  54. continue;
  55. }
  56. return UDR0;
  57. }
  58. void output_ch_0(char data)
  59. {
  60. while ((STAT0RXTX_REG & (1<<UDR0E)) != (1<<UDR0E)) {
  61. continue;
  62. }
  63. UDR0 = data;
  64. return;
  65. }
  66. #if (NUM_OF_UARTS == 2)
  67. /*if you want to be able to change the baud rate "on the fly", just add in some
  68. code that calculates the proper baud rate register settings. The calculations
  69. are used above with #define BAUD_RATE1_REG. However, if you want to change
  70. baud rates BUT will only need a few, it would be easier to pre-calculate
  71. the baud register settings in pre-processor, and use a switch statement that
  72. lets you select between a few baud rates*/
  73. void init_uart1 (void)
  74. {
  75. //turn on TX and RX
  76. RXTXEN1_REG = (1<<RX1EN) | (1<<TX1EN);
  77. //set up baud rate
  78. #if (BAUDREGS == 2)
  79. BAUD1H_REG = (unsigned char)(BAUD_RATE1_REG >> 8);
  80. BAUD1L_REG = (unsigned char)BAUD_RATE1_REG;
  81. #else
  82. BAUD1L_REG = (unsigned char)BAUD_RATE1_REG;
  83. #endif
  84. return;
  85. }
  86. unsigned char input_ch_w_timeout_1(char *data, volatile unsigned int timeout)
  87. {
  88. unsigned int timeout_counter = 0;
  89. //check if a byte has been recieved or if the timeout has been excedded
  90. while (timeout_counter != timeout) {
  91. if ((STAT1RXTX_REG & (1<<RX1C)) == (1<<RX1C)) {
  92. *data = UDR1;
  93. return BYTE_REC;
  94. }
  95. timeout_counter++;
  96. }
  97. return TIMEOUT;
  98. }
  99. char input_ch_1(void)
  100. {
  101. //check if a byte has been recieved or if the timeout has been excedded
  102. while ((STAT1RXTX_REG & (1<<RX1C)) != (1<<RX1C)) {
  103. continue;
  104. }
  105. return UDR1;
  106. }
  107. void output_ch_1(char data)
  108. {
  109. while ((STAT1RXTX_REG & (1<<UDR1E)) != (1<<UDR1E)) {
  110. continue;
  111. }
  112. UDR1 = data;
  113. return;
  114. }
  115. #endif