代码如下
void can_config(can_parameter_struct can_parameter, can_filter_parameter_struct can_filter)
{
/* initialize CAN register */
can_deinit(CAN0);
can_deinit(CAN1);
/* 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;
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;
/* 1MBps */
#if CAN_BAUDRATE == 1000
can_parameter.prescaler = 6;
/* 500KBps */
#elif CAN_BAUDRATE == 500
can_parameter.prescaler = 12;
/* 250KBps */
#elif CAN_BAUDRATE == 250
can_parameter.prescaler = 24;
/* 125KBps */
#elif CAN_BAUDRATE == 125
can_parameter.prescaler = 48;
/* 100KBps */
#elif CAN_BAUDRATE == 100
can_parameter.prescaler = 60;
/* 50KBps */
#elif CAN_BAUDRATE == 50
can_parameter.prescaler = 120;
/* 20KBps */
#elif CAN_BAUDRATE == 20
can_parameter.prescaler = 300;
#else
#error "please select list can baudrate in private defines in main.c "
#endif
/* initialize CAN */
can_init(CAN0, &can_parameter);
can_init(CAN1, &can_parameter);
/* initialize filter */
can_filter.filter_number=0;
can_filter.filter_mode = CAN_FILTERMODE_MASK;
can_filter.filter_bits = CAN_FILTERBITS_32BIT;
can_filter.filter_list_high = 0;
can_filter.filter_list_low = 0x0000;
can_filter.filter_mask_high = 0;
can_filter.filter_mask_low = 0x0000;
can_filter.filter_fifo_number = CAN_FIFO0;
can_filter.filter_enable = ENABLE;
can_filter_init(&can_filter);
}
void nvic_config(void)
{
/* configure CAN0 NVIC */
nvic_irq_enable(CAN0_RX0_IRQn,0,0);
/* configure CAN1 NVIC */
nvic_irq_enable(CAN1_RX0_IRQn,1,1);
}
void can_gpio_config(void)
{
/* enable CAN clock */
rcu_periph_clock_enable(RCU_CAN0);
rcu_periph_clock_enable(RCU_CAN1);
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_AF);
/* configure CAN0 GPIO */
gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,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_FULL_REMAP,ENABLE);
/* configure CAN1 GPIO */
// gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_5);
// gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_6);
// gpio_pin_remap_config(GPIO_CAN1_REMAP,ENABLE);
}
void can_init(void)
{
can_gpio_config();
/* configure NVIC */
nvic_config();
printf("\r\nGD32F10x dual CAN test, please press Wakeup key or Tamper key to start communication!\r\n");
/* initialize CAN and filter */
can_config(can_init_parameter, can_filter_parameter);
/* enable can receive FIFO0 not empty interrupt */
can_interrupt_enable(CAN0, CAN_INT_RFNE0);
can_interrupt_enable(CAN1, CAN_INT_RFNE0);
}
不进中断void CAN0_RX0_IRQHandler(void) void CAN1_RX0_IRQHandler(void)为什么呢? 请大神指点,特急 谢谢
|