本帖最后由 crystal1987 于 2022-6-19 16:25 编辑
最近调试GD32F107的单片机CAN通信,由于系统中需要使用双CAN,目前使用CAN0和CAN1都遇到问题。其中CAN0数据收发都没有数据(用示波器测量总线上没有数据),CAN1能够将数据发送出来,接收数据进不了中断。之前用过GD32F103的单片机,CAN收发都是正常的,所以直接移植过来使用,但是在GS32F107上遇到了问题。先将CAN0的代码发出来,大家帮忙分析一下。
void CAN0_Init_Func(void)
{
/*************************************************
*CAN0配置:PA11, PA12
*************************************************/
gpio_init(GPIOA,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_11); //RX
gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_12); //TX
// gpio_pin_remap_config(GPIO_CAN0_FULL_REMAP,DISABLE);
nvic_irq_enable(CAN0_RX1_IRQn,0,0);
can_networking_init();
can_interrupt_enable(CAN0, CAN_INT_RFNE1);
can_struct_para_init(CAN_RX_MESSAGE_STRUCT, &can0_receive_message);
can0_transmit_message.tx_ft = CAN_FT_DATA;
can0_transmit_message.tx_ff = CAN_FF_STANDARD;
can_struct_para_init(CAN_TX_MESSAGE_STRUCT, &can0_transmit_message);
}
void can_networking_init(void)
{
can_parameter_struct can_parameter;
can_filter_parameter_struct can_filter;
can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
can_struct_para_init(CAN_FILTER_STRUCT, &can_filter);
can_deinit(CAN0);
can_parameter.time_triggered = DISABLE;//时间触发模式
can_parameter.auto_bus_off_recovery = DISABLE;
can_parameter.auto_wake_up = DISABLE;
can_parameter.no_auto_retrans = DISABLE;
can_parameter.rec_fifo_overwrite = DISABLE;
can_parameter.trans_fifo_order = DISABLE;
can_parameter.working_mode = CAN_NORMAL_MODE;
can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
can_parameter.time_segment_1 = CAN_BT_BS1_5TQ;
can_parameter.time_segment_2 = CAN_BT_BS2_3TQ;
/* baudrate 1Mbps can_parameter.prescaler = 6 */
// 54M / [(SEG_1+1+SEG_2)*(PRESC +1)]
can_parameter.prescaler = 600; //10K
can_init(CAN0, &can_parameter);
can_filter.filter_number = 0;
can_filter.filter_mode = CAN_FILTERMODE_MASK;
can_filter.filter_bits = CAN_FILTERBITS_32BIT;
can_filter.filter_list_high = 0x0000;
can_filter.filter_list_low = 0x0000;
can_filter.filter_mask_high = 0x0000;
can_filter.filter_mask_low = 0x0000;
can_filter.filter_fifo_number = CAN_FIFO1;
can_filter.filter_enable = ENABLE;
can_filter_init(&can_filter);
}
void CAN0_TEST(void)
{
can0_transmit_message.tx_data[0] = 0x01;
can0_transmit_message.tx_data[1] = 0x02;
can0_transmit_message.tx_data[2] = 0x03;
can0_transmit_message.tx_data[3] = 0x04;
can0_transmit_message.tx_data[4] = 0x05;
can0_transmit_message.tx_data[5] = 0x06;
can0_transmit_message.tx_data[6] = 0x07;
can0_transmit_message.tx_data[7] = 0x08;
can0_transmit_message.tx_dlen = 8;
can_message_transmit(CAN0, &can0_transmit_message);
// CAN_Send(0x18001000,CAN_RTR_DATA,8,&can0_transmit_message.tx_data[0]);
}
//50ms一次
void TIMER1_IRQHandler(void)
{
if(SET == timer_interrupt_flag_get(TIMER1, TIMER_INT_FLAG_UP))
{
timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP);
lec_count++;
if(lec_count == 10)
{
LED_ON;
CAN0_TEST();
}
else if(lec_count >= 20)
{
LED_OFF;
CAN1_TEST();
lec_count = 0;
}
}
}
|