Hi
Tuss4470 IC uses SPI to communicate with the Master.
Data format: (Reading the register)
msb = 1 r/w bit + 6 regi-addr bits + parity bit
lsb = 0x00
Odd parity bit should be calculated.
The open source project uses the next:
byte tuss4470Parity(byte* spi16Val)
{
return parity16(BitShiftCombine(spi16Val[0], spi16Val[1]));
}
unsigned int BitShiftCombine(unsigned char x_high, unsigned char x_low) {
return (x_high << 8) | x_low; // Combine high and low bytes
}
byte parity16(unsigned int val) {
byte ones = 0;
for (uint8_t i = 0; i < 16; i++) {
if ((val >> i) & 1) {
ones++;
}
}
return (ones + 1) % 2; // Odd parity calculation
}
Is it possible to calculate the parity bit for msb only (as the lsb = 0x00)?
Is it better to calculate the parity bit for integer (msb << 8) + lsb ?
Thanks
Parity bit
-
viktor_aust
- Posts: 32
- http://meble-kuchenne.info.pl
- Joined: Fri May 17, 2024 1:04 am
- Has thanked: 10 times
- Been thanked: 5 times
-
viktor_aust
- Posts: 32
- Joined: Fri May 17, 2024 1:04 am
- Has thanked: 10 times
- Been thanked: 5 times
Re: Parity bit
Example
Data (16-bit): 1010 0101 1100 0011
Count of 1s: 8 (even)
Odd Parity Bit: 1
Resulting frame (with parity): 1010 0101 1100 0011 + 1
Data (16-bit): 1010 0101 1100 0011
Count of 1s: 8 (even)
Odd Parity Bit: 1
Resulting frame (with parity): 1010 0101 1100 0011 + 1
-
mnfisher
- Valued Contributor
- Posts: 1905
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 154 times
- Been thanked: 896 times
Re: Parity bit
I would probably count the bits in both MSB and LSB. If a byte (LSB or MSB) is 0 - it won't affect the parity value - but if there is an error in transmission then it might not be 0 - and will affect the final result (if you use (MSB << 8) + LSB)
Alternatively - if LSB must be 0 - ignore it - and use result = (MSB << 8)
I think option '1' would be more likely to detect errors (due to noise) on the line?
Martin
Alternatively - if LSB must be 0 - ignore it - and use result = (MSB << 8)
I think option '1' would be more likely to detect errors (due to noise) on the line?
Martin
-
viktor_aust
- Posts: 32
- Joined: Fri May 17, 2024 1:04 am
- Has thanked: 10 times
- Been thanked: 5 times
Re: Parity bit
Hi
Thanks Martin
However I do something wrong.
On the attached example the
byte = 10011100 (4 ones)
however the parity check = 5
---
As I use the byte only, this byte has: 1bit (r/w)+ 6 bits (regi addr) + 1bit (parity).
Should I remove the parity bit during the calculations?
Thanks Martin
However I do something wrong.
On the attached example the
byte = 10011100 (4 ones)
however the parity check = 5
---
As I use the byte only, this byte has: 1bit (r/w)+ 6 bits (regi addr) + 1bit (parity).
Should I remove the parity bit during the calculations?
- Attachments
-
- FC11_Nano_parity_1.fcfx
- (18.88 KiB) Downloaded 3 times
-
mnfisher
- Valued Contributor
- Posts: 1905
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 154 times
- Been thanked: 896 times
Re: Parity bit
Rather than using an if .... statement to calculate the parity bit (and brackets might change the result here if (.x == 1) || (.x == 3) etc
Depending on whether you need odd or even parity:
Use .parity = .x & 1 Returns .parity = 1 if an odd number of bits set
Or if it needs to be 1 for an even number of bits:
.parity = (.x + 1) & 1
Where .x is the number of bits set.
Martin
Depending on whether you need odd or even parity:
Use .parity = .x & 1 Returns .parity = 1 if an odd number of bits set
Or if it needs to be 1 for an even number of bits:
.parity = (.x + 1) & 1
Where .x is the number of bits set.
Martin