51 MCU and PC serial port asynchronous communication continued

Publisher:SereneGardenerLatest update time:2016-05-20 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The previous article mainly listed some necessary knowledge points for programming serial communication. In specific programming, there are still many things to pay attention to.

The main function of the following program is to send a letter to the 51 single-chip microcomputer through the serial port debugging assistant. After the single-chip microcomputer receives it, it displays the letter on the digital tube and displays the letter in reverse in the interface of the serial port debugging assistant.

A series of special function registers such as serial port buffer have been defined in the header file of 51 single chip microcomputer. You can find the following definition by opening the header file:

sfr SCON = 0x98;
sfr SBUF = 0x99;

etc.

First, create a new header file and write the variable and function declarations to be used in the program:

#include 
#include 

#define uchar unsigned char

sbit dula=P2^6;
sbit wela=P2^7;

//Letters sent from the computer to the microcontroller
uchar letter;

//The table array stores the codes corresponding to the digital tube display 0~F
//Only A~F are used here
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};

void Inital();
void Display(uchar mydata);
void Putchar(uchar mydata);
void ShortDelay();


The main purpose of including the header file is to use the library function toupper() to convert the read letters into uppercase, making it easier to call the Display function to display them on the digital tube.

 

The main function is very simple. It first initializes and then enters a loop that is always true, waiting for the interrupt to occur and processing it.

void main()
{
  Inital(); // Initialization
  while(1)  
  {
  }
}


The initialization function is used to enable interrupts, set timers, serial communication working mode, and variable initialization:

void Inital()
{
  // Clear the variable
  letter=0;
  wela=0;
  dula=0;
 
  //Set the baud rate to 9600
  SCON=0x50;
  TMOD=0x20;
  TH1=0xfd;
  TL1=0xfd;
  TR1=1;
 
  //Open serial port interrupt
  EA=1;
  ES=1;
}


The interrupt service function is called by the CPU when an interrupt occurs, and Display is called inside it. The Putchar function is used to display letters on the digital tube and echo the input letters on the computer.

//Interrupt service function
void ser() interrupt 4
{
  //If you have finished reading the data
  if(RI==1)
  {
    RI=0; //software clear
    letter=SBUF; //Read data from the MCU Receive buffer
    Putchar(letter); //Echo letter on the computer
    Display(letter); //Display the corresponding letter on the digital tube
  }
}

 

The Display function is only used in the previous article about digital tubes. Here is the code directly:

 

void Display(uchar mydata)  
{
  uchar BigLetter;  
  BigLetter=toupper(mydata); //First convert all the letters read into uppercase so that they can be processed uniformly
  
  //Digital selector tube
  wela=1;
  P0=0xfb;
  wela=0;
  
  P0=0xff;
  
  //Segment selection
  //BigLetter-'A'+10 index is the subscript of the letter in the table
  dula=1;
  P0=table[BigLetter-'A'+10];
  dula=0;
  ShortDelay();
}
 
void ShortDelay()
{
  uchar a=100;
  while(a--);
}


Putchar function to send information to the computer

void Putchar(uchar mydata)
{
  SBUF=mydata; //Write data into the transfer buffer of the microcontroller
  while(!TI); //Is the sending finished?
  TI=0; //software clears after sending
}


In this way, you can use the serial port debugging assistant to send letters to the microcontroller, display the letters on the digital tube, and display the content of the letters just sent on the software interface.

 
Reference address:51 MCU and PC serial port asynchronous communication continued

Previous article:51 MCU and PC serial port asynchronous communication
Next article:51 MCU simulated serial port code

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号