cancel
Showing results for 
Search instead for 
Did you mean: 

BxCAN doesn't work

Aaron hsu
Associate
Posted on February 10, 2017 at 04:10

I am currently working with the development board STM32F103RBT6. It was my intention to test the bxCAN in the microcontroller by using the normal-mode to directly send message to CAN analyzer. Here is the code I wrote until now.

the problem is I haven't receive any message from my CAN analyzer. Please help! Thanks!

//#include 'can.h'

#include 'stm32f10x_can.h'

//void Delay_ms( uint16_t time_ms );

uint16_t Time2;

uint32_t CAN_ID;

uint32_t CAN_DATA0,CAN_DATA1,CAN_DATA2,CAN_DATA3,CAN_DATA4,CAN_DATA5,CAN_DATA6,CAN_DATA7;

uint8_t CanFlag,Display;

//extern unsigned char send_flag;

extern float ADC_Value;

void Delayms(__IO uint32_t nCount);

void delay_ms(u16 nms);

//Voltage(void);

/* Private function prototypes -----------------------------------------------*/

/**

* @file CAN_Configuration

* @brief Configures the different GPIO ports.

* @param æ— 

* @retval æ— 

*/

void GPIO_Configuration(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

//RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 | RCC_APB2Periph_GPIOA, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能PORTA时钟

RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);//使能CAN1时钟

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //IO端�的第11�

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //端�模�为上拉输入方�

GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //IO端�的第12�

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //端å�£æ¨¡å¼�为å¤�ç�?¨æŽ¨æ‹‰è¾“出方å¼�

GPIO_Init(GPIOA, &GPIO_InitStructure);

}

/**

* @file CAN_Configuration

* @brief Configures the CAN

* @param æ— 

* @retval æ— 

*/

void CAN_Configuration(void)

{

/**/

CAN_InitTypeDef CAN_InitStructure;

CAN_FilterInitTypeDef CAN_FilterInitStructure;

/* CAN register init */

CAN_DeInit(CAN1); //��CAN1的所有寄存器

CAN_StructInit(&CAN_InitStructure); //将寄存器全部设置�默认值

/* CAN cell init */

CAN_InitStructure.CAN_TTCM=DISABLE; //�止时间触�通信方�

CAN_InitStructure.CAN_ABOM=DISABLE; //�止CAN总线自动关闭管�

CAN_InitStructure.CAN_AWUM=DISABLE; //ç¦�止自动å�?¤é†’模å¼�

CAN_InitStructure.CAN_NART=DISABLE; //�止�自动�传模�

CAN_InitStructure.CAN_RFLM=DISABLE; //ç¦�止接æ�?¶FIFOé�?�定

CAN_InitStructure.CAN_TXFP=DISABLE; //�止��FIFO优先级

CAN_InitStructure.CAN_Mode=CAN_Mode_Normal; //设置CAN工作方å¼�为正常æ�?¶å�‘模å¼�

CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; //设置�新�步跳转的时间��

CAN_InitStructure.CAN_BS1=CAN_BS1_13tq; //设置字段1的时间��数

CAN_InitStructure.CAN_BS2=CAN_BS2_2tq; //设置字段2的时间��数

CAN_InitStructure.CAN_Prescaler=9; //�置时间��长度为1周期

CAN_Init(CAN1, &CAN_InitStructure); //ç�?¨ä»¥ä¸Šå�‚æ•°åˆ�始化CAN1端å�£

/* CAN filter init */

CAN_FilterInitStructure.CAN_FilterNumber=0; //选择CAN过滤器0

CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; //åˆ�始化为标识/å±�è�?½æ¨¡å¼�

CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; //选择过滤器为32�

CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000; //过滤器标识�高16�

CAN_FilterInitStructure.CAN_FilterIdLow=0x0000; //过滤器标识�低16�

CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000; //æ ¹æ�®æ¨¡å¼�选择过滤器标识å�·æˆ–å±�è�?½å�·çš„高16ä½�

CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000; //æ ¹æ�®æ¨¡å¼�选择过滤器标识å�·æˆ–å±�è�?½å�·çš„低16ä½�

CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0; //将FIFO 0分�给过滤器0

CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //使能过滤器

CAN_FilterInit(&CAN_FilterInitStructure);

}

/**

* @file CanWriteData

* @brief Can Write Date to CAN-BUS

* @param æ— 

* @retval æ— 

*/

void CAN_TX(void)

{

// CanTxMsg TxMessage;

// CAN_DATA0=0xaa; CAN_DATA1=0xbb;

// CAN_DATA2=0xcc; CAN_DATA3=0xdd;

// CAN_DATA4=0xee; CAN_DATA5=0xff;

/* transmit */

// TxMessage.StdId = ID; /* 设置标准id 注�标准id的最高7��能全是�性(1)。共11� ,This parameter can be a value between 0 to 0x7FF. */

//TxMessage.ExtId = 0x00; //设置扩展id 扩展id共18�

// TxMessage.RTR = CAN_RTR_DATA; /* 设置为数�帧 */

// TxMessage.IDE = CAN_ID_STD; /* 使ç�?¨æ ‡å‡†id */

// TxMessage.DLC = 6; /* 数�长度, can报文规定最大的数�长度为8字节 */

// TxMessage.Data[0] = CAN_DATA0;

// TxMessage.Data[1] = CAN_DATA1;

// TxMessage.Data[2] = CAN_DATA2;

// TxMessage.Data[3] = CAN_DATA3;

// TxMessage.Data[4] = CAN_DATA4;

// TxMessage.Data[5] = CAN_DATA5;

// CAN_Transmit(CAN,&TxMessage); /* è¿�?回这个信æ�¯è¯·æ±‚å�‘é€�的邮箱å�·0,1,2或没有邮箱ç�?³è¯·å�‘é€�no_box */

CanTxMsg TxMessage;

// CAN_TDT0R_TIME==10;

/* transmit 1 ECU message */

//TxMessage.StdId=0x00; //�置报文的标准标识符

TxMessage.ExtId=0x0CFF3F27; //�置扩�标识符

TxMessage.IDE=CAN_ID_EXT; //报文使ç�?¨æ ‡å‡†æ ‡è¯†ç¬¦+扩å±�标识符方å¼�

TxMessage.RTR=CAN_RTR_DATA; //报文为数�帧

TxMessage.DLC=8; //报文��数�长度为2字节

TxMessage.Data[0]=0xAA; //���的数�第0字节

TxMessage.Data[1]=0xBB; //���的数�第1字节

TxMessage.Data[2]=0xCC;

TxMessage.Data[3]=0xDD;

TxMessage.Data[4]=0xEE;

TxMessage.Data[5]=0xFF;

TxMessage.Data[6]=0x99;

TxMessage.Data[7]=0x88;

CAN_Transmit(CAN1,&TxMessage); /* è¿�?回这个信æ�¯è¯·æ±‚å�‘é€�的邮箱å�·0,1,2或没有邮箱ç�?³è¯·å�‘é€�no_box */

//Delayms(100);

delay_ms(100);

//TIM2_delay(delay_time);

//delay_ms(10);

//send_flag=0;

}

int main(void)

{

/* System Clocks Configuration **********************************************/

SystemInit();

GPIO_Configuration();

CAN_Configuration();

while(1)

{

CAN_TX();

}

}
4 REPLIES 4
T J
Lead
Posted on February 10, 2017 at 10:42

this code will send data out a bxCAN port, if it is configured properly

 CAN->sTxMailBox[0].TDTR = canTxMsgLength[canTxMsgOUT]; // (2) length CAN->sTxMailBox[0].TDLR = canTxMsgBottomWord[canTxMsgOUT]; // (3) 4bytes CAN->sTxMailBox[0].TDHR = canTxMsgTopWord[canTxMsgOUT]; // (3) 4bytes CAN->sTxMailBox[0].TIR = ((uint32_t)canTxMsgID [canTxMsgOUT] << 21 | CAN_TI0R_TXRQ); // (4) // send it now if the line is idle�?

I am pumping data to a CANDO device without errors.

T J
Lead
Posted on February 10, 2017 at 10:57

to receive bxCan,

I think your filters are wrong,

CAN_FilterInitStructure.CAN_FilterActivation=DISABLE;   this should get it working

Posted on February 10, 2017 at 17:11

Looks to be using 250kbaud

Please use the status returns rather than arbitrary delays, and review examples posted to the forum, and supplied in the SPL. The status/errors might give some insight into what is going wrong.

Is there another active node on the CAN bus?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 11, 2017 at 04:27

Thank you for helping.

Yes, I have connected active node(CAN analyzer) and use 250k baudrate.