这里采用分别介绍查询发送、中断接收和中断发送、中断接收两种方式。 第一步,初始化各端口与寄存器,这里只介绍CAN_Configuration代码如下: CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; /* CAN register init */ CAN_DeInit(); CAN_StructInit(&CAN_InitStructure); /* CAN cell init */ CAN_InitStructure.CAN_TTCM=DISABLE; CAN_InitStructure.CAN_ABOM=DISABLE; CAN_InitStructure.CAN_AWUM=DISABLE; CAN_InitStructure.CAN_NART=DISABLE; CAN_InitStructure.CAN_RFLM=DISABLE; CAN_InitStructure.CAN_TXFP=DISABLE; CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;// 正常模式 CAN_InitStructure.CAN_SJW=CAN_SJW_2tq; CAN_InitStructure.CAN_BS1=CAN_BS1_9tq; CAN_InitStructure.CAN_BS2=CAN_BS2_7tq; CAN_InitStructure.CAN_Prescaler=8; //36MHz/(1+8+7)*9 =250Kb/s CAN_Init(&CAN_InitStructure); /* CAN filter init */初始化滤波器 CAN_FilterInitStructure.CAN_FilterNumber=0; //选择过滤器0 CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;//标识符屏蔽位模式 CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;//32位过滤器 CAN_FilterInitStructure.CAN_FilterIdHigh =tempid ;//接收板的CAN地址 CAN_FilterInitStructure.CAN_FilterIdLow =0x0004;//选择扩展标识符(见手册CAN_RIxR) CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xffff;//接收板的地址要和tempid一致 CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0004;//下面有介绍 CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0; //选择FIFO0 CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//使能过滤器 CAN_FilterInit(&CAN_FilterInitStructure); //进入初始化函数 /* CAN FIFO0 message pending interrupt enable */ CAN_ITConfig(CAN_IT_FMP0, ENABLE); //FIFO0 消息挂号中断屏蔽 CAN_ITConfig(CAN_IT_TME, ENABLE); //发送邮箱空中断屏蔽 CAN_ITConfig(CAN_IT_BOF, ENABLE); //离线中断允许
|