1、CAN时钟
GD32F303主时钟频率最大是120Mhz,然后APB1时钟最大是60Mhz,APB2时钟最大是120Mhz,CAN挂载在APB1总线上面
所以一般CAN的时钟频率是60Mhz,这个频率和后面配置波特率有关
2、GD32F303时钟配置
首先我们知道芯片有几个时钟
HXTAL:高速外部时钟;
LXTAL:低速外部时钟;
IRC8M:高速内部时钟;
IRC40K:低速内部时钟;
代码配置时钟时我们要确定时选择内部晶振还是外部晶振,他们配置频率都不一样,例如下面的宏定义可以配置不同的时钟频率
比如 如果定义了__SYSTEM_CLOCK_120M_PLL_IRC8M宏定义,那么芯片时钟采用内部晶振8Mhz,然后主时钟频率是120Mhz
如果定义了__SYSTEM_CLOCK_120M_PLL_HXTAL宏定义,那么芯片时钟采用外部晶振时钟,这个晶振一般都是8Mhz,但是针对不同的系列有所不一样,比如GD32F303RET6的外部晶振就是12Mhz,然后使能__SYSTEM_CLOCK_120M_PLL_HXTAL宏定义之后他的主时钟频率不是120Mhz,看代码可知 (12/2)*30不等于120Mhz,所以时钟的频率需要确定好,下面这个代码只适配外部晶振是8Mhz的。
3、CAN的波特率配置
首先我们需要知道波特率的计算公式,其中的PCLK就是CAN挂载总线APB1的时钟,一般为60Mhz,如果我们配置为下面的参数,及代表着CAN的波特率为250kbits。
/* baudrate 250Kbps */can_parameter.resync_jump_width=CAN_BT_SJW_1TQ;can_parameter.time_segment_1 = CAN_BT_BS1_14TQ;can_parameter.time_segment_2 = CAN_BT_BS2_1TQ;can_parameter.prescaler = 15;
4、CAN通信代码
can.c
//can.c文件
#include "can.h"
#include "led.h"void gd32_can_init(void)
{can_parameter_struct can_parameter;can_filter_parameter_struct can_filter;/* initialize CAN register */can_deinit(CAN0);/* enable CAN clock */rcu_periph_clock_enable(RCU_CAN0);rcu_periph_clock_enable(RCU_GPIOA);//使能时钟gpio_init(GPIOA, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ,GPIO_PIN_11);gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_12);//IO复用为CAN功能/* configure CAN0 NVIC */nvic_irq_enable(CAN0_RX1_IRQn,0,0); //中断配置can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);can_struct_para_init(CAN_FILTER_STRUCT, &can_filter);//初始化参数/* baudrate 250Kbps */can_parameter.time_segment_1 = CAN_BT_BS1_14TQ;can_parameter.time_segment_2 = CAN_BT_BS2_1TQ;can_parameter.prescaler = 15;/* initialize CAN */can_init(CAN0, &can_parameter);can_filter.filter_fifo_number = CAN_FIFO1;can_filter.filter_enable = ENABLE;can_filter_init(&can_filter);/* enable can receive FIFO0 not empty interrupt */can_interrupt_enable(CAN0, CAN_INT_RFNE1|CAN_INT_TME);}/*CAN通信数据发送can_frame---要发送的数据发送成功返回0,失败返回1
*/
uint8_t gd32_can_send(can_trasnmit_message_struct can_frame)
{ uint8_t ret=0;ret = can_message_transmit(CAN0,&can_frame);if(ret == CAN_NOMAILBOX){return 1;}return 0;
}/*CAN出错--重启CAN
*/
void gd32_can_error(void)
{if( can_flag_get(CAN0, CAN_FLAG_MTE2) != RESET ||can_flag_get(CAN0, CAN_FLAG_MTE0) != RESET ||can_flag_get(CAN0, CAN_FLAG_MTE1) != RESET || can_flag_get(CAN0, CAN_FLAG_PERR) != RESET ||can_flag_get(CAN0, CAN_FLAG_WERR) != RESET){can_flag_clear(CAN0, CAN_FLAG_MTE0);can_flag_clear(CAN0, CAN_FLAG_MTE1);can_flag_clear(CAN0, CAN_FLAG_MTE2);can_flag_clear(CAN0, CAN_FLAG_PERR);can_flag_clear(CAN0, CAN_FLAG_WERR);can_wakeup(CAN0);gd32_can_init();}
}/*CAN接收中断函数
*/
void CAN0_RX1_IRQHandler(void)
{can_receive_message_struct can_mes;memset(&can_mes,0,sizeof(can_mes)); can_message_receive(CAN0, CAN_FIFO1, &can_mes); LED1_ON();
}
can.h
#ifndef __CAN_H
#define __CAN_H#include "gd32f30x.h"
#include "string.h"void gd32_can_init(void);
uint8_t gd32_can_send(can_trasnmit_message_struct can_frame);
void gd32_can_error(void);#endif
main.c
//CAN初始化gd32_can_init();transmit_message.tx_sfid = 0x7ab;transmit_message.tx_efid = 0x00;transmit_message.tx_ft = CAN_FT_DATA;//帧的类型:数据帧或者遥控帧transmit_message.tx_ff = CAN_FF_STANDARD;//帧的格式:标准帧或者拓展帧transmit_message.tx_dlen = 8;//数据长度小于8transmit_message.tx_data[0] = 0x55;transmit_message.tx_data[1] = 0x55;transmit_message.tx_data[2] = 0x55;transmit_message.tx_data[3] = 0x55;transmit_message.tx_data[4] = 0x55;transmit_message.tx_data[5] = 0x55;transmit_message.tx_data[6] = 0x55;transmit_message.tx_data[7] = 0x55;gd32_can_send(transmit_message);
5、USBCAN-II+的指示灯含义
如果sys 亮绿灯,则代表驱动安装成功,如果亮红灯,则表示驱动安装失败
如果CAN0或者CAN1的绿灯常亮,说明开始通信
6、CAN通信失败原因可能
(1)波特率和上位机没有一一配对
(2) CAN的时钟频率配置问题
(3)CANtest上位机如果一直出现打开设备失败,则选择使用ZCanPro上位机
(4)可以使用示波器挂一下单片机的两个输出引脚,是否有波形发出,如果有,说明软件发出了数据,然后挂载另一边,检查芯片是否有问题
7、USBCAN-II+驱动下载
驱动下载 (zlg.cn)