Electricity/USER/mian.c

127 lines
3.0 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, GPIO_OType_PP, GPIO_NOPULL, OUTPUT_FAST, PDRV_4mA, 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();
// 初始化波形生成器(电刺激)
wavegen_Init();
// 配置电刺激参数
EMS_Config_TypeDef ems_config = {
.frequency = 100, // 100Hz
.duration = 1000, // 1000ms
.intensity = 128, // 中等强度
.rest_time = 100, // 100ms休息时间
.silent_time = 50, // 50ms静默时间
// 缓进缓出控制参数
.ramp_up_time = 2, // 缓进时间2秒
.hold_time = 6, // 保持时间6秒
.ramp_down_time = 2, // 缓出时间2秒
.enable_ramp = 1 // 启用渐进控制
};
EMS_Configure(&ems_config);
// 启动电刺激
EMS_Start();
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(); // 启动电刺激
}
// 处理电刺激
EMS_Process();
} else if (ems_control_count <= 20) {
// 后10秒关闭电刺激
if (ems_state == 1) {
ems_state = 0;
EMS_Stop(); // 停止电刺激
}
} else {
// 重置计数器,开始新的周期
ems_control_count = 0;
}
GPIO_Overturn(GPIO_19); // GPIO19翻转
time_flags->T_1s = 0; // 清除标志位
}
// 定时器中断处理在 TIMER0_Handler() 中
}
}