Tech Tips #7102 MICRO/C-51 V1.xx

Generating Even or Odd Serial Parity

This Tech Tip explains how to generate even or odd parity for 7-bit ASCII characters using MICRO/C-51.

Introduction

Most applications that use serial communications transmit 7-bit ASCII characters with no parity. Most of us are familiar with the 8,N,1 settings when connecting to a BBS using terminal software on our PC. Well, the N stands for None, meaning No Parity. In fact, our standard putchar() in the MICRO/C-51 Run-Time Library works just this way. However, there may be times when you need to communicate with another device that requires even or odd parity.

Even parity specifies that the total number of bits set within a transmitted 8-bit byte be an even number. Odd parity specifies that an odd number of bits are set. When sending 7-bit ASCII characters with parity, the most significant (8th) bit, which is not used in ASCII code, is set or cleared to provide the required number of bits set for the transmitted 8-bit byte.

Application

The MICRO/C-51 code samples below demonstrate odd and even parity generator functions.

odd_parity(c)
char(c)
{
if (acc = c, ~p) /* Load the character in ACC, test the Parity Flag, if Even*/
   c = 0x80 | c; /* Set parity bit */
return(c);
}

even_parity(c)
char(c)
{
if (acc = c, p) /* Load the character in ACC, test the Parity Flag, if Odd */
   c = 0x80 | c; /* Set parity bit */
return(c);
}

In both these functions, the received character argument is returned with the proper parity in the most significant (8th) bit position.

We would like to receive your comments on this Tech Tip and suggestions for others. E-mail us at info@mcc-us.com