-
Notifications
You must be signed in to change notification settings - Fork 2
/
uart.h
332 lines (225 loc) · 9.13 KB
/
uart.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/****************************************************************************
* PIC24 Generic UART driver header
*****************************************************************************
* FileName: uart.h
* Dependencies: system.h
* Processor: PIC24
* Hardware: none
* Complier: Microchip C30 v3.10 or higher
* Company: Microchip Technology, Inc.
*
* Copyright and Disclaimer Notice
*
* Copyright ?007-2008 Microchip Technology Inc. All rights reserved.
*
* Microchip licenses to you the right to use, modify, copy and distribute
* Software only when embedded on a Microchip microcontroller or digital
* signal controller and used with a Microchip radio frequency transceiver,
* which are integrated into your product or third party product (pursuant
* to the terms in the accompanying license agreement).
*
* You should refer to the license agreement accompanying this Software for
* additional information regarding your rights and obligations.
*
* SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS?WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A
* PARTICULAR PURPOSE. IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE
* LIABLE OR OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY,
* CONTRIBUTION, BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY
* DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO
* ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES,
* LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF SUBSTITUTE GOODS,
* TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT
* NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
KO 12-Feb-2008 Modified to use HardwareProfile.h
KO 11-Oct-2006 v1.0
Anton Alkhimenok 18-Oct-2005
Anton Alkhimenok 17-Feb-2009 Added UARTChar2Hex(), UARTHex2Char(),
UARTClearError(), UARTDataReceived()
Brant Ivey 09-Sep-2009 Changed name to uart.c & modified to use
generic UART.
Added option to invert UART.
*****************************************************************************/
#ifndef _UART_H
#define _UART_H
/****************************************************************************
Section: Includes
***************************************************************************/
#include "system.h"
/****************************************************************************
Section: UART baud rate calculation
***************************************************************************/
#ifdef BRGH_VAL
#define BRG_DIV 4
#else
#define BRG_DIV 16
#endif
#define BAUDRATEREG ((FCY + (BRG_DIV/2*BAUDRATE))/BRG_DIV/BAUDRATE-1)
#define BAUD_ACTUAL (FCY/BRG_DIV/(BAUDRATEREG+1))
#define BAUD_ERROR ((BAUD_ACTUAL > BAUDRATE) ? BAUD_ACTUAL-BAUDRATE : BAUDRATE-BAUD_ACTUAL)
#define BAUD_ERROR_PRECENT ((BAUD_ERROR*100+BAUDRATE/2)/BAUDRATE)
#if (BAUD_ERROR_PRECENT > 3)
#error "UART frequency error is worse than 3%"
#elif (BAUD_ERROR_PRECENT > 2)
#warning "UART frequency error is worse than 2%"
#endif
/****************************************************************************
Section: UART generic register defInitions
***************************************************************************/
//This section defines a generic Set of UART registers (e.g. UxMODE) which
//allow any UART module on the device to be used.
//The define UARTNUM selects which UART module is used.
#define UARTREG2(a,b) U##a##b
#define UARTREG(a,b) UARTREG2(a,b)
#define UARTINT2(a,b) _U##a##b
#define UARTINT(a,b) UARTINT2(a,b)
#define UxMODE UARTREG(UARTNUM,MODE)
#define UxBRG UARTREG(UARTNUM,BRG)
#define UxSTA UARTREG(UARTNUM,STA)
#define UxRXREG UARTREG(UARTNUM,RXREG)
#define UxTXREG UARTREG(UARTNUM,TXREG)
#define UxMODEbits UARTREG(UARTNUM,MODEbits)
#define UxSTAbits UARTREG(UARTNUM,STAbits)
#define _UxMD UARTINT(UARTNUM,MD)
#define _UxRXIF UARTINT(UARTNUM,RXIF)
#define _UxTXIF UARTINT(UARTNUM,TXIF)
#define mFlushUART() while(!UxSTAbits.TRMT)
/****************************************************************************
Section: UART function prototypes
***************************************************************************/
/*********************************************************************
Function: char UARTGetChar()
PreCondition: none
Input: none
Output: last character received
Side Effects: none
Overview: returns last character received
Note: none
********************************************************************/
char UARTGetChar();
/*********************************************************************
Function: void UARTPutChar(char ch)
PreCondition: none
Input: none
Output: none
Side Effects: none
Overview: puts character
Note: none
********************************************************************/
void UARTPutChar( char ch );
/*********************************************************************
Function: void UARTInit(void)
PreCondition: none
Input: none
Output: none
Side Effects: none
Overview: Initializes UART
Note: none
********************************************************************/
void UARTInit();
/*******************************************************************************
Function: UARTIsPressed()
Precondition:
UARTInit must be called prior to calling this routine.
Overview:
This routine checks to see if there is a new byte in UART reception buffer.
Input: None.
Output:
0 : No new data received.
1 : Data is in the receive buffer
*******************************************************************************/
char UARTIsPressed();
/*******************************************************************************
Function: UARTPrintString( char *str )
Precondition:
UARTInit must be called prior to calling this routine.
Overview:
This function prints a string of characters to the UART.
Input: Pointer to a null terminated character string.
Output: None.
*******************************************************************************/
void UARTPrintString( char *str );
/*******************************************************************************
Function: UARTPutDec(unsigned char dec)
Precondition:
UARTInit must be called prior to calling this routine.
Input: Binary data
Output: none
Side Effects: none
Overview: This function converts decimal data into a string
and outputs it to UART.
Note: none
*******************************************************************************/
void UARTPutDec( unsigned char dec );
/*******************************************************************************
Function: UARTPutHex
Precondition:
UARTInit must be called prior to calling this routine.
Input: Binary data
Output: none
Side Effects: none
Overview: This function converts hex data into a string
and outputs it to UART.
Note: none
*******************************************************************************/
void UARTPutHex( int toPrint );
/*******************************************************************************
Function: UARTPutHexWord(unsigned int toPrint)
Precondition:
UARTInit must be called prior to calling this routine.
Input: Binary data
Output: none
Side Effects: none
Overview: This function converts hex data into a string
and outputs it to UART.
Note: none
*******************************************************************************/
#if defined( __C30__ ) || defined( __PIC32MX__ )
void UARTPutHexWord( unsigned int toPrint );
void UARTPutHexDWord( unsigned long int toPrint );
#endif
/*********************************************************************
Function: char UARTChar2Hex(char ch)
PreCondition: none
Input: ASCII to be converted
Output: number
Side Effects: none
Overview: converts ASCII coded digit into number
Note: none
********************************************************************/
char UARTChar2Hex(char ch);
/*********************************************************************
Function: char UARTHex2Char(char hex)
PreCondition: none
Input: number
Output: ASCII code
Side Effects: none
Overview: converts low nibble into ASCII coded digit
Note: none
********************************************************************/
char UARTHex2Char(char hex);
/*********************************************************************
Function: void UARTClrError(void)
PreCondition: none
Input: none
Output: character received
Side Effects: none
Overview: wait for character
Note: none
********************************************************************/
void UARTClrError(void);
/*********************************************************************
Macros: UARTDataReceived()
PreCondition: none
Input: none
Output: zero if character is not received
Side Effects: none
Overview: checks if data is available
Note: none
********************************************************************/
#define UARTDataReceived() (UxSTAbits.URXDA)
#endif