Home > Other > 51 microcontroller and PC serial port communication program and hardware circuit diagram

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++;
}
}

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
community

Robot
development
community

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号