262 lines
6.0 KiB
Markdown
262 lines
6.0 KiB
Markdown
|
|
# 时间管理系统使用指南
|
|||
|
|
|
|||
|
|
## 概述
|
|||
|
|
|
|||
|
|
本时间管理系统基于1ms定时器中断,实现了多周期任务调度功能。通过标志位机制,可以在主循环中方便地执行不同频率的任务,避免在中断中执行复杂操作。
|
|||
|
|
|
|||
|
|
## 系统特性
|
|||
|
|
|
|||
|
|
### 支持的时间周期
|
|||
|
|
- **2ms**: 高频控制任务(如陀螺仪姿态解算)
|
|||
|
|
- **6ms**: 中高频任务
|
|||
|
|
- **10ms**: 中频控制任务(如传感器数据采集)
|
|||
|
|
- **20ms**: 中低频任务
|
|||
|
|
- **100ms**: 低频控制任务(如状态监控、通信处理)
|
|||
|
|
- **1s**: 超低频任务(如系统状态报告、GPIO翻转)
|
|||
|
|
|
|||
|
|
### 核心函数
|
|||
|
|
|
|||
|
|
#### 1. 初始化函数
|
|||
|
|
```c
|
|||
|
|
void Time_Manager_Init(void);
|
|||
|
|
```
|
|||
|
|
- 清零所有标志位和计数器
|
|||
|
|
- 在系统启动时调用一次
|
|||
|
|
|
|||
|
|
#### 2. 处理函数
|
|||
|
|
```c
|
|||
|
|
void Time_Manager_Process(void);
|
|||
|
|
```
|
|||
|
|
- 在定时器中断中自动调用
|
|||
|
|
- 更新所有计数器并设置相应标志位
|
|||
|
|
|
|||
|
|
#### 3. 标志位管理
|
|||
|
|
```c
|
|||
|
|
Time_Flag_TypeDef* Time_Manager_GetFlags(void);
|
|||
|
|
void Time_Manager_ClearFlags(void);
|
|||
|
|
void Time_Manager_Reset(void);
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 使用方法
|
|||
|
|
|
|||
|
|
### 1. 系统初始化
|
|||
|
|
|
|||
|
|
```c
|
|||
|
|
int main() {
|
|||
|
|
// 系统初始化
|
|||
|
|
MTP_init();
|
|||
|
|
ClockInit();
|
|||
|
|
|
|||
|
|
// 初始化定时器(1ms中断)
|
|||
|
|
TIMER0_Init(1);
|
|||
|
|
|
|||
|
|
// 初始化时间管理器
|
|||
|
|
Time_Manager_Init();
|
|||
|
|
|
|||
|
|
// 其他初始化...
|
|||
|
|
|
|||
|
|
while(1) {
|
|||
|
|
// 主循环处理
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 2. 主循环中的任务调度
|
|||
|
|
|
|||
|
|
```c
|
|||
|
|
while(1) {
|
|||
|
|
// 获取时间标志位
|
|||
|
|
Time_Flag_TypeDef* time_flags = Time_Manager_GetFlags();
|
|||
|
|
|
|||
|
|
// 2ms周期任务 - 高频控制
|
|||
|
|
if (time_flags->T_2ms) {
|
|||
|
|
// 执行2ms周期任务
|
|||
|
|
// 例如:陀螺仪姿态解算
|
|||
|
|
Process_Gyroscope_Data();
|
|||
|
|
time_flags->T_2ms = 0; // 清除标志位
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 10ms周期任务 - 传感器采集
|
|||
|
|
if (time_flags->T_10ms) {
|
|||
|
|
// 执行10ms周期任务
|
|||
|
|
// 例如:传感器数据采集
|
|||
|
|
Collect_Sensor_Data();
|
|||
|
|
time_flags->T_10ms = 0; // 清除标志位
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 100ms周期任务 - 状态监控
|
|||
|
|
if (time_flags->T_100ms) {
|
|||
|
|
// 执行100ms周期任务
|
|||
|
|
// 例如:系统状态监控
|
|||
|
|
Monitor_System_Status();
|
|||
|
|
time_flags->T_100ms = 0; // 清除标志位
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 1s周期任务 - 系统报告
|
|||
|
|
if (time_flags->T_1s) {
|
|||
|
|
// 执行1s周期任务
|
|||
|
|
// 例如:GPIO翻转、状态报告
|
|||
|
|
GPIO_Overturn(GPIO_19);
|
|||
|
|
printf("系统运行正常 - 1s定时任务\n");
|
|||
|
|
time_flags->T_1s = 0; // 清除标志位
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 其他任务处理...
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 3. 自定义任务函数示例
|
|||
|
|
|
|||
|
|
```c
|
|||
|
|
// 陀螺仪数据处理函数
|
|||
|
|
void Process_Gyroscope_Data(void) {
|
|||
|
|
// 读取陀螺仪数据
|
|||
|
|
// 进行姿态解算
|
|||
|
|
// 更新控制参数
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 传感器数据采集函数
|
|||
|
|
void Collect_Sensor_Data(void) {
|
|||
|
|
// 读取温度传感器
|
|||
|
|
// 读取压力传感器
|
|||
|
|
// 数据预处理
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 系统状态监控函数
|
|||
|
|
void Monitor_System_Status(void) {
|
|||
|
|
// 检查系统温度
|
|||
|
|
// 检查电源状态
|
|||
|
|
// 检查通信状态
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 优势特点
|
|||
|
|
|
|||
|
|
### 1. 模块化设计
|
|||
|
|
- 时间管理功能独立封装
|
|||
|
|
- 易于维护和扩展
|
|||
|
|
- 代码结构清晰
|
|||
|
|
|
|||
|
|
### 2. 高效调度
|
|||
|
|
- 基于标志位的轻量级调度
|
|||
|
|
- 避免在中断中执行复杂操作
|
|||
|
|
- 减少中断响应时间
|
|||
|
|
|
|||
|
|
### 3. 灵活配置
|
|||
|
|
- 支持多种时间周期
|
|||
|
|
- 可根据需要启用/禁用特定周期
|
|||
|
|
- 易于添加新的时间周期
|
|||
|
|
|
|||
|
|
### 4. 实时性好
|
|||
|
|
- 基于硬件定时器中断
|
|||
|
|
- 时间精度高
|
|||
|
|
- 响应及时
|
|||
|
|
|
|||
|
|
## 注意事项
|
|||
|
|
|
|||
|
|
### 1. 中断处理
|
|||
|
|
- `Time_Manager_Process()` 在中断中调用,应保持简洁
|
|||
|
|
- 不要在中断中执行复杂操作
|
|||
|
|
- 复杂任务应放在主循环中处理
|
|||
|
|
|
|||
|
|
### 2. 标志位管理
|
|||
|
|
- 及时清除已处理的标志位
|
|||
|
|
- 避免标志位累积导致任务丢失
|
|||
|
|
- 注意标志位的原子性操作
|
|||
|
|
|
|||
|
|
### 3. 任务设计
|
|||
|
|
- 合理分配任务到不同时间周期
|
|||
|
|
- 避免在短周期任务中执行耗时操作
|
|||
|
|
- 考虑任务的优先级和依赖关系
|
|||
|
|
|
|||
|
|
### 4. 系统资源
|
|||
|
|
- 监控CPU使用率
|
|||
|
|
- 避免任务过载
|
|||
|
|
- 合理设置任务执行时间
|
|||
|
|
|
|||
|
|
## 扩展功能
|
|||
|
|
|
|||
|
|
### 1. 添加新的时间周期
|
|||
|
|
```c
|
|||
|
|
// 在Time_Flag_TypeDef中添加新标志位
|
|||
|
|
typedef struct {
|
|||
|
|
uint8_t T_2ms;
|
|||
|
|
uint8_t T_6ms;
|
|||
|
|
uint8_t T_10ms;
|
|||
|
|
uint8_t T_20ms;
|
|||
|
|
uint8_t T_100ms;
|
|||
|
|
uint8_t T_1s;
|
|||
|
|
uint8_t T_500ms; // 新增500ms周期
|
|||
|
|
} Time_Flag_TypeDef;
|
|||
|
|
|
|||
|
|
// 在Time_Counter_TypeDef中添加新计数器
|
|||
|
|
typedef struct {
|
|||
|
|
uint8_t t_2ms;
|
|||
|
|
uint8_t t_6ms;
|
|||
|
|
uint8_t t_10ms;
|
|||
|
|
uint8_t t_20ms;
|
|||
|
|
uint8_t t_100ms;
|
|||
|
|
uint16_t t_1s;
|
|||
|
|
uint16_t t_500ms; // 新增500ms计数器
|
|||
|
|
} Time_Counter_TypeDef;
|
|||
|
|
|
|||
|
|
// 在Time_Manager_Process()中添加处理逻辑
|
|||
|
|
void Time_Manager_Process(void) {
|
|||
|
|
// 现有代码...
|
|||
|
|
|
|||
|
|
// 500ms控制周期
|
|||
|
|
if (g_time_counters.t_500ms >= 500) {
|
|||
|
|
g_time_counters.t_500ms = 0;
|
|||
|
|
g_time_flags.T_500ms = 1;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 2. 任务优先级管理
|
|||
|
|
```c
|
|||
|
|
// 可以添加任务优先级结构
|
|||
|
|
typedef struct {
|
|||
|
|
uint8_t priority;
|
|||
|
|
void (*task_function)(void);
|
|||
|
|
} Task_Item_TypeDef;
|
|||
|
|
|
|||
|
|
// 任务队列管理
|
|||
|
|
Task_Item_TypeDef task_queue[MAX_TASKS];
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 调试技巧
|
|||
|
|
|
|||
|
|
### 1. 添加调试输出
|
|||
|
|
```c
|
|||
|
|
if (time_flags->T_1s) {
|
|||
|
|
printf("1s任务执行时间: %d ms\n", get_system_time());
|
|||
|
|
time_flags->T_1s = 0;
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 2. 监控任务执行
|
|||
|
|
```c
|
|||
|
|
// 添加任务执行计数器
|
|||
|
|
static uint32_t task_2ms_count = 0;
|
|||
|
|
static uint32_t task_10ms_count = 0;
|
|||
|
|
|
|||
|
|
if (time_flags->T_2ms) {
|
|||
|
|
task_2ms_count++;
|
|||
|
|
printf("2ms任务执行次数: %d\n", task_2ms_count);
|
|||
|
|
time_flags->T_2ms = 0;
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 3. 性能分析
|
|||
|
|
```c
|
|||
|
|
// 测量任务执行时间
|
|||
|
|
uint32_t start_time = get_system_time();
|
|||
|
|
Process_Gyroscope_Data();
|
|||
|
|
uint32_t end_time = get_system_time();
|
|||
|
|
printf("陀螺仪处理耗时: %d us\n", end_time - start_time);
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 总结
|
|||
|
|
|
|||
|
|
这个时间管理系统提供了一个简单而强大的多周期任务调度解决方案。通过合理使用,可以实现高效、实时的嵌入式系统控制。关键是要根据实际应用需求,合理分配任务到不同的时间周期,并注意系统的实时性和稳定性。
|