这是一段PIC单片机CAN总线波特率初始化设置的程序,我想自动查找外部CAN的波特率,先自己设定了一个波特率10Kbps,然后监听是否收到外部数据,如果没收到外部数据,就换下一个波特率20Kbps,依次类推将50,100,125,250,500这几个波特率全部搜索完毕。先看这段程序
#include "p18f4680.h"
#define uchar unsigned char
#define uint unsigned int
extern int iTCANID;
extern int iTCanID_PDO1;
extern int iRCANID;
extern int iRCanID_PDO1;
extern int iRCanID_PDO2;
static volatile int table[7]={0x31,0x18,0x09,0x04,0x03,0x01,0x00};
volatile unsigned char num,x,y;
void initcan()
{
num=0;
TRISB= 0x08;
PIE3= 0x00;
PIR3= 0x00;
CANCON=0X80;
while(CANSTAT&0X80==0) {;}
BRGCON1=table[num];
num++;
BRGCON2=0X90;
BRGCON3=0X42;
TXB0CON=0X31;
TXB0SIDL=(iTCanID_PDO1&0x00ff);
TXB0SIDH=(iTCanID_PDO1&0xff00)>>8;
TXB0DLC=0X08;
TXB0D0=0X00;
TXB0D1=0X00;
TXB0D2=0X00;
TXB0D3=0X00;
TXB0D4=0X00;
TXB0D5=0X00;
TXB0D6=0X00;
TXB0D7=0X00;
TXB0CONbits.TXREQ= 1;
RXB0SIDL=0x00;
RXB0SIDH=0x00;
RXB0CON=0X20;
RXB0DLC=0X08;
RXB0D0=0X00;
RXB0D1=0X00;
RXB0D2=0X00;
RXB0D3=0X00;
RXB0D4=0X00;
RXB0D5=0X00;
RXB0D6=0X00;
RXB0D7=0X00;
RXF0SIDH=(iRCanID_PDO1&0xff00)>>8;
RXF0SIDL=(iRCanID_PDO1&0x00ff);
RXM0SIDH=0XFF;
RXM0SIDL=0XE0;
RXF1SIDH=(iRCanID_PDO2&0xff00)>>8;
RXF1SIDL=(iRCanID_PDO2&0x00ff);
RXM1SIDH=0XFF;
RXM1SIDL=0XE0;
PIE3=PIE3|0X01;
IPR3=IPR3|0X01;
PIR3=0X00;
while(num<7)
{
PIE3=0X00;
IPR3=0X00;
PIR3=0X00;
CANCON=0x60;
while((CANSTAT&0x60)!=0X60)
{
;
}
PIE3=PIE3|0X01;
IPR3=IPR3|0X01;
PIR3=0X00;
for(x=5;x>0;x--)
for(y=5;y>0;y--); /延迟
if((PIR3bits.ERRIF==1)|(PIR3bits.RXB0IF==0))
{
CANCON=0x80;
while((CANSTAT&0X80)!=0x80)
{
;
}
BRGCON1=table[num];
num++;
if(num==7)
{num=0;}
}
else
num=7;
}
CIOCON=0X20;
CANCON=0X00;
while((CANSTAT&0XE0)!=0x00)
{
;
}
PIE3=PIE3|0X01;
IPR3=IPR3|0X01;
PIR3=0X00;
这段程序,为什么num=0,1,2,3时,程序在while()循环体内都能正常运行,但到了num=4时,程序在 while((CANSTAT&0X80)!=0x80) {;}中就成了死循环出不来了,也就是说状态寄存器CANSTAT怎么一直处于0x60的监听模式,而不随着控制寄存器CANCON变为0x80的配置模式而变为0x80,苦恼啊。
大哥们,高手们帮我解答一下吧,谢谢。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>