Serial communication based on AT89C52 and serial communication assistant

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

The function we want to achieve is to send a string such as "hello world" to the serial debugging assistant through the serial port of the microcontroller when the program is running. After the serial communication assistant receives the string, it will display it and the user will feedback a string of length 4 and display it on the digital tube.

Let's see the effect

Write the picture description here

The first step is to configure the serial communication circuit and set up the relevant serial ports with the help of VSPD and serial communication assistant.


  • Use VSPD to create two virtual serial ports that can communicate with each other, such as COM1 and COM2. After clicking the Add Port button, you can see the two newly created virtual ports in the Virtual Ports section on the left column.

  • Open the Serial Debug Assistant V2.2 and configure it. Change the serial port to COM2 port, set the baud rate to 9600, select NONE for the parity bit, 8 data bits, and 1 stop bit.

  • Configuration in the circuit diagram: First, change the crystal oscillator to 11.0592MHZ, and then change the crystal oscillator in the AT89C52 chip to 11.0592MHZ. Then configure the serial port, set the physical port to COM1, the physical baud rate to 9600, the physical data bit to 8 bits, and the physical parity bit to NONE. The following virtual baud rate, virtual data bit, and virtual parity bit are the same as above.


  • Next is the circuit diagram

    Write the picture description here

    Create two virtual ports using VSPD

    Write the picture description here

    You can use the device manager to check whether the serial port is created successfully.

    Write the picture description here

    AT89C52 configuration

    Write the picture description here

    Configuration of COMPIM

    Write the picture description here

    Some port settings

    Write the picture description here

    The hardware configuration is almost done. Next, let's see how to write code in Keil. Here are only some important code snippets. If you are interested, you can [download the code](http://download.csdn.net/detail/lrwwll/9720330)

/*Digital tube display function, using 74HC595 to achieve serial input and parallel output*/

void ser_inout(uchar datas)      

{

    uchar i; //loop control variable

    STCP = 0; //P2.6 pin set to 0

    for(i = 0 ; i < 8 ; i++) //for loop, transmit data from high to low

    {

        SHCP = 0; //P2.7 pin set to 0

        if( (datas & 0x80) == 0) //If the highest bit of data is equal to 0

        {

            DS = 0; // pass data 0 to P2.5 pin

        }

        else

        {

            DS = 1; //If the highest bit data is not equal to 0, pass data 1 to P2.5 pin

        }

        datas <<= 1; //Shift the transmitted 8-bit data one bit to the left

        SHCP = 1; //P2.7 pin is set to 1, generating a rising edge, and transferring the data on P2.5 pin to 74HC595

    }

    STCP = 1; //P2.6 is set to 1, a rising edge is generated, and eight bits of data are sent

}


/*Dynamic display function, digital tube dynamically displays data*/

void Display()

    uint i = 0; //i is the loop control variable 

  while( i!=900 ) //while loop, loop 90 times, let the digital tube dynamically display data for about 1.5S

    {

      ser_inout(table[show[0]]); //Send the highest bit data to the digital tube

    wx0 = 0; //Select the first digital tube, low level is valid  

    delay_ms(1); //delay 1ms

    wx0 = 1; //Cancel bit selection


      ser_inout(table[show[1]]); //Send the second bit of data to the digital tube  

    wx1 = 0; //Select the second digital tube, low level is valid

    delay_ms(1); //delay 1ms

    wx1 = 1; //Cancel bit selection


      ser_inout(table[show[2]]); //Send the third digit to the digital tube

    wx2 = 0; //Select the third digital tube, low level is valid 

      delay_ms(1); //delay 1ms

    wx2 = 1; //Cancel bit selection


    ser_inout(table[show[3]]); //Send the fourth digit to the digital tube

    wx3 = 0; //Select the fourth digital tube, low level is valid 

      delay_ms(1); //delay 1ms

    wx3 = 1; //Cancel bit selection  

      i++; //i+1

    }       

}


/*Output function, display the corresponding data through the digital tube*/

void printf(uchar dat[]) //dat stores the data to be displayed

{

    uint i = 0; //loop control variable

    for(i = 4;i > 0;i--) //Loop 4 times and store the data to be displayed in the show array

    {

        show[4-i]=dat[4-i];

  }

    Display(); //Call the dynamic display function to let the digital tube display data dynamically

}


/*Serial communication related initialization*/

void init()

{

    TMOD=0X20; //Set timer 1 mode 2

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

    TL1=0XFD; //same as above

    SCON=0X50; //Set the serial port control register to serial port working mode 1 and enable interrupt to receive data

    PCON&=0XEF; //Baud rate is not doubled

    TR1=1; //Start timer 1

    IE=0X0; //Disable any interrupt

}

/*MCU serial port sends a character*/

void send(uchar txd)

{

    SBUF=txd;

    while(!IF);

    IF = 0;

}


/*MCU serial port sends a string*/

void send_buf(uchar dat[])

{

    uint i=0;

    while(dat[i]!='\0')

    {

        send(dat[i]);

        i++;

    }

}


Some explanations

SCON=0x50:

SCON is the serial port control register, 0x50 is hexadecimal, converted to binary is 01010000, corresponding to the following table, that is, the serial port control register is set to serial port working mode 1 and interrupt receiving data is allowed.

Appendix: scon register structure table

SCON SM0 SM1 SM2 REN TB8 RB8 TI RI

Bit address 9FH 9EH 8DH 9CH 9BH 9AH 99H 98H 

TMOD=0x20: TMOD is the timer/counter mode control register, so TMOD=0x20 sets the timer/counter 1 to working mode 2.

TH1=0xE8,TL1=0xE8:

Set the initial value of timer/counter 1 to determine the timing duration, and the specific time is related to the crystal oscillator.

TI=1,TR=1:

From the above table, we can see that TI and RI are the lowest two bits of the SCON register. TI: Transmit interrupt flag, RI: Receive interrupt flag. TI=1 indicates that the frame transmission is completed, and RI=1 indicates that the frame reception is completed.


Keywords:AT89C52 Reference address:Serial communication based on AT89C52 and serial communication assistant

Previous article:Character Problems in Communication between PC Serial Debug Assistant and MCU
Next article:(C51 Learning Five) MCU and PC communicate through serial port

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号