#申请原创# #每日话题# 最近拿到了一块GD32E507R开发板,我在调试CAN通讯的过程中碰到了问题,想和21C的网友们讨论一下哈哈问题:已经初始化CAN并且确认IO配置没有问题,但是CAN的发送和接受都没有反应,所以就有点奇怪了,下面贴代码请教
初始化代码:
- void MX_CAN0_Init(void)
- {
- can_parameter_struct can_parameter;
- can_filter_parameter_struct can_filter;
- CAN_MSPInit();
- // can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
- /* initialize CAN register */
- can_deinit(CAN0);
-
- /* initialize CAN parameters */
- can_parameter.time_triggered = DISABLE;
- can_parameter.auto_bus_off_recovery = DISABLE;
- can_parameter.auto_wake_up = DISABLE;
- can_parameter.auto_retrans = DISABLE;
- can_parameter.rec_fifo_overwrite = DISABLE;
- can_parameter.trans_fifo_order = DISABLE;
- can_parameter.working_mode = CAN_NORMAL_MODE;
- /* configure CAN0 baud rate 500Kbps */
- /* APB1总线最大速率为PCLK/2,90MHZ CAN波特率 = 90M/9*(1+7+2) */
- can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
- can_parameter.time_segment_1 = CAN_BT_BS1_7TQ;
- can_parameter.time_segment_2 = CAN_BT_BS2_2TQ;
- can_parameter.prescaler = 9U;
- /* initialize CAN */
- can_init(CAN0, &can_parameter);
-
- /* initialize filter */
- /* configure filter mode */
- can_filter.filter_mode = CAN_FILTERMODE_MASK;
- can_filter.filter_bits = CAN_FILTERBITS_32BIT;
- /* configure filter ID, only the 0xaabb expand ID can be received */
- can_filter.filter_list_high = 0x0000U;
- can_filter.filter_list_low = 0x0000U;
- /* configure filter mask */
- can_filter.filter_mask_high = 0x0000U;
- can_filter.filter_mask_low = 0x0000U;
- /* select receiver fifo */
- can_filter.filter_fifo_number = CAN_FIFO0;
- can_filter.filter_number = 0U;
- can_filter.filter_enable = ENABLE;
- can_filter_init(CAN0, &can_filter);
-
- /* configure CAN0 NVIC */
- nvic_irq_enable(CAN0_RX0_IRQn, 0U, 0U);
- /* enable can receive FIFO0 not empty interrupt */
- can_interrupt_enable(CAN0, CAN_INTEN_RFNEIE0);
- }
- void CAN_MSPInit(void)
- {
- //0.时钟初始化配置
-
- rcu_periph_clock_enable(RCU_CAN0);
- rcu_periph_clock_enable(RCU_AF);
- rcu_periph_clock_enable(RCU_GPIOB);
-
-
- //1.引脚初始化配置
-
- gpio_init(GPIOB,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_8);
- gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_9);
-
-
- gpio_pin_remap_config(GPIO_CAN0_PARTIAL_REMAP,ENABLE); //开启CAN部分重映射
- }
数据发送部分:
- can_trasnmit_message_struct g_transmit_message;
- can_receive_message_struct g_receive_message;
- FlagStatus can0_receive_flag;
- void MYCAN_Init(void)
- {
- /* initialize transmit message */
- can_struct_para_init(CAN_TX_MESSAGE_STRUCT, &g_transmit_message);
- g_transmit_message.tx_sfid = 0x00U;
- g_transmit_message.tx_efid = 0x00U;
- g_transmit_message.tx_ft = CAN_FT_DATA;
- g_transmit_message.tx_ff = CAN_FF_STANDARD;
- g_transmit_message.tx_dlen = 8U;
- /* initialize receive message */
- can_struct_para_init(CAN_RX_MESSAGE_STRUCT, &g_receive_message);
- }
- void MYCAN_Process(void)
- {
- g_transmit_message.tx_sfid = 0x00FFU;
- g_transmit_message.tx_data[0] = 0xA0U;
- g_transmit_message.tx_data[1] = 0xA1U;
- g_transmit_message.tx_data[2] = 0xA2U;
- g_transmit_message.tx_data[3] = 0xA3U;
- g_transmit_message.tx_data[4] = 0xA4U;
- g_transmit_message.tx_data[5] = 0xA5U;
- g_transmit_message.tx_data[6] = 0xA6U;
- g_transmit_message.tx_data[7] = 0xA7U;
- can_message_transmit(CAN0, &g_transmit_message);
- vTaskDelay(1000);
- }
图为发送过程中的CAN0寄存器,能明显地看到CAN总线有标准ID和邮箱中都是有数据的。
这个是上位软件的配置参数。
开发板使用TJA1050收发器连接CAN接收器,接线为
PB8(CAN0_RX)->TJA1050RX
PB9(CAN0_TX)->TJA1050TX
|
编辑推荐:奖励200家园币!