之前一直用f103系列的,刚接触f072,总体是差不多的,can用的是PA11和PA12管脚,loopback没问题,Normal 模式收发都不行,can收发器肯定没问题,在f103平台上验证过的,感觉还是软件上没有配置好,参考了官方的代码,下面是can的配置代码: #define CANx CAN #define CAN_CLK RCC_APB1Periph_CAN #define CAN_RX_PIN GPIO_Pin_11 #define CAN_TX_PIN GPIO_Pin_12 #define CAN_GPIO_PORT GPIOA #define CAN_GPIO_CLK RCC_AHBPeriph_GPIOA #define CAN_AF_PORT GPIO_AF_4 #define CAN_RX_SOURCE GPIO_PinSource11 #define CAN_TX_SOURCE GPIO_PinSource12 static void CAN_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; /* CAN GPIOs configuration **************************************************/ /* Enable GPIO clock */ RCC_AHBPeriphClockCmd(CAN_GPIO_CLK, ENABLE); /* Connect CAN pins to AF7 */ GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_RX_SOURCE, CAN_AF_PORT); GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_TX_SOURCE, CAN_AF_PORT); /* Configure CAN RX and TX pins */ GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN | CAN_TX_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(CAN_GPIO_PORT, &GPIO_InitStructure); /* NVIC configuration *******************************************************/ NVIC_InitStructure.NVIC_IRQChannel = CEC_CAN_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 0x0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* CAN configuration ********************************************************/ /* Enable CAN clock */ RCC_APB1PeriphClockCmd(CAN_CLK, ENABLE); /* CAN register init */ CAN_DeInit(CANx); 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_1tq; /* CAN Baudrate = 500kBps (CAN clocked at 36 MHz) */ CAN_InitStructure.CAN_BS1 = CAN_BS1_9tq; CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq; CAN_InitStructure.CAN_Prescaler = 4; CAN_Init(CANx, &CAN_InitStructure); /* CAN filter init */ CAN_FilterInitStructure.CAN_FilterNumber = 0; CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; 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 = 0; CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; CAN_FilterInit(&CAN_FilterInitStructure); /* Enable FIFO 0 message pending Interrupt */ CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE); } 中断响应函数: void CEC_CAN_IRQHandler(void) { if(CAN_GetITStatus(CAN, CAN_IT_FMP0) != RESET) { CAN_Receive(CAN, CAN_FIFO0, &RxMessage); printf("CAN IRQ"); } 用的是外部8M晶振,总线时钟按HAL库默认的配置,请大神指点一下,万分感激!! |
我说一下我之前遇到类似的问题。我的情况也是loopback是可以的。我也认为自己的收发器没问题。
结果呢?
我的收发器使用的是5V供电,但调试的时候图方便,直接从外部为单片机系统供的3.3v。5V没有单独供电。
这就出了问题。
不知道你手边现在有没有CAN分析仪,最好把你的电路和CAN分析仪连接起来再测试。
(1)总线匹配首尾各1个120欧电阻接入没有?
(2)因为F0系列与F1系列最高工作频率不一样,CAN通讯的波特率计算可能会出错,楼主重新检查一遍。
(3)看代码楼主用的是标准库函数,而并非是HAL库,楼主先要检查一下F0的标准库是否工作正常。比如说配置晶振时钟是否正确,用一个IO口点灯看一下是否能隔3秒反转一次。如果晶振都不起振那就更不要说CAN通讯了。
再就是频率问题 晶振采用系统默认晶振 主频是否一致
您好,请问问题解决了吗?我也遇到了同样的问题,搞了两天了没有进展,谢谢!