MCU serial port sending and receiving program, the following is about GPS_CHECKTIAMER, local application program #define UART_R_LEN 60 #define UART_T_LEN 34 uchar idata trdata[UART_T_LEN]; //Define the serial port send buffer uchar idata gpsdata[UART_R_LEN]; //Define the GPS data serial port receiving buffer uchar *inlast_t = trdata; //Last put into the send buffer uchar *outlast_t = trdata; //The last out from the send buffer uchar *inlast_r = gpsdata; //The last entry into the receive buffer uchar *outlast_r = gpsdata; //The last one taken from the receiving buffer bit t_empty,t_full,r_empty,r_full; //Buffer status flag bit new_time,t_finish; //Receiving completion flag and sending completion flag void serail(void) interrupt 4 { if(TI) //If the send interrupt is set { TI = 0; if(!t_empty) //If the send buffer data is not sent out { SBUF = *outlast_t; //The last character out of the send buffer is sent to SBUF outlast_t++; //The last character pointer address is increased by 1 t_full = 0; //The sending area status is not full if(outlast_t >= (trdata + UART_T_LEN)) //If the address of the last character sent exceeds outlast_t = trdata; //Address to the top and back to the bottom if(outlast_t == inlast_t) //If the address of the last character sent is the address of the last character sent t_empty = 1; } else t_finish = 1; } if(RI) //If the receive interrupt is set { RI = 0; if(~r_full) //If the receive buffer is not full { *inlast_r = SBUF; //The last character entering the receive buffer is sent to SBUF inlast_r++; //The last character address entering the receive buffer increases by 1 r_empty = 0; //Receive buffer is not empty if(inlast_r >= (gpsdata + UART_R_LEN)) //If the address of the last received character exceeds inlast_r = gpsdata; //Address to the top and back to the bottom if(inlast_r == outlast_r)
//If the address of the last character received is equal to the address of the last character taken from the receiving area r_full = 1; //The receiving area is full } } } void loadmsg(uchar *msg, int num) //Put the string into the send buffer and prepare to send { int i; for(i=0; i= (trdata+UART_T_LEN)) inlast_t = trdata; //Address to the top and back to the bottom if(inlast_t == outlast_t) t_full = 1; //Send buffer is full } if(t_finish) { TI = 1; t_finish = 0; } } uchar getbyte_r(void) //Get a byte from the receive buffer { uchar c; while(r_empty); //When the receive buffer is waiting for reception ES = 0; //Disable serial interrupt c = *outlast_r; //The last data taken from the receiving buffer is assigned to C r_full = 0; //Receiving is not full outlast_r++; //The last data address taken from the receiving buffer increases by 1 if(outlast_r >= (gpsdata + UART_R_LEN)) //If the address exceeds outlast_r = gpsdata; //Address back to original place if(outlast_r == inlast_r)
//If the address of the last character received is equal to the address of the last character taken from the receiving area r_empty = 1; //The receive buffer is empty ES = 1; //Resume serial interrupt return c; } uchar r_state = 0; uchar r_byte = 0; uchar gps_chksum= 0; void r_string(void) // Receive string { uint *ptr; uchar temp; uchar tp[7];
//Define the time stamp receiving area. The stored time stamp data is the time stamp that has not been converted in the receiving buffer. It is used as the transfer storage of the global time stamp while(!r_empty && !new_time) //If the receive buffer is not empty and the receive is not completed { temp = getbyte_r(); //Each while loop takes in one byte of received data switch(r_state)
//If the data sequentially taken in conforms to the header "@@Hb", r_state position 4, enter the time stamp and GPS status byte { case 0: if(temp == 0x40) //@ r_state = 1; break; case 1: if(temp == 0x40) //@ { r_state = 2; gps_chksum = 0; } else r_state = 0; break; case 2: if(temp == 0x48) //H { r_state = 3; gps_chksum = gps_chksum^temp; } else r_state = 0; break; case 3: if(temp == 0x62) //b { r_state = 4; r_byte = 0; gps_chksum = gps_chksum^temp; } else r_state = 0; break; case 4: if(r_byte == 47) { if(gps_chksum == temp)
//After completing the useful data collection, after data conversion, the standard characters are stored in the global time stamp buffer { ptr = (uint*)(tp+2); //Send the converted data to the time stamp buffer gps_dt[0] = (*ptr)%100; //year gps_dt[1] = tp[0]; //month gps_dt[2] = tp[1]; //day gps_dt[3] = tp[4]; //time gps_dt[4] = tp[5]; // points gps_dt[5] = tp[6]; // seconds new_time = 1; } r_state = 0; //Reset the data used for judgment to zero so that the data can be retrieved next time } else { gps_chksum = gps_chksum^temp; if(r_byte <7) tp[r_byte] = temp; else if(r_byte == 38) gps_state = temp; r_byte++; } break; default: break; } } }
Previous article:The magic of pointers in C language
Next article:MCU detects short and continuous keystrokes C program
Recommended ReadingLatest update time:2024-11-17 01:48
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- Does the embedded industry strictly restrict college education?
- BAW filters help 5G achieve high-quality communications and higher efficiency
- [RISC-V MCU CH32V103 Review] Hardware IIC drive OLED (finally)
- 2 mistakes in the RSL10 official getting started guide
- ADC10 multi-channel input of msp430g2553
- 【TI Recommended Course】#Texas Instruments Field Transmitter Output Interface/Fieldbus Solution#
- 【CH579M-R1】+Love for CH579M
- Module Design and FPGA Verification of 64-bit MIPS Microprocessor
- MSP430 MCU Example 10-8-bit Digital Tube Display Clock
- Getting Started with the ST SensorTile.box Sensor Kit (4) Programming Mode Experience