Electricity/USER/mian.c

139 lines
3.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
*Copyright ,2023 , NANOCHAP
*File name: MIAN.C
*Author:
*Version: V1.0
*Date: 2023-11-
*Description: 定时器电刺激综合测试TIMER0 + 电刺激)
*Function List:
History:
1.V1.0
Date:
Author:
Modification: 初版
*/
#include "my_header.h"
#include "ENS1_MTP.h"
#include "ENS1_CLOCK.h"
#include "ENS1_UART.h"
#include "ENS1_TIMER.h"
#include "ENS1_GPIO.h"
#include "ENS1_WAVEGEN.h"
int main(){
// 初始化系统
MTP_init();
ClockInit();
// 初始化GPIO19用于定时器指示
GPIO_IO_Init(GPIO_19, OUTPUT, 0x00, 0x02, 0x00, 0x00, ENABLE);
GPIO_Output(GPIO_19, LOW_LEVEL);
// 初始化UART
UART_Init(CMSDK_UART1, &UART1_Init);
UART_ITConfig(CMSDK_UART1, &UART1_ITSet);
// 初始化定时器在wavegen_Init之前
TIMER0_Init(1);
// 初始化时间管理器
Time_Manager_Init();
// 添加调试信息
// printf("APB_Clock_Freq: %d Hz\n", APB_Clock_Freq);
// printf("TIMER0初始化完成\n");
// 初始化波形生成器(电刺激)
wavegen_Init();
// 配置电刺激参数
EMS_Config_TypeDef ems_config = {
.frequency = 100, // 100Hz
.duration = 1000, // 1000ms
.intensity = 128, // 中等强度
.rest_time = 100, // 100ms休息时间
.silent_time = 50 // 50ms静默时间
};
EMS_Configure(&ems_config);
// 启动电刺激
EMS_Start();
// printf("========================================\n");
// printf("定时器电刺激综合测试启动\n");
// printf("========================================\n");
// printf("GPIO19: 定时器指示1秒翻转\n");
// printf("电刺激: 方波输出强度128\n");
// printf("系统时钟: 32MHz HSI\n");
// printf("升压电压: 26V\n");
// printf("UART波特率: 115200\n");
// printf("========================================\n");
// 电刺激控制变量需要在main函数外部定义
static uint8_t ems_control_count = 0; // 电刺激控制计数器
static uint8_t ems_state = 0; // 电刺激状态0=关闭1=开启
while(1)
{
// 获取时间标志位
Time_Flag_TypeDef* time_flags = Time_Manager_GetFlags();
// 处理电刺激(在主循环中运行)
// 注意这里不再直接调用EMS_Process(),而是通过状态控制
// 基于时间标志位执行不同周期的任务
if (time_flags->T_2ms) {
// 2ms周期任务 - 高频控制任务
time_flags->T_2ms = 0; // 清除标志位
}
if (time_flags->T_10ms) {
// 10ms周期任务 - 中频控制任务
time_flags->T_10ms = 0; // 清除标志位
}
if (time_flags->T_100ms) {
// 100ms周期任务 - 低频控制任务
time_flags->T_100ms = 0; // 清除标志位
}
if (time_flags->T_1s) {
// 1s周期任务 - 超低频任务
ems_control_count++; // 每秒递增计数器
// 间断性放电控制逻辑
if (ems_control_count <= 10) {
// 前10秒开启电刺激
if (ems_state == 0) {
ems_state = 1;
EMS_Start(); // 启动电刺激
// printf("电刺激开启 - 第%d秒\n", ems_control_count);
}
// 处理电刺激
EMS_Process();
} else if (ems_control_count <= 20) {
// 后10秒关闭电刺激
if (ems_state == 1) {
ems_state = 0;
EMS_Stop(); // 停止电刺激
// printf("电刺激关闭 - 第%d秒\n", ems_control_count);
}
} else {
// 重置计数器,开始新的周期
ems_control_count = 0;
// printf("电刺激周期重置\n");
}
GPIO_Overturn(GPIO_19); // GPIO19翻转
// printf("1s定时任务执行 - 系统运行正常,电刺激状态: %s\n",
// ems_state ? "开启" : "关闭");
time_flags->T_1s = 0; // 清除标志位
}
// 定时器中断处理在 TIMER0_Handler() 中
}
}