在配置can的时候,遇到一些问题记录一下
1:注意使用can1的时候,can0的时钟也要打开。
2、在can_filter_parameter_struct can_filter;中
can_filter.filter_list_high = 0x0000;
can_filter.filter_list_low = 0x0000;
can_filter.filter_mask_high = 0x0000;
can_filter.filter_mask_low = 0x0000;
目前依然不了解对应原理,先默认0!!!
3、用GD32F407调试CAN时,发现第二个CAN进入不了中断,可能就和上面两个因素有关系。
4、 can的中断服务函数有
void CAN0_RX0_IRQHandler(void)
void CAN1_RX0_IRQHandler(void)
void CAN0_RX1_IRQHandler(void)
void CAN1_RX1_IRQHandler(void)
用哪个和can_filter.filter_fifo_number 配置有关,CAN_FIFO0就是RX0_IRQHandler。CAN_FIFO1就是RX1_IRQHandler;
/*
引脚配置,时钟使能在别处。就不复制过来了。
*/
void Can_Init(uint8_t canxselect,uint8_t canbaud)
{
can_parameter_struct can_parameter;
can_filter_parameter_struct can_filter;
/* initialize CAN register */
if(CAN0_SELECT == canxselect)
{
can_deinit(CAN0);
can_parameter.time_triggered = DISABLE; //时间触发通信模式
can_parameter.auto_bus_off_recovery = ENABLE; //自动总线关闭恢复
can_parameter.auto_wake_up = DISABLE; //自动唤醒模式
can_parameter.auto_retrans = DISABLE; //自动重传模式
can_parameter.rec_fifo_overwrite = DISABLE; //接收FIFO覆盖模式
can_parameter.trans_fifo_order = DISABLE; //发送FIFO指令
can_parameter.working_mode = CAN_NORMAL_MODE; //正常通信模式
//(Sync_Segment + TSEG1) / (Sync_Segment + TSEG1 + TSEG2) = (1+6)/(1+1+6)
if(canbaud == CAN_BAUD_100K)
{
//100k
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 = 42;
}
else if(canbaud == CAN_BAUD_125K)
{
//125K
can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
can_parameter.time_segment_1 = CAN_BT_BS1_6TQ;
can_parameter.time_segment_2 = CAN_BT_BS2_1TQ;
can_parameter.prescaler = 42;
}
else if(canbaud == CAN_BAUD_250K)
{
//250K
can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
can_parameter.time_segment_1 = CAN_BT_BS1_6TQ;
can_parameter.time_segment_2 = CAN_BT_BS2_1TQ;
can_parameter.prescaler = 21;
}
else if(canbaud == CAN_BAUD_500K)
{
//500k
can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
can_parameter.time_segment_1 = CAN_BT_BS1_9TQ;
can_parameter.time_segment_2 = CAN_BT_BS2_2TQ;
can_parameter.prescaler = 7;
}
else if(canbaud == CAN_BAUD_1M)
{
//1000k
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_1TQ;
can_parameter.prescaler = 6;
}
else
{
//250K
can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
can_parameter.time_segment_1 = CAN_BT_BS1_6TQ;
can_parameter.time_segment_2 = CAN_BT_BS2_1TQ;
can_parameter.prescaler = 21;
}
can_init(CAN0, &can_parameter);
#if (CAN0_ID_FILTER_NUM > 0)
can_filter.filter_fifo_number = CAN_FIFO0; //接收与过滤器相关联的FIFO
can_filter.filter_enable = ENABLE;//
can_filter.filter_bits = CAN_FILTERBITS_32BIT; //filter位数
can_filter.filter_number = 0; //filter数量
can_filter.filter_mode = CAN_FILTERMODE_MASK; //filter模式
can_filter.filter_list_high = 0x0000;
can_filter.filter_list_low = 0x0000;
can_filter.filter_mask_high = 0x0000;
can_filter.filter_mask_low = 0x0000;
#endif
#if (CAN0_ID_FILTER_NUM > 1)
can_filter.filter_number = 1;
can_filter.filter_mode = CAN_FILTERMODE_MASK;
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_init(&can_filter);
#endif
#if (CAN0_ID_FILTER_NUM > 2)
can_filter.filter_number = 2;
can_filter.filter_mode = CAN_FILTERMODE_MASK;
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_init(&can_filter);
#endif
#if (CAN0_ID_FILTER_NUM > 3)
can_filter.filter_number=3;
can_filter.filter_mode = CAN_FILTERMODE_MASK;
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_init(&can_filter);
#endif
/* configure CAN0 NVIC */
/* enable CAN receive FIFO0 not empty interrupt */
//can_interrupt_enable(CAN0, CAN_INT_RFNE0);
}
else if(CAN1_SELECT == canxselect)
{
//GPIO引脚配置
can_deinit(CAN1);
/* initialize CAN */
can_parameter.time_triggered = DISABLE;
can_parameter.auto_bus_off_recovery = ENABLE;
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;
if(canbaud == CAN_BAUD_100K)
{
//100k
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 = 42;
}
else if(canbaud == CAN_BAUD_125K)
{
//125K
can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
can_parameter.time_segment_1 = CAN_BT_BS1_6TQ;
can_parameter.time_segment_2 = CAN_BT_BS2_1TQ;
can_parameter.prescaler = 42;
}
else if(canbaud == CAN_BAUD_250K)
{
//250K
can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
can_parameter.time_segment_1 = CAN_BT_BS1_6TQ;
can_parameter.time_segment_2 = CAN_BT_BS2_1TQ;
can_parameter.prescaler = 21;
}
else if(canbaud == CAN_BAUD_500K)
{
//500k
can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
can_parameter.time_segment_1 = CAN_BT_BS1_9TQ;
can_parameter.time_segment_2 = CAN_BT_BS2_2TQ;
can_parameter.prescaler = 7;
}
else if(canbaud == CAN_BAUD_1M)
{
//1000k
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_1TQ;
can_parameter.prescaler = 6;
}
else
{
//250K
can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
can_parameter.time_segment_1 = CAN_BT_BS1_6TQ;
can_parameter.time_segment_2 = CAN_BT_BS2_1TQ;
can_parameter.prescaler = 21;
}
can_init(CAN1, &can_parameter);
#if (CAN1_ID_FILTER_NUM > 0)
can_filter.filter_fifo_number = CAN_FIFO0;
can_filter.filter_enable = ENABLE;
can_filter.filter_bits = CAN_FILTERBITS_32BIT;
can_filter.filter_number = 15;
can_filter.filter_mode = CAN_FILTERMODE_MASK;
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_init(&can_filter);
#endif
#if (CAN1_ID_FILTER_NUM > 1)
can_filter.filter_number = 16;
can_filter.filter_mode = CAN_FILTERMODE_MASK;
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_init(&can_filter1);
#endif
#if (CAN1_ID_FILTER_NUM > 2)
can_filter.filter_number = 17;
can_filter.filter_mode = CAN_FILTERMODE_MASK;
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_init(&can_filter);
#endif
#if (CAN1_ID_FILTER_NUM > 3)
can_filter.filter_number = 18;
can_filter.filter_mode = CAN_FILTERMODE_MASK;
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_init(&can_filter);
#endif
/* configure CAN0 NVIC */
/* enable CAN receive FIFO0 not empty interrupt */
can_interrupt_enable(CAN1, CAN_INTEN_RFNEIE0);
}
}
//CAN0发送函数
uint8_t can01_send_data(uint32_t id,uint8_t data[8],uint8_t len)
{
uint8_t ret=0;
uint8_t i=0;
can_trasnmit_message_struct message_can;
if(id>0x7FF)
{
message_can.tx_ff=CAN_FF_EXTENDED;
message_can.tx_efid=id;
}
else
{
message_can.tx_ff=CAN_FF_STANDARD;
message_can.tx_sfid=id;
}
message_can.tx_ft = CAN_FT_DATA;
message_can.tx_dlen = len;
for(i=0;i<8;i++) { message_can.tx_data=data; };
ret=can_message_transmit(CAN0, &message_can);
return ret;
}
//CAN1发送函数
uint8_t can02_send_data(uint32_t id,uint8_t data[8],uint8_t len)
{
uint8_t ret=0;
uint8_t i=0;
can_trasnmit_message_struct message_can;
if(id>0x7FF)
{
message_can.tx_ff=CAN_FF_EXTENDED;
message_can.tx_efid=id;
}
else
{
message_can.tx_ff=CAN_FF_STANDARD;
message_can.tx_sfid=id;
}
message_can.tx_ft = CAN_FT_DATA;
message_can.tx_dlen = len;
for(i=0;i<8;i++) { message_can.tx_data=data; };
ret=can_message_transmit(CAN1, &message_can);
return ret;
}
uint16_t Can_Data[8]={0};
//测试
void can_test(void)
{
uint8_t can_send[8]={0};
static uint8_t num=0;
can_send[0]=Can_Data[0]++;
can_send[1]=Can_Data[1];
can_send[2]=Can_Data[2];
can_send[3]=0;
if(num>255)
{
num=0;
}
can01_send_data(0x45,(uint8_t*)can_send,4);
can02_send_data(0x1b32fd,(uint8_t*)can_send,4);
}
//CAN0中断处理
void CAN0_RX0_IRQHandler(void)
{
can_message_receive(CAN0, CAN_FIFO0, &RxMess_Can0);
Can_Data[1]++;
can_interrupt_flag_clear(CAN0,CAN_INT_FLAG_RFF0);
}
//CAN1中断处理
void CAN1_RX0_IRQHandler(void)
{
can_message_receive(CAN1, CAN_FIFO0, &RxMess_Can1);
Can_Data[2]++;
can_interrupt_flag_clear(CAN1,CAN_INT_FLAG_RFF0);
}
上面就是满足can0,can1,主动对外发送,也满足进接收中断函数的代码。下面图片没啥用
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/GQli2296523792/article/details/143691321
|