Subversion Repositories HomeAutomation

Rev

Rev 97 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

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