对不起,对不起。因此,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)
{}
对不起,对不起。因此,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)
{}
举报