-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnu_avr_sciutil.c
315 lines (267 loc) · 9.45 KB
/
gnu_avr_sciutil.c
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
// filename *************** SCIutil.C ********
// Serial port I/O routines, utilities
#ifndef CR
#define CR 0x0d
#endif
#ifndef LF
#define LF 0x0a
#endif
#ifndef BS
#define BS 0x08
#endif
#ifndef SPACE
#define SPACE 0x20
#endif
extern void SCI_OutChar(char );
extern unsigned short SCI_InChar(void);
/*
// 하나의 문자를 입출력 하는 함수(SCI_OutChar, SCI_InChar)로 장치
// ( 예: Debug, LCD 등)의 특성에 따라 이 함수를 다시 작성 하면 아래의
// 문자 및 숫자를 입출력 하는 함수를 모두 사용할 수 있다.
extern void SCI_OutChar(char );
extern unsigned short SCI_InChar(void)
//-------------------------SCI_OutChar to USART------------------------
void SCI_OutChar(letter){
tx0_char(letter);
}
//-------------------------SCI_InChar from USART------------------------
unsigned short SCI_InChar(){
return (rx0_char());
}
*/
//-------------------------SCI_OutString------------------------
// Output String (NULL termination)
void SCI_OutString(char *pt){ char letter;
while((letter=*pt++)){
SCI_OutChar(letter);
}
}
//--------------------SCI_UpCase-------------------------------
// converts lowercase to uppercase
// char by subtracting $20 from lowercase ASCII to make uppercase ASCII
char SCI_UpCase(char character){
return ((character>='a') && (character<='z'))?character-0x20:character;}
//----------------------SCI_InUDec-------------------------------
// InUDec accepts ASCII input in unsigned decimal format
// and converts to a 16 bit unsigned number
// with a maximum value of 65535
// If you enter a number above 65535, it will truncate without reporting the error
// Backspace will remove last digit typed
unsigned short SCI_InUDec(void){
unsigned short number=0, length=0;
unsigned char character;
character=SCI_InChar();
// 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
while(((character>='0') && (character<='9'))){
number = 10*number+(character-'0'); // this line overflows if above 65535
length++;
SCI_OutChar(character);
character=SCI_InChar();
}
return number;
}
//----------------------SCI_InUDec-------------------------------
// InUDec accepts ASCII input in unsigned decimal format
// and converts to a 16 bit unsigned number
// with a maximum value of 65535
// If you enter a number above 65535, it will truncate without reporting the error
// Backspace will remove last digit typed
unsigned short SCI_InUDec_OpCode(char * op_code){
unsigned short number=0, length=0;
unsigned char character;
character=SCI_InChar();
// 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
while(((character>='0') && (character<='9'))){
number = 10*number+(character-'0'); // this line overflows if above 65535
length++;
SCI_OutChar(character);
character=SCI_InChar();
}
*op_code = character;
return number;
}
//----------------------------SCI_InSDec-----------------------------
// InSDec accepts ASCII input in signed decimal format
// and converts to a signed 16 bit number
// with an absolute value up to 32767
// If you enter a number above 32767 or below -32767,
// it will truncate without reporting the error
// Backspace will remove last digit typed
short SCI_InSDec(void){
short number=0, sign=1; // sign flag 1=positive 0=negative
unsigned int length=0;
unsigned char character;
character=SCI_InChar();
if(character=='-'){
sign = -1;
length++;
SCI_OutChar('-'); // if - inputted, sign is negative
}
else if(character=='+'){
length++;
SCI_OutChar('+'); //if + inputted, sign is positive
}
if((character=='-') || (character=='+'))character=SCI_InChar();
// 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
while(((character>='0') && (character<='9'))){
number = 10*number+(character-'0'); // this line overflows if above 65535
length++;
SCI_OutChar(character);
character=SCI_InChar();
}
return sign*number;
}
//----------------------------SCI_InSDec-----------------------------
// InSDec accepts ASCII input in signed decimal format
// and converts to a signed 16 bit number
// with an absolute value up to 32767
// If you enter a number above 32767 or below -32767,
// it will truncate without reporting the error
// Backspace will remove last digit typed
short SCI_InSDec_OpCode(char * op_code){
short number=0, sign=1; // sign flag 1=positive 0=negative
unsigned int length=0;
unsigned char character;
character=SCI_InChar();
if(character=='-'){
sign = -1;
length++;
SCI_OutChar('-'); // if - inputted, sign is negative
}
else if(character=='+'){
length++;
SCI_OutChar('+'); //if + inputted, sign is positive
}
if((character=='-') || (character=='+'))character=SCI_InChar();
// 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
while(((character>='0') && (character<='9'))){
number = 10*number+(character-'0'); // this line overflows if above 65535
length++;
SCI_OutChar(character);
character=SCI_InChar();
}
*op_code = character;
return sign*number;
}
//-----------------------SCI_OutUDec-----------------------
// Output a 16 bit number in unsigned decimal format
// Variable format 1-5 digits with no space before or after
// This function uses recursion to convert decimal number
// of unspecified length as an ASCII string
void SCI_OutUDec(unsigned short n){
if(n >= 10){
SCI_OutUDec(n/10); // Recursive Operation
n=n%10;
}
SCI_OutChar(n+'0'); /* n is between 0 and 9 */
}
//----------------------SCI_OutSDec---------------------------------------
// Output a 16 bit number in signed decimal format
// Variable format (optional sign)1 to 5 digits with no space before or after
// This function checks if the input parameter is negative,
// If the number is negative, then
// 1) it outputs a "-",
// 2) negates the number and
// 3) outputs it with OutUDec.
// Otherwise, it just calls OutUDec (i.e., no "+" sign)
void SCI_OutSDec(short number){
if(number<0){
number = -number;
SCI_OutChar('-');
}
SCI_OutUDec(number);
}
//---------------------SCI_InUHex----------------------------------------
// InUHex accepts ASCII input in unsigned hexadecimal (base 16) format
// No '$' or '0x' need be entered, just the 1 to 4 hex digits
// It will convert lower case a-f to uppercase A-F
// and converts to a 16 bit unsigned number
// with a maximum value of FFFF
// If you enter a number above FFFF, it will truncate without reporting the error
// Backspace will remove last digit typed
unsigned short SCI_InUHex(void){
unsigned short number=0, digit, length=0;
unsigned char character;
while((character=SCI_UpCase(SCI_InChar()))!=CR){
digit = 0x10; // assume bad
if((character>='0') && (character<='9')){
digit = character-'0';
}
else if((character>='A') && (character<='F')){
digit = (character-'A')+0xA;
}
// If the character is not 0-9 or A-F, it is ignored and not echoed
if(digit<=0xF ){
number = number*0x10+digit;
length++;
SCI_OutChar(character);
}
// Backspace outputted and return value changed if a backspace is inputted
else if(character==BS && length){
number /=0x10;
length--;
SCI_OutChar(character);
}
}
return number;
}
//--------------------------SCI_OutUHex----------------------------
// Output a 16 bit number in unsigned hexadecimal format
// Variable format 1 to 4 digits with no space before or after
// This function uses recursion to convert the number of
// unspecified length as an ASCII string
void SCI_OutUHex(unsigned short number){
if(number>=0x10) {
SCI_OutUHex(number/0x10);
SCI_OutUHex(number%0x10);
}
else if(number<0xA){
SCI_OutChar(number+'0');
}
else{
SCI_OutChar((number-0x0A)+'A');
}
}
//------------------------SCI_InString------------------------
// This function accepts ASCII characters from the serial port
// and adds them to a string until a carriage return is inputted
// or until max length of the string is reached.
// It echoes each character as it is inputted.
// If a backspace is inputted, the string is modified
// and the backspace is echoed
// InString terminates the string with a null character
// -- Modified by Agustinus Darmawan + Mingjie Qiu --
void SCI_InString(char *string, unsigned int max) {
unsigned int length=0;
unsigned char character;
while((character=SCI_InChar())!=CR){
if(character==BS){
if(length){
string--;
length--;
SCI_OutChar(BS);
}
}
else if(length<max){
*string++=character;
length++;
SCI_OutChar(character);
}
}
*string = 0; // 문자열의 끝에 Null code 삽입함.
}
//------------------------SCI_upCaseString------------------------
// converts a NULL terminated string to uppercase
void SCI_upCaseString(char *inString){
char *pt = inString;
// 'a' = 0x61 and 'A' = 0x41, so their difference is 0x20
while(*pt){ // NULL => done
if((*pt >= 'a') && (*pt <= 'z'))
*pt -= 0x20;
pt++;
}
}