你好!
我在调试xep100时遇到一个问题,问题是这样的:
我使用CAN2可以进行正确发送,但是无法接收,正确地说是无法进入接收中断;我通过仿真发现,我可以接收的数据,但是RXF并不置位,CAN2CTL0_RXFRM有被置位,can初始化代码如下:
void bsp_can2_init(void)
{
CAN2CTL0_INITRQ = 1; // 进入初始化状态
while (CAN2CTL1_INITAK == 0); //等待进入初始化状态
/*配置时钟
*同步跳转宽度为1,预分频因子为4。
*bit7:6=00,同步跳转宽度为1,bit5:0=000111,预分频因子为Pre value=7+1=8
*/
CAN2BTR0 = 0x07;
/*bit7=0,单次采样
*bit6:4=010,时间段2位Seg2=2+1=3;
*bit3:0=0011,时间段1位Seg1=3+1=4,
*fcanclk = 32000000 这里应该是分频前的can时钟来源频率
*Bit Time = (Pre value / fcanclk) * (1 + Seg1 + Seg2) = (8 / fcanclk) * (1 + 3 + 4) = 0.000002
*baudrate = 1 / bit time = 500k
*/
CAN2BTR1 = 0x1C; //250kb/s
// CAN0BTR0_SJW = 0; //设置同步
// CAN0BTR0_BRP = 7; //设置波特率-8
// CAN0BTR1 = 0x1c; //设置时段1-13和时段2的Tq-2个数 ,总线频率为250kb/s
CAN2IDAC = 0x20;
// 关闭滤波器
CAN2IDMR0 = 0xFF;
CAN2IDMR1 = 0xFF;
CAN2IDMR2 = 0xFF;
CAN2IDMR3 = 0xFF;
CAN2IDMR4 = 0xFF;
CAN2IDMR5 = 0xFF;
CAN2IDMR6 = 0xFF;
CAN2IDMR7 = 0xFF;
CAN2CTL1 = 0xC0; //使能MSCAN模块,设置为一般运行模式、使用总线时钟源
// CAN2CTL1_LOOPB = 1; //环回自测
CAN2CTL0 = 0x00; //返回一般模式运行
while(CAN2CTL1_INITAK); //等待回到一般运行模式
while(CAN2CTL0_SYNCH == 0); //等待总线时钟同步
CAN2RIER_OVRIE = 1;
CAN2RIER_RXFIE = 1; //使能接收中断
}
Hi,
First of all I would like to ask you to use English for communication to be question and solution understandable more people around the world.
The setup is not enough to see and what could be wrong. There is a lot of inputs which could cause the error you describe.
I would like to suggest you to check an examples for CAN peripheral on S12XE device I made a few years ago. https://community.nxp.com/docs/DOC-329209 for example XEP100 - CAN0 - CAN1 - CW45 uses polling method to get frame.
If you see any issue with the can I suggest following procedure to be able to localize possible place of mistake or error.
1 ) Use loopback mode to be sure the can is able to correctly send and receive message without presence of any additional device. This is test of your routines.
2) In order to exclude problems with transceivers, it is also good as a step 2 use connection described in the Figure 2. of https://www.mikrocontroller.net/attachment/28831/siemens_AP2921.pdf with two can peripherals at the same device.
3) Use transceiver.
Best regards,
Ladislav
Hi lame,
I'm a beginner.
I try to the MSCAN function with xep100 family (MC9S12XEQ512), but got some problem.
I download and refer the AN3034SW.zip from the NXP website.
Please help me to check my code and some question.
Q1:At the fosc=4MHz & CAN data rate 250kbps. Is the CAN0BTR0 = 0xC0 & CAN0BTR1 = 0x67 right ??
Q2:could the filter be closed ??
below the photo, it is debug status.
I step by step, but stop on the "canSendFrame". and repeat 0000 to 000B .
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#define ST_ID_100 0x100
#define ACC_CODE_ID100 0x2000
#define ACC_CODE_ID100_HIGH ((ACC_CODE_ID100 & 0xFF00)>>8)
#define ACC_CODE_ID100_LOW (ACC_CODE_ID100 & 0x00FF)
/* Mask Code Definitions */
#define MASK_CODE_ST_ID 0x0007
#define MASK_CODE_ST_ID_HIGH ((MASK_CODE_ST_ID & 0xFF00)>>8)
#define MASK_CODE_ST_ID_LOW (MASK_CODE_ST_ID & 0xFF)
void canInit()
{
//MSCAN Initial mode
CAN0CTL0 = 0x01;
//Check Entry the MSCAN Initial mode
while(!(CAN0CTL1 & 0x01));
//MSCAN Enable & Loop Enable
CAN0CTL1 = 0xA0;
//fOSC=4MHz,CAN speed=250kbps
CAN0BTR0 = 0xC0;
CAN0BTR1 = 0x67;
/* Close Filters */
CAN0IDAC = 0x11;
//CAN0IDAR0 = ACC_CODE_ID100_HIGH; //|\ 16-bit Filter 0
//CAN0IDMR0 = MASK_CODE_ST_ID_HIGH; //| \__ Accepts Standard Data Frame Msg
//CAN0IDAR1 = ACC_CODE_ID100_LOW; //| / with ID 0x100
//CAN0IDMR1 = MASK_CODE_ST_ID_LOW; //|/
/* Acceptance Filters */
//CAN0IDAC = 0x10; /* Set four 16-bit Filters */
//CAN0IDAR2 = 0x00; //|\ 16-bit Filter 1
//CAN0IDMR2 = MASK_CODE_ST_ID_HIGH; //| \__ Accepts Standard Data Frame Msg
//CAN0IDAR3 = 0x00; //| / with ID 0x100
//CAN0IDMR3 = MASK_CODE_ST_ID_LOW; //|/
//CAN0IDAR4 = 0x00; //|\ 16-bit Filter 2
//CAN0IDMR4 = MASK_CODE_ST_ID_HIGH; //| \__ Accepts Standard Data Frame Msg
//CAN0IDAR5 = 0x00; //| / with ID 0x100
//CAN0IDMR5 = MASK_CODE_ST_ID_LOW; //|/
//CAN0IDAR6 = 0x00; //|\ 16-bit Filter 3
//CAN0IDMR6 = MASK_CODE_ST_ID_HIGH; //| \__ Accepts Standard Data Frame Msg
//CAN0IDAR7 = 0x00; //| / with ID 0x100
//CAN0IDMR7 = MASK_CODE_ST_ID_LOW; //|/
/* Exit Initialization Mode Request */
CAN0CTL0 = 0x00;
/* Wait for Normal Mode */
while ((CAN0CTL1&0x00) != 0);
}
unsigned char canSendFrame(unsigned long id, unsigned char priority, unsigned char length, unsigned char *txdata)
{
unsigned char txbuffer;
unsigned char index;
//Check TXE Buffer
if(!CAN0TFLG) return 1;
//Set TXE Buffer
CAN0TBSEL = CAN0TFLG;
txbuffer = CAN0TBSEL;
//Write ID
*((unsigned long *) ((unsigned long)(&CAN0TXIDR0))) = id;
//Write Data
for (index=0;index<length;index++) {
*(&CAN0TXDSR0 + index) = txdata[index];
}
/* Set Data Length Code */
CAN0TXDLR = length;
/* Set Priority */
CAN0TXTBPR = priority;
/* Start transmission */
CAN0TFLG = txbuffer;
/* Wait transmission */
while ( (CAN0TFLG & txbuffer) != txbuffer);
}
#pragma CODE_SEG NON_BANKED
void interrupt CAN0RxISR(void) {
unsigned char length, index;
unsigned char rxdata[8];
length = (CAN0RXDLR & 0x0F);
for (index=0; index<length; index++)
rxdata[index] = *(&CAN0RXDSR0 + index); /* Get received data */
CAN0RFLG = 0x01; /* Clear RXF */
}
#pragma CODE_SEG DEFAULT
void main(void)
{
unsigned char errorflag;
unsigned char txbuff[] = "abcd";
//MSCAN Setting Function
canInit();
//Wait CAN BUS SYNCH
while (!(CAN0CTL0 & 0x10));
//Clear CAN0 RXF flag
CAN0RFLG &= 0x01;
//Enable CAN0 RX interrupt
CAN0RIER = 0x01;
//Enable all interrupt
EnableInterrupts;
for (;;)
{
//CAN0 Send Data
errorflag = canSendFrame((ST_ID_100), 0x00, sizeof(txbuff)-1, txbuff);
}
}