//General UART send function void sendUART(unsigned char *frame_header, unsigned char *data, unsigned char length) { int i; unsigned char checksum=0; //send frame header for(i = 0; i < FRAME_HEADER_LEN; i++) { checksum += frame_header[i]; while(BusyUART1()); WriteUART1(frame_header[i]); } //send payload data for (i = 0; i < length; i++) { checksum += data[i]; while(BusyUART1()); WriteUART1(data[i]); } //Send Checksum Data while(BusyUART1()); WriteUART1(0xFF - checksum); }
//Handle an AT response packet and pass it to the sendUART function void xbeeHandleATR(unsigned char frame_id, WordVal command, unsigned char *data, unsigned char length) { unsigned char frame_header[5]; frame_header[RX_API_POS] = AT_RESPONSE; frame_header[ATR_FRAME_ID_POS] = frame_id; frame_header[ATR_COMMAND_HB_POS] = command.byte.HB; frame_header[ATR_COMMAND_LB_POS] = command.byte.LB; frame_header[ATR_STATUS_POS] = 0x00; CRITICAL_SECTION_START //Start Byte while(BusyUART1()); WriteUART1(RX_START); //Length High Byte while(BusyUART1()); WriteUART1(0x00); //Length Low Byte while(BusyUART1()); WriteUART1(length+FRAME_HEADER_LEN); sendUART(frame_header, data, length); CRITICAL_SECTION_END }
/* Text a phone number */ void gsmText(char *number, char *msg) { /* Store the beginning and ending sequences */ char *prefix = "AT+CMGS=\""; char *suffix = "\"\r\n"; /* Allocate space and then store concatenated text command */ size_t len1 = strlen(prefix); size_t len2 = strlen(number); size_t len3 = strlen(suffix); char *s = malloc(len1 + len2 + len3 + 1); memcpy(s, prefix, len1); memcpy(s + len1, number, len2); memcpy(s + len1 + len2, suffix, len3 + 1); /* Set GSM module to text mode */ gsmSendCommand("AT+CMGF=1\r"); /* Set the number */ gsmSendCommand(s); /* Delay to allow GSM module to get ready */ DELAY_MS(100); /* Set the message */ gsmSendCommand(msg); /* Send Text */ while(BusyUART1()); WriteUART1((unsigned int)CTRL_Z); /* Free memory */ free(s); }
/** * interface_test_uart: Loop n times (a long int), repeating the * character 'U' on the UART tx pin. */ void interface_test_uart(long int n) { // Enable transmit. U1STAbits.UTXEN = 1; while(n--) { while(BusyUART1()); WriteUART1('U'); } }
void SendLong(long num) { unsigned char c1, c10, c100, c1000, c10000, c100000; if(num < 0) { while(BusyUART1()); WriteUART1('-'); num = -num; } else { while(BusyUART1()); WriteUART1('+'); } c1 = num % 10; num = num / 10; c10 = num % 10; num = num / 10; c100 = num % 10; num = num / 10; c1000 = num % 10; num = num / 10; c10000 = num % 10; num = num / 10; c100000 = num % 10; num = num / 10; while(BusyUART1()); WriteUART1('0' + c100000); while(BusyUART1()); WriteUART1('0' + c10000); while(BusyUART1()); WriteUART1('0' + c1000); while(BusyUART1()); WriteUART1('0' + c100); while(BusyUART1()); WriteUART1('0' + c10); while(BusyUART1()); WriteUART1('0' + c1); }
// UART3 RX ISR void __attribute__ ((interrupt,no_auto_psv)) _U3RXInterrupt(void) { // Clear the interrupt status of UART1 RX U3RX_Clear_Intr_Status_Bit; if(DataRdyUART3()) { incoming_char = ReadUART3(); // Get the character coming from USB UART while(BusyUART1()); WriteUART1(incoming_char); // Send the character to the GSM UART while(BusyUART3()); WriteUART3(incoming_char); // Send the character to the USB UART } }
void PushUART1(byte b) { while (U1STAbits.UTXBF); // UART1 TX Buffer Full WriteUART1(b); checksumAX += b; }