AnsweredAssumed Answered

BxCAN doesn't work

Question asked by Hsu.Aaron on Feb 10, 2017
Latest reply on Feb 10, 2017 by Clive One

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();
}
}

Outcomes