9.3 多任务的裸机实现(下):状态机
长延时任务的分解
将运行时间较长的任务分解为多个子任务
举例:厨师与顾客i
多任务的状态机实现
解决方案
数码管显示:每次只刷新一个数码管
按键扫描:按键按下、按键消抖、按键释放
裸机状态机例程
/************************************************
* Filename: state_machine.c
* Author: litao.wang
* Description:
* Create: 2018-07-11 17:29:08
* Last Modified: 2018-07-31 21:47:30
*************************************************/
#include<stdio.h>
unsigned int count;
void rtc_interrupt(void)
{
for(int i=0;i<500000;i++)
;
count++;
}
void task1(void)
{
static int task1_state=0;
switch(task1_state){ //进入一次只处理一个状态
case 0:
task1_state++;
printf("task1:step 0\n");
break;
case 1:
task1_state++;
printf("task1:step 1\n");
break;
case 2:
task1_state++;
printf("task1:step 2\n");
break;
case 3:
task1_state++;
printf("task1:step 3\n");
break;
default:
printf("task1: undefined step\n");
break;
}
}
void task2(void)
{
static int task2_state=0;
switch(task2_state){
case 0:
task2_state++;
printf("task2:step 0\n");
break;
case 1:
task2_state++;
printf("task2:step 1\n");
break;
case 2:
task2_state++;
printf("task2:step 2\n");
break;
case 3:
task2_state++;
printf("task2:step 3\n");
break;
default:
printf("task2: undefined step\n");
break;
}
}
int main(void)
{
while(1)
{
if(count%1000==0)
task1();
if(count%2000==0)
task2();
rtc_interrupt();
}
return 0;
}