CAN现场总线的应用最重要的就是其接口端口映射、初始化及数据的发送、接收。 1.接口映射 STM32中的CAN物理引脚可以设置成三种:默认模式、重定义地址1模式、重定义地址2模式。 CAN信号可以被映射到端口A、端口B或端口D上,如下表所示,对于端口D,在36、48和64脚的封装上没有重映射功能。
表1:CAN复用功能重映射 重映射不适用于36脚的封装 当PD0和PD1没有被重映射到OSC_IN和OSC_OUT时,重映射功能只适用于100脚和144脚的封装上 ---------------------------------------------------------------------------------------------------------------------- 默认模式 - /* Configure CAN pin: RX */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIO_CAN_Key, &GPIO_InitStructure);
-
- /* Configure CAN pin: TX */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
重定义地址1模式- /* Configure CAN pin: RX */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIO_CAN_Key, &GPIO_InitStructure);
-
- /* Configure CAN pin: TX */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIO_CAN_Key, &GPIO_InitStructure);
-
- /*Configure CAN Remap 重映射 */
- GPIO_PinRemapConfig(GPIO_Remap1_CAN,ENABLE);
- ----------------------------------------------------------------------------------------------------------------------
- /* Configure CAN pin: RX */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIO_CAN_Key, &GPIO_InitStructure);
-
- /* Configure CAN pin: TX */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIO_CAN_Key, &GPIO_InitStructure);
-
- /*Configure CAN Remap 重映射 */
- GPIO_PinRemapConfig(GPIO_Remap2_CAN,ENABLE);
|