#include <xc.h>
//returns 0x00 (even parity) or 0x80 (odd parity)
unsigned char parity(volatile unsigned char dat) //("volatile" is required because no C code reads the parameter)
{
asm("swapf parity@dat,w"); //assume correct bank is still selected
asm("xorwf parity@dat,w"); //W has 8 bits reduced to 4 with same parity
asm("addlw 41h"); // bit 1 becomes B0^B1 and bit 7 becomes B6^B7
asm("iorlw 7Ch"); // for carry propagation from bit 1 to bit 7
asm("addlw 2"); // Done! the parity bit is bit 7 of W
asm("andlw 80h"); // set NZ if odd parity, and leave 00 or 80 in W
asm("return");
return 1; //dummy instruction to defeat "no return value" error
}
void main(void) {
unsigned char idx=0;
while(1)
{
PORTA = parity(idx);
idx++;
}
}
I'm not sure if there's a cleaner way to suppress the "no return value" error, without generating extra code.
Question
ric
This is my attempt to convert the blazingly fast assembler routine for calculating parity into a C function.
The original comes from here: https://www.microchip.com/forums/m4762.aspx
Unfortunately Microchip have killed off the board where the original discussion took place at http://asp.microchip.com/webboard/wbpx.dll/~DevTools/read?21443,5
I'm not sure if there's a cleaner way to suppress the "no return value" error, without generating extra code.
Link to comment
Share on other sites
9 answers to this question
Recommended Posts
Archived
This topic is now archived and is closed to further replies.