原理图
代码程序
main.c
#include "common.h"
task_t data tasks[]={
{display,1,0,TS_READY},
{timecount,1000,0,TS_READY},
{timeshow,50,0,TS_READY},
{keyalone,8,0,TS_READY},
};
code u8 tasks_size=sizeof(tasks)/sizeof(task_t);
void init_task(){
display_h=tasks+0;
timecount_h = tasks + 1;
timeshow_h =tasks +2;
keyalone_h =tasks +3;
TMOD=0X01;
TL0 = 0x18;
TH0 = 0xFC;
TF0 = 0;
TR0 = 1;
ET0=1;
EA=1;
}
u32 tick = 0;
void Time0() interrupt 1
{
TL0 = 0x18;
TH0 = 0xFC;
++tick;
}
task_t data * get_nearest_task(){
u8 i;
task_t data * data task;
u32 timeout=u32_max;
for(i=0;i<tasks_size;++i){
if(tasks[i].t<timeout&&TS_READY==tasks[i].s){
task=tasks+i;
timeout=task->t;
}
}
return task;
}
void main()
{
init_task();
for(;;){
static task_t data * data task;
task=get_nearest_task();
if(task->t>tick)
continue;
task->s=TS_RUNNING;
((fun_t)(task->f))();
task->s=TS_READY;
task->t=tick+task->i;
}
}
task.c
#include "common.h"
u8 code dsp_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
u8 dsp_buff[]={0,0,0,0,0,0};
sbit k1= P2^6;
sbit k2=P2^7;
sbit k3=P3^0;
sbit k4=P3^1;
u8 show_stat=0;
u8 time_sec=54;
u8 time_min=59;
u8 time_hou=16;
u8 sec_flag=0;
u8 min_flag=0;
u8 ch_start=0;
u8 run_flag=1;
u8 temp=0;
task_t* display_h;
void display(){
static u8 dsp_com=0;
SEG_LE=1;
P0=0xff;
P0=dsp_code[dsp_buff[dsp_com]];
P2=(1<<dsp_com)|0xc0;
SEG_LE=0;
if(++dsp_com==6) dsp_com=0;
}
task_t* timecount_h;
void timecount(){
if(run_flag==1){
if(++time_sec==60){
time_sec=0;
sec_flag=1;
}
if(sec_flag==1){
if(++time_min==60){
time_min=0;
min_flag=1;
}
sec_flag=0;
}
if(min_flag==1){
if(++time_hou==24){
time_hou=0;
}
min_flag=0;
}
}
}
task_t* timeshow_h;
void timeshow(){
static u16 count=0;
static u8 blink=0;
if(show_stat==0){
dsp_buff[0]=time_hou/10;
dsp_buff[1]=time_hou%10;
dsp_buff[2]=time_min/10;
dsp_buff[3]=time_min%10;
dsp_buff[4]=time_sec/10;
dsp_buff[5]=time_sec%10;
}
else if(show_stat==1){
if(blink){
dsp_buff[0]=time_hou/10;
dsp_buff[1]=time_hou%10;
dsp_buff[2]=time_min/10;
dsp_buff[3]=time_min%10;
dsp_buff[4]=time_sec/10;
dsp_buff[5]=time_sec%10;
}
else{
dsp_buff[0]=10;
dsp_buff[1]=time_hou%10;
dsp_buff[2]=time_min/10;
dsp_buff[3]=time_min%10;
dsp_buff[4]=time_sec/10;
dsp_buff[5]=time_sec%10;
}
}
else if(show_stat==2){
if(blink){
dsp_buff[0]=time_hou/10;
dsp_buff[1]=time_hou%10;
dsp_buff[2]=time_min/10;
dsp_buff[3]=time_min%10;
dsp_buff[4]=time_sec/10;
dsp_buff[5]=time_sec%10;
}
else{
dsp_buff[0]=time_hou/10;
dsp_buff[1]=10;
dsp_buff[2]=time_min/10;
dsp_buff[3]=time_min%10;
dsp_buff[4]=time_sec/10;
dsp_buff[5]=time_sec%10;
}
}
else if(show_stat==3){
if(blink){
dsp_buff[0]=time_hou/10;
dsp_buff[1]=time_hou%10;
dsp_buff[2]=time_min/10;
dsp_buff[3]=time_min%10;
dsp_buff[4]=time_sec/10;
dsp_buff[5]=time_sec%10;
}
else{
dsp_buff[0]=time_hou/10;
dsp_buff[1]=time_hou%10;
dsp_buff[2]=10;
dsp_buff[3]=time_min%10;
dsp_buff[4]=time_sec/10;
dsp_buff[5]=time_sec%10;
}
}
else if(show_stat==4){
if(blink){
dsp_buff[0]=time_hou/10;
dsp_buff[1]=time_hou%10;
dsp_buff[2]=time_min/10;
dsp_buff[3]=time_min%10;
dsp_buff[4]=time_sec/10;
dsp_buff[5]=time_sec%10;
}
else{
dsp_buff[0]=time_hou/10;
dsp_buff[1]=time_hou%10;
dsp_buff[2]=time_min/10;
dsp_buff[3]=10;
dsp_buff[4]=time_sec/10;
dsp_buff[5]=time_sec%10;
}
}
else if(show_stat==5){
if(blink){
dsp_buff[0]=time_hou/10;
dsp_buff[1]=time_hou%10;
dsp_buff[2]=time_min/10;
dsp_buff[3]=time_min%10;
dsp_buff[4]=time_sec/10;
dsp_buff[5]=time_sec%10;
}
else{
dsp_buff[0]=time_hou/10;
dsp_buff[1]=time_hou%10;
dsp_buff[2]=time_min/10;
dsp_buff[3]=time_min%10;
dsp_buff[4]=10;
dsp_buff[5]=time_sec%10;
}
}
else if(show_stat==6){
if(blink){
dsp_buff[0]=time_hou/10;
dsp_buff[1]=time_hou%10;
dsp_buff[2]=time_min/10;
dsp_buff[3]=time_min%10;
dsp_buff[4]=time_sec/10;
dsp_buff[5]=time_sec%10;
}
else{
dsp_buff[0]=time_hou/10;
dsp_buff[1]=time_hou%10;
dsp_buff[2]=time_min/10;
dsp_buff[3]=time_min%10;
dsp_buff[4]=time_sec/10;
dsp_buff[5]=10;
}
}
count+=50;
if(count>600){
blink=1;
}
else
blink=0;
if(count>1200)
count=0;
}
task_t* keyalone_h;
void keyalone(){
static u8 key_stat=0;
u8 key_val=0;
if(k3==0)
key_val = 3;
else if(k1==0)
key_val = 1;
else if(k2==0)
key_val =2;
else if(k4==0)
key_val=4;
else
key_val=0;
switch(key_stat){
case 0:
if(key_val!=0)
key_stat=1;
break;
case 1:
if(key_val==0)
key_stat=0;
else{
key_stat=2;
if(key_val==1){
if(show_stat==1){
if(time_hou/10<2){
temp = (time_hou/10)+1;
time_hou=temp*10+time_hou%10;
}
}
else if(show_stat==2){
if(time_hou%10<9){
time_hou=time_hou + 1;
}
}
else if(show_stat==3){
if(time_min/10<5){
temp = (time_min/10)+1;
time_min=temp*10+time_min%10;
}
}
else if(show_stat==4){
if(time_min%10<9){
time_min=time_min + 1;
}
}
else if(show_stat==5){
if(time_sec/10<5){
temp = (time_sec/10)+1;
time_sec=temp*10+time_sec%10;
}
}
else if(show_stat==6){
if(time_sec%10<9){
time_sec=time_sec + 1;
}
}
}
else if(key_val==2){
if(show_stat==1){
if(time_hou/10>0){
temp = (time_hou/10)-1;
time_hou=temp*10+time_hou%10;
}
}
else if(show_stat==2){
if(time_hou%10>0){
time_hou=time_hou - 1;
}
}
else if(show_stat==3){
if(time_min/10>0){
temp = (time_min/10)-1;
time_min=temp*10+time_min%10;
}
}
else if(show_stat==4){
if(time_min%10>0){
time_min=time_min - 1;
}
}
else if(show_stat==5){
if(time_sec/10>0){
temp = (time_sec/10)-1;
time_sec=temp*10+time_sec%10;
}
}
else if(show_stat==6){
if(time_sec%10>0){
time_sec=time_sec - 1;
}
}
}
else if(key_val==3){
switch(show_stat){
case 0:
show_stat=1;
run_flag=0;
break;
case 1:
show_stat=2;
run_flag=0;
break;
case 2:
show_stat=3;
run_flag=0;
break;
case 3:
show_stat=4;
run_flag=0;
break;
case 4:
show_stat=5;
run_flag=0;
break;
case 5:
show_stat=6;
run_flag=0;
break;
case 6:
show_stat=0;
run_flag=1;
break;
}
}
else if(key_val==4){
show_stat=0;
run_flag=1;
}
}
break;
case 2:
if(key_val==0)
key_stat=0;
break;
}
}
#ifndef __COMMON_H_
#define __COMMON_H_
#include<reg52.h>
#include "intrins.h"
sbit SEG_LE = P1^0;
sbit led2 = P1^1;
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
#define u8_max ((u8)-1)
#define u16_max ((u16)-1)
#define u32_max ((u32)-1)
#define true 1
#define false 0
typedef void fun_t(void);
typedef struct{
u16 f;
u16 i;
u32 t;
u8 s;
}task_t;
enum TS_STATE{TS_DISABLE,TS_READY,TS_RUNNING};
extern u32 tick;
void display();
void timecount();
void timeshow();
void keyalone();
extern task_t* display_h;
extern task_t* timecount_h;
extern task_t* timeshow_h;
extern task_t* keyalone_h;
#endif
|