Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Blame | Last modification | View Log | SVN | RSS feed

  1. /*********************************************************************
  2.  *
  3.  *                  UART
  4.  *
  5.  *********************************************************************
  6.  * FileName:        uart.c
  7.  *
  8.  * Author               Date    Comment
  9.  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10.  * Johan Böhlin     21-10-06    Original        (Rev 1.0)
  11.  ********************************************************************/
  12.  
  13. #include <uart.h>
  14. #include <stackTasks.h>
  15.  
  16. #ifdef USE_UART
  17.  
  18. static char uartBusy(void);
  19.  
  20.  
  21. /*
  22. *   Function: uartInit
  23. *
  24. *   Input:  none
  25. *   Output: none
  26. *   Pre-conditions: none
  27. *   Affects: none
  28. *   Depends: none
  29. */
  30. void uartInit()
  31. {
  32.         // Configure USART
  33.         TRISCbits.TRISC7=1;
  34.         TRISCbits.TRISC6=0;
  35.         TXSTA=0b00100000; // Low BRG speed
  36.         RCSTA=0b10010000;
  37.         SPBRG = SPBRG_VAL;
  38.         // USART RX interrupt
  39.         PIR1bits.RCIF=0;
  40.         PIE1bits.RCIE=1;
  41.         // High interrupt
  42.         IPR1bits.RCIP=1;
  43. }
  44.  
  45.  
  46. /*
  47. *   Function: uartPutc
  48. *
  49. *   Input:  BYTE c to send on uart
  50. *   Output: none
  51. *   Pre-conditions: call to uartInit()
  52. *   Affects: none
  53. *   Depends: none
  54. */
  55. void uartPutc(BYTE c)
  56. {
  57.     // Write the data byte to the USART
  58.     TXREG = c;      
  59.     while(uartBusy());
  60. }
  61.  
  62.  
  63. /*
  64. *   Function: uartPuts
  65. *
  66. *   Input:  Byte array to send on uart
  67. *   Output: none
  68. *   Pre-conditions: call to uartInit()
  69. *   Affects: none
  70. *   Depends: none
  71. */
  72. void uartPuts(BYTE c[])
  73. {
  74.     do
  75.     {  // Transmit a byte
  76.         while(uartBusy());
  77.         uartPutc(*c);
  78.     } while( *c++ );
  79. }
  80.  
  81. /*
  82. *   Function: uartPutrs
  83. *
  84. *   Input:  Byte array to send on uart (static)
  85. *   Output: none
  86. *   Pre-conditions: call to uartInit()
  87. *   Affects: none
  88. *   Depends: none
  89. */
  90. void uartPutrs(const rom char *c)
  91. {
  92.     do
  93.     {  // Transmit a byte
  94.         while(uartBusy());
  95.         uartPutc(*c);
  96.     } while( *c++ );
  97. }
  98.  
  99.  
  100.  
  101.  
  102. /*
  103. *   Function: uartBusy
  104. *
  105. *   Input:  none.
  106. *   Output: If usart busy or not.
  107. *   Pre-conditions: call to uartInit()
  108. *   Affects: none
  109. *   Depends: none
  110. */
  111. char uartBusy(void)
  112. {
  113.     return !TXSTAbits.TRMT;
  114. }
  115.  
  116.  
  117. /*
  118. *   Function: uartGet
  119. *
  120. *   Input:  none
  121. *   Output: Byte read
  122. *   Pre-conditions: call to uartInit()
  123. *   Affects: none
  124. *   Depends: none.
  125. */
  126. BYTE uartGet(void)
  127. {
  128.     // Return the received data
  129.     return RCREG;  
  130. }
  131.  
  132.  
  133. /*
  134. *   Function: uartISR
  135. *
  136. *   Input:  none
  137. *   Output: none
  138. *   Pre-conditions: call to uartInit()
  139. *   Affects: call to uartParse
  140. *   Depends: none.
  141. */
  142. void uartISR(void)
  143. {
  144.     if (PIR1bits.RCIF==1 && PIE1bits.RCIE==1)
  145.     {
  146.         if (RCSTAbits.OERR) { RCSTAbits.CREN=0; RCSTAbits.CREN=1; }
  147.  
  148.         uartParse(uartGet());
  149.         PIR1bits.RCIF=0;
  150.     }
  151. }
  152.  
  153.  
  154. /*
  155. *   Function: uartDataRedy
  156. *
  157. *   Input:  none.
  158. *   Output: If usart data are redy to be read.
  159. *   Pre-conditions: call to uartInit()
  160. *   Affects: none
  161. *   Depends: none
  162. */
  163. char uartDataRedy(void)
  164. {
  165.     if(RCSTAbits.OERR)
  166.     {
  167.         RCSTAbits.CREN = 0;
  168.         RCSTAbits.CREN = 1;
  169.     }
  170.   return PIR1bits.RCIF;
  171. }
  172.  
  173. #endif
  174.