完成CANFD通信调试

This commit is contained in:
cbb
2025-10-14 00:49:58 +08:00
parent 2d807260c5
commit a63a344f89
13 changed files with 704 additions and 509 deletions
+31 -30
View File
@@ -1,22 +1,31 @@
#include "Timer.h"
#include "Can.h"
#include "adc.h"
#include "gd32c10x.h"
#include "gd32c10x_gpio.h"
#include "gd32c10x_timer.h"
#include "initial.h"
#include <cstdio>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
constexpr int TIMER1_PRESCALER = 1199;
constexpr int TIMER1_PERIOD = 9;
// 配置定时器每100微秒中断一次
// 系统时钟为120MHz,要得到100微秒的中断周期
// 可以使用预分频器119,定时器时钟为120MHz/(119+1) = 1MHz
// 然后设置周期为99,即计数100次,1MHz/100 = 10kHz = 100微秒
constexpr int TIMER1_PRESCALER = 119; // 120MHz / 120 = 1MHz
constexpr int TIMER1_PERIOD = 99; // 1MHz / 100 = 100us
constexpr int TIMER0_PRESCALER = 5999;
constexpr int TIMER0_PERIOD = 15999;
constexpr int TIMER0_CH0_PULSE = 4000;
constexpr int TIMER0_CH1_PULSE = 6000;
constexpr int TIMER0_CH2_PULSE = 8000;
static u32 total_us100_counter = 0; // 总计数器,每100微秒增加1
extern u16 adc_value[7];
extern CAN_transmiter CAN0_transmiter;
/**
* @brief 设置引脚中断优先级
@@ -40,8 +49,8 @@ void nvic_config(void) {
void timer1_config(void) {
/* ----------------------------------------------------------------------------
TIMER1 Configuration:
TIMER1CLK = SystemCoreClock/12000 = 10KHz, the period is 1s(10000/10000 =
1s).
TIMER1CLK = SystemCoreClock/120 = 1MHz, the period is 100us(100/1000000 =
100us).
----------------------------------------------------------------------------
*/
timer_parameter_struct timer_struct;
@@ -55,11 +64,11 @@ void timer1_config(void) {
timer_struct_para_init(&timer_struct);
/* TIMER1配置参数设置 */
timer_struct.prescaler =
TIMER1_PRESCALER; /* 预分频值:12000分频(从0开始计数) */
TIMER1_PRESCALER; /* 预分频值:120分频(从0开始计数) */
timer_struct.alignedmode = TIMER_COUNTER_EDGE; /* 边沿对齐模式 */
timer_struct.counterdirection = TIMER_COUNTER_UP; /* 向上计数模式 */
timer_struct.period =
TIMER1_PERIOD; /* 自动重装载值:10000次计数(从0开始计数) */
TIMER1_PERIOD; /* 自动重装载值:100次计数(从0开始计数) */
timer_struct.clockdivision = TIMER_CKDIV_DIV1; /* 时钟分频值:1分频 */
timer_init(TIMER1, &timer_struct);
@@ -92,35 +101,27 @@ void TIMER1_IRQHandler(void) {
if (timer_interrupt_flag_get(TIMER1, TIMER_INT_UP) != RESET) {
// 清除更新中断标志
timer_interrupt_flag_clear(TIMER1, TIMER_INT_UP);
// 使用静态变量来跟踪定时器事件
static u8 ms_counter = 0;
static u8 tens_counter = 0;
static u8 hundreds_counter = 0;
static u16 seconds_counter = 0;
// 增加总计数器
total_us100_counter++;
// 增加计数器
ms_counter++;
if (ms_counter >= 10) {
ms_counter = 0;
tens_counter++;
// 每100us执行一次的操作可以放在这里
if (total_us100_counter % 10 == 0) {
// 每1ms执行的代码可以放在这里
if (tens_counter >= 10) {
tens_counter = 0;
hundreds_counter++;
CAN0_transmiter.can0_send_flag = SET;
if (total_us100_counter % 100 == 0) {
// 每10ms执行的代码可以放在这里
}
if (hundreds_counter >= 10) {
hundreds_counter = 0;
seconds_counter++;
// 每100ms执行的代码可以放在这里
if (total_us100_counter % 1000 == 0) {
// 每100ms执行的代码可以放在这里
}
if (seconds_counter >= 10) {
seconds_counter = 0;
// 每1s执行的代码可以放在这里
}
}
if (total_us100_counter % 10000 == 0) {
// 每1s执行的代码可以放在这里
// 重置计数器避免溢出
total_us100_counter = 0;
}
}
}