STC89C52_51 MCU_Serial port configuration_UART serial communication

Publisher:二进制游侠Latest update time:2020-06-27 Source: eefocusKeywords:STC89C52  UART Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Register Configuration

PCON Power Management Register

Bit number D7 D6 D5 D4 D3 D2 D1 D0

Bit Symbol SM0 SM1 SM2 REN TB8 RB8 TI RI

– Mode Mode Mode 1 directly clears Enable serial port reception Mode 1 receives stop bit Send flag bit, cleared by software Receive flag bit, cleared by software

//Cannot bit address


SCON serial port control register

Bit number D7 D6 D5 D4 D3 D2 D1 D0

Bit Symbol SM0 SM1 SM2 REN TB8 RB8 TI RI

– Mode Mode Mode 1 directly clears Enable serial port reception Mode 1 receives stop bit Send flag bit, cleared by software Receive flag bit, cleared by software

/* DISCON */

sbit SM0 = SCON^7;

sbit SM1 = SCON^6;

sbit SM2   = SCON^5;

sbit REN   = SCON^4;

sbit TB8   = SCON^3;

sbit RB8   = SCON^2;

sbit TI = SCON^1;

sbit RI    = SCON^0;


Mode & Baud Rate (Song Xuesong P183)

SCON mainly uses mode 1, the baud rate

Correspondingly, use mode 2 of timer T1 & T2


TH1 = TL1 = 256 - Crystal value/12/2/16/Baud rate

(256 is the overflow value of TL1, 12 refers to 12 clock cycles, and 16 is the hardware factor)


SBUF

Two SBUF registers, responsible for receiving and sending buffers respectively


process

Configure the serial port to mode 1

Configure timer T1 to mode 2

Calculate the value of TH0&TL0 according to the baud rate

Configure PCON & SCON registers

Turn on the timer


IO port simulates UART serial communication

Schematic diagram of UART serial port data transmission

insert image description here

Digging a hole: How are the baud rate & TH0 calculated?



#include


sbit PIN_RXD = P3^0;

sbit PIN_TXD = P3^1;


bit RxdOrTxd = 0;

bit RxdEnd = 0;

bit TxdEnd = 0;

unsigned char RxdBuf = 0;

unsigned char TxdBuf = 0;


void configUART(unsigned long baud);

void startRXD(void);

void startTXD(unsigned char dat);


void main(void)

{

EA = 1;

configUART(9600);


while(1)

{

while(PIN_RXD);

startRXD();

while(!RxdEnd);

startTXD(RxdBuf+1);

while(!TxdEnd);

}

}


void configUART(unsigned long baud)

{

TMOD &= ~(0xF<<0);

TMOD |= 0x1<<1;

TH0 = 256 - (11059200 / 12) / baud;

}


void startRXD(void)

{

TL0 = 256 - ((256 - TH0) >> 1);


ET0 = 1;

TR0 = 1;


RxdEnd = 0;

RxdOrTxd = 0;

}


void startTXD(unsigned char dat)

{

TxdBuf = dat;


TL0 = TH0;


ET0 = 1;

TR0 = 1;


PIN_TXD = 0;

RxdEnd = 0;

RxdOrTxd = 1;

}


void timer0(void) interrupt 1

{

static unsigned char cnt = 0;


if(RxdOrTxd)

{

cnt++;

if(cnt<=8)

{

PIN_TXD = TxdBuf & 0x01;

TxdBuf >>= 1;

}

else if(cnt==9)

{

PIN_TXD = 1;

}

else 

{

cnt = 0;

TR0 = 0;

TxdEnd = 1;

}

}


else

{

if(cnt==0)

{

if(!PIN_RXD)

{

RxdBuf = 0;

cnt++;

}

else

{

TR0 = 0;

}

}

else if(cnt<=8)

{

RxdBuf >>= 1;

if(PIN_RXD)

{

RxdBuf |= 0x80;

}

cnt++;

}

else

{

cnt = 0;

TR0 = 0;

if(PIN_RXD)

{

RxdEnd = 1;

}

}

}

}


UART serial communication

Teaching Edition


#include


void configUART(unsigned long baud);


void main(void)

{

configUART(9600);

while(!RI);

RI = 0;

SBUF = SBUF + 1;

while(!IF);

IF = 0;

}


void configUART(unsigned long baud)

{

SCON = 0x50;


TH1 = 256 - (11059200 / 12 / 2 / 16) / baud;

TL1 = TH1;

TMOD &= ~(0xF<<4);

TMOD |= 0x2<<4;

ET1 = 0;

TR1 = 1;

}


Industrial Edition


#include


void configUART(unsigned long baud);


void main(void)

{

EA = 1;

configUART(9600);

while(1);

}


void configUART(unsigned long baud)

{

SCON = 0x50;


TH1 = 256 - (11059200 / 12 / 2 / 16) / baud;

TL1 = TH0;

TMOD &= ~(0xF<<4);

TMOD |= 0x2<<4;

ET1 = 0;

EN = 1;

TR1 = 1;

}


void UART(void) interrupt 4

{

if(RI)

{

RI = 0;

SBUF = SBUF + 1;

}

if(TI)

{

IF = 0;

}

}


The computer sends data and displays it in the digital tube


#include


sbit road = P2^7;

sbit want = P2^6;


unsigned char code weitable[6] = 

{

~0x20,~0x10,~0x08,~0x04,~0x02,~0x01

};


unsigned char code duantable[16] = 

{

0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,

0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71

};


unsigned char ledbuff[6] = 

{

0x00,0x00,0x00,0x00,0x00,0x00

};


unsigned char T0RH = 0,T0RL = 0;

unsigned char RxdByte = 0;


void configtimer0(unsigned char ms);

void configUART(unsigned long baud);


void main(void)

{

EA = 1;

configtimer0(1);

configUART(9600);

while(1)

{

ledbuff[0] = duantable[RxdByte & 0x0F];

ledbuff[1] = duantable[RxdByte >> 4];

}

}


void configtimer0(unsigned char ms)

{

unsigned long tmp;


tmp = 11059200 / 12;

tmp = (tmp * ms) / 1000;

tmp = 65536 - tmp;

tmp = tmp + 12;


T0RH = (unsigned char)(tmp>>8);

T0RL = (unsigned char)tmp;


TH0 = T0RH;

TL0 = T0RL;

TMOD &= ~(0xF<<0);

TMOD |= 0x1<<0;

ET0 = 1;

TR0 = 1;

}


void configUART(unsigned long baud)

{

TH1 = 256 - (11059200 / 12 / 2 / 16) / baud;

TL1 = TH0;

SCON = 0x50;

TMOD &= ~(0xF<<4);

TMOD |= 0x2<<4;

ET1 = 0;

EN = 1;

TR1 = 1;

}


void ledscan(void)

{

static unsigned char i = 0;


P0 = 0x00;

want = 1;

want = 0;


P0 = wideable[i];

wei = 1;

wei = 0;

P0 = ledbuff[i];   

want = 1;

want = 0;


if(i<5)

i++;

else

i = 0;

}


void timer0(void) interrupt 1

{

TH0 = T0RH;

TL0 = T0RL;


ledscan();

}


void UART(void) interrupt 4

{

if(RI)

{

RI = 0;

RxdByte = SBUF;

SBUF = RxdByte;

}

if(TI)

{

IF = 0;

}

}

Keywords:STC89C52  UART Reference address:STC89C52_51 MCU_Serial port configuration_UART serial communication

Previous article:51 MCU simple serial communication-1
Next article:51 MCU | Serial communication experiment (simulated serial communication/multi-machine communication example)

Recommended ReadingLatest update time:2024-11-16 13:34

Experiment 2: Sorting and addition (80C51 single-chip computer assembly language programming)
There are two numbers with a length of 10H, which are placed in the memory with the first address of 30H and 40H respectively (low byte). Find their corresponding sum and place it in the memory with the first address of 50H (process the carry bit), then arrange them in ascending order and place them in the memory with
[Microcontroller]
Electronic locker system based on 80C51 single chip microcomputer
  Electronic Locker System Circuit Description Based on 80C51 MCU : Security is the most concerned issue in our daily life. Everyone feels that security is very crucial in the door and security at home, and can be as safe as possible. For the door access security therefore, we intend to introduce an electronic pass
[Microcontroller]
Electronic locker system based on 80C51 single chip microcomputer
AT89C51 MCU lights up LED
1. Environmental requirements Install proteus (for microcontroller simulation) Install keli (used to generate the HEX file required by the microcontroller) 2. Proteus renderings As shown in the figure, the AT89C51 chip is used here to light up the LEDs on the chip P0.0, P0.1, P1.0, and P1.1 respectively. Since, ex
[Microcontroller]
AT89C51 MCU lights up LED
51 MCU Getting Started Tutorial (4) - Button Control
There are two main types of information interaction between the microcontroller and the outside world: input information and output information. The previous blog introduced how to control LED lights and digital tubes to output information to the outside world through a single-chip microcomputer. This tutorial intro
[Microcontroller]
51 MCU Getting Started Tutorial (4) - Button Control
Basic operation of 8051 microcontroller
1. P0   1.1 Composition   1.2 Make input port   1.3 Make output port 2.P1   2.1 Composition   2.2 Make input port   2.3 Make output port 3.P2   3.1 Composition   3.2 Make input port   3.3 Make output port 4. P3   4.1 Composition   4.2 Make input and output ports 5. Summary 51 microcontroller parallel GPIO, divided int
[Microcontroller]
Basic operation of 8051 microcontroller
8051 MCU realizes serial communication in polling mode (51 receives data and sends it back to PC)
#include STC89C5xRC.H   int main() { char buf; TMOD = 0x21; //Set the working mode of timer1 SCON = 0x50; //Set serial communication format TH1 = 0xE6; //Set the initial value of timer1 TL1 = 0xE6;  TR1 = 1; //Start timer1 while(1) { if(RI == 1) { //If data is received buf = SBUF; RI = 0; SBUF = buf
[Microcontroller]
8051 MCU realizes serial communication in polling mode (51 receives data and sends it back to PC)
A brief discussion on 3 ways of external data exchange using 8051 microcontroller
There are usually several ways to exchange information between the microcontroller CPU and external devices: unconditional transmission, query transmission and interrupt transmission. We use the interface between a microcontroller and a microprinter as an example to describe these three methods. Assume that the user w
[Microcontroller]
Development and Application of AT89S51 Single Chip Microcomputer Experimental System
At present, the application of single-chip microcomputers has penetrated into various fields, and the development of single-chip microcomputer technology has also changed with each passing day. As an application-oriented discipline with strong practicality, single-chip microcomputer research and development and teachin
[Microcontroller]
Development and Application of AT89S51 Single Chip Microcomputer Experimental System
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号