473,659 Members | 2,987 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Odd and even parity

1 New Member
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_MOD E_NONE 0(无奇偶校验)
row4 #define UART_PARITY_MOD E_ODD 1(奇校验)
row5 #define UART_PARITY_MOD E_EVEN 2(偶校验)

Then define a current UART parity mode, here we define it as odd parity.
row7 #define UART_PARITY_MOD E UART_PARITY_MOD E_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 "Developmen t 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_MOD E_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 "developmen t 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_MOD E_ODD)
Row96 ACC = byte;
Row97 TB8 = !p;
Row98 #elseif(UART_PA RITY_MODE!== UART_PARITY_MOD E_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_MOD E_ODD)
Row96 ACC = byte[i];
Row97 TB8 = !p;
Row98 #elseif(UART_PA RITY_MODE!== UART_PARITY_MOD E_EVEN)
Row96 ACC = byte[i];
Row97 TB8 = p;
#endif
To row121

Then comes the data receiving part.
Copy#if(UART_PA RITY_MODE!== UART_PARITY_MOD E_ODD)
ACC = res;
if(p==RB8)
return;
#elseif(UART_PA RITY_MODE!== UART_PARITY_MOD E_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 5728

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

Similar topics

8
5236
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 space to even parity. And when I get the response I need to convert that from even to space parity. The perl module String::Parity is what I have used to do this under perl. Chris
2
12141
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 master. MDB protocol is the 9bit serial protocol: (TX, RX lines only) 9600bps, 9bits, No Parity, 1 Start, 1 Stop. I would like to control the UART "parity bit" to try to simulate 9bit communication.
15
23600
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 that with some extra code, but I guess there should be at least an ASM command? (Intel) Thanks for any help!
2
1478
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 following compiler error: error C3610: value type must be 'boxed' Here is the code:
0
1302
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. I can detect the first parity error, and then the following data is processed without a parity error, just like it should be, but then the last byte of data, which should have a parity error, doesn't detect the parity error. Am I just running...
11
12324
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
1633
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 static StopBits stopBits = StopBits.One; public static Handshake handShake = Handshake.None;
8
5840
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
3976
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 bit. If this is not possible either, the matrix should be classified as corrupt. 1Stolen from Miguel A. Revilla, Error Correction, http://online- judge.uva.es/p/v5/541.html, accessed 1 Nov, 2005 1 The Input The input file will contain one or...
18
4897
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 an appreciation for it. So I decided to toy with some idea, just for fun and for evaluating how rusty I'd become. "Let me write a simple functor for sorting the elements of a collection! I will start with a simple collection of integers.", I...
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8335
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8528
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.