51 microcontroller and PC serial port communication program and hardware circuit diagram
Source: InternetPublisher:刘德华河北分华 Updated: 2020/11/14


#include
#define BUFFERLEGTH 10
//--------------------------------------------- ---------------------
void UART_init(); //Serial port initialization function
void COM_send(void); //Serial port sending function
char str;
char j;
// -------------------------------------------------- ------------------
void main(void)
{
unsigned char i;
UART_init();
j=0; //Initialize the serial port
for(i = 0;i < 10;i++ )
{
COM_send(); //First send data once for testing purposes
};
while(1);
}
//------------------------------------------------ -------------
//---------------------------------- -------------------------------------------------- -------------
// Function name: UART_init() serial port initialization function
// Function function: When the system clock is 11.059MHZ, set the serial port baud rate to 9600bit/s
// Serial port Receive interrupt is enabled, send interrupt is disabled
//----------------------------------------- -------------------------------------------------- -------
void UART_init()
{
//Initialize the serial port and baud rate generator
SCON =0x50; //Select the serial port working mode 1, open the reception permission
TMOD =0x20; //Timer 1 works in Mode 2, timer 0 works in mode 1
TH1 =0xfA; //Achieve baud rate 9600 (system clock 11.0592MHZ)
PCON = 0x80;
TR1 =1; //Start timer T1
ET1 =0;
ES=1; / /Allow serial port interrupt
PS=1; //Design serial port interrupt priority
EA =1; //MCU interrupt allowed
}
//------------------- -------------------------------------
void COM_send(void)
{
unsigned char point = 0;
for(point=0;str!="\0";point++) //Continuously send twenty bits of data //Send all the data in the buffer area to the serial port
{
SBUF=str;
while(!TI);
TI =0;
//str="\0";
}
}
//-------------------------------------------------- ---------------
//--------------------------------- -------------------------------------------------- ---------------
// Function name: com_interrup() serial port reception interrupt processing function
// Function function: Receive ten-digit data including the start bit "S" to the data buffer district
//----------------------------------------------- -------------------------------------------------- -
void com_interrupt(void) interrupt 4 using 3
{
unsigned char RECEIVR_buffer;
bit flag=1;
if(RI) //Handle the receive interrupt
{
RI=0; //Clear the interrupt flag bit
RECEIVR_buffer=SBUF; //Receive serial port data
str =SBUF;
if (RECEIVR_buffer == "$")
{ ES=0;
str="\0";
SCON =0x40; //reception is not allowed
COM_send(); //send data
ES=1;
j=0;
flag =0;
SCON=0x50; //Receive allowed
}
if(flag)
j++;
}
}
- What is the LiFePO4 discharge curve? Advantages and applications of LiFePO4 discharge curve
- A simple device activator circuit
- How to Make a Bass Boost Speaker Box
- Build an Automatic Candy Vending Machine
- IoT-based pet feeder
- How to Use the Radio to Record Phone Calls Automatically
- The production principle of high voltage self-defense flashlight and electric stick
- A novel resonance demonstration device
- Production of wake-up device
- How to create image processing solutions using HLS capabilities
- How does an optocoupler work? Introduction to the working principle and function of optocoupler
- 8050 transistor pin diagram and functions
- What is the circuit diagram of a TV power supply and how to repair it?
- Analyze common refrigerator control circuit diagrams and easily understand the working principle of refrigerators
- Hemisphere induction cooker circuit diagram, what you want is here
- Circuit design of mobile phone anti-theft alarm system using C8051F330 - alarm circuit diagram | alarm circuit diagram
- Humidity controller circuit design using NAND gate CD4011-humidity sensitive circuit
- Electronic sound-imitating mouse repellent circuit design - consumer electronics circuit diagram
- Three-digit display capacitance test meter circuit module design - photoelectric display circuit
- Advertising lantern production circuit design - signal processing electronic circuit diagram







京公网安备 11010802033920号