單片機(jī)電子時(shí)鐘程序
參數(shù)配置
*******************************************************/
#define length_whack_ms 100//按鍵按下大于1000ms為長擊
#define auto_regulator_ms 10//自動調(diào)整參數(shù)100ms一次
#define flash_cymometer_ms 40//調(diào)參數(shù)閃爍頻率400ms一次
#define Alarm_switch_on_off_ms 250//按鍵按下大于2500ms鬧鐘開關(guān)切換一次
#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};
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;
}
}
/***************************************************
產(chǎn)生十毫秒時(shí)基
****************************************************/
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;
}
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;
}
}
/***************************************************
產(chǎn)生十毫秒時(shí)基
****************************************************/
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;
}
/************************************
按鍵一長按處理(可改為函數(shù) 在這調(diào)用)
*************************************/
isr_button1_proces();
/*************************************
按鍵2長擊判斷 (可改為函數(shù) 在這調(diào)用)
*************************************/
isr_button2_proces();
/*************************************
按鍵3長擊判斷(可改為函數(shù) 在這調(diào)用)
*************************************/
isr_button3_proces();
/***********************************
秒表功能 (可改為函數(shù) 在這調(diào)用)
************************************/
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;
按鍵一長按處理(可改為函數(shù) 在這調(diào)用)
*************************************/
isr_button1_proces();
/*************************************
按鍵2長擊判斷 (可改為函數(shù) 在這調(diào)用)
*************************************/
isr_button2_proces();
/*************************************
按鍵3長擊判斷(可改為函數(shù) 在這調(diào)用)
*************************************/
isr_button3_proces();
/***********************************
秒表功能 (可改為函數(shù) 在這調(diào)用)
************************************/
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;
}
//*計(jì)數(shù)器 (可改為函數(shù) 在這調(diào)用)
if(cymometer_start_flag)
{
TR1 = 1;
time.cymometer[1]=TH1;
time.cymometer[0]=TL1;
}
//頻率計(jì) (可改為函數(shù) 在這調(diào)用)
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;
}
}
//*計(jì)數(shù)器 (可改為函數(shù) 在這調(diào)用)
if(cymometer_start_flag)
{
TR1 = 1;
time.cymometer[1]=TH1;
time.cymometer[0]=TL1;
}
//頻率計(jì) (可改為函數(shù) 在這調(diào)用)
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;
}
}
/*******************************
調(diào)參數(shù)閃爍處理 (可改為函數(shù) 在這調(diào)用)
*******************************/
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;
}
else flash_control = 0;
/***********************************
時(shí)間產(chǎn)生 (可改為函數(shù) 在這調(diào)用)
************************************/
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 ++;
}
時(shí)間產(chǎn)生 (可改為函數(shù) 在這調(diào)用)
************************************/
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.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]++;
if(time.mon>12)
{
time.mon = 1;
time.year[0]++;
}
if(time.year[0]>99)
time.year[1]++;
}
/************************************************
按鍵一中斷處理函數(shù)
*************************************************/
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;*/
}
/************************************************
按鍵一中斷處理函數(shù)
*************************************************/
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)
{
/************************************
按鍵一長按處理
*************************************/
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;
}
}
}
}
/************************************************
按鍵二中斷處理函數(shù)
************************************************/
/*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();*/
//}
/************************************************
處理函數(shù)
************************************************/
void button2_proces(void)
{
if((menu_button2_down==0)&&(button2==0))
{
menu_button2_down = 1;
time.delaycount =length_whack_ms;
{
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;
}
}
}
}
/************************************************
按鍵二中斷處理函數(shù)
************************************************/
/*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();*/
//}
/************************************************
處理函數(shù)
************************************************/
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();
}
/************************************
按鍵二長按處理
*************************************/
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;
}
}
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;
}
}
}
/************************************************
按鍵三處理函數(shù)
************************************************/
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;
}
}
}
評論