Hello everyone, today's tutorial is about UART parity.
We have already introduced how to write the driver for the UART of T5L screen in the previous course, so today we will explain the parity check based on the content of chapter 9.
1. Theoretical knowledge
Before formal explanation of the encoding, let me clarify the theoretical part about UART parity.
It contains two kinds: odd parity and even parity.
You can choose to use either one of them.
Generally speaking, the data bits of UART are eight bits by default, which means no parity.
Apart from that, the data of the UART also has a nine-bit mode, which means that the data takes up eight bits and the parity takes up one bit.
The nine-bit mode can be enabled by setting the corresponding register. Detailed description can be found in “Development Guide of T5L ASIC”.
Next, let's introduce what are odd parity and even parity.
For example, we need to send a byte 0x45 to a slave using the DWIN screen. at this time, we enable odd parity, so our data takes up eight bits, that is: 0100 0101.
According to the definition of odd parity, the number of 1's in the data is odd, so the parity bits need to be 0.
And the definition of even parity is that the number of 1's is even.
Then also take 0x45 as an example.
At this time, the number of 1's in the eight-bit data 0100 0101 is odd, then the parity bits need to be 1.
2. Code
The code part of this tutorial is based on the code of chapter 9.
First, open the sys.c file, and as in chapter 19, we also add a wait instruction to prevent bugs that may occur in special cases.
Next, look at the main loop (main.c file): receive data from the UART assistant, while adding a T5LC51 prefix to the received data and returning it out, then displaying the received data on the DWIN screen.
Next, open the file of UART2 (UART2.C). Now in this code, it is no parity mode. In order to facilitate your understanding, in this tutorial, both parity and even parity code we will written and explained.
Firstly, switching is done by means of macro definition.
row3 #define UART_PARITY_MODE_NONE 0(无奇偶校验)
row4 #define UART_PARITY_MODE_ODD 1(奇校验)
row5 #define UART_PARITY_MODE_EVEN 2(偶校验)
Then define a current UART parity mode, here we define it as odd parity.
row7 #define UART_PARITY_MODE UART_PARITY_MODE_ODD
You may know that the C51 microcontroller UART has four modes.
These four modes are determined by the two bits SM1 and SM0 inside SCON (UART1 control register). For example, when these two bits are 0, UART1 is mode 0.
As we just said, there are 8bit mode and 9bit mode for UART. If we want to enable parity, we should use 9bit mode. And as you can see in the code, the baud rate is adjustable, so here we choose the mode 3.
The above content is about the registers of 51 MCU, so what is the register setting of T5L smart screen?
This is the 3.7 section "Development Guide of T5L ASIC", UART 2, 3, 4 and 5 are introduced here. They are all similar in usage.
In this tutorial, I will demonstrate the function of UART2.
You can see the definition of UART2, there is SCON0, SM0 and SM1, which means both bits should be set to 1 to enable 9bit mode.
Next we will start the code part.
Open the code file, row65 SCON0 = 0x50 are changed to to 0xD0; that is, 1101, thus enabling the 9bit mode
Here we first determine the UART is not at no parity mode.
row65 #if(UART_PARITY_MODE!= UART_PARITY_MODE_NONE)
row67 #else
row68 SCON0=0x50;
row69 #endif
The above content is to enable 9bit mode, and the odd or even parity mode is also decided by the code.
Now the UART initialization part is finished, next write the code to send bytes.
First open the "development guide of T5L ASIC", you can see here are RB8 and TB8. this is our ninth bit, when in the transmit state it is TB8, counting from 0 it is the ninth bit. Similarly, rb8 is the ninth bit received. These two bits are bit addressable. Let's define them next.
Row9 sbit TB = SCON0^3;
Let's see, TB8 is the third bit of SCON0, and then define RB8.
Row10 sbit RB = SCON0^2;
Next, we write the code to operate through the internal registers of T5L ASIC. We do not need to count the number of 1s inside the byte, which can be counted directly with the num register.
Row94换行
ODD is assumed to be used here
Row95 #if(UART_PARITY_MODE!== UART_PARITY_MODE_ODD)
Row96 ACC = byte;
Row97 TB8 = !p;
Row98 #elseif(UART_PARITY_MODE!== UART_PARITY_MODE_EVEN)
Row96 ACC = byte;
Row97 TB8 = p;
#endif
This p refers to a bit in the PSW register, PSW is Program Status Word, which is a register in the processor. This p can determine the number of 1's in the ACC register, and if it is an odd number, p will be automatically set to 1 by the hardware, otherwise it will be set to 0.
ACC is an accumulator.
Suppose the number of 1's in ACC is odd, then the value of p is 1, which is equivalent to assigning 0 to TB8 by inverting it.
The next part is to send the data.
Copy
#if(UART_PARITY_MODE!== UART_PARITY_MODE_ODD)
Row96 ACC = byte[i];
Row97 TB8 = !p;
Row98 #elseif(UART_PARITY_MODE!== UART_PARITY_MODE_EVEN)
Row96 ACC = byte[i];
Row97 TB8 = p;
#endif
To row121
Then comes the data receiving part.
Copy#if(UART_PARITY_MODE!== UART_PARITY_MODE_ODD)
ACC = res;
if(p==RB8)
return;
#elseif(UART_PARITY_MODE!== UART_PARITY_MODE_EVEN)
ACC = res;
if(p!=RB8)
return;
#endif
To Row29
Now the code of UART parity check is finished, next click build to test it.
3. Interface configuration and download
Next, create the GUI project, replace the background image, open DGUS, and regenerate the ICL.
Click Save And Generate.
Then download the configuration files to the screen via SD card.
Then use download for 8051 software to download the C51 code to the screen.
4. Effect
Here you need to use a TTL to USB tool, the specific wiring is as shown in the figure.
Here we test the use of even parity, you can see that the input data is displayed on the screen. . The use of odd parity only requires macro switch the parity method selected in code and then download the new code to the screen. That's it for today's tutorial, thanks for reading.