ADC已完成
This commit is contained in:
+48
-39
@@ -1,18 +1,22 @@
|
||||
#include "Timer.h"
|
||||
#include "adc.h"
|
||||
#include "gd32c10x.h"
|
||||
#include "gd32c10x_gpio.h"
|
||||
#include "gd32c10x_timer.h"
|
||||
#include "initial.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
constexpr int TIMER1_PRESCALER = 11999;
|
||||
constexpr int TIMER1_PERIOD = 9999;
|
||||
constexpr int TIMER1_PRESCALER = 1199;
|
||||
constexpr int TIMER1_PERIOD = 9;
|
||||
|
||||
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;
|
||||
extern u16 adc_value[7];
|
||||
|
||||
/**
|
||||
* @brief 设置引脚中断优先级
|
||||
@@ -22,7 +26,7 @@ constexpr int TIMER0_CH2_PULSE = 8000;
|
||||
*/
|
||||
void nvic_config(void) {
|
||||
/* 设置NVIC优先级分组,1位抢占优先级,3位响应优先级 */
|
||||
nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
|
||||
nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
|
||||
/* 使能TIMER1中断,设置抢占优先级为1,子优先级为1 */
|
||||
nvic_irq_enable(TIMER1_IRQn, 1, 1);
|
||||
}
|
||||
@@ -40,7 +44,7 @@ void timer1_config(void) {
|
||||
1s).
|
||||
----------------------------------------------------------------------------
|
||||
*/
|
||||
timer_parameter_struct timer_initpara;
|
||||
timer_parameter_struct timer_struct;
|
||||
|
||||
/* 使能TIMER1时钟 */
|
||||
rcu_periph_clock_enable(RCU_TIMER1);
|
||||
@@ -48,16 +52,16 @@ void timer1_config(void) {
|
||||
/* 复位TIMER1外设到默认状态 */
|
||||
timer_deinit(TIMER1);
|
||||
/* 初始化TIMER初始化参数结构体 */
|
||||
timer_struct_para_init(&timer_initpara);
|
||||
timer_struct_para_init(&timer_struct);
|
||||
/* TIMER1配置参数设置 */
|
||||
timer_initpara.prescaler =
|
||||
timer_struct.prescaler =
|
||||
TIMER1_PRESCALER; /* 预分频值:12000分频(从0开始计数) */
|
||||
timer_initpara.alignedmode = TIMER_COUNTER_EDGE; /* 边沿对齐模式 */
|
||||
timer_initpara.counterdirection = TIMER_COUNTER_UP; /* 向上计数模式 */
|
||||
timer_initpara.period =
|
||||
timer_struct.alignedmode = TIMER_COUNTER_EDGE; /* 边沿对齐模式 */
|
||||
timer_struct.counterdirection = TIMER_COUNTER_UP; /* 向上计数模式 */
|
||||
timer_struct.period =
|
||||
TIMER1_PERIOD; /* 自动重装载值:10000次计数(从0开始计数) */
|
||||
timer_initpara.clockdivision = TIMER_CKDIV_DIV1; /* 时钟分频值:1分频 */
|
||||
timer_init(TIMER1, &timer_initpara);
|
||||
timer_struct.clockdivision = TIMER_CKDIV_DIV1; /* 时钟分频值:1分频 */
|
||||
timer_init(TIMER1, &timer_struct);
|
||||
|
||||
/* 清除更新中断标志位 */
|
||||
timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP);
|
||||
@@ -73,10 +77,10 @@ void timer1_config(void) {
|
||||
* @return void
|
||||
*/
|
||||
void User_timer_init(void) {
|
||||
/* 配置TIMER1中断 */
|
||||
nvic_config();
|
||||
/* 配置TIMER1 */
|
||||
timer1_config();
|
||||
/* 配置TIMER1中断 */
|
||||
nvic_config();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,35 +88,41 @@ void User_timer_init(void) {
|
||||
* @return void
|
||||
*/
|
||||
void TIMER1_IRQHandler(void) {
|
||||
u32 cnt = timer_counter_read(TIMER1);
|
||||
// 检查更新中断标志并处理
|
||||
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;
|
||||
|
||||
// 根据计数器值执行不同的定时任务
|
||||
// 每100us执行一次
|
||||
// (此处可以添加需要每100us执行的代码)
|
||||
// 增加计数器
|
||||
ms_counter++;
|
||||
if (ms_counter >= 10) {
|
||||
ms_counter = 0;
|
||||
tens_counter++;
|
||||
// 每1ms执行的代码可以放在这里
|
||||
if (tens_counter >= 10) {
|
||||
tens_counter = 0;
|
||||
hundreds_counter++;
|
||||
|
||||
// 每1ms执行一次 (100us * 10)
|
||||
if (cnt % 10 == 0) {
|
||||
// 添加需要每1ms执行的代码
|
||||
}
|
||||
// 每10ms执行的代码可以放在这里
|
||||
|
||||
// 每10ms执行一次 (100us * 100)
|
||||
if (cnt % 100 == 0) {
|
||||
// 添加需要每10ms执行的代码
|
||||
}
|
||||
if (hundreds_counter >= 10) {
|
||||
hundreds_counter = 0;
|
||||
seconds_counter++;
|
||||
// 每100ms执行的代码可以放在这里
|
||||
|
||||
// 每100ms执行一次 (100us * 1000)
|
||||
if (cnt % 1000 == 0) {
|
||||
// 添加需要每100ms执行的代码
|
||||
}
|
||||
|
||||
// 每1s执行一次 (100us * 10000)
|
||||
if (cnt % 10000 == 0) {
|
||||
// 添加需要每1s执行的代码
|
||||
}
|
||||
|
||||
if (SET == timer_interrupt_flag_get(TIMER1, TIMER_INT_FLAG_UP)) {
|
||||
/* 清除更新中断标志位 */
|
||||
timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP);
|
||||
if (seconds_counter >= 10) {
|
||||
seconds_counter = 0;
|
||||
// 每1s执行的代码可以放在这里
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,7 +143,6 @@ void timer0_gpio_config(void) {
|
||||
gpio_pin_remap_config(GPIO_TIMER0_FULL_REMAP, ENABLE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief TIMER0 配置函数
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user