A Brief Discussion on UART Serial Communication (Part 3) -- Character and Data Conversion

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

The application of learning serial communication is mainly to realize the exchange of information between the microcontroller and the computer. The computer can control some information of the microcontroller, and some information status of the microcontroller can be sent to the software on the computer. The following is a simple routine to realize the data sent by the microcontroller serial port debugging assistant and display it on the digital tube on the development board.

#include

sbit ADDR3 = P1^3; //LED select address line 3

sbit ENLED = P1^4; //LED total enable pin

unsigned char code LedChar[] = { // Digital tube display character conversion table

    0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,

    0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E

};

unsigned char LedBuff[6] = { //digital tube

    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF

};

unsigned char T0RH = 0; //high byte of T0 reload value

unsigned char T0RL = 0; //Low byte of T0 reload value

unsigned char RxdByte = 0; //Byte received by the serial port

void ConfigTimer0(unsigned int ms);

void ConfigUART(unsigned int baud);

void main ()

{

    P0 = 0xFF; //P0 port initialization

    ADDR3 = 1; //Select digital tube

    ENLED = 0; //LED is always enabled

    EA = 1; // Enable general interrupt

    ConfigTimer0(1); //Configure T0 timing 1ms

    ConfigUART(9600); //Configure the baud rate to 9600

 

    while(1)

    { //Display the received byte in hexadecimal format on the digital tube

        LedBuff[0] = LedChar[RxdByte & 0x0F];

        LedBuff[1] = LedChar[RxdByte >> 4];

    }

}

void ConfigTimer0(unsigned int ms) //T0 configuration function

{

    unsigned long tmp;

 

    tmp = 11059200 / 12; //Timer counting frequency

    tmp = (tmp * ms) / 1000; //Calculate the required count value

    tmp = 65536 - tmp; //Calculate the timer reload value

    tmp = tmp + 31; //Correct the error caused by interrupt response delay

 

    T0RH = (unsigned char)(tmp >> 8); //Timer reload value is split into high and low bytes

    T0RL = (unsigned char)tmp;

    TMOD &= 0xF0; // Clear the control bit of T0

    TMOD |= 0x01; //Configure T0 to mode 1

    TH0 = T0RH; //Load T0 reload value

    TL0 = T0RL;

    ET0 = 1; // Enable T0 interrupt

    TR0 = 1; //Start T0

}

void ConfigUART(unsigned int baud) //Serial port configuration function, baud is the baud rate

{

    SCON = 0x50; //Configure the serial port to mode 1

    TMOD &= 0x0F; // Clear the control bit of T1

    TMOD |= 0x20; //Configure T1 to mode 2

    TH1 = 256 - (11059200/12/32) / baud; //Calculate T1 reload value

    TL1 = TH1; //initial value equals reload value

    ET1 = 0; //Disable T1 interrupt

    ES = 1; // Enable serial port interrupt

    TR1 = 1; //Start T1

}

void LedScan() //LED display scanning function

{

    static unsigned char index = 0;

 

    P0 = 0xFF; // Turn off all segment selection bits and display blanking

    P1 = (P1 & 0xF8) | index; //Assign the bit selection index value to the lower 3 bits of port P1

    P0 = LedBuff[index]; //Assign the corresponding display buffer value to P0 port

    if (index < 5) //Bit selection index 0-5 loop, because there are 6 digital tubes

        index++;

    else

        index = 0;

}

void InterruptTimer0() interrupt 1 //T0 interrupt service function

{

    TH0 = T0RH; //Timer reloads the reload value

    TL0 = T0RL;

    LedScan(); //LED scan display

}

void InterruptUART() interrupt 4

{

    if (RI) // Byte received

    {

        RI = 0; //Manually clear the receive interrupt flag

        RxdByte = SBUF; //The received data is saved in the received byte variable

        SBUF = RxdByte; //The received data is sent back directly, which is called "echo" to prompt the user whether the information entered has been received correctly

    }

    if (TI) //bytes sent

    {

        TI = 0; //Manually clear the transmit interrupt flag

    }

}

When you do this experiment, there is a small problem that you should pay attention to. Because the STC89C52RC download program uses the UART serial port to download, after downloading the program, the program runs, but the download software will send some additional data through the serial port at the end, so the program does not display 00 when it is just downloaded, but may display other data. Restart and open it once.

Commonly used characters include numbers from 0 to 9, letters from A to Z/a to z, and various punctuation marks, etc. So how do we represent them in the microcontroller system?

ASCII code (American Standard Code for Information Interchange) can accomplish this mission: in a single-chip microcomputer, a byte of data can have 256 values ​​from 0 to 255. We take 128 values ​​from 0 to 127 and give it another meaning, that is, let them represent a commonly used character respectively. The specific correspondence is shown in the following table.

Table 1-1 ASCII table

ASCII Value

Control Characters

ASCII Value

character

ASCII Value

character

ASCII Value

character

000

NULL

032

(space)

064

@

096

001

SOH

033

!

065

A

097

a

002

STX

034

"

066

B

098

b

003

ETC

035

#

067

C

099

c

004

EOT

036

$

068

D

100

d

005

END

037

%

069

E

101

e

006

ACK

038

&

070

F

102

f

007

BEL

039

'

071

G

103

g

008

BS

040

(

072

H

104

h

009

HT

041

)

073

I

105

i

010

LF

042

*

074

J

106

j

011

VT

043

+

075

K

107

k

012

FF

044

076

L

108

l

013

CR

045

-

077

M

109

m

014

SO

046

078

N

110

n

015

AND

047

/

079

O

111

o

016

ACCORDING TO

048

0

080

P

112

p

017

DC1

049

1

081

Q

113

q

018

DC2

050

2

082

R

114

r

019

DC3

051

3

083

S

115

s

020

DC4

052

4

084

T

116

t

021

WANT

053

5

085

IN

117

in

022

SYN

054

6

086

IN

118

in

023

ETB

055

7

087

IN

119

In

024

CAN

056

8

088

X

120

x

025

EM

057

9

089

AND

121

and

026

SUB

058

:

090

WITH

122

With

027

ESC

059

;

091

[

123

{

028

FS

060

<

092

\

124

|

029

GS

061

=

093


125

}

030

RS

062

>

094

^

126

~

031

US

063

?

095

_

127

OF THE

In this way, a one-to-one correspondence is established between commonly used characters and byte data. Now a byte can represent both an integer and a character, but it is essentially just a byte of data, which can be given different meanings. When to give it that meaning depends on the programmer's intention. ASCII code is widely used in microcontroller systems. Let's have an intuitive understanding of it and make sure to deeply understand its essence.

By referring to the above table, you can realize the conversion between characters and numbers. For example, still using this program, we change the sending format to character format, and still use hexadecimal receiving, so that it is easier to compare the receiving and digital tube.

Sending a lowercase a in character format returns a hexadecimal 0x61, and the digital tube also displays 61. The decimal corresponding to the character a in the ASCII code table is 97, which is equal to the hexadecimal 0x61; sending a number 1 in character format returns a hexadecimal 0x31, and the digital tube also displays 31. The decimal corresponding to the character 1 in the ASCII table is 49, which is equal to the hexadecimal 0x31. Now everyone should be clear: the so-called hexadecimal sending and receiving are both carried out according to the real value of the byte data; while the character format sending and receiving are carried out according to the character form in the ASCII code table, but it actually transmits a byte of data in the end. Of course, you don't need to remember and understand this table, just check it when you need it.

The study of communication is not as intuitive as the previous control part. Our program can only get a result in the communication part, but we cannot directly see the process. So slowly you may know that there are measuring instruments such as oscilloscopes and logic analyzers. If there are instruments such as oscilloscopes or logic analyzers in the school laboratory or company, you can use them to capture the serial port waveform and understand it intuitively. If you don’t have these instruments yet, you should know what it is first and then talk about it when you have the conditions. Because some tools are relatively expensive, you can try to use the ones in school or company if you have the conditions. Here I use a simple logic analyzer to capture the waveform of serial communication for everyone to see, and you can understand it, as shown in Figure 1-1.

Figure 1-1 Schematic diagram of logic analyzer serial port data

The role of the analyzer and oscilloscope is to capture the waveform of the communication process for analysis. Let's first briefly explain the meaning of the waveform. The left side of the waveform is the low bit, and the right side is the high bit. The waveform on the top is sent by the computer to the microcontroller, and the waveform on the bottom is sent back by the microcontroller to the computer. Take the waveform above as an example. The first bit on the left is the start bit 0. From low to high, it is 10001100. If the order is reversed, it is data 0x31, which is '1' in the ASCII code table. You can notice that the analyzer marks a white dot on each data bit, indicating that it is data. There is no white dot when it is the start bit or no data. The difference between the time mark T1 and T2 is shown on the right as 0.102ms, which is about 1 in 9600. There is a slight deviation, which is within the allowable range. Through Figure 1-1, we can clearly understand the detailed process of serial communication.

Now let's learn more about this. If we use the serial port debugging assistant to directly send a "12" in character format, what should be displayed on our digital tube? What should the serial port debugging assistant return? After testing, we found that our digital tube displays 32, while the serial port debugging assistant returns two hexadecimal data, 31 and 32, as shown in Figure 1-2.

Figure 1-2 Serial port debugging assistant data display

We use a logic analyzer to capture this data, as shown in Figure 1-3.

Figure 1-3 Logic analyzer captures data

For the ASCII code table, the numbers themselves are characters rather than data, so if "12" is sent, it actually sends two characters "1" and "2" separately. The microcontroller receives the first character "1" first, and the corresponding number 31 is displayed on the digital tube, but it immediately receives the character "2" and the digital tube instantly changes from 31 to 32. Visually, we have no way of detecting this rapid change, so we feel that the digital tube directly displays 32.


Keywords:UART Reference address:A Brief Discussion on UART Serial Communication (Part 3) -- Character and Data Conversion

Previous article:Serial communication between 51 microcontroller and PC
Next article:51 MCU Study Notes [VI] - Serial Communication Experiment

Recommended ReadingLatest update time:2024-11-23 07:41

STM8 UART transmitter
STM8 UART transmitter The transmitter sends an 8-bit or 9-bit data word depending on the state of the M bit. When the M bit is set, the word length is 9 bits and the ninth bit (MSB) should be written to the T8 bit of register UART_CR1. When the transmit enable bit (TE) is set, the data in the transmit shift register
[Microcontroller]
STM8 UART transmitter
[Self-study 51 single-chip microcomputer] 11 -- UART serial communication
1. Preliminary understanding of serial communication UART serial communication is the most commonly used communication technology for microcontrollers, and is usually used for communication between microcontrollers and computers and between microcontrollers. Communication can be divided into parallel communication
[Microcontroller]
[Self-study 51 single-chip microcomputer] 11 -- UART serial communication
AVR dual CPU communication program (asynchronous mode) UART
;****************************************   ;Function: AVR dual CPU communication program, asynchronous mode, similar to UART   ;Device: AT90S2313   ;Hardware: PD.2 of master and slave CPUs are connected through pull-up resistors   ;Clock: 4.0MHz    ;********************************************   .include"2313def.inc"
[Microcontroller]
UART&DMA HAL library for stm32F4XX
1: Serial port related initialization operations We all know that the initialization of stm32 related peripherals requires turning on the corresponding clock and configuring the corresponding IO. In addition, we also need to assign values ​​to the relevant member variables in the serial port library. Here we will take
[Microcontroller]
Introduction to ARM platform printf function positioning to uart output
1.1 ARM serial port output function uart_printf It would be very convenient if ARM could use the printf function format that comes with the C function library, but the default printf is located at the stdout terminal, not the serial port. This article describes how to locate the serial port of ARM. 1.1.1 Main func
[Microcontroller]
PCONP with UART2 and UART3 of LPC23XX
What is the difference between UART2, UART3 and UART0, UART1 of LPC2368? The same processing method UART0, UART1 is normal, UART2, UART3 crash, the following is the program: #include LPC23xx.h #define CR     0x0D void Delay(unsigned int cnt) {     do     {         cnt--;     }     while(cnt!=0); } int sendchar0 (int
[Microcontroller]
8051/2 microcontroller commonly used local communication methods UART, RS485, I2C, SPI UART serial communication 1
1. Description 1. Serial port UART, baud rate: 9600 When connecting a device, generally only GND RX TX is connected, and Vcc is not connected to avoid power supply conflicts with the target device. 1.1 RS485 standard (+2V ~ +6V: 1 / -6V ~ -2V: 0) 1.2 RS232 standard (-15V ~ -3V: 1 / +3V ~ +15V: 0), MAX232 needs
[Microcontroller]
8051/2 microcontroller commonly used local communication methods UART, RS485, I2C, SPI UART serial communication 1
PIC reading notes 5: Serial communication interface SPI, I2C, UART
1: Synchronous serial interface I2C and SPI wiring method: 1.1: Some notes on SPI 2: Synchronous serial interface I2C and SPI connect multiple devices and their differences: 3: Asynchronous serial interface UART Hardware handshaking is especially necessary when communicating with Windows
[Microcontroller]
PIC reading notes 5: Serial communication interface SPI, I2C, UART
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号