Line No. | Rev | Author | Line |
---|---|---|---|
1 | 32 | kaklik | /* |
2 | |||
3 | UART2 Driver File for PIC24. |
||
4 | |||
5 | ******************************************************************************** |
||
6 | FileName: uart2.c |
||
7 | Dependencies: HardwareProfile.h |
||
8 | Processor: PIC24 |
||
9 | Compiler: MPLAB C30 |
||
10 | Linker: MPLAB LINK30 |
||
11 | Company: Microchip Technology Incorporated |
||
12 | |||
13 | Author Date Comment |
||
14 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
15 | KO 12-Feb-2008 Modified to use HardwareProfile.h |
||
16 | KO 11-Oct-2006 v1.0 |
||
17 | Anton Alkhimenok 18-Oct-2005 |
||
18 | Anton Alkhimenok 17-Feb-2009 Added UART2Char2Hex(), UART2Hex2Char(), |
||
19 | UART2ClearError(), UART2DataReceived() |
||
20 | PAT 27-Jan-2010 Added UART2GetBaudError() for dynamic checking |
||
21 | of baud rate percentage error. |
||
22 | |||
23 | ******************************************************************************** |
||
24 | Software License Agreement |
||
25 | |||
26 | Microchip Technology Inc. ("Microchip") licenses to you the right to use, copy, |
||
27 | modify and distribute the software - including source code - only for use with |
||
28 | Microchip microcontrollers or Microchip digital signal controllers; provided |
||
29 | that no open source or free software is incorporated into the Source Code |
||
30 | without Microchips prior written consent in each instance. |
||
31 | |||
32 | The software is owned by Microchip and its licensors, and is protected under |
||
33 | applicable copyright laws. All rights reserved. |
||
34 | |||
35 | SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, |
||
36 | EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF |
||
37 | MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. |
||
38 | IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER |
||
39 | CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR |
||
40 | OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES |
||
41 | INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT OR |
||
42 | CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF |
||
43 | SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, ANY CLAIMS BY THIRD PARTIES (INCLUDING |
||
44 | BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. |
||
45 | |||
46 | ******************************************************************************** |
||
47 | */ |
||
48 | |||
49 | #include "Compiler.h" |
||
50 | #include "HardwareProfile.h" |
||
51 | #include "uart2.h" |
||
52 | |||
53 | //****************************************************************************** |
||
54 | // Constants |
||
55 | //****************************************************************************** |
||
56 | |||
57 | //U2BRG register value and baudrate mistake calculation |
||
58 | |||
59 | #if defined (__C30__) |
||
60 | #define BAUDRATEREG2 (((GetSystemClock()/2)+(BRG_DIV2/2*BAUDRATE2))/BRG_DIV2/BAUDRATE2-1) |
||
61 | #elif defined (__PIC32MX__) |
||
62 | #define BAUDRATEREG2 ((GetPeripheralClock()+(BRG_DIV2/2*BAUDRATE2))/BRG_DIV2/BAUDRATE2-1) |
||
63 | #else |
||
64 | #error Cannot calculate BRG value |
||
65 | #endif |
||
66 | |||
67 | #if defined (__C30__) |
||
68 | #define BAUD_ACTUAL ((GetSystemClock()/2)/BRG_DIV2/(BAUDRATEREG2+1)) |
||
69 | #elif defined (__PIC32MX__) |
||
70 | #define BAUD_ACTUAL (GetPeripheralClock()/BRG_DIV2/(BAUDRATEREG2+1)) |
||
71 | #else |
||
72 | #error Cannot calculate actual baud rate |
||
73 | #endif |
||
74 | |||
75 | |||
76 | #define BAUD_ERROR ((BAUD_ACTUAL > BAUDRATE2) ? BAUD_ACTUAL-BAUDRATE2 : BAUDRATE2-BAUD_ACTUAL) |
||
77 | #define BAUD_ERROR_PERCENT ((BAUD_ERROR*100+BAUDRATE2/2)/BAUDRATE2) |
||
78 | |||
79 | #if defined (__C30__) |
||
80 | |||
81 | #if (BAUD_ERROR_PERCENT > 3) |
||
82 | #error UART frequency error is worse than 3% |
||
83 | #elif (BAUD_ERROR_PERCENT > 2) |
||
84 | #warning UART frequency error is worse than 2% |
||
85 | #endif |
||
86 | |||
87 | #endif // #if defined (__C30__) |
||
88 | |||
89 | /******************************************************************************* |
||
90 | Function: UART2GetBaudError() |
||
91 | |||
92 | Precondition: |
||
93 | None. |
||
94 | |||
95 | Overview: |
||
96 | This routine checks the UART baud rate error percentage and returns it. |
||
97 | |||
98 | Input: None. |
||
99 | |||
100 | Output: Returns the baud rate error in percent. |
||
101 | |||
102 | *******************************************************************************/ |
||
103 | char UART2GetBaudError() |
||
104 | { |
||
105 | unsigned int errorPercent = 0; |
||
106 | |||
107 | errorPercent = ((BAUD_ERROR*100+BAUDRATE2/2)/BAUDRATE2); |
||
108 | return (char)errorPercent; |
||
109 | } |
||
110 | |||
111 | |||
112 | /******************************************************************************* |
||
113 | Function: UART2GetChar() |
||
114 | |||
115 | Precondition: |
||
116 | UART2Init must be called prior to calling this routine. |
||
117 | |||
118 | Overview: |
||
119 | This routine waits for a byte to be received. It then returns that byte. |
||
120 | |||
121 | Input: None. |
||
122 | |||
123 | Output: Byte received. |
||
124 | |||
125 | *******************************************************************************/ |
||
126 | char UART2GetChar() |
||
127 | { |
||
128 | char Temp; |
||
129 | |||
130 | while(IFS1bits.U2RXIF == 0); |
||
131 | |||
132 | Temp = U2RXREG; |
||
133 | IFS1bits.U2RXIF = 0; |
||
134 | return Temp; |
||
135 | } |
||
136 | |||
137 | /******************************************************************************* |
||
138 | Function: UART2Init() |
||
139 | |||
140 | Precondition: None. |
||
141 | |||
142 | Overview: |
||
143 | This routine sets up the UART2 module. |
||
144 | |||
145 | Input: None. |
||
146 | |||
147 | Output: None. |
||
148 | |||
149 | Notes: |
||
150 | Allow the peripheral to set the I/O pin directions. If we set the TRIS |
||
151 | bits manually, then when we disable the UART, the shape of the stop bit |
||
152 | changes, and some terminal programs have problems. |
||
153 | *******************************************************************************/ |
||
154 | void UART2Init() |
||
155 | { |
||
156 | U2BRG = BAUDRATEREG2; |
||
157 | U2MODE = 0; |
||
158 | U2MODEbits.BRGH = BRGH2; |
||
159 | U2STA = 0; |
||
160 | U2MODEbits.UARTEN = 1; |
||
161 | U2STAbits.UTXEN = 1; |
||
162 | IFS1bits.U2RXIF = 0; |
||
163 | |||
164 | #if defined (__PIC32MX__) |
||
165 | U2STAbits.URXEN = 1; |
||
166 | #endif |
||
167 | } |
||
168 | |||
169 | /******************************************************************************* |
||
170 | Function: UART2IsPressed() |
||
171 | |||
172 | Precondition: |
||
173 | UART2Init must be called prior to calling this routine. |
||
174 | |||
175 | Overview: |
||
176 | This routine checks to see if there is a new byte in UART reception buffer. |
||
177 | |||
178 | Input: None. |
||
179 | |||
180 | Output: |
||
181 | |||
182 | 1 : Data is in the receive buffer |
||
183 | |||
184 | *******************************************************************************/ |
||
185 | char UART2IsPressed() |
||
186 | { |
||
187 | if(IFS1bits.U2RXIF == 1) |
||
188 | return 1; |
||
189 | return 0; |
||
190 | } |
||
191 | |||
192 | /******************************************************************************* |
||
193 | Function: UART2PrintString( char *str ) |
||
194 | |||
195 | Precondition: |
||
196 | UART2Init must be called prior to calling this routine. |
||
197 | |||
198 | Overview: |
||
199 | This function prints a string of characters to the UART. |
||
200 | |||
201 | Input: Pointer to a null terminated character string. |
||
202 | |||
203 | Output: None. |
||
204 | |||
205 | *******************************************************************************/ |
||
206 | void UART2PrintString( char *str ) |
||
207 | { |
||
208 | unsigned char c; |
||
209 | |||
210 | while( (c = *str++) ) |
||
211 | UART2PutChar(c); |
||
212 | } |
||
213 | |||
214 | /******************************************************************************* |
||
215 | Function: UART2PutChar( char ch ) |
||
216 | |||
217 | Precondition: |
||
218 | UART2Init must be called prior to calling this routine. |
||
219 | |||
220 | Overview: |
||
221 | This routine writes a character to the transmit FIFO, and then waits for the |
||
222 | transmit FIFO to be empty. |
||
223 | |||
224 | Input: Byte to be sent. |
||
225 | |||
226 | Output: None. |
||
227 | |||
228 | *******************************************************************************/ |
||
229 | void UART2PutChar( char ch ) |
||
230 | { |
||
231 | U2TXREG = ch; |
||
232 | #if !defined(__PIC32MX__) |
||
233 | Nop(); |
||
234 | #endif |
||
235 | while(U2STAbits.TRMT == 0); |
||
236 | } |
||
237 | |||
238 | /******************************************************************************* |
||
239 | Function: UART2PutDec(unsigned char dec) |
||
240 | |||
241 | Precondition: |
||
242 | UART2Init must be called prior to calling this routine. |
||
243 | |||
244 | Overview: |
||
245 | This function converts decimal data into a string and outputs it to UART. |
||
246 | |||
247 | Input: Binary data. |
||
248 | |||
249 | Output: None. |
||
250 | |||
251 | *******************************************************************************/ |
||
252 | void UART2PutDec(unsigned char dec) |
||
253 | { |
||
254 | unsigned char res; |
||
255 | |||
256 | res = dec; |
||
257 | |||
258 | if (res/100) |
||
259 | { |
||
260 | UART2PutChar( res/100 + '0' ); |
||
261 | } |
||
262 | res = res - (res/100)*100; |
||
263 | |||
264 | if (res/10) |
||
265 | { |
||
266 | UART2PutChar( res/10 + '0' ); |
||
267 | } |
||
268 | res = res - (res/10)*10; |
||
269 | |||
270 | UART2PutChar( res + '0' ); |
||
271 | } |
||
272 | |||
273 | /******************************************************************************* |
||
274 | Function: UART2PutHex |
||
275 | |||
276 | Precondition: |
||
277 | UART2Init must be called prior to calling this routine. |
||
278 | |||
279 | Overview: |
||
280 | This function converts hex data into a string and outputs it to UART. |
||
281 | |||
282 | Input: Binary data. |
||
283 | |||
284 | Output: None. |
||
285 | |||
286 | *******************************************************************************/ |
||
287 | |||
288 | const unsigned char CharacterArray[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; |
||
289 | |||
290 | void UART2PutHex( int toPrint ) |
||
291 | { |
||
292 | int printVar; |
||
293 | |||
294 | printVar = toPrint; |
||
295 | toPrint = (toPrint>>4) & 0x0F; |
||
296 | UART2PutChar( CharacterArray[toPrint] ); |
||
297 | |||
298 | toPrint = printVar & 0x0F; |
||
299 | UART2PutChar( CharacterArray[toPrint] ); |
||
300 | |||
301 | return; |
||
302 | } |
||
303 | |||
304 | /******************************************************************************* |
||
305 | Function: UART2PutHexWord(unsigned int toPrint) |
||
306 | |||
307 | Precondition: |
||
308 | UART2Init must be called prior to calling this routine. |
||
309 | |||
310 | Overview: |
||
311 | This function converts hex data into a string and outputs it to UART. |
||
312 | |||
313 | Input: Binary data. |
||
314 | |||
315 | Output: None. |
||
316 | |||
317 | *******************************************************************************/ |
||
318 | #if defined( __C30__ ) || defined( __PIC32MX__ ) |
||
319 | void UART2PutHexWord( unsigned int toPrint ) |
||
320 | { |
||
321 | unsigned int printVar; |
||
322 | |||
323 | printVar = (toPrint>>12) & 0x0F; |
||
324 | UART2PutChar( CharacterArray[printVar] ); |
||
325 | |||
326 | printVar = (toPrint>>8) & 0x0F; |
||
327 | UART2PutChar( CharacterArray[printVar] ); |
||
328 | |||
329 | printVar = (toPrint>>4) & 0x0F; |
||
330 | UART2PutChar( CharacterArray[printVar] ); |
||
331 | |||
332 | printVar = toPrint & 0x0F; |
||
333 | UART2PutChar( CharacterArray[printVar] ); |
||
334 | |||
335 | return; |
||
336 | } |
||
337 | |||
338 | void UART2PutHexDWord( unsigned long toPrint ) |
||
339 | { |
||
340 | unsigned long printVar; |
||
341 | |||
342 | printVar = (toPrint>>28) & 0x0F; |
||
343 | UART2PutChar( CharacterArray[printVar] ); |
||
344 | |||
345 | printVar = (toPrint>>24) & 0x0F; |
||
346 | UART2PutChar( CharacterArray[printVar] ); |
||
347 | |||
348 | printVar = (toPrint>>20) & 0x0F; |
||
349 | UART2PutChar( CharacterArray[printVar] ); |
||
350 | |||
351 | printVar = (toPrint>>16) & 0x0F; |
||
352 | UART2PutChar( CharacterArray[printVar] ); |
||
353 | |||
354 | printVar = (toPrint>>12) & 0x0F; |
||
355 | UART2PutChar( CharacterArray[printVar] ); |
||
356 | |||
357 | printVar = (toPrint>>8) & 0x0F; |
||
358 | UART2PutChar( CharacterArray[printVar] ); |
||
359 | |||
360 | printVar = (toPrint>>4) & 0x0F; |
||
361 | UART2PutChar( CharacterArray[printVar] ); |
||
362 | |||
363 | printVar = toPrint & 0x0F; |
||
364 | UART2PutChar( CharacterArray[printVar] ); |
||
365 | |||
366 | return; |
||
367 | } |
||
368 | |||
369 | #endif |
||
370 | |||
371 | /********************************************************************* |
||
372 | Function: char UART2Char2Hex(char ch) |
||
373 | |||
374 | PreCondition: none |
||
375 | |||
376 | Input: ASCII to be converted |
||
377 | |||
378 | Output: number |
||
379 | |||
380 | Side Effects: none |
||
381 | |||
382 | Overview: converts ASCII coded digit into number |
||
383 | |||
384 | Note: none |
||
385 | |||
386 | ********************************************************************/ |
||
387 | char UART2Char2Hex(char ch){ |
||
388 | // Wrong char |
||
389 | if(ch > 102) |
||
390 | return 0; |
||
391 | |||
392 | // From a to f |
||
393 | if(ch > 96) |
||
394 | return (ch-87); |
||
395 | |||
396 | // Wrong char |
||
397 | if(ch > 70) |
||
398 | return 0; |
||
399 | |||
400 | // From A to F |
||
401 | if(ch > 64) |
||
402 | return (ch-55); |
||
403 | |||
404 | // Wrong char |
||
405 | if(ch > 57) |
||
406 | return 0; |
||
407 | |||
408 | // From 0 - 9 |
||
409 | if(ch > 47) |
||
410 | return(ch-48); |
||
411 | else |
||
412 | // Wrong char |
||
413 | return 0; |
||
414 | } |
||
415 | |||
416 | /********************************************************************* |
||
417 | Function: char UART2Hex2Char(char hex) |
||
418 | |||
419 | PreCondition: none |
||
420 | |||
421 | Input: number |
||
422 | |||
423 | Output: ASCII code |
||
424 | |||
425 | Side Effects: none |
||
426 | |||
427 | Overview: converts low nibble into ASCII coded digit |
||
428 | |||
429 | Note: none |
||
430 | |||
431 | ********************************************************************/ |
||
432 | char UART2Hex2Char(char hex){ |
||
433 | char h; |
||
434 | h = hex&0x0f; |
||
435 | // From 0xa to 0xf |
||
436 | if(h>9) |
||
437 | return (h+55); |
||
438 | else |
||
439 | return (h+48); |
||
440 | } |
||
441 | |||
442 | /********************************************************************* |
||
443 | Function: void UART2ClrError(void) |
||
444 | |||
445 | PreCondition: none |
||
446 | |||
447 | Input: none |
||
448 | |||
449 | Output: character received |
||
450 | |||
451 | Side Effects: none |
||
452 | |||
453 | Overview: wait for character |
||
454 | |||
455 | Note: none |
||
456 | |||
457 | ********************************************************************/ |
||
458 | void UART2ClrError(void){ |
||
459 | // Clear error flag |
||
460 | if(U2STAbits.OERR) |
||
461 | U2STAbits.OERR = 0; |
||
462 | } |
||
463 |
Powered by WebSVN v2.8.3