在工程的main函数中如果加入:
CAN1_Init();
接下来是创建LED的task和启动task;
CAN1_Init函数具体如下:
void CAN1_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); //使能CAN1时钟
//引脚复用映射配置
GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_CAN1); //GPIOA11复用为CAN1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_CAN1); //GPIOA12复用为CAN1
//初始化GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11| GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA11,PA12
//CAN单元设置
CAN_InitStructure.CAN_TTCM=DISABLE;
CAN_InitStructure.CAN_ABOM=DISABLE;
CAN_InitStructure.CAN_AWUM=DISABLE;
CAN_InitStructure.CAN_NART=ENABLE;
CAN_InitStructure.CAN_RFLM=DISABLE;
CAN_InitStructure.CAN_TXFP=DISABLE;
CAN_InitStructure.CAN_Mode= CAN_Mode_Nomal;
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_3tq;
CAN_InitStructure.CAN_BS2=CAN_BS2_1tq;
CAN_InitStructure.CAN_Prescaler=9;
CAN_Init(CAN1, &CAN_InitStructure); //初始化CAN1
//配置过滤器
CAN_FilterInitStructure.CAN_FilterNumber = 0;
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh =0x0000
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment= CAN_Filter_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation = DISABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
}
在LED的task中
void led(void)
{
canTxMsg msg1;
msg1.ExtId =0x01234567;
msg1.IDE = CAN_Id_Extended;
msg1.DLC=0x01;
msg1.RTR =CAN_RTR_Data;
msg1.Data[0]=0x21;
while(1)
{
LED0=~LED0;
CAN_Transmit(CAN1,&msg1);
OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_HMSM_STRICT,&err);
}
}
CAN总线上无波形。PA12也无数据输出,LED闪烁,这是为什么呀? 把模式设置为环回模式,数据正常收到,PA12上依然无波形。
TX管脚映射到PH13上,在正常模式下 PH13 无波形,但是在环回模式下 PH13 出现了输出发送的波形
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>