Parity calculation guidance
so working on project, , unfortunately new see path complete current task. advice on (or how conceptually solution task appreciated.
this application simulate rfid card read using arduino. have sketch can crete card number (and code kinda ugly works), have disable parity checking in order whole thing work.
the data looks this:
p[data]p
…where p single bit , data 35 bits (35 bits plus 2 parity 37 bits total). beginning parity bit , covers first nineteen bits inclusive, ending parity bit odd , covers last nineteen bits inclusive.
(see parity pic)
while not 100% there yet, believe can construct 35 bits of data reliably. cannot come method sum group of bits up, decide if or odd number, , decide how set parity bit.
i using long store value because int short.
i can manually via following process (i.e. leading parity bit):
1. walk thru each of "e" bits counting 1 bits (18bits total).
2. decide if number odd or even.
3. if odd, set parity bit 1, making sum even.
4. else set parity bit 0.
i think can:
-walk thru data , bitread each bit
-increment int variable 1 each bit 1.
-when finished, read lsb of int variable determine if int or odd.
-bitwrite parity bit position in long
can suggest better way or going code method of doing calculation pen , paper??
this application simulate rfid card read using arduino. have sketch can crete card number (and code kinda ugly works), have disable parity checking in order whole thing work.
the data looks this:
p[data]p
…where p single bit , data 35 bits (35 bits plus 2 parity 37 bits total). beginning parity bit , covers first nineteen bits inclusive, ending parity bit odd , covers last nineteen bits inclusive.
(see parity pic)
while not 100% there yet, believe can construct 35 bits of data reliably. cannot come method sum group of bits up, decide if or odd number, , decide how set parity bit.
i using long store value because int short.
i can manually via following process (i.e. leading parity bit):
1. walk thru each of "e" bits counting 1 bits (18bits total).
2. decide if number odd or even.
3. if odd, set parity bit 1, making sum even.
4. else set parity bit 0.
i think can:
-walk thru data , bitread each bit
-increment int variable 1 each bit 1.
-when finished, read lsb of int variable determine if int or odd.
-bitwrite parity bit position in long
can suggest better way or going code method of doing calculation pen , paper??
ok, kind of tricky, overlapping nature makes hard craft structs and/or unions it.
fist though, long 32 bits, can't store whole packet in it. you need bigger. let's @ how craft struct store raw data. we'll best off using "unsigned long long" value basic data storage, that's 64 bits. over that, using union, can place pair of structs, 1 odd , 1 parity calculation. to should add struct @ packet portion of long long. we might end like:
fist though, long 32 bits, can't store whole packet in it. you need bigger. let's @ how craft struct store raw data. we'll best off using "unsigned long long" value basic data storage, that's 64 bits. over that, using union, can place pair of structs, 1 odd , 1 parity calculation. to should add struct @ packet portion of long long. we might end like:
code: [select]
struct packet {
union {
// raw storage area - 64 bits
unsigned long long data;
// split area, 1:18:45 split.
struct {
unsigned long long evenpar:1; // set parity here
unsigned long long evendata:18; // count bits here
};
// split odd area
struct {
unsigned long oddpad1:18;
unsigned long odddata:18; // count bits here
unsigned long oddpar:1; // set parity here
};
// access area 37 bit packet data
struct {
unsigned long long packetdata:37; // full result
};
// area manipulating data content
struct {
unsigned long long epar:1;
unsigned long long pdata:35; // set raw data value
unsigned long long opar:1;
};
};
} __attribute__((packed));
struct packet p;
// count number of set bits in value. pass value , the
// total number of bits in value.
byte countbits(unsigned long long val, byte bits) {
byte bcount = 0;
while(bits > 0) {
bcount += (val & 1 ? 1 : 0);
val >>= 1;
bits--;
}
return bcount;
}
// print 37-bit value binary information
// lines wrapping it.
void printbin(unsigned long long v) {
serial.println();
serial.println("edddddddddddddddddd");
for (int = 0; < 37; i++) {
serial.print(v & (1llu << (36-i)) ? 1 : 0);
}
serial.println();
serial.println(" ddddddddddddddddddo");
}
void setup() {
serial.begin(9600);
// data value. note use of ull suffix make unsigned long long
p.pdata = 0b11001010110101011100010101101011100ull;
// calculate 2 parity bits.
p.evenpar = (countbits(p.evendata, 18) & 1) ? 0 : 1;
p.oddpar = (countbits(p.odddata, 18) & 1) ? 1 : 0;
// print results.
printbin(p.packetdata);
}
void loop() {
}
Arduino Forum > Using Arduino > Programming Questions > Parity calculation guidance
arduino
Comments
Post a Comment