Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 25 | johboh | 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 | |||
| 24 | johboh | 13 | #include "../Include/stackTasks.h" |
| 14 | #include "../Include/uart.h" |
||
| 15 | |||
| 16 | #ifdef USE_UART |
||
| 17 | |||
| 18 | static char uartBusy(void); |
||
| 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; // Low BRG speed |
||
| 35 | RCSTA=0b10010000; |
||
| 36 | SPBRG = SPBRG_VAL; |
||
| 37 | // USART RX interrupt |
||
| 38 | PIR1bits.RCIF=0; |
||
| 39 | PIE1bits.RCIE=1; |
||
| 40 | // High interrupt |
||
| 41 | IPR1bits.RCIP=1; |
||
| 42 | } |
||
| 43 | |||
| 44 | |||
| 45 | /* |
||
| 46 | * Function: uartPutc |
||
| 47 | * |
||
| 48 | * Input: BYTE c to send on uart |
||
| 49 | * Output: none |
||
| 50 | * Pre-conditions: call to uartInit() |
||
| 51 | * Affects: none |
||
| 52 | * Depends: none |
||
| 53 | */ |
||
| 54 | void uartPutc(BYTE c) |
||
| 55 | { |
||
| 56 | // Write the data byte to the USART |
||
| 57 | TXREG = c; |
||
| 58 | while(uartBusy()); |
||
| 59 | } |
||
| 60 | |||
| 61 | |||
| 62 | /* |
||
| 63 | * Function: uartPuts |
||
| 64 | * |
||
| 65 | * Input: Byte array to send on uart |
||
| 66 | * Output: none |
||
| 67 | * Pre-conditions: call to uartInit() |
||
| 68 | * Affects: none |
||
| 69 | * Depends: none |
||
| 70 | */ |
||
| 71 | void uartPuts(BYTE c[]) |
||
| 72 | { |
||
| 73 | do |
||
| 74 | { // Transmit a byte |
||
| 75 | while(uartBusy()); |
||
| 76 | uartPutc(*c); |
||
| 77 | } while( *c++ ); |
||
| 78 | } |
||
| 79 | |||
| 80 | /* |
||
| 81 | * Function: uartPutrs |
||
| 82 | * |
||
| 83 | * Input: Byte array to send on uart (static) |
||
| 84 | * Output: none |
||
| 85 | * Pre-conditions: call to uartInit() |
||
| 86 | * Affects: none |
||
| 87 | * Depends: none |
||
| 88 | */ |
||
| 89 | void uartPutrs(const rom char *c) |
||
| 90 | { |
||
| 91 | do |
||
| 92 | { // Transmit a byte |
||
| 93 | while(uartBusy()); |
||
| 94 | uartPutc(*c); |
||
| 95 | } while( *c++ ); |
||
| 96 | } |
||
| 97 | |||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | /* |
||
| 102 | * Function: uartBusy |
||
| 103 | * |
||
| 104 | * Input: none. |
||
| 105 | * Output: If usart busy or not. |
||
| 106 | * Pre-conditions: call to uartInit() |
||
| 107 | * Affects: none |
||
| 108 | * Depends: none |
||
| 109 | */ |
||
| 110 | char uartBusy(void) |
||
| 111 | { |
||
| 112 | return !TXSTAbits.TRMT; |
||
| 113 | } |
||
| 114 | |||
| 115 | |||
| 116 | /* |
||
| 117 | * Function: uartGet |
||
| 118 | * |
||
| 119 | * Input: none |
||
| 120 | * Output: Byte read |
||
| 121 | * Pre-conditions: call to uartInit() |
||
| 122 | * Affects: none |
||
| 123 | * Depends: none. |
||
| 124 | */ |
||
| 125 | BYTE uartGet(void) |
||
| 126 | { |
||
| 127 | // Return the received data |
||
| 128 | return RCREG; |
||
| 129 | } |
||
| 130 | |||
| 131 | |||
| 132 | /* |
||
| 133 | * Function: uartISR |
||
| 134 | * |
||
| 135 | * Input: none |
||
| 136 | * Output: none |
||
| 137 | * Pre-conditions: call to uartInit() |
||
| 138 | * Affects: call to uartParse |
||
| 139 | * Depends: none. |
||
| 140 | */ |
||
| 141 | void uartISR(void) |
||
| 142 | { |
||
| 143 | if (PIR1bits.RCIF==1 && PIE1bits.RCIE==1) |
||
| 144 | { |
||
| 145 | uartParse(uartGet()); |
||
| 146 | PIR1bits.RCIF=0; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | |||
| 151 | #endif |