//---------------------SCI_InUHex---------------------------------------- // Accepts ASCI0I input in unsigned hexadecimal (base 16) format // Input: SCI port // 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 SCI_InUHex(unsigned char port){ unsigned short number=0, digit, length=0; char character; character = SCI_InChar(port); 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++; SCI_OutChar(port, character); } // Backspace outputted and return value changed if a backspace is inputted else if((character==BS) && length){ number /= 0x10; length--; SCI_OutChar(port, character); } character = SCI_InChar(port); } return number; }
/** * \fn BOOL Packet_Put(const UINT8 command, const UINT8 parameter1, const UINT8 parameter2, const UINT8 parameter3) * \brief Builds a packet and places it in the transmit FIFO buffer. * \param command command byte * \param parameter1 first parameter byte * \param parameter2 second parameter byte * \param parameter3 third parameter byte * \return TRUE if a valid packet was queued for transmission successfully */ BOOL Packet_Put(const UINT8 command, const UINT8 parameter1, const UINT8 parameter2, const UINT8 parameter3) { return SCI_OutChar(command) && SCI_OutChar(parameter1) && SCI_OutChar(parameter2) && SCI_OutChar(parameter3) && SCI_OutChar(Packet_Checksum(command, parameter1, parameter2, parameter3)); }
//--------------------------SCI_OutUHex---------------------------- // Output a 16 bit number in unsigned hexadecimal format // Input: SCI port, 16-bit number to be transferred // Output: none // Variable format 1 to 4 digits with no space before or after void SCI_OutUHex(unsigned char port, unsigned short number){ // This function uses recursion to convert the number of // unspecified length as an ASCI0I string if(number >= 0x10){ SCI_OutUHex(port, number/0x10); SCI_OutUHex(port, number%0x10); } else if(number < 0xA){ SCI_OutChar(port, number+'0'); } else{ SCI_OutChar(port, (number-0x0A)+'A'); } }
//-----------------------SCI_OutSDec----------------------- // Output a 16-bit number in signed decimal format // Input: SCI port, 16-bit signed number to be transferred // Output: none // Variable format 2-6 digits with no space before or after void SCI_OutSDec(unsigned char port, short n){ if(n < 0){ SCI_OutChar(port, '-'); /* negative */ n = -n; } SCI_OutUDec(port, n); }
//-----------------------SCI_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 SCI_OutUDec(unsigned short n){ // This function uses recursion to convert decimal number // of unspecified length as an ASCII string if(n >= 10){ SCI_OutUDec(n/10); n = n%10; } SCI_OutChar(n+'0'); /* n is between 0 and 9 */ }
//-----------------------SCI_OutUDec32----------------------- // Output a 32-bit number in unsigned decimal format // Input: SCI port, 32-bit number to be transferred // Output: none // Variable format 1-10 digits with no space before or after void SCI_OutUDec32(unsigned char port, unsigned long n){ // This function uses recursion to convert decimal number // of unspecified length as an ASCI0I string if(n >= 10){ SCI_OutUDec32(port, n/10); n = n%10; } SCI_OutChar(port, n+'0'); /* n is between 0 and 9 */ }
//------------------------SCI_InString------------------------ // Accepts ASCI0I 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 interrupt synchronization on RDRF // Input: SCI port, pointer to empty buffer, size of buffer // Output: Null terminated string // -- Modified by Agustinus Darmawan + Mingjie Qiu -- void SCI_InString(unsigned char port, char *bufPt, unsigned short max) { int length=0; char character; character = SCI_InChar(port); while(character != CR){ if(character == BS){ if(length){ bufPt--; length--; SCI_OutChar(port, BS); } } else if(length < max){ *bufPt = character; bufPt++; length++; SCI_OutChar(port, character); } character = SCI_InChar(port); } *bufPt = 0; }
//----------------------SCI_InUDec------------------------------- // InUDec accepts ASCI0I input in unsigned decimal format // and converts to a 16 bit unsigned number // valid range is 0 to 65535 // Input: SCI port // 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 SCI_InUDec(unsigned char port){ unsigned short number=0, length=0; char character; character = SCI_InChar(port); while(character!=CR){ // accepts until carriage return input // 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++; SCI_OutChar(port, 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--; SCI_OutChar(port, character); } character = SCI_InChar(port); } return number; }
unsigned short SCI_InUDec2(void){ unsigned short number1=0, number2=0, number3=0, number4=0,number5=0, contnum=1, length=0; char character; character = SCI_InChar2(); 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') && (contnum==1)) { number1 = 10*number1+(character-'0'); // this line overflows if above 65535 length++; SCI_OutChar(character);OutCRLF(); } character = SCI_InChar(); } return number1; }
unsigned short SCI_InUDec3(void){ unsigned short number1=0, number2=0, number3=0, number4=0,number5=0, contnum=1, length=0; char character; character = SCI_InCharInitial(); while((character != CR)&&(InputTimeoutFlag==0)){ // accepts until <enter> is typed or timeout activated // 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') && (contnum==1)) { number1 = 10*number1+(character-'0'); // this line overflows if above 65535 length++; SCI_OutChar(character);//OutCRLF(); } character = SCI_InCharSecondary(); } if(InputTimeoutFlag==1){ return 66; } return number1; }
void OutCRLF(void){ SCI_OutChar(CR); SCI_OutChar(LF); PTJ ^= 0x20; // toggle LED D2 }
//---------------------OutCRLF--------------------- // Output a CR,LF to SCI to go to a new line // Input: none // Output: none // toggle PortT bit 0 each time, debugging profile void OutCRLF(void){ SCI_OutChar(CR); SCI_OutChar(LF); }
//-----------------------SCI_SOutFix2----------------------- // Output a 16-bit number in signed decimal fixed-point // with resolution = 0.01 // Input: SCI port, 16-bit signed number -9999 to 9999 // Output: none // fixed size, 6 characters, 4 digits of output, right justified // if input is -1234, then display is -12.34 void SCI_SOutFix2(unsigned char port, short num){ unsigned short n; if(num<0){ /* negative */ n = -num; if(n < 10){ SCI_OutString(port, " -0.0"); SCI_OutChar(port, n+'0'); /* n is between 0 and 9 */ } else if(n<100){ SCI_OutString(port, " -0."); SCI_OutChar(port, n/10+'0'); /* tens digit */ SCI_OutChar(port, n%10+'0'); /* ones digit */ } else if(n<1000){ SCI_OutString(port, " -"); SCI_OutChar(port, n/100+'0'); /* hundreds digit */ n = n%100; SCI_OutChar(port, '.'); /* decimal point */ SCI_OutChar(port, n/10+'0'); /* tens digit */ SCI_OutChar(port, n%10+'0'); /* ones digit */ } else if(n<10000){ SCI_OutChar(port, '-'); SCI_OutChar(port, n/1000+'0'); /* thousands digit */ n = n%1000; SCI_OutChar(port, n/100+'0'); /* hundreds digit */ n = n%100; SCI_OutChar(port, '.'); /* decimal point */ SCI_OutChar(port, n/10+'0'); /* tens digit */ SCI_OutChar(port, n%10+'0'); /* ones digit */ } else { SCI_OutString(port, "-**.**"); } } else{ /* positive */ n = num; if(n < 10){ SCI_OutString(port, " 0.0"); SCI_OutChar(port, n+'0'); /* n is between 0 and 9 */ } else if(n<100){ SCI_OutString(port, " 0."); SCI_OutChar(port, n/10+'0'); /* tens digit */ SCI_OutChar(port, n%10+'0'); /* ones digit */ } else if(n<1000){ SCI_OutString(port, " "); SCI_OutChar(port, n/100+'0'); /* hundreds digit */ n = n%100; SCI_OutChar(port, '.'); /* decimal point */ SCI_OutChar(port, n/10+'0'); /* tens digit */ SCI_OutChar(port, n%10+'0'); /* ones digit */ } else if(n<10000){ SCI_OutChar(port, ' '); SCI_OutChar(port, n/1000+'0'); /* thousands digit */ n = n%1000; SCI_OutChar(port, n/100+'0'); /* hundreds digit */ n = n%100; SCI_OutChar(port, '.'); /* decimal point */ SCI_OutChar(port, n/10+'0'); /* tens digit */ SCI_OutChar(port, n%10+'0'); /* ones digit */ } else { SCI_OutString(port, " **.**"); } } }
//-----------------------SCI_OutFix3----------------------- // Output a 16-bit number in unsigned decimal fixed-point // with resolution = 0.001 // Input: SCI port, 16-bit unsigned number // Output: none // fixed size is 6 characters of output, right justified // if input is 12345, then display is 12.345 void SCI_OutFix3(unsigned char port, unsigned short n){ if(n < 10){ SCI_OutString(port, " 0.00"); SCI_OutChar(port, n+'0'); /* n is between 0 and 9 */ } else if(n<100){ SCI_OutString(port, " 0.0"); SCI_OutChar(port, n/10+'0'); /* tens digit */ SCI_OutChar(port, n%10+'0'); /* ones digit */ } else if(n<1000){ SCI_OutString(port, " 0."); SCI_OutChar(port, n/100+'0'); /* hundreds digit */ n = n%100; SCI_OutChar(port, n/10+'0'); /* tens digit */ SCI_OutChar(port, n%10+'0'); /* ones digit */ } else if(n<10000){ SCI_OutChar(port, ' '); SCI_OutChar(port, n/1000+'0'); /* thousands digit */ SCI_OutChar(port, '.'); /* decimal point */ n = n%1000; SCI_OutChar(port, n/100+'0'); /* hundreds digit */ n = n%100; SCI_OutChar(port, n/10+'0'); /* tens digit */ SCI_OutChar(port, n%10+'0'); /* ones digit */ } else { SCI_OutChar(port, n/10000+'0'); /* ten-thousands digit */ n = n%10000; SCI_OutChar(port, n/1000+'0'); /* thousands digit */ SCI_OutChar(port, '.'); /* decimal point */ n = n%1000; SCI_OutChar(port, n/100+'0'); /* hundreds digit */ n = n%100; SCI_OutChar(port, n/10+'0'); /* tens digit */ SCI_OutChar(port, n%10+'0'); /* ones digit */ } }
//---------------------SCI_OutCRLF--------------------- // Output a CR,LF to SCI0 to go to a new line // Input: SCI port // Output: none // toggle PortT bit 0 each time, debugging profile void SCI_OutCRLF(unsigned char port){ SCI_OutChar(port, CR); SCI_OutChar(port, LF); }
//-------------------------SCI_OutString------------------------ // Output String (NULL termination), busy-waiting synchronization // Input: pointer to a NULL-terminated string to be transferred // Output: none void SCI_OutString(char *pt){ while(*pt){ SCI_OutChar(*pt); pt++; } }
//-------------------------SCI_OutString------------------------ // Output String (NULL termination), interrupt synchronization // Input: SCI port, pointer to a NULL-terminated string to be transferred // Output: none void SCI_OutString(unsigned char port, char *pt){ while(*pt){ SCI_OutChar(port, *pt); pt++; } }