Microchip
直播中

武彩霞

7年用户 209经验值
私信 关注
[问答]

如何调整PIC16F628A以便其能够处理8位数据和1个奇偶校验位

嗨,我有一个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

回帖(19)

李维兴

2019-7-12 12:46:18
为什么不在16F628 A中使用内置的UART模块呢?

以上来自于百度翻译


      以下为原文

    Why don't you use the built-in USART module in the 16F628A?
举报

王璨

2019-7-12 12:54:02
您好,有人可能会批评您的编码风格,但是您的主要问题是RX变量是无符号字符——当奇偶校验打开时只需要8位。(不知道当前的值,但我敢肯定它需要乘以2.BTW:标准UART移位LSBIT第一。因此,用0x80 Re.0x100处理RX缓冲器,当下一个比特到达时右移是相当有效的。

以上来自于百度翻译


      以下为原文

    Hello,
one might criticize your coding style, but yor main problem is that your RX variable is unsigned char - holding only 8 bits where 9 are required when parity is on. (Don't know about the current value, but I'm quite sure it will need a multiplication by 2.
BTW: standard UARTs shift LSBit first. Thus ORing the RX buffer with 0x80 resp.0x100 and shifting right when the next bit arrived is quite efficient.
Regards
举报

李猛

2019-7-12 13:07:12
HI @ DU000 000 01 -所以基本上我只是想忽略传入奇偶校验,所以8位应该是好的,因此,无符号字符。我会想到第一个输入位的延迟,然后处理其他8位应该按照预期工作。一个简单的回音在没有输入奇偶校验位的情况下工作得很好,所以我认为我的方法和右移等方法可以吗?@1和0-我不太熟悉这些模块,因为我对这个还不熟悉,但是内置的模块是否接受传入的奇偶校验位(我猜是第9位)?任何例子都是很棒的。在接收到的8位数据+ 1位奇偶校验时,我找不到任何东西。

以上来自于百度翻译


      以下为原文

    Hi 
@du00000001 - so basically i just want to ignore the incoming parity so 8 bits should be ok hence the unsigned char. I would've thought a delay of the first incoming bit and then handling the other 8 bits should work as expected. A simple echo works fine without an incoming parity bit - so i think the method i have and shifting right etc is ok ?
 
@1and0 - im not too familiar with the modules as I'm new to this , but does the built in module accept incoming parity bits (a 9th bit i guess) ? Any examples of this would be great. I can't find anything on receiving incoming 8 bits of data + 1 bit of parity.
 
 
举报

王焕树

2019-7-12 13:15:32
有一个鲜为人知的文件,您可以阅读有关这一点。在74页上,您将看到USART接收机(RCSTA)的控制寄存器包含一个名为“RX9:9位接收启用位”的位和另一个名为“RX9D:9位接收数据(可以是奇偶校验位)”的位,因此您完全能够启用9位模式。,但是忽略接收到的奇偶校验位的值。

以上来自于百度翻译


      以下为原文

   
There is a little known document where you can read about this. It is known as the PIC16F628A datasheet
On page 74 you will observe that the control register for the USART receiver (RCSTA) contains a bit called "RX9: 9-bit Receive Enable bit" and another one called "RX9D: 9th bit of received data (Can be parity bit)"
 
So you are perfectly able to enable 9-bit mode, but ignore the value of the received parity bit.
 
举报

更多回帖

发帖
×
20
完善资料,
赚取积分