Download thesis2 - AUS Masters Theses
Transcript
Appendix D: Microcontroller Code
215
}
//−−−−−−−−−−−−−−−−−−−−−−SCI1 InSDec−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// InSDec accepts ASCII input in signed decimal format
//
and converts to a 16 bit signed number
//
valid range is −32768 to +32767
// Input: none
// Output: 16−bit signed number
// If you enter a number outside +/−32767, it will truncate
// without an error
// Backspace will remove last digit typed
signed int SCI1 InSDec(void) {
signed int number=0, length=0;
char sign = 0; // '0': pos, '1': neg
char character;
character = SCI1 InChar();
while(character!=CR) {
// accepts until carriage return input
// The next lines checks for an optional sign character ('+' or '−')
// and then that the input is a digit, 0−9.
// If the character is not 0−9, it is ignored and not echoed
if(character=='+') {
SCI1 OutChar(character);
} else if(character=='−') {
sign = 1;
SCI1 OutChar(character);
} else if((character≥'0') && (character≤'9')) {
// this line overflows if above 4294967296
number = 10*number+(character−'0');
length++;
SCI1 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−−;
SCI1 OutChar(character);
}
character = SCI1 InChar();
}
if(sign == 1) return −number;
else
return number;
}