Introduction: This article mainly introduces the C source program of 51 IO port simulation serial communication.
#include sbitBT_SND=P1^0; sbitBT_REC=P1^1; /********************************************** IO port simulation 232 communication program C program using two methods to occupy timer 0 **********************************************/ #defineMODE_QUICK #defineF_TMF0 #defineTIMER0_ENABLETL0=TH0;TR0=1; #defineTIMER0_DISABLETR0=0; sbitACC0=ACC^0; sbitACC1=ACC^1; sbitACC2=ACC^2; sbitACC3=ACC^3; sbitACC4=ACC^4; sbitACC5=ACC^5; sbitACC6=ACC^6; sbitACC7=ACC^7; voidIntTimer0() interrupt1 { F_TM=1; } //Send a character voidPSendChar(unsignedcharinch) { #ifdefMODE_QUICK ACC=inch; F_TM=0; BT_SND=0;//start bit TIMER0_ENABLE; //Start while(!F_TM); BT_SND=ACC0; //Send low bit first F_TM=0; while(!F_TM); BT_SND=ACC1; F_TM=0; while(!F_TM); BT_SND=ACC2; F_TM=0; while(!F_TM); BT_SND=ACC3; F_TM=0; while(!F_TM); BT_SND=ACC4; F_TM=0; while(!F_TM); BT_SND=ACC5; F_TM=0; while(!F_TM); BT_SND=ACC6; F_TM=0; while(!F_TM); BT_SND=ACC7; F_TM=0; while(!F_TM); BT_SND=1; F_TM=0; while(!F_TM); TIMER0_DISABLE; //Stop timer #else unsignedcharii; ii=0; F_TM=0; BT_SND=0;//start bit TIMER0_ENABLE; //Start while(!F_TM); while(ii<8) { if(inch&1) { BT_SND=1; } else { BT_SND=0; } F_TM=0; while(!F_TM); ii++; inch>>=1; } BT_SND=1; F_TM=0; while(!F_TM); #endif TIMER0_DISABLE; //Stop timer } //Receive a character unsignedcharPGetChar() { #ifdefMODE_QUICK TIMER0_ENABLE; F_TM=0; while(!F_TM);//wait until the start position ACC0=BT_REC; TL0=TH0; F_TM=0; while(!F_TM); ACC1=BT_REC; F_TM=0; while(!F_TM); ACC2=BT_REC; F_TM=0; while(!F_TM); ACC3=BT_REC; F_TM=0; while(!F_TM); ACC4=BT_REC; F_TM=0; while(!F_TM); ACC5=BT_REC; F_TM=0; while(!F_TM); ACC6=BT_REC; F_TM=0; while(!F_TM); ACC7=BT_REC; F_TM=0; while(!F_TM) { if(BT_REC) { break; } } TIMER0_DISABLE; //Stop timer returnACC; #else unsignedcharrch,ii; TIMER0_ENABLE; F_TM=0; ii=0; rch=0; while(!F_TM);//wait until the start position while(ii<8) { rch>>=1; if(BT_REC) { rch|=0x80; } ii++; F_TM=0; while(!F_TM); } F_TM=0; while(!F_TM) { if(BT_REC) { break; } } TIMER0_DISABLE; //Stop timer returnrch; #endif } // Check if there is a start bit bitStartBitOn() { return(BT_REC==0); } voidmain() { unsignedchargch; TMOD = 0x22; /*Timer 1 is in working mode 2 (8-bit auto-reload), 0 is in mode 2 (8-bit Automatic reload)*/ PCON=00; TR0=0; //Start using when sending or receiving TF0=0; TH0=(256-96); //9600bps is 1000000/9600=104.167 microseconds timer is // 104.167*11.0592/12=96 TL0=TH0; ET0=1; EA=1; PSendChar(0x55); PSendChar(0xaa); PSendChar(0x00); PSendChar(0xff); while(1) { if(StartBitOn()) { gch=PGetChar(); PSendChar(gch); } } }
Previous article:Realization of the communication test system between computer serial port and single chip microcomputer
Next article:Four methods of extending interrupts of 51 MCU
- 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
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Does DDR design require back drilling?
- [Integrated subway security control system based on Raspberry Pi 400] Material unboxing - Raspberry Pi 400
- Single chip microcomputer CPU card programming system main frequency setting
- Award-winning live broadcast: Microchip's trusted platform for the CryptoAuthentication family
- GD32307E-START IO analog SPI control RGBLED (03)
- 【GD32E503 Review】+ cJSON Porting Test
- Is one of the two MOS tubes in the step-down switching power supply chip used to replace the diode?
- The NiMH battery charging circuit is relatively complex, and some SMD components cannot be found in the specification sheet.
- Ding~ The weekly evaluation information has been delivered~
- Is there any plugin or function in KEIL or other compilation environment that can help check if there is an error like if ( a=1 )