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