473,421 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,421 developers and data experts.

Odd and even parity

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.
Aug 26 '22 #1
0 5668

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: snacktime | last post by:
Is there a module that sets the parity of a string? I have an application that needs to communicate with a host using even parity So what I need is before sending the message, convert it from...
2
by: Petr Jakes | last post by:
Hi, I am trying to set-up communication to the coin change-giver from my Linux box using the Python code. The change giver uses MDB (Multi Drop Bus) serial protocol to communicate with the...
15
by: Herbert Haas | last post by:
Hi everyone, is there a simple and fast method to check the parity of a word (e. g. a 4-byte integer)? That is I want to know whether the number of ones are even or odd. Of course I could do...
2
by: Peter Oliphant | last post by:
OK, I'm mixing old style with new style, so sue me... : ) Will under old syntax, I'm able to create a SerialPort instance. But, when I try to convert the Parity proerty to a String*, I get the...
0
by: Loren | last post by:
I am using parity error checking as I receive data into the input buffer from the comm port. The data I receive consists of a parity error, followed by received data, followed by a parity error. ...
11
by: nick.stefanov | last post by:
I am trying to compute an even parity bit for a variable of type char. Are there any C++, C# examples on how to first compute the bit and then check it. Thank you very much ahead of time. Nick
0
by: Radu Crisan | last post by:
Hi all, i have this RS232 settings public static string portName; public static Int16 baudRate = 19200; public static Parity parity = Parity.Mark; public static Int16 dataBits = 8; public...
8
by: philbo30 | last post by:
I admit, I didn't spend any time researching this yet so this is the first step... Is there a C function available to determine byte parity or is the usual course to just count the 1s?
4
by: sunfeifei | last post by:
Your job is to write a C++ program that reads in a matrix and checks if it has the parity property. If not, your program should check if the parity property can be established by changing only one...
18
by: =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?= | last post by:
Hello. It seems a year is all it takes for one's proficiency in C++ to become too rusty. Professionally, I've been away from the language (and from programming in general), but I still preserve...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.