嗨,我有一个PIC16F628A,我可以创建一个传入字节的简单回声,它工作得很好。但是我需要调整它,以便它能够处理8位数据和1个奇偶校验位。我试过几件事,但没用。我尝试改变方法接受9位,我尝试添加计时器忽略第一位,以及…但是这种情况并没有发生。希望得到任何帮助。这是我为发送和接收方法提供的代码(我在网上找到了,这是由于Charles)无符号char._._byte(void){unsigned char RxChr;//保存收到的无符号char i的串行字节;//位索引i=8;//8位数据位以接收TMR0=(256-(SER_BAUD));//加载TMR1值以偏移RxBit的中心,而(TMR0&1<<7);//等待波特,而(i)//接收8位串行位,LSB优先{RxChr=(RxChr>>1);//如果(RxPin==1)//保存数据位RxChr=RxCR SeriBIT & lt;lt;7;否则RXCHR=RXCHCHR!SER_BIT<7;i-//Next Bit TMR0-=SER_BAUD;//load校正了波特值,而(TMR0&1<<7);//waitthebaud}TMR0-=SER_BAUD;//等待停止位,确保串行端口是空闲的,而(TMR0&1<<7);/返回RxChr;},或者如果任何人有任何代码,则返回RxChr。KS
以上来自于百度翻译
以下为原文
Hi,
I have a PIC16F628A, I can create a simple echo of an incoming byte and it works fine.
However I need to adjust it so it can handle 8 bits of data and 1 parity bit. I've tried a few things and its not working. Ive tried changing the method to accept 9 bits, I've tried adding a
timer to ignore the first bit as well... but it just ain't happening.
Any help would be appreciated.
This is the code I have for the send and receive methods (which i found online thanks to Charles)
unsigned char receive_serial_byte(void)
{
unsigned char RxChr; // holds the serial byte that was received
unsigned char i; // Bit Index
i = 8; // 8 data bits to receive
TMR0 =(256 - (SER_BAUD)); // load TMR1 value to offset ~center of RxBit
while(TMR0 & 1<<7); // wait for baud
while(i) // receive 8 serial bits, LSB first
{
RxChr = (RxChr>>1); // rotate right to store each bit
if( RxPin == 1) // save data bit
RxChr = RxChr | SER_BIT<<7;
else RxChr = RxChr |!SER_BIT<<7;
i--; // Next Bit
TMR0 -= SER_BAUD; // load corrected baud value
while(TMR0 & 1<<7); // wait for baud
}
TMR0 -= SER_BAUD; // wait for stop bit, ensure serial port is free
while(TMR0 & 1<<7);
return RxChr;
}
Or if anyone has any code that would be great.
Thanks