📄 sch.c
字号:
#include "Vt201.h"
uint8 Error_code_G = 0; /*全局错误标志*/
static uint16 Error_tick_count_G; /*全局错误显示计时*/
static uint8 Last_error_code_G; /*全局错误计录*/
sTask SCH_tasks_G[SCH_MAX_TASKS]; /*任务队列*/
/*------------------------------------------------------------------*-
SCH_Init_Time()
初始化定时器
Scheduler initialisation function. Prepares scheduler data
structures and sets up timer interrupts at required rate.
Must call this function before using the scheduler.
-*------------------------------------------------------------------*/
void SCH_Init_Time(void) {
uint8 i;
for (i = 0; i < SCH_MAX_TASKS; i++) {
SCH_Delete_Task(i);
}
OCR1A = 1250; /*计数周期为10mS,F=1M*/
TIMSK1 = 0x02; /*比较中断A允许*/
SREG = 0x80;
TCCR1A = 0x00;
TCCR1B = 0x08; /*定时器工作在CTC计数器模式*/
TCCR1B |= 0x02; /*设置定时器的分频值为8分频*/
Error_code_G = 0; /*Reset the global error variable*/
}
/*------------------------------------------------------------------*-
SCH_Start()
启动调度器
Starts the scheduler, by enabling interrupts.
NOTE: Usually called after all regular tasks are added,
to keep the tasks synchronised.
NOTE: ONLY THE SCHEDULER INTERRUPT SHOULD BE ENABLED!!!
-*------------------------------------------------------------------*/
void SCH_Start(void) {
/*开全局中断*/
SEI();
//EA = 1;
}
/*------------------------------------------------------------------*-
SCH_Update()
任务刷新
This is the scheduler ISR. It is called at a rate determined by
the timer settings in SCH_Init_T2(). This version is
triggered by Timer 2 interrupts: timer is automatically reloaded.
定时器'T1',A组比较中断
-*------------------------------------------------------------------*/
#pragma interrupt_handler SCH_Update: 12
void SCH_Update(void) {
uint8 index;
for (index = 0; index < SCH_MAX_TASKS; index++) {
if (SCH_tasks_G[index].pTask) { /*Check if there is a task at this location*/
if (SCH_tasks_G[index].Delay != 0) {
SCH_tasks_G[index].Delay --;
}
if (SCH_tasks_G[index].Delay == 0) { /*The task is due to run*/
SCH_tasks_G[index].RunMe += 1; /*Incr. the run flag*/
if (SCH_tasks_G[index].Period) {
SCH_tasks_G[index].Delay = SCH_tasks_G[index].Period;/*Schedule period tasks to run again*/
}
}
}
}
/*
二次确认振铃,铃声原
*/
if (taskBeepInfo.timeS != 0) {
if (TCCR0B == 0) { /*第一次初始化*/
TCCR0A = 0x42;
TCCR0B = 0x02;
OCR0A = 60;
taskBeepInfo.id = 0x00;
}
index = taskBeepInfo.timeS % 10;
if ((index % 2 == 0) && (index != 2)) {
if (taskBeepInfo.id == 0) {
TCCR0A = 0x42;
//OCR0A = 53;
OCR0A = 55;
}
else {
if (taskBeepInfo.id == 50) {
//OCR0A = 67;
OCR0A = 70;
}
}
taskBeepInfo.id ++;
}
else {
TCCR0A = 0x02;
BEEP_CLR;
taskBeepInfo.id = 0;
}
}
else {
if (TCCR0B != 0) { /*关闭信号源*/
TCCR0B = 0;
TCCR0A = 0;
BEEP_CLR;
}
}
}
/*------------------------------------------------------------------*-
SCH_Add_Task()
任务添加
Causes a task (function) to be executed at regular intervals
or after a user-defined delay
Fn_P - The name of the function which is to be scheduled.
NOTE: All scheduled functions must be 'void, void' -
that is, they must take no parameters, and have
a void return type.
DELAY - The interval (TICKS) before the task is first executed
PERIOD - If 'PERIOD' is 0, the function is only called once,
at the time determined by 'DELAY'. If PERIOD is non-zero,
then the function is called repeatedly at an interval
determined by the value of PERIOD (see below for examples
which should help clarify this).
-*------------------------------------------------------------------*/
uint8 SCH_Add_Task(void (* pFunction)(), const uint16 delay, const uint16 period) {
uint8 index = 0;
while ((SCH_tasks_G[index].pTask != 0) && (index < SCH_MAX_TASKS)) {
index++;
}
if (index == SCH_MAX_TASKS) { /*任务队列溢出*/
#if TASK_ADD_DEBUG == 'A' || TASK_ADD_DEBUG == 'B'
QXF_Out_Str("task_over", 'n');
#endif
Error_code_G = 'O'; /*Set the global error variable*/
return SCH_MAX_TASKS; /*Also return an error code*/
}
SCH_tasks_G[index].pTask = pFunction; /*If we're here, there is a space in the task array*/
SCH_tasks_G[index].Delay = delay;
SCH_tasks_G[index].Period = period;
SCH_tasks_G[index].RunMe = 0;
#if TASK_ADD_DEBUG == 'A'
QXF_Out_Byte(index, 'H');
#endif
return index; /*return position of task (to allow later deletion)*/
}
/*------------------------------------------------------------------*-
SCH_Dispatch_Tasks()
任务调度
This is the 'dispatcher' function. When a task (function)
is due to run, SCH_Dispatch_Tasks() will run it.
This function must be called (repeatedly) from the main loop.
-*------------------------------------------------------------------*/
void SCH_Dispatch_Tasks(void) {
uint8 index;
for (index = 0; index < SCH_MAX_TASKS; index++) {
if (SCH_tasks_G[index].RunMe > 0) {
(*SCH_tasks_G[index].pTask)(); /*Run the task*/
SCH_tasks_G[index].RunMe -= 1; /*Reset / reduce RunMe flag*/
if (SCH_tasks_G[index].Period == 0) { /*删除单周期任务*/
SCH_Delete_Task(index);
}
}
}
}
/*------------------------------------------------------------------*-
SCH_Delete_Task()
任务删除
Removes a task from the scheduler. Note that this does
*not* delete the associated function from memory:
it simply means that it is no longer called by the scheduler.
task_index - The task index. Provided by SCH_Add_Task().
RETURN VALUE: RETURN_ERROR or RETURN_NORMAL
-*------------------------------------------------------------------*/
uint8 SCH_Delete_Task(const uint8 task_id) {
uint8 Return_code;
if (SCH_tasks_G[task_id].pTask == 0) {
/*
No task at this location...
Set the global error variable
*/
Error_code_G = 'K';
Return_code = 'N'; /*also return an error code*/
}
else {
Return_code = 'Y';
}
SCH_tasks_G[task_id].pTask = 0x0000;
SCH_tasks_G[task_id].Delay = 0;
SCH_tasks_G[task_id].Period = 0;
SCH_tasks_G[task_id].RunMe = 0;
return Return_code; /*return status*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -