$ cat hdlc_nrzi_dec.c #include <stdio.h>
void main() { unsigned char data[10] = {0xFE, 0x5A, 0xD2, 0x22, 0xFA, 0xCA, 0xFE, 0x00, 0x00, 0x00}; unsigned char lsbmsb[10] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; unsigned char bitstuff[10] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; unsigned char nrzi[10] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
int i = 0; int b = 0;
// zrzi printf("NRZ-I ----------------------------------------\n"); unsigned char last = 0; unsigned char curr = 0; unsigned char out = 0; for(i = 0; i < 80; i++) { curr = (data[i / 8] >> (7 - (i % 8))) & 0x01; // printf("i:%d l:%d c:%d ", i, last, curr); if(last != curr) out = 0; else out = 1 ; last = curr; // printf("o:%d\n", out); nrzi[i / 8] |= out << (7 - (i % 8));
if(i % 8 == 7) printf("%02X -> %02X\n", data[i/8],nrzi[i/8]); }
// msb to lsb, data order change printf("bitwise LSB convert ----------------------------------------\n"); for(i = 0; i < 10; i++) { unsigned char tempval = 0; unsigned char orival = 0; orival = nrzi[i]; for(b = 0; b < 8; b++) { tempval |= ((orival & 0x01) << (7 - b)); orival = orival >> 1; } lsbmsb[i] = tempval; printf("%02X -> %02X\n",nrzi[i],lsbmsb[i]); }
} |