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