forked from KarenWest/EmbeddedSystems_Lab11_UART
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUART.c
More file actions
321 lines (296 loc) · 9.84 KB
/
UART.c
File metadata and controls
321 lines (296 loc) · 9.84 KB
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
// UART.c
// Runs on TM4C123 or LM4F120
// Lab 11 involves switching this from UART1 to UART0.
// switching from PC5,PC4 to PA1,PA0
// Daniel Valvano
// August 3, 2013
/* This example accompanies the book
"Embedded Systems: Introduction to ARM Cortex M Microcontrollers",
ISBN: 978-1469998749, Jonathan Valvano, copyright (c) 2013
Copyright 2013 by Jonathan W. Valvano, valvano@mail.utexas.edu
You may use, edit, run or distribute this file
as long as the above copyright notice remains
THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
For more information about my classes, my research, and my books, see
http://users.ece.utexas.edu/~valvano/
*/
// this connection occurs in the USB debugging cable
// U0Rx (PA0) connected to serial port on PC
// U0Tx (PA1) connected to serial port on PC
// Ground connected ground in the USB cable
#include "tm4c123gh6pm.h"
#include "UART.h"
unsigned char String[10];
//------------UART_Init------------
// Initialize the UART for 115200 baud rate (assuming 80 MHz UART clock),
// 8 bit word length, no parity bits, one stop bit, FIFOs enabled
// Input: none
// Output: none
void UART_Init(void){
// as part of Lab 11, modify this program to use UART0 instead of UART1
// switching from PC5,PC4 to PA1,PA0
SYSCTL_RCGC1_R |= SYSCTL_RCGC1_UART0; // activate UART0
SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOA; // activate port A
UART0_CTL_R &= ~UART_CTL_UARTEN; // disable UART
UART0_IBRD_R = 43; // IBRD = int(80,000,000 / (16 * 115200)) = int(43.402778)
UART0_FBRD_R = 26; // FBRD = round(0.402778 * 64) = 26
// 8 bit word length (no parity bits, one stop bit, FIFOs)
UART0_LCRH_R = (UART_LCRH_WLEN_8|UART_LCRH_FEN);
UART0_CTL_R |= UART_CTL_UARTEN; // enable UART
GPIO_PORTA_AFSEL_R |= 0x03; // enable alt funct on PA1,PA0
GPIO_PORTA_DEN_R |= 0x03; // enable digital I/O on PA1,PA0
// configure PA1,PA0 as UART0
GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R&0xFFFFFF00)+0x00000011;
GPIO_PORTA_AMSEL_R &= ~0x03; // disable analog functionality on PA1,PA0
}
//------------UART_InChar------------
// Wait for new serial port input
// Input: none
// Output: ASCII code for key typed
unsigned char UART_InChar(void){
// as part of Lab 11, modify this program to use UART0 instead of UART1
while((UART0_FR_R&UART_FR_RXFE) != 0);
return((unsigned char)(UART0_DR_R&0xFF));
}
//------------UART_InCharNonBlocking------------
// Get oldest serial port input and return immediately
// if there is no data.
// Input: none
// Output: ASCII code for key typed or 0 if no character
unsigned char UART_InCharNonBlocking(void){
// as part of Lab 11, modify this program to use UART0 instead of UART1
if((UART0_FR_R&UART_FR_RXFE) == 0){
return((unsigned char)(UART0_DR_R&0xFF));
} else{
return 0;
}
}
//------------UART_OutChar------------
// Output 8-bit to serial port
// Input: letter is an 8-bit ASCII character to be transferred
// Output: none
void UART_OutChar(unsigned char data){
// as part of Lab 11, modify this program to use UART0 instead of UART1
while((UART0_FR_R&UART_FR_TXFF) != 0);
UART0_DR_R = data;
}
//------------UART_InUDec------------
// InUDec accepts ASCII input in unsigned decimal format
// and converts to a 32-bit unsigned number
// valid range is 0 to 4294967295 (2^32-1)
// Input: none
// Output: 32-bit unsigned number
// If you enter a number above 4294967295, it will return an incorrect value
// Backspace will remove last digit typed
unsigned long UART_InUDec(void){
unsigned long number=0, length=0;
char character;
character = UART_InChar();
while(character != CR){ // accepts until <enter> is typed
// The next line checks that the input is a digit, 0-9.
// If the character is not 0-9, it is ignored and not echoed
if((character>='0') && (character<='9')) {
number = 10*number+(character-'0'); // this line overflows if above 4294967295
length++;
UART_OutChar(character);
}
// If the input is a backspace, then the return number is
// changed and a backspace is outputted to the screen
else if((character==BS) && length){
number /= 10;
length--;
UART_OutChar(character);
}
character = UART_InChar();
}
return number;
}
//------------UART_OutString------------
// Output String (NULL termination)
// Input: pointer to a NULL-terminated string to be transferred
// Output: none
void UART_OutString(unsigned char buffer[]){
// as part of Lab 11 implement this function
unsigned char *pBuffer;
if (buffer != 0) {
pBuffer = &buffer[0];
while (*pBuffer != 0) {
UART_OutChar(*pBuffer);
pBuffer++;
}
//UART_OutChar(0);
}
}
//-----------------------UART_ConvertUDec-----------------------
// Converts a 32-bit number in unsigned decimal format
// Input: 32-bit number to be transferred
// Output: store the conversion in global variable String[10]
// Fixed format 4 digits, one space after, null termination
// Examples
// 4 to " 4 "
// 31 to " 31 "
// 102 to " 102 "
// 2210 to "2210 "
//10000 to "**** " any value larger than 9999 converted to "**** "
void UART_ConvertUDec(unsigned long n){
// as part of Lab 11 implement this function
unsigned long onesDigit = 0;
unsigned long numDivBy10 = n;
unsigned char numDigits = 0;
unsigned char i;
unsigned char digitsArray[5];
for (i = 0; i < 5; i++)
digitsArray[i] = 0;
for (i = 0; i < 10; i++)
String[i] = 0;
if (n < 10000) {
if (n == 0) {
String[0] = ' ';
String[1] = ' ';
String[2] = ' ';
String[3] = 0x30;
String[4] = ' ';
}
else {
while (numDivBy10 > 0) {
onesDigit = numDivBy10 % 10;
numDivBy10 = numDivBy10 / 10;
digitsArray[numDigits] = onesDigit + 0x30;
numDigits++;
}
switch(numDigits) {
case 1:
String[0] = ' ';
String[1] = ' ';
String[2] = ' ';
String[3] = digitsArray[0];
String[4] = ' ';
break;
case 2:
String[0] = ' ';
String[1] = ' ';
String[2] = digitsArray[1];
String[3] = digitsArray[0];
String[4] = ' ';
break;
case 3:
String[0] = ' ';
String[1] = digitsArray[2];
String[2] = digitsArray[1];
String[3] = digitsArray[0];
String[4] = ' ';
break;
case 4:
String[0] = digitsArray[3];
String[1] = digitsArray[2];
String[2] = digitsArray[1];
String[3] = digitsArray[0];
String[4] = ' ';
break;
}
}
}
else { //n > 10000
for (i = 0; i < 4; i++)
String[i] = '*';
String[4] = ' ';
String[5] = 0;
}
}
//-----------------------UART_OutUDec-----------------------
// Output a 32-bit number in unsigned decimal format
// Input: 32-bit number to be transferred
// Output: none
// Fixed format 4 digits, one space after, null termination
void UART_OutUDec(unsigned long n){
UART_ConvertUDec(n); // convert using your function
UART_OutString(String); // output using your function
}
//-----------------------UART_ConvertDistance-----------------------
// Converts a 32-bit distance into an ASCII string
// Input: 32-bit number to be converted (resolution 0.001cm)
// Output: store the conversion in global variable String[10]
// Fixed format 1 digit, point, 3 digits, space, units, null termination
// Examples
// 4 to "0.004 cm"
// 31 to "0.031 cm"
// 102 to "0.102 cm"
// 2210 to "2.210 cm"
//10000 to "*.*** cm" any value larger than 9999 converted to "*.*** cm"
void UART_ConvertDistance(unsigned long n){
// as part of Lab 11 implement this function
unsigned long onesDigit= 0;
unsigned long numDivBy10 = n;
unsigned char numDigits = 0;
unsigned char i;
unsigned char digitsArray[8];
for (i = 0; i < 5; i++)
digitsArray[i] = 0;
for (i = 0; i < 10; i++)
String[i] = 0;
String[1] = '.';
String[5] = ' ';
String[6] = 'c';
String[7] = 'm';
if (n < 10000) {
if (n == 0) {
String[0] = 0x30;
String[2] = 0x30;
String[3] = 0x30;
String[4] = 0x30;
}
else {
while ((numDivBy10 > 0) && (numDigits < 5) ){
onesDigit = numDivBy10 % 10;
numDivBy10 = numDivBy10 / 10;
digitsArray[numDigits] = onesDigit + 0x30;
numDigits++;
}
}
switch(numDigits) {
case 1:
String[0] = 0x30;
String[2] = 0x30;
String[3] = 0x30;
String[4] = digitsArray[0];
break;
case 2:
String[0] = 0x30;
String[2] = 0x30;
String[3] = digitsArray[1];
String[4] = digitsArray[0];
break;
case 3:
String[0] = 0x30;
String[2] = digitsArray[2];
String[3] = digitsArray[1];
String[4] = digitsArray[0];
break;
case 4:
String[0] = digitsArray[3];
String[2] = digitsArray[2];
String[3] = digitsArray[1];
String[4] = digitsArray[0];
break;
}
}
else { //n > 10000
for (i = 0; i < 5; i++) {
if (i != 1)
String[i] = '*';
}
}
}
//-----------------------UART_OutDistance-----------------------
// Output a 32-bit number in unsigned decimal fixed-point format
// Input: 32-bit number to be transferred (resolution 0.001cm)
// Output: none
// Fixed format 1 digit, point, 3 digits, space, units, null termination
void UART_OutDistance(unsigned long n){
UART_ConvertDistance(n); // convert using your function
UART_OutString(String); // output using your function
}