//相关的头文件已包含
bit sys_TIME;//定时器0 1MS中断标志位
#define n_real_task 1 #define n_task 3 typedef void (*fct_ptsk)(void);
unchar D_taskNOWR; unchar D_taskNOW; unchar C_taskCON[n_task];
extern void TSK_0(void); extern void TSK_1(void); extern void TSK_2(void);
code const fct_ptsk CT_taskADD[]= { TSK_0, TSK_1, TSK_2, };
void main(void) { unchar i,tmp; EA=0; WDT_INIT(); PORT_INIT(); USART_INIT(); ET0=1; TR0=1; ES=1; IP0=0X10; IP0H=0X10; EA=1;
D_taskNOWR=0xff; D_taskNOW=n_real_task-1; while(1) { OS_LOOP: WDT_RST(); if(sys_TIME)//1ms中断 { sys_TIME=0; i=0; while(i<n_task) { if(C_taskCON>0) C_taskCON--; i++; } } OS_T: D_taskNOWR++; if(D_taskNOWR==n_real_task)//Y { D_taskNOWR=0xff; D_taskNOW++; if(D_taskNOW==n_task) { D_taskNOWR=0xff; D_taskNOW=n_real_task-1; } else { tmp=D_taskNOW; goto OS_R; } goto OS_LOOP; } else tmp=D_taskNOWR;
OS_R: if(C_taskCON[tmp]>0) goto OS_T; else (*CT_taskADD[tmp])();
} }
void TSK_0(void) {
C_taskCON[0]=1; C_taskCON[2]=100; } void TSK_1(void) { C_taskCON[1]=2; } void TSK_2(void) { C_taskCON[2]=1; } |