锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

单片机电子时钟程序(功能:秒表,闹钟,日历,计数器,频率计)

时间:2024-05-26 07:37:09




/********************************************************************************
品名:电子时钟
功能:秒表,闹钟,日历, ,频率计 时钟
作者:阿飞
版本:v1.4
修改:二月调整bug code unsigned char leab_day[12]={31,28,31,30,31,30,31,31,30,31,30,31}; 改成 code unsigned char leab_day[12]={31,29,31,30,31,30,31,31,30,31,30,31}; 时间:2012,8,11 版权所有:http://www.51hei.com
********************************************************************************/
#include
sbit sh=P1^7;
sbit ds=P1^5;
sbit st=P1^6;
sbit led0 = P1^2;
sbit led1 = P1^3;
sbit led2 = P1^4;
sbit led3 = P1^1;
sbit button1 = P3^2;
sbit button2 = P3^3;
sbit button3 = P3^4;
sbit buzzer = P1^0;
sbit RCLK = P3^7;
typedef struct
{
unsigned char count;
unsigned char sec;
unsigned char min;
unsigned char hou;
unsigned char day;
unsigned char mon;
unsigned char year[2];
unsigned char AlarmMin;
unsigned char AlarmHou;
unsigned char stopwatch[3];
unsigned char cymometer[2];
unsigned char cymometer_count;
unsigned char flashcount;
unsigned char mode;
unsigned char delaycount;
unsigned char ditheringcount;
unsigned char February_day;
} ME;
typedef union
{
struct
{
unsigned char bit0:1;
unsigned char bit1:1;
unsigned char bit2:1;
unsigned char bit3:1;
unsigned char bit4:1;
unsigned char bit5:1;
unsigned char bit6:1;
unsigned char bit7:1;
}BIT;
unsigned char byte;
}BETY_BIT;
extern BETY_BIT BIT_FLAG,BIT_FLAG1;
extern TIME time; extern void isr_button1_proces(void);
extern void isr_button2_proces(void);
extern void isr_button3_proces(void);
#define menu_button1_down_1 BIT_FLAG.BIT.bit0
#define menu_button1_down BIT_FLAG.BIT.bit1
#define flash_flag BIT_FLAG.BIT.bit2
#define flash_control BIT_FLAG.BIT.bit3
#define menu_button2_down BIT_FLAG.BIT.bit4
#define button2_length_whack_flagBIT_FLAG.BIT.bit5
#define Alarm_on_offBIT_FLAG.BIT.bit6
#define start_stopwatch BIT_FLAG.BIT.bit7
#define menu_button3_down BIT_FLAG1.BIT.bit0
#define button3_length_whack_flagBIT_FLAG1.BIT.bit1
#define stopwatch_clear_flag BIT_FLAG1.BIT.bit2
//#define Alarm_on_off_display BIT_FLAG1.BIT.bit3
#define cymometer_start_flagBIT_FLAG1.BIT.bit4
#define cymometer1_start_flagBIT_FLAG1.BIT.bit5
#define leap_year_flag BIT_FLAG1.BIT.bit6
#define dithering_flag BIT_FLAG1.BIT.bit7 /*******************************************************
参数配置
*******************************************************/
#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;
=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 ++;
锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章