uart.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "uart.h"
  2. /*! Define that selects the Usart used in example. */
  3. #define USART USARTC0
  4. #define TIMEOUT 0
  5. #define BYTE_REC 1
  6. void init_uart0
  7. (
  8. void
  9. )
  10. {
  11. /* This PORT setting is only valid to USARTC0 if other USARTs is used a
  12. * different PORT and/or pins is used. */
  13. /* PIN3 (TXD0) as output. */
  14. PORTC.DIRSET = PIN3_bm;
  15. /* PC2 (RXD0) as input. */
  16. PORTC.DIRCLR = PIN2_bm;
  17. /* USARTC0, 8 Data bits, No Parity, 1 Stop bit. */
  18. USART_Format_Set(&USART, USART_CHSIZE_8BIT_gc, USART_PMODE_DISABLED_gc, false);
  19. /* Set Baudrate to 9600 bps:
  20. * Use the default I/O clock fequency that is 2 MHz.
  21. * Do not use the baudrate scale factor
  22. *
  23. * Baudrate select = (1/(16*(((I/O clock frequency)/Baudrate)-1)
  24. * = 12
  25. */
  26. #if SS_VER==SS_VER_2_1
  27. USART_Baudrate_Set(&USART, 1, 0);
  28. #else
  29. USART_Baudrate_Set(&USART, 11, 0);
  30. #endif
  31. /* Enable both RX and TX. */
  32. USART_Rx_Enable(&USART);
  33. USART_Tx_Enable(&USART);
  34. }
  35. unsigned char input_ch_w_timeout_0
  36. (
  37. char * data,
  38. volatile unsigned int timeout
  39. )
  40. {
  41. unsigned int timeout_counter = 0;
  42. //check if a byte has been recieved or if the timeout has been excedded
  43. while (timeout_counter != timeout)
  44. {
  45. if (USART_IsRXComplete(&USART))
  46. {
  47. *data = USART_GetChar(&USART);
  48. return BYTE_REC;
  49. }
  50. timeout_counter++;
  51. }
  52. return TIMEOUT;
  53. }
  54. char input_ch_0
  55. (
  56. void
  57. )
  58. {
  59. //check if a byte has been recieved or if the timeout has been excedded
  60. while (!USART_IsRXComplete(&USART))
  61. {
  62. continue;
  63. }
  64. return USART_GetChar(&USART);;
  65. }
  66. void output_ch_0
  67. (
  68. char data
  69. )
  70. {
  71. while(!USART_IsTXDataRegisterEmpty(&USART));
  72. USART_PutChar(&USART, data);
  73. return;
  74. }