void New_Line(int i) { switch(i) { case 1: UART1_OutChar(0x0A); /* n is between 0 and 9 */ UART1_OutChar(0x0D); break; case 2: UART2_OutChar(0x0A); UART2_OutChar(0x0D); break; case 3: UART3_OutChar(0x0A); UART3_OutChar(0x0D); break; case 4: UART4_OutChar(0x0A); UART4_OutChar(0x0D); break; case 5: UART5_OutChar(0x0A); UART5_OutChar(0x0D); break; case 7: UART7_OutChar(0x0A); UART7_OutChar(0x0D); break; } }
//---------------------UART_InUHex---------------------------------------- // Accepts ASCII input in unsigned hexadecimal (base 16) format // Input: none // Output: 32-bit unsigned number // No '$' or '0x' need be entered, just the 1 to 8 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 FFFFFFFF // If you enter a number above FFFFFFFF, it will return an incorrect value // Backspace will remove last digit typed unsigned long UART1_InUHex(void){ unsigned long number=0, digit, length=0; char character; character = UART1_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++; UART1_OutChar(character); } // Backspace outputted and return value changed if a backspace is inputted else if((character==BS) && length){ number /= 0x10; length--; UART1_OutChar(character); } character = UART1_InChar(); } return number; }
void Print_Space(int n,int i) { while(n) { switch(i) { case 1: UART1_OutChar(' '); /* n is between 0 and 9 */ break; case 2: UART2_OutChar(' '); break; case 3: UART3_OutChar(' '); break; case 4: UART4_OutChar(' '); break; case 5: UART5_OutChar(' '); break; case 7: UART7_OutChar(' '); break; } n--; } }
void UART_OutUDec(unsigned long n,int i){ if(n >= 10){ UART_OutUDec(n/10,i); n = n%10; } switch(i) { case 1: UART1_OutChar(n+'0'); /* n is between 0 and 9 */ break; case 2: UART2_OutChar(n+'0'); break; case 3: UART3_OutChar(n+'0'); break; case 4: UART4_OutChar(n+'0'); break; case 5: UART5_OutChar(n+'0'); break; case 7: UART7_OutChar(n+'0'); break; } }
//--------------------------UART_OutUHex---------------------------- // Output a 32-bit number in unsigned hexadecimal format // Input: 32-bit number to be transferred // Output: none // Variable format 1 to 8 digits with no space before or after void UART1_OutUHex(unsigned long number){ // This function uses recursion to convert the number of // unspecified length as an ASCII string if(number >= 0x10){ UART1_OutUHex(number/0x10); UART1_OutUHex(number%0x10); } else{ if(number < 0xA){ UART1_OutChar(number+'0'); } else{ UART1_OutChar((number-0x0A)+'A'); } } }
//-----------------------UART_OutUDec----------------------- // Output a 32-bit number in unsigned decimal format // Input: 32-bit number to be transferred // Output: none // Variable format 1-10 digits with no space before or after void UART1_OutUDec(unsigned long n){ // This function uses recursion to convert decimal number // of unspecified length as an ASCII string if(n >= 10){ UART1_OutUDec(n/10); n = n%10; } UART1_OutChar(n+'0'); /* n is between 0 and 9 */ }
//------------UART_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 UART1_InString(char *bufPt, unsigned short max) { int length=0; char character; character = UART1_InChar(); while(character != CR){ if(character == BS){ if(length){ bufPt--; length--; UART1_OutChar(BS); } } else if(length < max){ *bufPt = character; bufPt++; length++; UART1_OutChar(character); } character = UART1_InChar(); } *bufPt = 0; }
//------------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 UART1_InUDec(void){ unsigned long number=0, length=0; char character; character = UART1_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++; UART1_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--; UART1_OutChar(character); } character = UART1_InChar(); } return number; }
void Timer2A_Handler(void){ uint32_t data2[2]; ADC0_In(data2); uint32_t data = data2[0]; // 2. Sample ADC (12-bit channel 1) // data=Convert(data); // 4. Convert to distance and create the 8-byte message uint8_t message[]= {0x02,0,0x2E,0,0,0,0x0D,0x03}; message[1] = (data/1000)%10 + 0x30; message[3] = (data/100)%10 + 0x30; message[4] = (data/10)%10 + 0x30; message[5] = data%10 + 0x30; for(int i=0; i<8; i++){ // 5. Send the 8-byte message to the other computer (calls UART1_OutChar 8 times) UART1_OutChar(message[i]); } TxCounter++; // 6. increment TxCounter (for debugging) TIMER2_ICR_R = TIMER_ICR_TATOCINT;// acknowledge TIMER2A timeout... Systick was using .25ms interrupts to transmit data }
void SysTick_Handler(){ PF2 ^= 0x04; // 1. Toggle heartbeat uint32_t data = ADC_In(); // 2. Sample ADC (12-bit channel 1) PF2 ^= 0x04; // 3. Toggle heartbeat data=Convert(data); // 4. Convert to distance and create the 8-byte message uint8_t message[]= {0x02,0,0x2E,0,0,0,0x0D,0x03}; message[1] = (data/1000)%10 + 0x30; message[3] = (data/100)%10 + 0x30; message[4] = (data/10)%10 + 0x30; message[5] = data%10 + 0x30; for(int i=0; i<8; i++){ // 5. Send the 8-byte message to the other computer (calls UART1_OutChar 8 times) UART1_OutChar(message[i]); } TxCounter++; // 6. increment TxCounter (for debugging) PF2 ^= 0x04; // 7. Toggle heartbeat NVIC_ST_RELOAD_R = 2000000-1; // 8. Return from interrupt NVIC_ST_CURRENT_R = 0; // Reset SysTick timer for .25ms }
void UART_OutChar(unsigned char data,int i) { switch(i) { case 1: UART1_OutChar(data); break; case 2: UART2_OutChar(data); break; case 3: UART3_OutChar(data); break; case 4: UART4_OutChar(data); break; case 5: UART5_OutChar(data); break; case 7: UART7_OutChar(data); break; } }
//------------UART_OutString------------ // Output String (NULL termination) // Input: pointer to a NULL-terminated string to be transferred // Output: none void UART1_OutString(char *pt){ while(*pt){ UART1_OutChar(*pt); pt++; } }