C51 writes serial communication, simulation and debugging in proteus————The ultimate nanny-level teaching

Publisher:悠闲自在Latest update time:2022-06-28 Source: eefocusKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Two communication methods

Serial Communication:

Serial communication is the process of dividing data bytes into bits.

Parallel communication:

Parallel communication usually involves transmitting each bit of a data byte simultaneously using multiple data lines.


Serial communication is divided into two types

Synchronous communication

Synchronous communication is to directly control the clocks of the sender and receiver so that the data transmission of both parties is completely synchronized. At this time, data is sent continuously in the form of bits.

Asynchronous communication

The sender and receiver of asynchronous communication use their own clocks to control the transmission and reception of data. The sender can send characters at any time. At this time, data is sent continuously in the form of frames (a frame data has ten bits: one start bit, eight data bits, and one stop bit).


Baud rate

calculate

The baud rate is used to measure the speed at which digital signals are transmitted. It is the number of binary bits transmitted per second and its unit is: bps.

For example, the most commonly used transmission rate is 9600, which means that 960 frames of data are transmitted per second, and one frame of data is ten binary numbers. That is, 960*10=9600


set up

Generally, timer interrupt 1 (T1) is used as the baud rate generator, and T1 adopts working mode 2.


At this time, the baud rate calculation formula is:

insert image description here

Explain the meaning of several formulas


SMOD: Baud rate selection bit

SMOD belongs to the PCON register and is not bit addressable. It can be used as a baud rate doubling flag. When it is set to 0, it does not double. When it is set to 1, the serial communication mode baud rate doubles.


fosc: crystal oscillator frequency of the microcontroller

The default frequency for 51 MCU simulation is 12Mhz, but the error is relatively large. 11.0529MHz is generally used for accurate timing.


256-T initial: the number of times each timer counts

At this time, the baud rate generator T1 is in working mode 2 (automatic loading of eight-bit data). The initial value is loaded into TH1 and TL1 at the beginning. During normal operation, only TH1 counts. When TH1 overflows, the initial value in TL1 is reassigned to TH1, thus realizing automatic loading.

Note: The range of eight-bit binary numbers is 0~255, a total of 256 numbers


Initial setting of baud rate 9600

1. Set the working mode of timer T1:

TMOD=0X20; //Set timer T1 to working mode 2;

2. Calculate the initial value of T1:

TH1=0XFD;

TL1=0XFD; // and load TH1 and TL1, baud rate is 9600bps

3. Start T1:

TR1=1; // Open T1 timer

PCON=0X80; //Double the baud rate

SCON=0X50; //Set to working mode 1: 8-bit data bit

Selection of serial port working mode

Set SM0 and SM1, set the working mode

insert image description here

The role of TI and RI

TI is the send interrupt flag. When the data is sent, TI is set to 1 by hardware. After TI is set to 1, an interrupt request is made to the CPU. After entering the interrupt function, the software needs to set it to 0 (TI=0;)

RI is the receive interrupt flag. When data reception is completed, RI is set to 1 by hardware. After RI is set to 1, an interrupt request is made to the CPU. After entering the interrupt function, it needs to be set to 0 by software (RI=0;)

Both are in the SCON register

insert image description here

Serial communication experiment source code

#include        

void delay(unsigned int x);

void putchar(unsigned char data1); //Character sending function

void putstring(unsigned char *dat); //String sending function

void main(void)

{

 unsigned char c = 0;

 SCON = 0x40; //Serial port working mode setting

 TMOD = 0x20; //Timer working mode setting

 PCON = 0x00; //Baud rate multiplication setting

 TL1 = 0xfd; //Baud rate setting

 TH1 = 0xfd;          

 TI = 0; // Clear the send interrupt flag         

 TR1 = 1; //Start the timer

 while(1)

{

// Skip the author's personal output here, if you need to output

// Add output in while, regular C language is enough

//For those who really can't write, please see the attachment below

}

}


void delay(unsigned int x) //delay

{

  unsigned char i;

 while(x--)

 {

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

 }

}

void putchar(unsigned char data1)  

{

 SBUF = data1; //Send the character to be sent into the send buffer

 while(TI == 0); //Wait for sending to complete

 TI = 0; //Send interrupt flag please 0

}


void putstring(unsigned char *dat)

{

  while(*dat != '') //Judge whether the string has been sent

 {

  putchar(*dat); //Send a single character

  dat++; //Add 1 to the character address, pointing to the next character

 }

}


The experimental principle diagram is as follows

The schematic diagram is very simple, only RX-TX, TX-RX are needed.

insert image description here

The simulated serial port detection here is here!

insert image description here

Some friends may find that the simulation is running but the serial terminal cannot be opened...

After running, click here

insert image description here

You can check what is displayed, otherwise you can't observe it.

insert image description here

(Ultimate babysitting tutorial…I tried my best)


Attach the packaged project files:

https://download.csdn.net/download/Alangman/18480056

Keywords:C51 Reference address:C51 writes serial communication, simulation and debugging in proteus————The ultimate nanny-level teaching

Previous article:Ultrasonic module HC-SR04 debugging experience sharing
Next article:Design digital tube with c51 language, interrupt design 1s display 0~F, use proteus simulation

Recommended ReadingLatest update time:2024-11-22 20:47

604 LCD screen input and output C51 source program
#include #include "INC\LCD1604.H" sfr LCD_INTER = 0x80; //Data bus interface  sbit LCDBUSY=LCD_INTER^7; //Flag bit sfr LCD_CONTROL = 0xA0; //Data bus interface  sbit LCDRS = LCD_CONTROL^0; //Data, instruction selection  sbit LCDRW = LCD_CONTROL^1; //Read and write selection  sbit LCDE = LCD_CONTROL^2; //Enab
[Microcontroller]
Design of Multi-gas Detection System Based on C51 Single Chip Microcomputer
  A gas sensor is a device that can convert information about the type of gas and its concentration into electrical signals. Based on the strength of these electrical signals, information about the presence of the gas to be tested in the environment can be obtained, so that detection, monitoring, and alarm can be perf
[Microcontroller]
Design of Multi-gas Detection System Based on C51 Single Chip Microcomputer
c51 74ls164--165--595
164 Marquee, #include reg51.h #include intrins.h #define uint unsigned int #define uchar unsigned char sbit dat=P2^6; sbit clk=P2^7; uchar ii; uchar code seg7code ={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0xff};//0-9,-,全灭 void sendbyte(uchar seg) {   uchar num,c;   num=seg7code ;  for(c=0;c 8;c++)  {  dat=num&
[Microcontroller]
c51 74ls164--165--595
What is the difference between sbit and bit in C51 programming
First of all, bit and sbit are both variable types extended by C51. sbit is generally used to define bit variables of special function registers to facilitate operations on a certain bit of the register. For example: sbit TXD=P3^0; This defines TXD as a bit variable, and this sbit has a certain address. Bit is simil
[Microcontroller]
1602 LCD related c51 programming
1. Writing data and instructions needs to be done on the rising edge of LCDEN level. To achieve this, you should: ensure that LCDEN=0 when the program executes other instructions; LCDEN level must be pulled low immediately after completing the execution of read and write data and instructions; Ensure that LCDEN=0 befo
[Microcontroller]
C51 programming requires pressing keys in a specified order
//There is a question that requires pressing four buttons in sequence. //If you do not press in the specified order, an error message will be displayed. //Requirements are as follows: //When SW1 is pressed for the first time, D1 is on, and when it is released, D1 is off (if the first press is not SW1, but SW2, 3, or
[Microcontroller]
C51 programming requires pressing keys in a specified order
Usage of interrupt and using in C51 - microcontroller
void INT0()interrupt 0 using 1 {.... ..... } interrupt 0 indicates external interrupt 0; interrupt 1 indicates timer interrupt 0;  interrupt 2 indicates external interrupt 1; interrupt 3 indicates timer interrupt 1; interrupt 4 indicates a serial port interrupt; using 0 is the 0th group of registers; using 1 is the f
[Microcontroller]
C51 program for simulating RS232 serial communication with microcontroller IO port
This program has been used in one of my projects and is very stable. During the writing process, I referred to    the article http://www.51hei.com/mcu/1541.html on the 51hei website. Some subroutines are excerpted here. #include "reg52.h" #include "intrins.h"   #include "math.h"      #include "stdio.h" sbit BT_SND
[Microcontroller]
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号