Serial communication between 51 microcontroller and PC

Publisher:limm20032003Latest update time:2018-07-15 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The serial port control register of the microcontroller is SCON, and its format is as follows:

The serial port has four working modes, namely 00, 01, 10, and 11. Mode 0 is a synchronous shift register, and modes 1, 2, and 3 are asynchronous 10, 11, and 11-bit transceiver modes. Mode 1, i.e. 10-bit asynchronous transceiver mode, is commonly used.

The specific analysis of the 10-bit data transmission and reception of mode 01 is as follows:

The data to be sent is SBUF=0X34. According to the waveform, the 10-bit data has a start bit of 1 (not marked on the waveform here), and the last bit is 1. The middle bit is 8-bit data 00110100 (0x34). When the 8th bit is sent, the MCU automatically sends an end flag, that is, sets TI to 1. Setting TI to 1 indicates that the data has been sent. TI cannot be automatically cleared, so it needs to be cleared by software after each data is sent: 

As above, when the microcontroller needs to receive data, it also executes the same steps. First, it receives 8 bits of data, and then sets the receiving flag RI to 1.

The initialization steps of the serial port are mainly to set the timer 1 that generates the baud rate, the serial port control box interrupt control, the steps are as follows:

Code:


//Serial port initialization program

TMOD&=0x0f;

TMOD|=0x20;

TH1=0XFD; //Set the baud rate to 9600

TL1=0xFD;

TR1=1; //Start timer T1

SCON=0X50; //10-bit data is sent and received asynchronously, the baud rate is controlled by timer T1, allowing serial port reception

  ES=1; //Enable serial port interrupt

    //Serial port initialization completed

Below I wrote a program for serial port communication with PC:


#include

 

unsigned char flag,i;

unsigned int k,j;

unsigned char a[32]={0};

 

unsigned char code table[6]="I get ";

 

void init()

{

 TMOD=0x20; //Set timer working mode 2

 TH1=0xfd; //Load initial value into timer 1

 TL1=0xfd; //same as above

 TR1=1; //Start T1 timer

 SM0=0; //Set working mode 1

 SM1=1; //same as above

 REN=1; // Allow serial port to receive

 EA=1; //Open the general interrupt

 ES=1; //Open serial port interrupt

}

 

void main()

{

 init();

 while(1)

 {

  if(flag==1)

  {

   ES=0;

   for(i=0;i<6;i++)

   {

    SBUF=table[i];

    while(!TI);

    TI=0;

   }

   ES=1;

  for(k=0;k<0xFFFF;k++);

  for(i=0;i<32;i++)

   {  

   SBUF=a[i];           

   while(!TI);

   TI=0;

   }

   flag=0;

  } 

 }  

}

void ser() interrupt 4 //interrupt program, receive data block, flag position 1

{   

flag=1;

if(RI==1){

a[j++]=SBUF;

   RI=0; //Manual clear  

}

}

The serial communication effect is as follows:

But there is a small problem with my program. I have tried many ways to solve it, but all failed. When the data I input is greater than 2 bytes, it can only return the first two bytes of data. As shown below:


Reference address:Serial communication between 51 microcontroller and PC

Previous article:Interaction with host computer via serial communication of C51 MCU
Next article:A Brief Discussion on UART Serial Communication (Part 3) -- Character and Data Conversion

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

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号