共2条
1/1 1 跳转至页
现在两个节点的自接收都正常了,互相发送却有问题
问
用示波器可以看见接收节点的RX0有信号,可接收缓冲器状态是空的,好像数据没有通过滤波。
发送节点的程序,另一个太大了,但初始化部分只是ACR不同(另一个为0xAB),并且那一个只是接收:
unsigned char xdata *SJA_BCANAdr;
unsigned char idata ControlDataIOF;
unsigned char idata ControlDataIDC[4];
unsigned char idata ControlDataBuf[8];
unsigned char idata StateDataIOF;
unsigned char idata StateDataIDC[4];
unsigned char idata StateDataBuf[8];
void main(void)
{
unsigned int time_delay;
unsigned int i;
P1_6=1;
MCU_SJACS=0;
SJA_INIT();
StateDataIOF = 0x04;
StateDataIDC[0] = 0xAB;
StateDataIDC[1] = 0x00;
StateDataIDC[2] = 0x00;
StateDataIDC[3] = 0x00;
for(i=0;i<4;i++)
StateDataBuf[i]=i+34;
while(1)
{
BCAN_DATA_WRITE();
for(time_delay=0;time_delay<50000;time_delay++)
;
}
}
/*用于检测CAN控制器的接口是否正常*/
/*返回值:0 表示SJA1000接口正常 1 表示SJA1000与处理器接口不正常*/
bit BCAN_CREATE_COMMUNATION(void)
{
SJA_BCANAdr=REG_TEST; //访问测试寄存器
*SJA_BCANAdr=0xaa;
if(*SJA_BCANAdr==0xaa)
{
*SJA_BCANAdr=0x3d;
if(*SJA_BCANAdr==0x3d)
return 0;
}
else
return 1;
}
/*SJA1000 enter the reset model*/
/*返回值:0 表示SJA1000成功进入复位模式 1 表示SJA1000进入复位模式失败*/
bit BCAN_ENTER_RETMODEL(void)
{
unsigned char TempData;
SJA_BCANAdr=REG_MODE;
TempData= *SJA_BCANAdr;
*SJA_BCANAdr=(TempData|0x01);
if((*SJA_BCANAdr&0x01)==1)
return 0;
else
return 1;
}
/*SJA1000退出复位工作模式*/
/*返回值:0 表示SJA1000成功退出复位模式 1 表示SJA1000退出复位模式失败*/
bit BCAN_QUIT_RETMODEL(void)
{
unsigned char TempData;
SJA_BCANAdr=REG_MODE;
TempData= *SJA_BCANAdr;
*SJA_BCANAdr=(TempData&0xfe);
if((*SJA_BCANAdr&0x01)==0)
return 0;
else
return 1;
}
/*设置CAN控制器的波特率800k*/
/*返回值:0 波特率设置成功 1 波特率设置失败*/
bit BCAN_SET_BANDRATE(void)
{
SJA_BCANAdr=REG_BTR0;
*SJA_BCANAdr=ByteRate_100k_BTR0;
if(*SJA_BCANAdr != ByteRate_100k_BTR0)
return 1;
SJA_BCANAdr=REG_BTR1;
*SJA_BCANAdr=ByteRate_100k_BTR1;
if(*SJA_BCANAdr != ByteRate_100k_BTR1)
return 1;
return 0;
}
/*设置CAN控制器的输出控制寄存器和时钟分频寄存器*/
/*返回值:0 设置成功 1 设置失败*/
void PCAN_SET_CDR(unsigned char Clock_Out)
{
SJA_BCANAdr=REG_CDR; //设置时钟分频寄存器
*SJA_BCANAdr=Clock_Out;
}
bit PCAN_SET_OCR(unsigned char Out_Control)
{
SJA_BCANAdr=REG_OCR; //设置输出控制寄存器
*SJA_BCANAdr=Out_Control;
if(*SJA_BCANAdr!=Out_Control)
return 1;
return 0;
}
/*写数据 到SJA1000的发送缓冲区*/
/*返回值:0 发送成功 1 发送失败*/
void BCAN_DATA_WRITE(void)
{
unsigned char Length; /* length of data frame*/
unsigned char i;
unsigned char TempData;
unsigned char FF; /*FF =0x00 :标准CAN帧*/
/*FF =0x80 :扩展CAN帧*/
SJA_BCANAdr=REG_SR;
TempData=*SJA_BCANAdr&0x04 ;
if(TempData== 0x04) /*发送缓冲区状态= 释放?*/
{
FF = StateDataIOF & 0x80; /*取帧格式*/
Length = StateDataIOF & 0x0F; /*取数据长度代码*/
if(Length > 0x08)
Length = 0x08;
SJA_BCANAdr=REG_TxBuffer0;
*SJA_BCANAdr = StateDataIOF;
SJA_BCANAdr=REG_TxBuffer1;
*SJA_BCANAdr = StateDataIDC[0];
SJA_BCANAdr=REG_TxBuffer2;
*SJA_BCANAdr = StateDataIDC[1];
if(FF)
{
SJA_BCANAdr=REG_TxBuffer3;
*SJA_BCANAdr = StateDataIDC[2];
SJA_BCANAdr=REG_TxBuffer4;
*SJA_BCANAdr = StateDataIDC[3];
}
SJA_BCANAdr++;
for (i=0; i<Length; i++,SJA_BCANAdr++) /* write data bytes */
{
*SJA_BCANAdr = StateDataBuf[i];
}
/*置位发送请求位*/
SJA_BCANAdr=REG_CMD;
*SJA_BCANAdr = 0x03; /*产生一次自接收性质的信息发送*/
}
}
/*从SJA1000的发送缓冲区读数据*/
/*返回值:0 读成功 1 读失败*/
void BCAN_DATA_RECEIVE(void)
{
unsigned char Length;
unsigned char i;
unsigned char FF;
/* Check the CAN status register for received message */
SJA_BCANAdr=REG_RxBuffer0; /* set address pointer to receive buffer */
ControlDataIOF = *SJA_BCANAdr; /* read DSCR_0*/
FF = ControlDataIOF & 0x80;
Length = ControlDataIOF & 0x0F;
if(Length > 0x08)
Length = 0x08;
/* Read data bytes and write to buffer */
SJA_BCANAdr=REG_RxBuffer1;
if(FF)
{
for(i=0; i<4; i++,SJA_BCANAdr++)
ControlDataIDC[i] = *SJA_BCANAdr;
}
else
{
for(i=0; i<2; i++,SJA_BCANAdr++)
ControlDataIDC[i] = *SJA_BCANAdr;
}
for (i=0; i < Length; i++,SJA_BCANAdr++)
ControlDataBuf[i] = *SJA_BCANAdr;
BCAN_CMD_PRG(RRB_CMD);
}
/*执行SJA1000命令*/
/*返回值:0 命令执行成功 1 命令执行失败*/
bit BCAN_CMD_PRG(unsigned char cmd)
{
SJA_BCANAdr=REG_CMD;
*SJA_BCANAdr=cmd;
switch(cmd)
{
case TR_CMD:
return 0;
break;
case AT_CMD:
SJA_BCANAdr=REG_SR;
if((*SJA_BCANAdr&0x20)==0)
{
return 0;
}
else
{
return 1;
}
break;
case RRB_CMD:
SJA_BCANAdr=REG_SR;
if((*SJA_BCANAdr&0x01)==1)
{
return 1;
}
else
{
return 0;
}
break;
case CDO_CMD:
SJA_BCANAdr=REG_SR;
if((*SJA_BCANAdr&0x02)==0)
{
return 0;
}
else
{
return 1;
}
break;
case SRR_CMD:
return 0;
break;
default:
return 1;
break;
}
}
/*initialize the SJA1000*/
void SJA_INIT(void)
{
unsigned char i;
for(i=0;i<125;i++)
MCU_SJARST=0; //reset the SJA1000
for(i=0;i<125;i++)
MCU_SJARST=1;
for(i=0;i<125;i++)
;
if(BCAN_CREATE_COMMUNATION()==0)
while(BCAN_ENTER_RETMODEL()!=0)
;
PCAN_SET_CDR(0xC8);
PCAN_SET_OCR(0xAA);
BCAN_SET_BANDRATE();
//SJA_BCANAdr=REG_RBSA;
// *SJA_BCANAdr=0x00;
SJA_BCANAdr=REG_ACR0;
*SJA_BCANAdr=0xAA;
SJA_BCANAdr=REG_ACR1;
*SJA_BCANAdr=0x00;
SJA_BCANAdr=REG_ACR2;
*SJA_BCANAdr=0x00;
SJA_BCANAdr=REG_ACR3;
*SJA_BCANAdr=0x00;
SJA_BCANAdr=REG_AMR0;
*SJA_BCANAdr=0xFF;
SJA_BCANAdr=REG_AMR1;
*SJA_BCANAdr=0xFF;
SJA_BCANAdr=REG_AMR2;
*SJA_BCANAdr=0xFF;
SJA_BCANAdr=REG_AMR3;
*SJA_BCANAdr=0xFF;
SJA_BCANAdr=REG_MODE;
i=*SJA_BCANAdr;
*SJA_BCANAdr=i|0x08;
while(BCAN_QUIT_RETMODEL()!=0);
SJA_BCANAdr=REG_IR_ABLE;
*SJA_BCANAdr=0x00;
}
答 1: re不要急,联网后也使用自收发方式发送,把验收屏蔽寄存器全部设置成0xff,看看能不能收到 答 2: 老兄,你先前的自收自发问题在哪?我的节点现在也是不能自收发
发送节点的程序,另一个太大了,但初始化部分只是ACR不同(另一个为0xAB),并且那一个只是接收:
unsigned char xdata *SJA_BCANAdr;
unsigned char idata ControlDataIOF;
unsigned char idata ControlDataIDC[4];
unsigned char idata ControlDataBuf[8];
unsigned char idata StateDataIOF;
unsigned char idata StateDataIDC[4];
unsigned char idata StateDataBuf[8];
void main(void)
{
unsigned int time_delay;
unsigned int i;
P1_6=1;
MCU_SJACS=0;
SJA_INIT();
StateDataIOF = 0x04;
StateDataIDC[0] = 0xAB;
StateDataIDC[1] = 0x00;
StateDataIDC[2] = 0x00;
StateDataIDC[3] = 0x00;
for(i=0;i<4;i++)
StateDataBuf[i]=i+34;
while(1)
{
BCAN_DATA_WRITE();
for(time_delay=0;time_delay<50000;time_delay++)
;
}
}
/*用于检测CAN控制器的接口是否正常*/
/*返回值:0 表示SJA1000接口正常 1 表示SJA1000与处理器接口不正常*/
bit BCAN_CREATE_COMMUNATION(void)
{
SJA_BCANAdr=REG_TEST; //访问测试寄存器
*SJA_BCANAdr=0xaa;
if(*SJA_BCANAdr==0xaa)
{
*SJA_BCANAdr=0x3d;
if(*SJA_BCANAdr==0x3d)
return 0;
}
else
return 1;
}
/*SJA1000 enter the reset model*/
/*返回值:0 表示SJA1000成功进入复位模式 1 表示SJA1000进入复位模式失败*/
bit BCAN_ENTER_RETMODEL(void)
{
unsigned char TempData;
SJA_BCANAdr=REG_MODE;
TempData= *SJA_BCANAdr;
*SJA_BCANAdr=(TempData|0x01);
if((*SJA_BCANAdr&0x01)==1)
return 0;
else
return 1;
}
/*SJA1000退出复位工作模式*/
/*返回值:0 表示SJA1000成功退出复位模式 1 表示SJA1000退出复位模式失败*/
bit BCAN_QUIT_RETMODEL(void)
{
unsigned char TempData;
SJA_BCANAdr=REG_MODE;
TempData= *SJA_BCANAdr;
*SJA_BCANAdr=(TempData&0xfe);
if((*SJA_BCANAdr&0x01)==0)
return 0;
else
return 1;
}
/*设置CAN控制器的波特率800k*/
/*返回值:0 波特率设置成功 1 波特率设置失败*/
bit BCAN_SET_BANDRATE(void)
{
SJA_BCANAdr=REG_BTR0;
*SJA_BCANAdr=ByteRate_100k_BTR0;
if(*SJA_BCANAdr != ByteRate_100k_BTR0)
return 1;
SJA_BCANAdr=REG_BTR1;
*SJA_BCANAdr=ByteRate_100k_BTR1;
if(*SJA_BCANAdr != ByteRate_100k_BTR1)
return 1;
return 0;
}
/*设置CAN控制器的输出控制寄存器和时钟分频寄存器*/
/*返回值:0 设置成功 1 设置失败*/
void PCAN_SET_CDR(unsigned char Clock_Out)
{
SJA_BCANAdr=REG_CDR; //设置时钟分频寄存器
*SJA_BCANAdr=Clock_Out;
}
bit PCAN_SET_OCR(unsigned char Out_Control)
{
SJA_BCANAdr=REG_OCR; //设置输出控制寄存器
*SJA_BCANAdr=Out_Control;
if(*SJA_BCANAdr!=Out_Control)
return 1;
return 0;
}
/*写数据 到SJA1000的发送缓冲区*/
/*返回值:0 发送成功 1 发送失败*/
void BCAN_DATA_WRITE(void)
{
unsigned char Length; /* length of data frame*/
unsigned char i;
unsigned char TempData;
unsigned char FF; /*FF =0x00 :标准CAN帧*/
/*FF =0x80 :扩展CAN帧*/
SJA_BCANAdr=REG_SR;
TempData=*SJA_BCANAdr&0x04 ;
if(TempData== 0x04) /*发送缓冲区状态= 释放?*/
{
FF = StateDataIOF & 0x80; /*取帧格式*/
Length = StateDataIOF & 0x0F; /*取数据长度代码*/
if(Length > 0x08)
Length = 0x08;
SJA_BCANAdr=REG_TxBuffer0;
*SJA_BCANAdr = StateDataIOF;
SJA_BCANAdr=REG_TxBuffer1;
*SJA_BCANAdr = StateDataIDC[0];
SJA_BCANAdr=REG_TxBuffer2;
*SJA_BCANAdr = StateDataIDC[1];
if(FF)
{
SJA_BCANAdr=REG_TxBuffer3;
*SJA_BCANAdr = StateDataIDC[2];
SJA_BCANAdr=REG_TxBuffer4;
*SJA_BCANAdr = StateDataIDC[3];
}
SJA_BCANAdr++;
for (i=0; i<Length; i++,SJA_BCANAdr++) /* write data bytes */
{
*SJA_BCANAdr = StateDataBuf[i];
}
/*置位发送请求位*/
SJA_BCANAdr=REG_CMD;
*SJA_BCANAdr = 0x03; /*产生一次自接收性质的信息发送*/
}
}
/*从SJA1000的发送缓冲区读数据*/
/*返回值:0 读成功 1 读失败*/
void BCAN_DATA_RECEIVE(void)
{
unsigned char Length;
unsigned char i;
unsigned char FF;
/* Check the CAN status register for received message */
SJA_BCANAdr=REG_RxBuffer0; /* set address pointer to receive buffer */
ControlDataIOF = *SJA_BCANAdr; /* read DSCR_0*/
FF = ControlDataIOF & 0x80;
Length = ControlDataIOF & 0x0F;
if(Length > 0x08)
Length = 0x08;
/* Read data bytes and write to buffer */
SJA_BCANAdr=REG_RxBuffer1;
if(FF)
{
for(i=0; i<4; i++,SJA_BCANAdr++)
ControlDataIDC[i] = *SJA_BCANAdr;
}
else
{
for(i=0; i<2; i++,SJA_BCANAdr++)
ControlDataIDC[i] = *SJA_BCANAdr;
}
for (i=0; i < Length; i++,SJA_BCANAdr++)
ControlDataBuf[i] = *SJA_BCANAdr;
BCAN_CMD_PRG(RRB_CMD);
}
/*执行SJA1000命令*/
/*返回值:0 命令执行成功 1 命令执行失败*/
bit BCAN_CMD_PRG(unsigned char cmd)
{
SJA_BCANAdr=REG_CMD;
*SJA_BCANAdr=cmd;
switch(cmd)
{
case TR_CMD:
return 0;
break;
case AT_CMD:
SJA_BCANAdr=REG_SR;
if((*SJA_BCANAdr&0x20)==0)
{
return 0;
}
else
{
return 1;
}
break;
case RRB_CMD:
SJA_BCANAdr=REG_SR;
if((*SJA_BCANAdr&0x01)==1)
{
return 1;
}
else
{
return 0;
}
break;
case CDO_CMD:
SJA_BCANAdr=REG_SR;
if((*SJA_BCANAdr&0x02)==0)
{
return 0;
}
else
{
return 1;
}
break;
case SRR_CMD:
return 0;
break;
default:
return 1;
break;
}
}
/*initialize the SJA1000*/
void SJA_INIT(void)
{
unsigned char i;
for(i=0;i<125;i++)
MCU_SJARST=0; //reset the SJA1000
for(i=0;i<125;i++)
MCU_SJARST=1;
for(i=0;i<125;i++)
;
if(BCAN_CREATE_COMMUNATION()==0)
while(BCAN_ENTER_RETMODEL()!=0)
;
PCAN_SET_CDR(0xC8);
PCAN_SET_OCR(0xAA);
BCAN_SET_BANDRATE();
//SJA_BCANAdr=REG_RBSA;
// *SJA_BCANAdr=0x00;
SJA_BCANAdr=REG_ACR0;
*SJA_BCANAdr=0xAA;
SJA_BCANAdr=REG_ACR1;
*SJA_BCANAdr=0x00;
SJA_BCANAdr=REG_ACR2;
*SJA_BCANAdr=0x00;
SJA_BCANAdr=REG_ACR3;
*SJA_BCANAdr=0x00;
SJA_BCANAdr=REG_AMR0;
*SJA_BCANAdr=0xFF;
SJA_BCANAdr=REG_AMR1;
*SJA_BCANAdr=0xFF;
SJA_BCANAdr=REG_AMR2;
*SJA_BCANAdr=0xFF;
SJA_BCANAdr=REG_AMR3;
*SJA_BCANAdr=0xFF;
SJA_BCANAdr=REG_MODE;
i=*SJA_BCANAdr;
*SJA_BCANAdr=i|0x08;
while(BCAN_QUIT_RETMODEL()!=0);
SJA_BCANAdr=REG_IR_ABLE;
*SJA_BCANAdr=0x00;
}
答 1: re不要急,联网后也使用自收发方式发送,把验收屏蔽寄存器全部设置成0xff,看看能不能收到 答 2: 老兄,你先前的自收自发问题在哪?我的节点现在也是不能自收发
共2条
1/1 1 跳转至页
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
与电子爱好者谈读图四被打赏50分 | |
与电子爱好者谈读图二被打赏50分 | |
【FRDM-MCXN947评测】Core1适配运行FreeRtos被打赏50分 | |
【FRDM-MCXN947评测】双核调试被打赏50分 | |
【CPKCORRA8D1B评测】---移植CoreMark被打赏50分 | |
【CPKCORRA8D1B评测】---打开硬件定时器被打赏50分 | |
【FRDM-MCXA156评测】4、CAN loopback模式测试被打赏50分 | |
【CPKcorRA8D1评测】--搭建初始环境被打赏50分 | |
【FRDM-MCXA156评测】3、使用FlexIO模拟UART被打赏50分 | |
【FRDM-MCXA156评测】2、rt-thread MCXA156 BSP制作被打赏50分 |