Microchip
直播中

武彩霞

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

该怎么做才能把波特率转换到所需的水平?

嗨,所以我有一个PIC16F628 A芯片。我可以使用HW UART模块以定制的波特率发送和接收数据。现在我需要在某个时候从另一个PIN发送数据,比如RB5或RB4,以不同的波特率(使用位敲击软件功能——它独立工作良好)。这里的标准是9600波特。问题是当使用软件功能发送数据时,它不能以正确的速度输出。这是因为设置HU-UART参数吗?如果是这样,我该怎么做才能把波特率转换到所需的水平?我试着改变SPBRG的价值,但没什么区别。谢谢。

以上来自于百度翻译


      以下为原文

    Hi,

So I have a PIC16F628A chip. I can send and receive data at a custom baud rate using the HW UART module.

Now I need to at some point send data out of another PIN, so like RB5 or RB4 at a different baud rate (using bit banging software functions - which works fine independently ) . Standard 9600 baud here.

Problem I have is when using the software functions to send data it doesn't go out at the right speeds. Is this due to setting the HW UART params  ? If so what should I do to switch the baud rate to the required level ?

I tried to change the value of : SPBRG , but no difference.

Thanks

回帖(7)

张娜

2019-6-20 12:52:08
那么你有一个UWART和一个SW UART?这应该有效(并且SPBRG寄存器不会影响SW):显示您的代码位敲击UART…

以上来自于百度翻译


      以下为原文

    So you have a HW UART and a SW UART?
This should work (and SPBRG register won't affect the SW one): show your code for bit-banged UART....
举报

王璨

2019-6-20 13:02:30
你的问题还不清楚,当通过UART的接收激活时,BIT-BANTX定时错误?如果是这样的话,这不是意外的,当交织(UART?)是活跃的。或者禁止在中断期间中断,或者如果可能的话,使用定时中断来获得敲击正时。(我只能希望你不会尝试通过软件接收。)在这种情况下,我建议使用一个PIC与2个或更多UART。

以上来自于百度翻译


      以下为原文

    Your question is not yet clear..
The bit-bang TX timing is wrong when reception via UART is active?
If so: this is not unexpected when interrups (UART?) are active. Either disable interrupts during baning or - if possible - use a timer interrupt to get the banging timing right. (I can only hope you will not try to receive via software. In this case I'd suggest to use a PIC with 2 or more UARTs.)
举报

李猛

2019-6-20 13:14:28
对不起,对不起。因此,HW UART(RB1,RB2)以其所需波特率发送和接收到一个设备(设备A)。它很好用。现在对于RB5,RB4我想发送和接收另一波特率通过比特敲击到另一个设备…我本以为位敲击代码应该像不与其他代码绑定时一样工作。没有HW UART...#._XTAL_FREQ 4000000//系统时钟速率define SER_BAUD 103define SER_BIT 1define TxPin PORTBbits.RB5define RxPin PORTBbits.RB4void send_._byte(无符号字符数据){无符号字符i=8;//8个数据位发送到sendTxPin=!SER_BIT;//make start bitTMR0=(256-SER_BAUD);//加载第一波特的TMR1值;.(TMR0&(1<<7));//wait baud.(i)//发送8个串行比特,LSB优先{if(data&1<0)TxPin=SER_BIT;//发送数据比特.TxPin=!data=(data>>1);//右旋转以得到下一位--;TMR0-=SER_BAUD;//负载校正波特值同时(TMR0&;1<;<7);//负载校正波特值同时(TMR0&;1<;<7);;//等待波特}/等待波特}TxPin}TxPin=SER_BIT;//使停止位TMRR0-=SER_BAUD;//使停止位TMR停止位TMRR0-=SER_BAUD;=SER_BAUD;//使停止位TMRR0停止位TMRR0-=SER_BAUD;//停止;//等待两个波特7)TMR0-=SER_BAUD;(TMR0&1<<<7);}无符号char._串_字节(无效){无符号char RxChr(无效){无符号字符RxChr;//无符号字符字符字符Rxchar R无符号char R._._._._seri_._串字节(无效){未签名字符RxChr RxChr RxChr RxChr(RRR0&1&1<1<无效(无效)RXXChr)RXChr);//保持接收到被接收到被接收到字符的串字节的串字节的字符i;//比特索引索引索引索引=8;//比特索引8;//8;//8数据w针对baud.(i)//接收8个串行位,LSB首先{RxChr=(RxChr>>1);//右转以存储每个位(RxPin==1)//保存数据位RxChr=RxChr|SER_BIT<7;否则RxChr=RxChr|!SER_BIT<7;i--//Next BitTMR0-=SER_BAUD;//load.edbaudvalue.(TMR0&1<<7);//wait wait baud}TMR0-=SER_BAUD;//wait等待停止位,确保串行端口是空闲的,同时(TMR0&1<7);在HW UART完成任务后,在主机内返回RxChr;}。要推出pin5@9600.TxPin=0;//Turn all Output OFFTRISBbits.TRISB5=0;TRISBbits.TRISB4=1;OP._REGbits.T0CS=0;//在Timer ModeOP._REGbits.PSA=1中选择TMR0;for(int a=0;a<100;a+){send_._byte(0xAA);u._ms(100);}.(1){}

以上来自于百度翻译


      以下为原文

    @du00000001 sorry to be clear. So the HW UART (RB1, RB2) are transmitting and receiving to one device (Device A) at its required baud rate. It works fine. Now for RB5, RB4 i want to send and receive at another baud rate via bit banging to another device... i would've thought the bit banging code should just work as it was when not tied in with any other code.
 
@DarioG heres my software bit bang, found online and works at 9600 when just coded as it is... no HW UART...
 
#define _XTAL_FREQ 4000000 // System Clock rate
#define SER_BAUD 103 
#define SER_BIT 1
#define TxPin PORTBbits.RB5
#define RxPin PORTBbits.RB4
 
 
 
void send_serial_byte(unsigned char data)
{
unsigned char i;
i = 8; // 8 data bits to send
 
TxPin = !SER_BIT; // make start bit
TMR0  = (256 - SER_BAUD); // load TMR1 value for first baud;
while(TMR0 & (1 << 7)); // wait for baud
 
while(i) // send 8 serial bits, LSB first
{
if(data & 1<<0)TxPin= SER_BIT; // send data bit
else           TxPin=!SER_BIT;
 
data = (data >> 1); // rotate right to get next bit
i--;
TMR0 -= SER_BAUD; // load corrected baud value
while(TMR0 & 1<<7); // wait for baud
}
TxPin =  SER_BIT; // make stop bit
TMR0 -= SER_BAUD; // wait a couple of baud for safety
while(TMR0 & 1<<7);
TMR0 -= SER_BAUD;
while(TMR0 & 1<<7);
}
 
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 - 19)); // 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;
}
 
 
inside the main i have this after HW UART has done its things. I want to push out data on pin5 @9600.
 
TxPin = 0; // Turn all Outputs OFF
TRISBbits.TRISB5 = 0;
TRISBbits.TRISB4 = 1;
OPTION_REGbits.T0CS = 0; // Select TMR0 in Timer Mode 
OPTION_REGbits.PSA = 1;
 
for(int a=0; a<100;a++)
{
send_serial_byte(0xAA);
__delay_ms(100);
}
while(1)
{}
举报

张娜

2019-6-20 13:28:20
所以,没有SPBRG,你能看到针脚上的活动吗?波特率是错的吗?你的照片跑的速度是多少?

以上来自于百度翻译


      以下为原文

    So, no SPBRG, definitely
Can you see activity at pins? Is just baud rate wrong? What speed is your PIC running at?
举报

更多回帖

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