51 MCU Experiment 1 - Serial Communication (TX/RX)

Publisher:cwk2003Latest update time:2022-02-13 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The simulation circuit diagram is as follows:
figure 1

code show as below:


#include "reg51.h"

#define uint unsigned int 

#define uchar unsigned char


uint code segcode[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D, 0x07, 0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71}; 

uchar code weicode[4]={0x0e,0x0d,0x0b,0x07};  

uchar disbuf[4]={0,0,0,0}; //Define and initialize the digital tube data buffer


void delayms(uint x) //delay 1ms function

{

                          uchar i;

                          while(x--)

                          {

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

                           }

}


void usart_Init() //Serial interrupt initialization routine

{

                         TMOD=0x20;     

                         TH1=0xfa;

                         TL1=0xfa; //Configure the serial port baud rate to 9600

                         SCON=0x50; //Set the serial port to work in mode 1, 8 data bits

                         PCON=0x80; //Configure baud rate multiplication

                         IE=0x90; // Enable interrupt

                         TR1=1; // Start the timer/counter

}


void segdisplay() //digital tube display function

{

                            uchar i;

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

                            {

                              P1=weicode[i];

                              P0=segcode[disbuf[i]];  

                              delayms(1);

                              P0=0x00;

                            }

}


void main() //Main function

{

                             usart_Init() ; //Serial port initialization

                             while(1) { segdisplay(); } //CPU loops to execute display function

}


void usart_Int() interrupt 4 //Serial interrupt service routine

{

                             if(RI==1) //Judge whether it is a receive interrupt

                             {

                               uint temp; //Temporarily open up a memory space to store the received data

                               RI=0; //Set RI to 0 in time to prevent data loss 

                           temp=SBUF; //Read the contents of the receive data buffer into the specified memory space

                           SBUF=temp; //Resend the input data to the host computer and echo it

                           while(TI==0);

                           TI=0; //Wait for sending to complete and reset TI

                           disbuf[3]=disbuf[1];

                           disbuf[2]=disbuf[0];

                           disbuf[1]=temp/0x10;

                           disbuf[0]=temp%0x10; //Modify the display buffer

                            }

}

 

After running the simulation, input the character '1', and the digital tube displays "31", as shown below:

figure 2

Entering other characters in sequence will display their ASCII codes accordingly. When you enter the second character '2', the digital tube displays "3132"; when you enter the third character '5', the digital tube displays "3235" (the previous 31 is removed), and so on after entering characters continuously.


Reference address:51 MCU Experiment 1 - Serial Communication (TX/RX)

Previous article:Serial communication based on 51 single chip microcomputer (PC sends characters)
Next article:Serial port receiving and sending experiment - type the number **.* on the keyboard, and the PC will receive and display **.* in a loop

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号