同位檢查位元 (Parity Check Bit)
同位檢查位元 (Parity Check Bit)
用來檢查傳輸資料的位元數中,1的個數是否一致。
→ 奇同位 (Odd Parity):1 的個數加起來須為奇數個
→ 偶同位 (Even Parity):1 的個數加起來須為偶數個
奇/偶 資料 同位元 資料正確性
奇同位 01001 1 可能對
奇同位 01001 0 一定錯
偶同位 01001 1 一定錯
偶同位 01001 0 可能對
=================================================================
struct Flag{
unsigned bit0:1;
unsigned bit1:1;
unsigned bit2:1;
unsigned bit3:1;
unsigned bit4:1;
unsigned bit5:1;
unsigned bit6:1;
unsigned bit7:1;
};
struct Flag2{
unsigned bps:1;
unsigned data:1;
unsigned parity:1;
unsigned stpbit:1;
unsigned enbfg:1;
unsigned bit5:1;
unsigned bit6:1;
unsigned bit7:1;
};
union{
unsigned char BITx;
struct {
unsigned bit0:1;
unsigned bit1:1;
unsigned bit2:1;
unsigned bit3:1;
unsigned bit4:1;
unsigned bit5:1;
unsigned bit6:1;
unsigned bit7:1;
};
} CByteMsg, OddByte, EvenByte;
#define ParityEven 1 //偶同位 (Even Parity):1 的個數加起來須為偶數個(Add bit7).
#define ParityOdd 2 //奇同位 (Odd Parity):1 的個數加起來須為奇數個(Add bit7).
void _app_paritybit_encode(unsigned char* byteda, unsigned char paritySte)
{
unsigned char j = 0;
bool oddflag;
//將數值轉換存入可以的做Bit 計算的記憶體中.
CByteMsg.BITx = byteda;
//計算 bit 為 1 的數量
j = CByteMsg.bit6+CByteMsg.bit5+CByteMsg.bit4+
CByteMsg.bit3+CByteMsg.bit2+CByteMsg.bit1+CByteMsg.bit0;
//顯示有幾個 1
printf("byteda : %x\n", byteda);
printf("bit cnt : %x\n", j);
//判斷even or odd, 在bit 7 補上相對應的數值 0 or 1.
if (paritySte == ParityEven) {
if ((j & 0x01) == 1) {
CByteMsg.bit7 = 1; // Parity even.
}
else {
CByteMsg.bit7 = 0;
}
}
else {
if ((j & 0x01) == 1) {
CByteMsg.bit7 = 0; // Parity odd.
}
else {
CByteMsg.bit7 = 1;
}
}
printf("Parity bit : %x\n", CByteMsg.bit7);
printf("------------\n");
printf("CByteMsg.bit7 : %d\n", CByteMsg.bit7);
printf("CByteMsg.bit6 : %d\n", CByteMsg.bit6);
printf("CByteMsg.bit5 : %d\n", CByteMsg.bit5);
printf("CByteMsg.bit4 : %d\n", CByteMsg.bit4);
printf("CByteMsg.bit3 : %d\n", CByteMsg.bit3);
printf("CByteMsg.bit2 : %d\n", CByteMsg.bit2);
printf("CByteMsg.bit1 : %d\n", CByteMsg.bit1);
printf("CByteMsg.bit0 : %d\n", CByteMsg.bit0);
printf("\n");
j = CByteMsg.bit7+CByteMsg.bit6+CByteMsg.bit5+CByteMsg.bit4
+CByteMsg.bit3+CByteMsg.bit2+CByteMsg.bit1+CByteMsg.bit0;
if (paritySte == ParityOdd) {
printf("Odd Parity bit j : %x\n", j);
}
else {
printf("Even Parity bit j : %x\n", j);
}
//printf("CByteMsg.BITx : %x\n", CByteMsg.BITx);
printf("CByteMsg.BITx : %x\n", CByteMsg);
printf("--------------------------------------------\n");
printf("\n");
}
//***************************************************************************
void _app_paritybit_check(unsigned char* byteda)
{
unsigned char i, j;
i = byteda;
printf("---- Check Odd and Even parity bit ---------\n");
printf("--------------------------------------------\n");
_app_paritybit_encode(i, ParityOdd);
_app_paritybit_encode(i, ParityEven);
}
void _app_paritybit_test(void)
{
unsigned char mi;
mi = 0x0a;
_app_paritybit_check(mi);
mi = 0x31;
_app_paritybit_check(mi);
mi = 0x32;
_app_paritybit_check(mi);
mi = 0x0d;
_app_paritybit_check(mi);
mi = 0x03;
_app_paritybit_check(mi);
}
int main(void)
{
unsigned char mi;
_app_paritybit_test();
//_app_paritybit_check(mi);
//_app_paritybit_decode(mi);
//bitset_control();
return 0;
}
用來檢查傳輸資料的位元數中,1的個數是否一致。
→ 奇同位 (Odd Parity):1 的個數加起來須為奇數個
→ 偶同位 (Even Parity):1 的個數加起來須為偶數個
奇/偶 資料 同位元 資料正確性
奇同位 01001 1 可能對
奇同位 01001 0 一定錯
偶同位 01001 1 一定錯
偶同位 01001 0 可能對
=================================================================
struct Flag{
unsigned bit0:1;
unsigned bit1:1;
unsigned bit2:1;
unsigned bit3:1;
unsigned bit4:1;
unsigned bit5:1;
unsigned bit6:1;
unsigned bit7:1;
};
struct Flag2{
unsigned bps:1;
unsigned data:1;
unsigned parity:1;
unsigned stpbit:1;
unsigned enbfg:1;
unsigned bit5:1;
unsigned bit6:1;
unsigned bit7:1;
};
union{
unsigned char BITx;
struct {
unsigned bit0:1;
unsigned bit1:1;
unsigned bit2:1;
unsigned bit3:1;
unsigned bit4:1;
unsigned bit5:1;
unsigned bit6:1;
unsigned bit7:1;
};
} CByteMsg, OddByte, EvenByte;
#define ParityEven 1 //偶同位 (Even Parity):1 的個數加起來須為偶數個(Add bit7).
#define ParityOdd 2 //奇同位 (Odd Parity):1 的個數加起來須為奇數個(Add bit7).
void _app_paritybit_encode(unsigned char* byteda, unsigned char paritySte)
{
unsigned char j = 0;
bool oddflag;
//將數值轉換存入可以的做Bit 計算的記憶體中.
CByteMsg.BITx = byteda;
//計算 bit 為 1 的數量
j = CByteMsg.bit6+CByteMsg.bit5+CByteMsg.bit4+
CByteMsg.bit3+CByteMsg.bit2+CByteMsg.bit1+CByteMsg.bit0;
//顯示有幾個 1
printf("byteda : %x\n", byteda);
printf("bit cnt : %x\n", j);
//判斷even or odd, 在bit 7 補上相對應的數值 0 or 1.
if (paritySte == ParityEven) {
if ((j & 0x01) == 1) {
CByteMsg.bit7 = 1; // Parity even.
}
else {
CByteMsg.bit7 = 0;
}
}
else {
if ((j & 0x01) == 1) {
CByteMsg.bit7 = 0; // Parity odd.
}
else {
CByteMsg.bit7 = 1;
}
}
printf("Parity bit : %x\n", CByteMsg.bit7);
printf("------------\n");
printf("CByteMsg.bit7 : %d\n", CByteMsg.bit7);
printf("CByteMsg.bit6 : %d\n", CByteMsg.bit6);
printf("CByteMsg.bit5 : %d\n", CByteMsg.bit5);
printf("CByteMsg.bit4 : %d\n", CByteMsg.bit4);
printf("CByteMsg.bit3 : %d\n", CByteMsg.bit3);
printf("CByteMsg.bit2 : %d\n", CByteMsg.bit2);
printf("CByteMsg.bit1 : %d\n", CByteMsg.bit1);
printf("CByteMsg.bit0 : %d\n", CByteMsg.bit0);
printf("\n");
j = CByteMsg.bit7+CByteMsg.bit6+CByteMsg.bit5+CByteMsg.bit4
+CByteMsg.bit3+CByteMsg.bit2+CByteMsg.bit1+CByteMsg.bit0;
if (paritySte == ParityOdd) {
printf("Odd Parity bit j : %x\n", j);
}
else {
printf("Even Parity bit j : %x\n", j);
}
//printf("CByteMsg.BITx : %x\n", CByteMsg.BITx);
printf("CByteMsg.BITx : %x\n", CByteMsg);
printf("--------------------------------------------\n");
printf("\n");
}
//***************************************************************************
void _app_paritybit_check(unsigned char* byteda)
{
unsigned char i, j;
i = byteda;
printf("---- Check Odd and Even parity bit ---------\n");
printf("--------------------------------------------\n");
_app_paritybit_encode(i, ParityOdd);
_app_paritybit_encode(i, ParityEven);
}
void _app_paritybit_test(void)
{
unsigned char mi;
mi = 0x0a;
_app_paritybit_check(mi);
mi = 0x31;
_app_paritybit_check(mi);
mi = 0x32;
_app_paritybit_check(mi);
mi = 0x0d;
_app_paritybit_check(mi);
mi = 0x03;
_app_paritybit_check(mi);
}
int main(void)
{
unsigned char mi;
_app_paritybit_test();
//_app_paritybit_check(mi);
//_app_paritybit_decode(mi);
//bitset_control();
return 0;
}
留言
張貼留言