I want to control the LED through the serial port, but the serial port can only send one character each time it is interrupted. So it is not easy to know which part each command controls when controlling it. So I refer to the information on the Internet and write a simple string receiving and sending function. The function of this program is: the computer serial port assistant sends data to the microcontroller, and the microcontroller returns it to the computer after receiving it. When sending, you need to leave two spaces and then enter the content you want to send, otherwise the transmitted data will be lost. Because this situation does not affect the serial port control of the LED, I don't care about it.
The microcontroller source program is as follows:
#include typedef unsigned char uchar; typedef unsigned int uint; sbit led=P2^0; void Usart_Init() { TMOD=0X20; PCON=0X80; SCON=0X50; TH1=0XF3; TL1=0XF3; TR1=1; EN=1; EA=1; } float strk[50]; void delay(unsigned int i){unsigned int j;for(i;i>0;i--)for(j=0;j<120;j++);}//延时函数 void sendbyte(unsigned char byt){SBUF=byt;while(!TI);TI=0;} //Serial port character sending function void receivebyte() /*Serial port receiving function*/ { uchar shiftdata; //Transfer data variable, store the data line received by SBUF to this place, and then transfer it to the array static uint i=0; if(RI){ RI=0;shiftdata=SBUF;strk[i]=shiftdata; i++; if(shiftdata==''||shiftdata=='n'){i=0;} } } /*The following is the serial port string sending function*/ void sendstring(unsigned char *string){ uint k; while(*string!='') { sendbyte(*string);delay(100);string++; }for(k=0;k<50;k++)strk[k]=0; } void main() { usart_Init(); while(1) { if(strk[2]=='o'&&strk[3]=='n')led=0; if(strk[2]=='o'&&strk[3]=='f'&&strk[4]=='f')led=1; if(RI){sendstring(strk);} } } void Usart_o() interrupt 4 { receivebyte(); }
Previous article:STC15 series MCU RS-485 communication routine (with CRC check)
Next article:Chinese menu MP3 song selector based on SC95F8616 microcontroller (2)
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Molex leverages SAP solutions to drive smart supply chain collaboration
- Pickering Launches New Future-Proof PXIe Single-Slot Controller for High-Performance Test and Measurement Applications
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- PWM output of MPS430 microcontroller
- PCB Design Considerations
- 【NXP Rapid IoT Review】+First Online Project
- Configuration and use of TI C2000 TMS320F28379D SCID SCIB
- Low-power multi-protocol chip ZigBee/6LoWPAN/BLE/Thread chip CC2652
- Share a STLINK V2_1 PCB project, which can be directly sampled and produced
- 3. [Learning LPC1768 library functions] External interrupt experiment
- Please advise on the design of driving circuit for inductive load
- Package placement issues
- Radio Books