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;//数据长度小于8
transmit_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)可以使用示波器挂一下单片机的两个输出引脚,是否有波形发出,如果有,说明软件发出了数据,然后挂载另一边,检查芯片是否有问题
(5)打开上位机然后CAN盒一直闪红灯,说明通信有问题
问题1:错误帧一直累加,说明单片机已经发出数据帧,但是帧的内容存在问题,原因可能是上位机和单片机的波特率不匹配
问题2:接收帧和错误帧都会累加,但是接收帧的帧ID,数据长度和内容都存在问题,原因可能是主时钟频率或者CAN频率出现问题
7、USBCAN-II+驱动下载
驱动下载 (zlg.cn)
总结:主时钟频率是程序的基础,针对和时钟有关的代码,必须将时钟频率调准再写,比如选择外部时钟源,具体是8Mhz还是12Mhz,比如选取完外部时钟源时钟分频和倍频是否正确。
8、gd32F303之CAN静默回环模式测试
本文章主要用于 看自己写的CAN协议代码是否正确 然后因为只有一块板子,不能通信的情况
can.h
#ifndef __CAN_H
#define __CAN_H
#include "head.h"
extern FlagStatus can0_receive_flag;
extern FlagStatus can0_error_flag;
extern can_trasnmit_message_struct transmit_message;
extern can_receive_message_struct receive_message;
void CAN0_init(void);
#endif
can.c
#include "can.h"
FlagStatus can0_receive_flag=RESET;
FlagStatus can0_error_flag=RESET;
can_trasnmit_message_struct transmit_message;
can_receive_message_struct receive_message;
void CAN0_init(void)
{
can_parameter_struct can_parameter;
can_filter_parameter_struct can_filter;
//使能CAN0和GPIOA的时钟
rcu_periph_clock_enable(RCU_CAN0);
rcu_periph_clock_enable(RCU_GPIOA);
//配置GPIO口,CAN0_RX:PA11 CAN0_TX:PA12
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);
//NVIC中断配置
nvic_irq_enable(USBD_LP_CAN0_RX0_IRQn ,0,0);
//初始化CAN0参数
can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
can_deinit(CAN0);
can_parameter.time_triggered = DISABLE;//非时间触发通信模式
can_parameter.auto_bus_off_recovery = DISABLE;//软件自动离线管理
can_parameter.auto_wake_up = DISABLE;//睡眠模式通过软件唤醒
can_parameter.auto_retrans = ENABLE;//报文自动重传
can_parameter.rec_fifo_overwrite = DISABLE;//报文不锁定,新的覆盖旧的
can_parameter.trans_fifo_order = DISABLE;
can_parameter.working_mode = CAN_SILENT_LOOPBACK_MODE;//四种通信模式:正常,回环,静默,回环静默
can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;//重新同步跳跃宽度
can_parameter.time_segment_1 = CAN_BT_BS1_5TQ;//时间段 1 占用时间单位
can_parameter.time_segment_2 = CAN_BT_BS2_4TQ;//时间段 2 占用时间单位
can_parameter.prescaler = 6;/* 1MBps */
can_init(CAN0, &can_parameter);
//初始化CAN过滤器,不过滤任何ID
can_struct_para_init(CAN_INIT_STRUCT, &can_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 = 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_FIFO0;
can_filter.filter_enable = ENABLE;
can_filter_init(&can_filter);
//使能接收中断,receive FIFO0 not empty interrupt enable
can_interrupt_enable(CAN0, CAN_INT_RFNE0);
}
/************************************函数功能描述************************************************
Function Name : USBD_LP_CAN0_RX0_IRQHandler(void)
Description : CAN0的接收中断,如果接收到指定的数据则让can0_receive_flag置1,否则can0_error_flag置1
Input : 无
Output : 无
*************************************************************************************************
例如,初始化发送结构体,发送完毕之后就会进入中断
transmit_message.tx_sfid = 0x300>>1;
transmit_message.tx_efid = 0x00;
transmit_message.tx_ft = CAN_FT_DATA;//帧的类型:数据帧或者遥控帧
transmit_message.tx_ff = CAN_FF_STANDARD;//帧的格式:标准帧或者拓展帧
transmit_message.tx_dlen = 2;
transmit_message.tx_data[0] = 0x55;
transmit_message.tx_data[1] = 0xAA;
can_message_transmit(CAN0, &transmit_message);
*************************************************************************************************/
void USBD_LP_CAN0_RX0_IRQHandler(void)
{
/* check the receive message */
can_message_receive(CAN0, CAN_FIFO0, &receive_message);
if((0x300>>1 == receive_message.rx_sfid)&&(CAN_FF_STANDARD == receive_message.rx_ff)&&(2 == receive_message.rx_dlen)){
can0_receive_flag = SET;
}else{
can0_error_flag = SET;
}
}
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/m0_61973119/article/details/140334489
|
|