新闻中心

EEPW首页>嵌入式系统>设计应用> 单片机电子时钟程序

单片机电子时钟程序

作者: 时间:2016-11-29 来源:网络 收藏

参数配置
*******************************************************/
#define length_whack_ms 100//按键按下大于1000ms为长击
#define auto_regulator_ms 10//自动调整参数100ms一次
#define flash_cymometer_ms 40//调参数闪烁频率400ms一次
#define Alarm_switch_on_off_ms 250//按键按下大于2500ms闹钟开关切换一次
#define cymometer_1s 100
TIME time;
BETY_BIT BIT_FLAG,BIT_FLAG1;
unsigned char scanf_dat[2];
//code P_leab_led_7[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
//code N_leab_led_7[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
code P_leab_led_7[10] = {0x0a,0xfa,0x4c,0x68,0xb8,0x29,0x09,0x7a,0x08,0x28};
code unsigned char leab_day[12]={31,29,31,30,31,30,31,31,30,31,30,31};
void Time_Count0_Init(void)
{
TMOD =0x51;
TH0=0Xd8;
TL0=0Xf0;
ET0=1;
TR0=1;
IT0=1;
EX0=1;
//IT1=1;
//EX1=1;
//TH1=0X00;
TL1=0X05;
EA=1;
}
void time_add(void)
{
switch(time.mode)
{
case 2: time.min++;if(time.min>59)time.min=0;break;
case 3: time.hou++;if(time.hou>23)time.hou=0;break;
case 4: time.day++;if(time.mon==2&&(time.day>time.February_day))time.day=1;
else if(time.day>leab_day[time.mon-1])time.day=1;break;
case 5: time.mon++;if(time.mon>59)time.mon=0;break;
case 6: time.year[0]++;if(time.year[0]>99)time.year[0]=0;break;
case 7: time.year[1]++;if(time.year[0]>99)time.year[1]=0;break;
case 8: time.AlarmMin++;if(time.AlarmMin>59)time.AlarmMin=0;break;
case 9: time.AlarmHou++;if(time.AlarmHou>59)time.AlarmHou=0;break;
}
}
void time_sub(void)
{
switch(time.mode)
{
case 2:if(time.min>0) time.min--;else time.min=59;break;
case 3: if(time.hou>0)time.hou--;else time.hou=23;break;
case 4: time.day--;if(time.day<1)
{
if(time.mon==2)time.day = time.February_day;
else time.day=leab_day[time.mon-1];
} break;
case 5: time.mon--;if(time.mon<1)time.mon=12;break;
case 6: if(time.year[0]>0)time.year[0]--;else time.year[0]=99;break;
case 7: if(time.year[0]>0)time.year[1]--;else time.year[1]=99;break;
case 8: if(time.AlarmMin>0)time.AlarmMin--;else time.AlarmMin=59;break;
case 9: if(time.AlarmHou>0)time.AlarmHou--;else time.AlarmHou=23;break;
}
}
/***************************************************
产生十毫秒时基
****************************************************/
void timer0(void) interrupt 1 using 1
{
unsigned int temp;
TH0=0Xd8;
TL0=0Xf0;
RCLK=~RCLK;
temp = time.year[1];
temp = temp*100;
temp +=time.year[0];
if(temp%4)
{
time.February_day = 28;
//leap_year_flag=1;
}
else
{
time.February_day = 29;
}
/************************************
按键一长按处理(可改为函数 在这调用)
*************************************/
isr_button1_proces();
/*************************************
按键2长击判断 (可改为函数 在这调用)
*************************************/
isr_button2_proces();
/*************************************
按键3长击判断(可改为函数 在这调用)
*************************************/
isr_button3_proces();
/***********************************
秒表功能 (可改为函数 在这调用)
************************************/
if((time.mode ==1)&&(start_stopwatch==1))
{
stopwatch_clear_flag = 0;
time.stopwatch[0]++;
if(time.stopwatch[0]>=100)
{
time.stopwatch[0] = 0;
time.stopwatch[1]++;
}
if(time.stopwatch[1]>=60)
{
time.stopwatch[1] = 0;
time.stopwatch[2]++;
}
if(time.stopwatch[2]>=60)
time.stopwatch[2]=0;
}
if(stopwatch_clear_flag==1)
{
time.stopwatch[0] = 0;
time.stopwatch[1] = 0;
time.stopwatch[2] = 0;
}
//*计数器 (可改为函数 在这调用)
if(cymometer_start_flag)
{
TR1 = 1;
time.cymometer[1]=TH1;
time.cymometer[0]=TL1;
}
//频率计 (可改为函数 在这调用)
if(cymometer1_start_flag)
{
TR1 = 1;
time.cymometer_count--;
if(time.cymometer_count==0)
{
TR1 = 0;
time.cymometer[1]=TH1;
time.cymometer[0]=TL1;
time.cymometer_count=cymometer_1s;
TH1=TL1=0;
}
}

/*******************************
调参数闪烁处理 (可改为函数 在这调用)
*******************************/
if((time.mode>1)&&(time.mode<10))
{
time.flashcount++;
if(time.flashcount>flash_cymometer_ms)
{
time.flashcount = 0;
flash_control = ~flash_control;
}
}
else flash_control = 0;
/***********************************
时间产生 (可改为函数 在这调用)
************************************/
time.count++;
if( time.count>=100)
{
time.count = 0;
time.sec ++;
}
if(time.sec>=60)
{
time.sec = 0;
time.min ++;
}
if(time.min>=60)
{
time.min = 0;
time.hou ++;
}
if(time.hou>23)
{
time.hou = 0;
time.day ++;
}
if(time.mon==2)
{
if((time.day>time.February_day))
{
time.day = 1;
time.mon ++;
}
}
else
{
if((time.day>leab_day[time.mon-1]))
{
time.day = 1;
time.mon ++;
}
}
if(time.mon>12)
{
time.mon = 1;
time.year[0]++;
}
if(time.year[0]>99)
time.year[1]++;
}
/************************************************
按键一中断处理函数
*************************************************/
void ext_inter0(void) interrupt 0 using 3
{
//if(time.mode==0)
time.ditheringcount = 100;
menu_button1_down = 1;
/* if(time.mode==1){time.mode =0;flash_flag=0;}
if ((time.mode<10)&&(time.mode>1))
{
time.mode++;
time.flashcount = 0;
}
else
{
time.mode = 0;
flash_flag = 0;
}
menu_button2_down = 0;*/
}
/**
/************************************
按键一长按处理
*************************************/
void isr_button1_proces(void)
{
if(menu_button1_down_1 ==1)
{
if(button1==0)
{
time.delaycount--;
if(time.delaycount==0)
{
time.delaycount = 0;
time.mode = 2;
menu_button1_down_1 = 0;
}
}
else
{
time.delaycount = 0;
time.mode = 1;
menu_button1_down_1 = 0;

}
}
/************************************************
消抖部分 (10~20ms)
************************************************/
if(menu_button1_down==1)
{
time.ditheringcount--;
if(time.ditheringcount==98)
{ if(button1==0)
{
if(time.mode==0){menu_button1_down_1=1;time.delaycount = length_whack_ms;}
else if(time.mode==1){time.mode =0;}
else if ((time.mode<10))
{
time.mode++;
time.flashcount = 0;
menu_button1_down_1=0;
}
else
{
time.mode = 0;
flash_flag = 0;
}
menu_button2_down = 0;
menu_button1_down=0;
}
}
}
}
/************************************************
按键二中断处理函数
************************************************/
/*void ext_inter1(void) interrupt 2 using 2
{
menu_button2_down = 1;
time.delaycount =length_whack_ms; */
/*if(time.mode==1) start_stopwatch = ~start_stopwatch ;
if(time.mode==10)cymometer_start_flag =~cymometer_start_flag;
time_add();*/
//}
/************************************************
处理函数
************************************************/
void button2_proces(void)
{
if((menu_button2_down==0)&&(button2==0))
{
menu_button2_down = 1;
time.delaycount =length_whack_ms;
}
}
/************************************
按键二长按处理
*************************************/
void isr_button2_proces(void)
{

if(menu_button2_down==1)
{
if(button2==0)
{
time.delaycount--;
if(time.delaycount==length_whack_ms-2)//消抖部分 (10~20ms)
{
if(time.mode==1) start_stopwatch = ~start_stopwatch ;
if(time.mode==10)
{
cymometer_start_flag =~cymometer_start_flag;
TL1 = 0;
TH1 = 0;
cymometer1_start_flag=0;
}
time_add();
}
if(time.delaycount==0)
{
time.delaycount = auto_regulator_ms;
button2_length_whack_flag = 1;
// menu_button2_down = 0;
}
}
else
{
menu_button2_down = 0;
time.delaycount = auto_regulator_ms;
button2_length_whack_flag = 0;
}

}
if(button2_length_whack_flag)
{
if(time.mode>1)
{
button2_length_whack_flag = 0;
time_add();
TR1 =0;
time.cymometer[0]=0;
TH1=0;
time.cymometer[1]=0;
TL1=0;
cymometer_start_flag =0;
}
}

}
/************************************************
按键三处理函数
************************************************/
void button3_proces(void)
{
if((menu_button3_down==0)&&(button3==0))
{
menu_button3_down = 1;
time.delaycount =length_whack_ms;
}
}
/************************************
按键三长按处理
*************************************/
void isr_button3_proces(void)
{
if(menu_button3_down==1)
{
if(button3==0)
{
time.delaycount--;
if(time.delaycount==length_whack_ms-2) //消抖部分 (10~20ms)
{
if(time.mode==1) stopwatch_clear_flag = 1;
if(time.mode==10){cymometer1_start_flag=1;time.cymometer_count =cymometer_1s;
TL1 = 0;
TH1 = 0;
start_stopwatch = 0;
}
time_sub();
}
if(time.delaycount==0)
{
time.delaycount = auto_regulator_ms;
button3_length_whack_flag = 1;
// menu_button2_down = 0;
}
}
else
{
menu_button3_down = 0;
time.delaycount = auto_regulator_ms;
button3_length_whack_flag = 0;
}

}
if(button3_length_whack_flag)
{
if(time.mode>1)
{
button3_length_whack_flag = 0;
time_sub();
TR1 =0;
time.cymometer[0]=0;
TH1=0;
time.cymometer[1]=0;
TL1=0;
cymometer1_start_flag =0;
}
}

}

关键词:单片机电子时

评论


技术专区

关闭