2017-02-09 07:10 PM
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();} }2017-02-10 01:42 AM
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.
2017-02-10 01:57 AM
to receive bxCan,
I think your filters are wrong,
CAN_FilterInitStructure.CAN_FilterActivation=DISABLE; this should get it working
2017-02-10 08:11 AM
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?
2017-02-10 08:27 PM
Thank you for helping.
Yes, I have connected active node(CAN analyzer) and use 250k baudrate.