GD32F103CBT6can通讯代码
时间: 2023-07-09 12:44:38 浏览: 211
以下是基于 GD32F103CBT6 MCU 的 CAN 通讯代码示例:
```c
#include "gd32f10x.h"
void CAN_Config(void)
{
/* 使能 CAN 和 GPIO 时钟 */
rcu_periph_clock_enable(RCU_CAN);
rcu_periph_clock_enable(RCU_AF);
rcu_periph_clock_enable(RCU_GPIOB);
/* 配置 CAN 接口引脚 */
gpio_pin_remap_config(GPIO_CAN0_REMAP, ENABLE); // 将 CAN0 的引脚映射到 PB8 和 PB9
gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8); // 将 PB8 配置为 AF 推挽输出,速度为 50MHz
gpio_init(GPIOB, GPIO_MODE_AF_INPUT, GPIO_OSPEED_50MHZ, GPIO_PIN_9); // 将 PB9 配置为 AF 输入,速度为 50MHz
/* 配置 CAN 模式、波特率和工作模式 */
can_parameter_struct can_init_struct;
can_struct_para_init(&can_init_struct);
can_init_struct.can_ttc = DISABLE; // 时间触发通信模式
can_init_struct.can_abo = DISABLE; // 自动离线管理
can_init_struct.can_awu = DISABLE; // 自动唤醒模式
can_init_struct.can_rflm = DISABLE; // 接收 FIFO 锁定模式
can_init_struct.can_txfp = DISABLE; // 发送优先级
can_init_struct.can_sjw = CAN_SJW_1TQ;
can_init_struct.can_bs1 = CAN_BS1_11TQ;
can_init_struct.can_bs2 = CAN_BS2_4TQ;
can_init_struct.can_psc = 1;
can_init(CAN0, &can_init_struct);
/* 配置 CAN 过滤器 */
can_filter_parameter_struct can_filter_init_struct;
can_filter_struct_para_init(&can_filter_init_struct);
can_filter_init_struct.can_filter_number = 0;
can_filter_init_struct.can_filter_mode = CAN_FILTERMODE_MASK;
can_filter_init_struct.can_filter_scale = CAN_FILTERSCALE_32BIT;
can_filter_init_struct.can_filter_id_high = 0x0000;
can_filter_init_struct.can_filter_id_low = 0x0000;
can_filter_init_struct.can_filter_mask_id_high = 0x0000;
can_filter_init_struct.can_filter_mask_id_low = 0x0000;
can_filter_init_struct.can_filter_fifo_number = CAN_FIFO0;
can_filter_init_struct.can_filter_enable = ENABLE;
can_filter_init(CAN0, &can_filter_init_struct);
/* 使能 CAN */
can_enable(CAN0);
}
void CAN_SendMessage(uint8_t *data, uint32_t len, uint32_t id)
{
can_trasnmit_message_struct can_tx_message;
can_tx_message.tx_sfid = id; // 标准帧 ID
can_tx_message.tx_efid = 0x00000000; // 扩展帧 ID
can_tx_message.tx_ff = CAN_FF_STANDARD; // 帧格式,标准帧
can_tx_message.tx_ft = CAN_FT_DATA; // 帧类型,数据帧
can_tx_message.tx_dlen = len; // 数据长度
can_tx_message.tx_data[0] = data[0];
can_tx_message.tx_data[1] = data[1];
can_tx_message.tx_data[2] = data[2];
can_tx_message.tx_data[3] = data[3];
can_tx_message.tx_data[4] = data[4];
can_tx_message.tx_data[5] = data[5];
can_tx_message.tx_data[6] = data[6];
can_tx_message.tx_data[7] = data[7];
can_message_transmit(CAN0, &can_tx_message); // 发送 CAN 消息
}
void CAN_ReceiveMessage(void)
{
can_receive_message_struct can_rx_message;
if (can_message_receive(CAN0, CAN_FIFO0, &can_rx_message) == SUCCESS) // 接收 CAN 消息
{
/* 处理接收到的 CAN 消息 */
uint8_t data[8];
data[0] = can_rx_message.rx_data[0];
data[1] = can_rx_message.rx_data[1];
data[2] = can_rx_message.rx_data[2];
data[3] = can_rx_message.rx_data[3];
data[4] = can_rx_message.rx_data[4];
data[5] = can_rx_message.rx_data[5];
data[6] = can_rx_message.rx_data[6];
data[7] = can_rx_message.rx_data[7];
}
}
int main(void)
{
CAN_Config();
uint8_t tx_data[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
uint32_t tx_id = 0x123;
CAN_SendMessage(tx_data, 8, tx_id); // 发送 CAN 消息
while (1)
{
CAN_ReceiveMessage(); // 接收 CAN 消息
}
}
```
上述代码中,`CAN_Config()` 函数用于配置 CAN,`CAN_SendMessage()` 函数用于发送 CAN 消息,`CAN_ReceiveMessage()` 函数用于接收 CAN 消息,`main()` 函数中的示例代码演示了如何发送和接收 CAN 消息。需要注意的是,这里使用的是标准帧 ID,如果需要使用扩展帧 ID,则需要修改上述代码中的相关配置。
阅读全文