//---------------------SCI1_InUHex---------------------------------------- // Accepts ASCII input in unsigned hexadecimal (base 16) format // Input: none // Output: 16-bit unsigned number // 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 // value range is 0 to FFFF // If you enter a number above FFFF, it will truncate without an error // Backspace will remove last digit typed unsigned short SCI1_InUHex(void){ unsigned short number=0, digit, length=0; char character; character = SCI1_InChar(); while(character != CR){ digit = 0x10; // assume bad if((character>='0') && (character<='9')){ digit = character-'0'; } else if((character>='A') && (character<='F')){ digit = (character-'A')+0xA; } 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++; SCI1_OutChar(character); } // Backspace outputted and return value changed if a backspace is inputted else if((character==BS) && length){ number /= 0x10; length--; SCI1_OutChar(character); } character = SCI1_InChar(); } return number; }
//--------------------------SCI1_OutUHex---------------------------- // Output a 16 bit number in unsigned hexadecimal format // Input: 16-bit number to be transferred // Output: none // Variable format 1 to 4 digits with no space before or after void SCI1_OutUHex(unsigned short number){ // This function uses recursion to convert the number of // unspecified length as an ASCII string if(number >= 0x10){ SCI1_OutUHex(number/0x10); SCI1_OutUHex(number%0x10); } else{ if(number < 0xA){ SCI1_OutChar(number+'0'); } else{ SCI1_OutChar((number-0x0A)+'A'); } } }
//-----------------------SCI1_OutUDec----------------------- // Output a 16-bit number in unsigned decimal format // Input: 16-bit number to be transferred // Output: none // Variable format 1-5 digits with no space before or after void SCI1_OutUDec(unsigned short n){ // This function uses recursion to convert decimal number // of unspecified length as an ASCII string if(n >= 10){ SCI1_OutUDec(n/10); n = n%10; } SCI1_OutChar(n+'0'); /* n is between 0 and 9 */ }
//------------------------SCI1_InString------------------------ // Accepts ASCII characters from the serial port // and adds them to a string until <enter> is typed // 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 // terminates the string with a null character // uses busy-waiting synchronization on RDRF // Input: pointer to empty buffer, size of buffer // Output: Null terminated string // -- Modified by Agustinus Darmawan + Mingjie Qiu -- void SCI1_InString(char *bufPt, unsigned short max) { int length=0; char character; character = SCI1_InChar(); while(character != CR){ if(character == BS){ if(length){ bufPt--; length--; SCI1_OutChar(BS); } } else if(length < max){ *bufPt = character; bufPt++; length++; SCI1_OutChar(character); } character = SCI1_InChar(); } *bufPt = 0; }
//----------------------SCI1_InUDec------------------------------- // InUDec accepts ASCII input in unsigned decimal format // and converts to a 16 bit unsigned number // valid range is 0 to 65535 // Input: none // Output: 16-bit unsigned number // If you enter a number above 65535, it will truncate without an error // Backspace will remove last digit typed unsigned short SCI1_InUDec(void){ unsigned short number=0, length=0; char character; character = SCI1_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 65535 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(); } return number; }
//-------------------------SCI1_OutString------------------------ // Output String (NULL termination), busy-waiting synchronization // Input: pointer to a NULL-terminated string to be transferred // Output: none void SCI1_OutString(char *pt){ while(*pt){ SCI1_OutChar(*pt); pt++; } }
//---------------------OutCRLF--------------------- // Output a CR,LF to SCI to go to a new line // Input: none // Output: none void OutCRLF(void){ SCI1_OutChar(CR); SCI1_OutChar(LF); PTP_PTP7 ^= 1; }