电子时钟程序代码
电子时钟源代码
x2=98*sin(s/30*PI)+451; /*秒针的 x 坐标值*/ y2=200-98*cos(s/30*PI); /*秒针的 y 坐标值*/ line(451,200,x2,y2);
/*开始循环,直至用户按下 ESC 键结束循环*/ setcolor(12);/*淡红色*/ sound(500);/*以指定频率打开 PC 扬声器,这里频率为 500Hz*/ delay(700);/*发一个频率为 500Hz 的音调,维持 700 毫秒*/ sound(200);/*以指定频率打开 PC 扬声器,这里频率为 200Hz*/ delay(300);
/*绘制新的分针*/ m=t[0].ti_min; digitclock(446,318,digitmin(m)); /*在数字时钟中显示新的分钟值*/ x1=80*sin(m/30*PI)+451; y1=200-80*cos(m/30*PI); line(451,200,x1,y1); }
/*小时处理*/ if((t[0].ti_hour*60+t[0].ti_min)!=(h*60+m)) {
count=1; } }
setcolor(15); digitclock(485,318,digitsec(s)+1);/*数字时钟增加1秒*/
setcolor(12); /*淡红色*/ x2=98*sin(s/30*PI)+451; y2=200-98*cos(s/30*PI); line(451,200,x2,y2);
51单片机电子时钟C语言程序
EX0=1;
ET0=1;
TR0=1;
yueqh(),riqh();//初始化日月切换
}
void ritiao()//日期的调节函数
{ if(P36==0)//日期加一调节键
{
p36xd();
if((numyue==1)||(numyue==3)||(numyue==5)||(numyue==7)||(numyue==8)||(numyue==10)||(numyue==12))
{numyue=1;}
yueqh();
}
if(P37==0)
{
p37xd();
numyue-=1;
if(numyue==0)
{numyue=12;}
yueqh();
}
}
void int0() interrupt 0
{
p32xd();
if(numsec==-1)
{
numsec=59;
}
secqh();
}
}
};
p32xd();
while(P32!=0)//第二次按下p32时,进行分调时
{
for(m=0;m<40;m++)
{
P2=0x00;
P0=table[hou1];
P2=0X20;
delayms(1);
P2=0x00;
P0=table[hou2]&0x7f;
P2=0X10;
delayms(1);
P2=0x00;
P0=table[sec1];
单片机电子时钟完整程序
/*2键手动奏乐,3键储存报警时间,报警时间到奏乐1键修改秒的时间,4键修改分的时间,8键修改小时12键启动定时器开始计数*/#include <reg52.h>#include <absacc.h>//#include "SST89x5x4.h"#define uint unsigned int#define uchar unsigned charuchar scounter=20;uchar flag=0;uchar i;#define a8255 XBYTE[0x7F00] //8255端口地址定义control 7-LED#define b8255 XBYTE[0x7F01] //send a~g#define c8255 XBYTE[0x7F02] //#define con8255 XBYTE[0x7F03]//unsigned char code table[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6,0xee,0x3e,0x9c,0x7a,0x9e,0x8e}; //共阴数码管显示编码unsigned char code table[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71};unsigned char b[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};ucharsecond=0,second_h=0,second_l=0,minute=0,minute_l=0,minute_h=0,hour=0,hour_h=0,hour_l=0; bit dp=0;unsigned char key_down=0;unsigned char key_value=0;unsigned char key_count=0;/////////////////////////////////////////////////sbit POUT = P3^0; //扬声器控制引脚//sbit POUT = P0^0; //扬声器控制引脚#define Clk 0x070000unsigned char data val_H; //计数器高字节unsigned char data val_L; //计数器低字节unsigned char data music_i; //计数器低字节unsigned int val;uchar Save_Second=61,Save_Minute=61,Save_Hour=25,Save_Flag=0,music_flag=0;//频率表unsigned int code freq_list[] = {371,495,495,495,624,556,495,556,624,495,495,624,742,833,833,833,742,624,624,495,556,495,556,624,495,416,416,371,495,833,742,624,624,495,556,495,556,833,742,624,624,742,833,990,742,624,624,495,556,495,556,624,495,416,416,371,495,0};//时间表unsigned char code time_list[]= {4, 6, 2, 4, 4, 6, 2, 4, 4, 6,2, 4, 4,12, 1, 3, 6, 2, 4, 4,6, 2, 4, 4, 6, 2, 4, 4,12, 4,6, 2, 4, 4, 6, 2, 4, 4, 6, 2,4, 4,12, 4, 6, 2, 4, 4, 6, 2,4, 4, 6, 2, 4, 4,12};void initTimer1(){TMOD|=0X10;TH1 = val_H; //重新装入计数值TL1 = val_L;EA=1;ET1=1;TR1=1;}void stopmusic(){TMOD&=0X01;EA=1;ET1=0;TR1=0;music_i=0;}void Delay_Music(unsigned char cnt) //单位延时{unsigned char i;unsigned int j;for(i=0; i<cnt; i++){for(j=0; j<0x3600; j++);}}void music(){val = Clk/(freq_list[music_i]);val = 0xFFFF - val; //计算计数值val_H = (val>>8)&0xff;val_L = val&0xff;Delay_Music(time_list[music_i]);music_i++;if(music_i==58){music_i=0;music_flag=0;}}void t1_isr() interrupt 3 using 2 //定时器1中断处理程序{POUT = ~POUT; //产生方波TH1 = val_H; //重新装入计数值TL1 = val_L;}void SaveTimer(){Save_Second=second;Save_Minute=minute;Save_Hour=hour;}/////////////////////////////////////////////////void display_value(){b[1]=second/10;b[0]=second%10;b[3]=minute/10;b[2]=minute%10;b[5]=hour/10;b[4]=hour%10;}void delay(uint i,uint j,uint k){for(;i>0;i--)for(;j>0;j--)for(;k>0;k--);}void initTimer0()TMOD|=0X01;TH0=(65536-50000)/256; //timer 50ms TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;}void CloseTimer(){EA=0;ET0=0;TR0=0;}void timer0(void) interrupt 1 using 1{TH0=(65536-50000)/256; //timer 50ms TL0=(65536-50000)%256;scounter--;if(scounter==0){flag=1; //timer 1s scounter=20;second++;if(second==60){second=0;minute++;if(minute==60){minute=0;hour++;if(hour==12){hour=0;}}}}}void init8255(){con8255=0x81;c8255=0x0ff;////////////////////////////////////////////////void display() //显示函数{unsigned char i, j = 0xDF;display_value(); //get display valuefor(i=0; i<6; i++){a8255 = 0xFF;b8255 = table[b[i]]; //查表输出显示a8255 = j;delay(1,2,200);j = (j>>1)|(j<<7);}}//////////////////////////////////////////////void keyscan() //按键扫描函数{unsigned char cc;a8255 = 0x00; //X1~X4置0cc = c8255; //得到Y1~Y4的值key_down = (~cc) & 0x0f;}/////////////////////////////////////////////////void getkey() //得到按键值{unsigned char value;unsigned char i, j = 0xFE;for(i=0; i<4; i++){a8255 = j;value = c8255; //read 8255 c portif(!(value & 0x01)) //行1{key_value = i + 1;return;}if(!(value & 0x02)) //行2{key_value = i + 4;return;}if(!(value & 0x04)) //行3{key_value = i + 8;return;}if(!(value & 0x08)) //行4{key_value = i + 12;return;}j <<= 1;}}void main(){initTimer0();init8255();key_count = 5;while(1){display(); //显示keyscan(); //按键扫描if(key_down) //判是否有键按下{delay(1000,1000,2000);keyscan();if(key_down){getkey(); //得到按键值key_down=0;}}/////////////////////////////////////////////modify time value,include second/minute/hourif(key_value){if(key_value==1) //modify second {key_value=0;CloseTimer();second++;if(second==60) second=0;}else if(key_value==4) //modify minute {key_value=0;CloseTimer();minute++;if(minute==60) minute=0;}else if(key_value==8) //modify hour {key_value=0;CloseTimer();hour++;if(hour==24) hour=0;}else if(key_value==12) //start timer{key_value=0;initTimer0();Save_Flag=1;}else if(key_value==2) //save value{music_flag=1;key_value=0;}else if(key_value==3) //save value{SaveTimer();Save_Flag=0;music_flag=0;}}if(Save_Flag==1){if((Save_Hour==hour)&&(Save_Minute==minute)&&(Save_Second==second)) {Save_Flag=0;music_flag=1;}}////////////////////////////////////////////按键模拟报警音乐if(music_flag) //start Music{initTimer1();music();}else{stopmusic();POUT=0;}}}。
电子钟汇编程序
电子钟汇编程序HOUR EQU 30H ;小时计数单元MIN EQU 31H ;分钟计数单元SEC EQU 32H ;秒计数单元COUNT EQU 33H ;50ms计数单元BUF EQU 38H ;显示缓冲区首地址,共3字节ORG 0LJMP STARTORG 03HLJMP TR ;外部INTO中断ORG 0BHLJMP TS ;50ms中断服务程序ORG 30HSTART: MOV SP,#60H ;设置堆栈MOV DPTR,#0FF20HMOV A,#03HMOVX @DPTR,A ;8155初始化MOV TMOD,#01H ;设T0为方式1MOV TH0,#9EHMOV TL0,#58H ;50ms定时初值SETB PT0 ;设T0为高级中断CLR PX0 ;INT0为低级中断SETB IT0 ;设外部中断0为边沿中断SETB ET0 ;允许T0中断MOV COUNT,#20 ; 50ms计数单元赋初值MOV HOUR,#0 ; 小时计数单元清零MOV MIN,#0 ;分钟计数单元清零MOV SEC,#0 ;秒计数单元清零SETB TR0 ;启动计数LOOP: JB P1.0,LM ;若K1=1则转移到LM SETB EX0 ;开INT0中断LJMP OUTLM: CLR EX0 ;关INT0中断OUT: CLR EA ;关中断,避免读数时显示跳动MOV BUF,HOUR ;存小时到显示缓冲区MOV BUF+1,MIN ; 存分钟到显示缓冲区MOV BUF+2,SEC ; 存秒到显示缓冲区SETB EA ;开中断LCALL DISP ;调显示子程序SJMP LOOP;显示程序;将BUF单元的3字节BCD码送数码管显示DISP: MOV R0,#BUF ;送显示缓冲区首址MOV R6,#3 ;共显示3个字节的BCD码MOV R7,#1 ;位码初值DISP1:MOV A,@R0 ;取要显示的压缩BCD码ANL A,#0FH ;屏蔽高4位MOV DPTR,#DISP_TAB ;MOVC A,@A+DPTR ;查表得段码ANL A,#7FH ;点亮末尾小数点MOV DPTR,#0FF22H ;指向8155B口MOVX @DPTR,A ;字形码送B口MOV A,R7MOV DPTR,#0FF21H ;指向A口MOVX @DPTR,ARL A ;位码A左移一位MOV R7,A ;存位码LCALL DELAY ;延时2msMOV A,#00HMOVX @DPTR,A ;消隐MOV A,@R0SWAP A ;高低四位交换ANL A,#0FHMOV DPTR,#DISP_TABMOVC A,@A+DPTRMOV DPTR,#0FF22HMOVX @DPTR,AMOV A,R7MOV DPTR,#0FF21HMOVX @DPTR,ARL AMOV R7,ALCALL DELAYMOV A,#00H ;消隐MOVX @DPTR,AINC R0 ;指向下一个待显示数据DJNZ R6,DISP1 ;没显示完循环RETDISP_TAB:DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H DB 80H,90H,88H,83H,0C6H,0A1H,86H,8EH,0BFH,07FH,0FFH ;50ms 定时中断服务程序TS: PUSH PSW ;保护现场PUSH ACCPUSH DPHPUSH DPLCLR T R0 ;停止T0计数MOV TH0,#9EH ;50ms定时初值MOV TL0,#58HSETB TR0 ;启动T0计数DJNZ COUNT,EXIT ;中断不够20次返回MOV COUNT,#20 ;送20次中断计数初值MOV A,SEC ;取秒计数ADD A,#1 ;秒加1DA A ;调整为BCD码MOV SEC,A ;存秒CJNE A,#60H,EXIT ;不到60秒则退出MOV SEC,#0 ;秒单元清零MOV A,MIN ;取分计数ADD A,#1 ;分加1DA A ;调整为BCD码MOV MIN,A ;存分CJNE A,#60H,EXIT ;不到60分则退出MOV MIN,#0 ;分清零MOV A,HOUR ;取小时ADD A,#1 ;小时加1DA A ;调整为BCD码MOV HOUR,A ;存小时CJNE A,#24H,EXIT ;不到24小时则退出MOV HOUR,#0 ;小时清零EXIT:POP DPL ;恢复现场POP DPHPOP ACCPOP PSWRETI;INT0中断服务程序TR: PUSH PSW ;保护现场PUSH ACCPUSH DPHPUSH DPLJB P1.1, LM1 ;若K2=1则转到LM1 MOV A, MIN ;取分计数ADD A, #1 ;分钟加1DA A ;调整为BCD码MOV MIN,A ;存分CJNE A,#60H,NEXT ;不到60分转NEXT MOV MIN,#0 ;分清零LJMP NEXTLM1: MOV A,HOUR ;取小时计数ADD A,#1DA AMOV HOUR,ACJNE A,#24H,NEXT ;不到24小时则转MOV HOUR,#0NEXT: POP DPL ;恢复现场POP DPHPOP ACCPOP PSWRETI ;中断返回DELAY: MOV R3,#250 ;2ms延时L1: NOPNOPDJNZ R3,L1RETEND。
51单片机电子日历(电子时钟)程序
51单片机电子日历(电子时钟)程序经过两天的调试,完成了51单片机电子日历课程设计,现在把C程序源代码帖出来纪念一下:~硬件实验箱是伟福LAB2000实验箱。
程序代码:/****************************************************************************//* 电子日历,有时间显示、闹铃、日期、秒表及键盘设置功能*//* 功能键A: 设置位数字+1 闹钟模式下为闹钟开关秒表模式下为记时开关*//* 功能键B: 设置位数字-1 闹钟模式下为闹钟开关*//* 功能键C:设置模式及设置位选择秒表模式下为清零键*//* 功能键D:在四种工作模式下切换设置闹钟开关*//* 曹宇03电子0201029 *//* 2006.6.3 更新*//****************************************************************************/#include#include/***************这里设置程序初始化时显示的时间****************/#define SET_HOUR 12 /*设置初始化小时*/#define SET_MINUTE 00 /*设置初始化分钟*/#define SET_SECOND 00 /*设置初始化秒数*//*************************系统地址****************************/#define BASE_PORT 0x8000 /*选通基地址*/#define KEY_LINE BASE_PORT+1 /*键盘行线地址*/#define KEY_COLUMN BASE_PORT+2 /*键盘列线地址*/#define LED_SEG BASE_PORT+4 /*数码管段选地址*/#define LED_BIT BASE_PORT+2 /*数码管位选地址*/#define LED_ON(x) XBYTE[LED_BIT]=(0x01<<X)&NBSP; *6位led的位选通,带参数宏,参数为0~5*=""#define LED_OFF XBYTE[LED_SEG]=0x00 /*LED显示空*//**************在设置模式下对秒分时的宏定义*****************/#define SECOND 0 /*对应数码管右边两位*/#define MINUTE 1 /*对应数码管中间两位*/#define HOUR 2 /*对应数码管左边两位*//********************定义四种工作模式***********************/#define CLOCK clockstr /*时钟模式*/#define ALART alartstr /*闹钟模式*/#define DATE datestr /*日期模式*/#define TIMER timerstr /*秒表模式*//****************以下是所有子函数的声明*********************/void sys_init(void); /*系统的初始化程序*/void display(void); /*动态刷新一次数码管子程序*/void clockplus(void); /*时间加1S的子程序*/void update_clockstr(void); /*更新时间显示编码*/void update_alartstr(void); /*更新闹钟时间的显示编码*/void update_datestr(void); /*更新日期显示编码*/void update_timerstr(void); /*更新秒表时间的显示编码*/void deley(int); /*延时子程序*/void update_dispbuf(unsigned char *); /*更新显示缓冲区*/unsigned char getkeycode(void); /*获取键值子程序*/void keyprocess(unsigned char); /*键值处理子程序*/unsigned char getmonthdays(unsigned int,unsigned char);/*计算某月的天数子程序*//*功能键功能子函数*/void Akey(void); /*当前设置位+1 开关闹钟开关秒表*/void Bkey(void); /*当前设置位-1 开关闹钟*/void Ckey(void); /*设置位选择秒表清零*/void Dkey(void); /*切换四种工作模式*//**********************全局变量声明部分*********************/unsigned char led[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};/*从0~9的LED编码*/unsigned char ledchar[3]={0x5c,0x54,0x71};/*o n f*///unsigned char key[24]={ /* 键值代码数组对应键位:*/// 0x70,0x71,0x72,0x73,0x74,0x75, /* 7 8 9 A TRACE RESET*/// 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5, /* 4 5 6 B STEP MON */// 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5, /* 1 2 3 C HERE LAST */// 0xe0,0xe1,0xe2,0xe3,0xe4,0xe5}; /* 0 F E D EXEC NEXT */struct{ /*时间结构体变量*/unsigned char s;unsigned char m;unsigned char h;}clock={SET_SECOND,SET_MINUTE,SET_HOUR};struct{ /*闹铃时间结构体变量*/unsigned char m;unsigned char h;}alart={SET_MINUTE,SET_HOUR};struct{ /*日期结构体变量*/unsigned int year;unsigned char month;unsigned char day;}date={6,1,1};struct{ /*秒表时间结构体变量*/unsigned char ms;unsigned char s;unsigned char m;}timer={0,0,0};unsigned char dispbuf[6]; /*显示缓冲区数组*/unsigned char clockstr[6]; /*时间显示的数码管编码数组*/unsigned char alartstr[6]; /*闹钟显示的数码管编码数组*/unsigned char datestr[6]; /*日期显示的数码管编码数组*/unsigned char timerstr[6]; /*秒表显示的数码管编码数组*/unsigned int itime=0,idot; /*定时器0中断计数*/unsigned char itime1=0; /*定时器1中断计数*/sbit P3_1=P3^1; /*外接蜂鸣器的管脚*/bdata bit IsSet=0; /*设置模式标志位0:正常走时1:设置模式*/bdata bit Alart_EN=0; /*闹铃功能允许位0:禁止闹铃1:允许闹铃*/bdata bit IsBeep=0; /*响铃标志位0:未响铃1:正在响铃*/unsigned char SetSelect=0; /*在设置模式IsSet=1时,正在被设置的位,对应上面的宏*/unsigned char *CurrentMode; /*标志当前正设置的功能,如CurrentMode=CLOCK或CurrentMode=A LART等*/void timerplus(void);/**************************函数部分*************************/void main(void){sys_init();while(1){XBYTE[KEY_COLUMN,0x00]; /*给键盘列线赋全零扫描码,判断是否有键按下*/while((XBYTE[KEY_LINE]&0x0f)==0x0f) /*检测是否有键按下,无则一直进行LED的刷新显示*/ {if(Alart_EN&&(clock.h==alart.h)&&(clock.m==alart.m)) {IsBeep=1;}else{ IsBeep=0;P3_1=0;}display();}keyprocess(getkeycode()); /*有键按下时得到键值,并送入键值处理程序*/display(); /*可要可不要*/}}void sys_init(void){TMOD=0x22; /*定时器0和1都设置为工作方式2,基准定时250×2=500us=0.5ms*/TH0=6; /*定时器0中断服务用来产生1秒时钟定时及闹钟蜂鸣器蜂鸣脉冲*/TL0=6; /*定时器1中断服务留给秒表使用,产生1/100秒定时*/TH1=6;TL1=6;ET0=1;ET1=1;EA=1;TR0=1;update_clockstr(); /*初始化时钟显示编码数组*/update_alartstr(); /*初始化闹钟显示编码数组*/update_datestr(); /*初始化日期显示编码数组*/update_timerstr(); /*初始化秒表显示编码数组*/update_dispbuf(clockstr);/*初始化显示缓冲数组*/CurrentMode=CLOCK; /*默认的显示摸式为时钟*/P3_1=0; /*蜂鸣器接线引脚复位*/}void timer0(void) interrupt 1 using 1 /*定时器0中断服务器,用来产生1秒定时*/{itime++;if(itime==1000){if(IsSet) /*在设置模式下,对正在设置的位闪烁显示*/{dispbuf[SetSelect*2]=0; /*对正在设置的位所对应的显示缓冲区元素赋0,使LED灭*/ dispbuf[SetSelect*2+1]=0;}if(IsBeep) P3_1=!P3_1; /*闹钟模式时,产生峰鸣器响脉冲*/if(CurrentMode==CLOCK){dispbuf[2]=dispbuf[2]&0x7f;dispbuf[4]=dispbuf[4]&0x7f;}}if(itime==2000) /*两千次计数为1S 2000×0.5ms=1s*/{itime=0; /*定时1s时间到,软计数清零*/clockplus(); /*时间结构体变量秒数加1 */update_clockstr(); /* 更新时间显示编码数组*/if(CurrentMode!=TIMER) update_dispbuf(CurrentMode); /* 用时间编码数组更新显示缓冲区* /}}void timer1(void) interrupt 3 using 2 /*定时器1中断服务器,用来产生1/100秒定时*/{idot++;if(++itime1==20) /*20*0.5ms=10ms*/{itime1=0;timerplus();update_timerstr();if(CurrentMode==TIMER){update_dispbuf(timerstr);dispbuf[2]=dispbuf[2]&0x7f; /*关闭小数点的显示*/dispbuf[4]=dispbuf[4]&0x7f;if(idot<1000) /*闪烁显示小数点*/{dispbuf[2]=dispbuf[2]|0x80;dispbuf[4]=dispbuf[4]|0x80;}else{dispbuf[2]=dispbuf[2]&0x7f;dispbuf[4]=dispbuf[4]&0x7f;}}}if(idot==2000) idot=0;}/*功能模块子函数*/void clockplus(void) /*时间加1s判断分,时子函数*/{if(++clock.s==60) /*秒位判断*/{clock.s=0;if(++clock.m==60) /*分位判断*/{clock.m=0;if(++clock.h==24) /*时位判断*/{clock.h=0;if(++date.day==(getmonthdays(date.year,date.month)+1)){date.day=1;if(++date.month==13) date.month=1;}}}}}void timerplus() /*秒表1/100秒位加1,判断秒、分子程序*/ {if(++timer.ms==100){timer.ms=0;if(++timer.s==60){timer.s=0;if(++timer.m==60){timer.m=0;}}}}void update_clockstr(void) /*更新时钟显示代码数组clockstr*/{clockstr[0]=led[clock.s%10]; /*给元素0赋相应数码管显示编码,编码序号是秒数的个位*/clockstr[1]=led[(int)(clock.s/10)]; /*给元素1赋相应数码管显示编码,编码序号是秒数的十位*/ clockstr[2]=led[clock.m%10]; /*以下类推*/clockstr[3]=led[(int)(clock.m/10)];clockstr[4]=led[clock.h%10];clockstr[5]=led[(int)(clock.h/10)];}void update_alartstr(void) /*更新闹钟显示代码数组alartstr*/{ /*右边两位显示on:闹钟开启of:闹钟关闭*/if(Alart_EN) alartstr[0]=ledchar[1];/*显示字母n*/else alartstr[0]=ledchar[2]; /*显示字母f*/alartstr[1]=ledchar[0]; /*显示字母o*/alartstr[2]=led[alart.m%10];alartstr[3]=led[(int)(alart.m/10)];alartstr[4]=led[alart.h%10];alartstr[5]=led[(int)(alart.h/10)];}void update_datestr(void) /*更新日期显示代码数组datestr*/{datestr[0]=led[date.day%10];datestr[1]=led[(int)(date.day/10)];datestr[2]=led[date.month%10];datestr[3]=led[(int)(date.month/10)];datestr[4]=led[date.year%10];datestr[5]=led[(int)(date.year/10)];}void update_timerstr(void) /*更新秒表显示代码数组timerstr*/{timerstr[0]=led[timer.ms%10];timerstr[1]=led[(int)(timer.ms/10)];timerstr[2]=led[timer.s%10];timerstr[3]=led[(int)(timer.s/10)];timerstr[4]=led[timer.m%10];timerstr[5]=led[(int)(timer.m/10)];}void display(void) /*刷新显示六位LED一次*/{unsigned char i;for(i=0;i<6;i++){LED_ON(i); /*选通相应位*/XBYTE[LED_SEG]=dispbuf[i]; /*写显示段码*/deley(50); /*延时显示*/LED_OFF; /*写LED全灭段码*/}}void update_dispbuf(unsigned char *str) /*更新显示缓冲区子函数,参数为要用来更新缓冲区的源字符数组的首地址*/{dispbuf[0]=str[0]; /*将要更新的源字符数组内容COPY至dispbuf数组,用作显示缓冲区*/dispbuf[1]=str[1];dispbuf[2]=str[2]|0x80; /*默认把时位和分位后面的小数点显示出来,根据需要再取舍*/dispbuf[3]=str[3];dispbuf[4]=str[4]|0x80;dispbuf[5]=str[5];}void deley(int i) /*延时子函数*/{while(i--);}unsigned char getkeycode(void) /*键盘扫描子程序,返回获得的键码*/{unsigned char keycode; /*键码变量,一开始存行码*/unsigned char scancode=0x20; /*列扫描码*/unsigned char icolumn=0; /*键的列号*/display(); /*用刷新数码管显示的时间去抖*/XBYTE[KEY_COLUMN]=0x00;keycode=XBYTE[KEY_LINE]&0x0f; /*从行端口读入四位行码*/while((scancode&0x3f)!=0) /*取scancode的低六位,只要没变为全0,则执行循环*/{XBYTE[KEY_COLUMN]=(~scancode)&0x3f; /*给列赋扫描码,第一次为011111*/if((XBYTE[KEY_LINE]&0x0f)==keycode) break; /*检测按键所在的列跳出循环*/scancode=scancode>>1; /*列扫描码右移一位*/icolumn++; /*列号加1*/}keycode=keycode<<4; /*把行码移到高四位*/keycode=keycode|icolumn; /*由行码和列码组成键码*///等待按键放开XBYTE[KEY_COLUMN]=0x00;while((XBYTE[KEY_LINE]&0x0f)!=0x0f) display();return keycode;}void keyprocess(unsigned char keycode) /*键值处理函数*/{switch (keycode){case 0x73: Akey();break;case 0xB3: Bkey();break;case 0xD3: Ckey();break;case 0xE3: Dkey();break;default: break;}update_dispbuf(CurrentMode);}unsigned char getmonthdays(unsigned int year,unsigned char month)/*得到某月的天数*/ {unsigned char days;switch (month){case 4:case 6:case 9:case 11:days=30;break;case 2: if(year%4==0) days=29;else days=28;break;default:days=31;break;}return days;}/*功能键子函数部分*/void Akey(void) /*对当前设置位进行加一操作,如果设置秒位,则给秒位清零*/{if(CurrentMode==TIMER) /*秒表模式下启闭走时*/{ TR1=!TR1;return;}if(!IsSet) return; /*如果不是设置模式退出*/switch (SetSelect){case SECOND:if(CurrentMode==CLOCK){clock.s=0; /*如果当前被设置位是秒位,则清零秒位*/update_clockstr();}if(CurrentMode==ALART){Alart_EN=!Alart_EN;update_alartstr();}if(CurrentMode==DATE){if(++date.day==(getmonthdays(date.year,date.month)+1)) date.day=1;update_datestr();}if(CurrentMode==TIMER){TR1=!TR1;}break;case MINUTE:if(CurrentMode==CLOCK){if(++clock.m==60) clock.m=0; /*如果当前被设置分位,则分位加1*/ update_clockstr();}if(CurrentMode==ALART){if(++alart.m==60) alart.m=0;update_alartstr();}if(CurrentMode==DATE){if(++date.month==13) date.month=1;update_datestr();}break;case HOUR: if(CurrentMode==CLOCK){if(++clock.h==24) clock.h=0; /*如果当前被设置时位,则时位加1*/ update_clockstr();}if(CurrentMode==ALART){if(++alart.h==24) alart.h=0;update_alartstr();}if(CurrentMode==DATE){if(++date.year==100) date.year=0;update_datestr();}break;default: break;}update_dispbuf(CurrentMode);}void Bkey(void) /*对当前设置位进行减一操作,如果设置秒分,则给秒位清零,类比Akey()函数*/ {if(!IsSet) return;switch (SetSelect){case SECOND:if(CurrentMode==CLOCK){clock.s=0;update_clockstr();}if(CurrentMode==ALART){Alart_EN=!Alart_EN;update_alartstr();}if(CurrentMode==DATE){if(--date.day==0x00) date.day=getmonthdays(date.year,date.month);update_datestr();}break;case MINUTE:if(CurrentMode==CLOCK){if(--clock.m==0xff) clock.m=59;update_clockstr();}if(CurrentMode==ALART){if(--alart.m==0xff) alart.m=59;update_alartstr();}if(CurrentMode==DATE){if(--date.month==0x00) date.month=12;update_datestr();}break;case HOUR: if(CurrentMode==CLOCK){if(--clock.h==0xff) clock.h=23;update_clockstr();}if(CurrentMode==ALART){if(--alart.h==0xff) alart.h=23;update_alartstr();}if(CurrentMode==DATE){if(--date.year==0xffff) date.year=99;update_datestr();}break;default: break;}update_dispbuf(CurrentMode);}void Ckey(void) /*正常走时模式和设置模式的切换*/{if(CurrentMode==TIMER){TR1=0; /*初始化定时器1设置,停止秒表记时*/ TH1=6;TL1=6;timer.ms=0; /*初始化秒表数组*/timer.s=0;timer.m=0;update_timerstr();}else{if(IsSet==0) /*在非秒表模式下,第一次按下C键进入设置模式,设置时位*/{IsSet=1; /*置位标志位,进入设置模式*/SetSelect=HOUR;return;} /*第二次按C键设置分位,第三次按键设置秒位,第四次按键完成退出设置*/if(SetSelect==0) /*按到第四次,即设置完秒位后,将标志位IsSet置0,完成设置*/{IsSet=0; /*复位标志位,进入正常走时模式*/return;}if(SetSelect>0) SetSelect--; /*设置位的标志变量SetSelect=0:时位1:分位2:秒位*/ }}void Dkey(void) /*工作状态切换:时钟、闹钟、日期、秒表*/{if(CurrentMode==CLOCK) /*切换至闹钟,同时开关闹钟*/{ CurrentMode=ALART;Alart_EN=!Alart_EN;update_alartstr();return;}if(CurrentMode==ALART) /*切换至日期*/{ CurrentMode=DATE;return;}if(CurrentMode==DATE) /*切换至秒表,同时关闭设置模式*/{CurrentMode=TIMER;IsSet=0;return;}if(CurrentMode==TIMER) /*切换至时钟*/{CurrentMode=CLOCK;return;}}当我被上帝造出来时,上帝问我想在人间当一个怎样的人,我不假思索的说,我要做一个伟大的世人皆知的人。
电子时钟设计代码
#include<reg51.h>#define uint unsigned int //宏定义,将unsigned int 用uint代替,下同#define uchar unsigned charsbit S1=P1^0;sbit S2=P1^1;sbit S3=P1^2;sbit S4=P1^3;char second;char minute;char hour;uchar T_time;//计数变量int a=0;uchar tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//0~9 void delay(uint k){uint i;for(i=0;i<k;i++);}void keyscan();void Displaysecond(uchar);void Displayminute(uchar);void Displayhour(uchar);void main(){TMOD=0x01; //定时器/计数器T0的工作方式1EA=1; //总中断允许ET0=1; //计时器T0中断允许位TR0=1; //开始计数TH0=(65536-46083)/256;TL0=(65536-46083)%256;minute=0;second=0;hour=0;T_time=0;while(1){keyscan();Displaysecond(second);delay(100);Displayminute(minute);delay(100);Displayhour(hour);delay(100);}}void Displaysecond(uchar s){P2=0xbf; //"秒十位"位选端P0=tab[s/10]; //刚开始显示数字0 delay(100); //延时一段时间P0=0x00; //消隐P2=0x7f; //"秒个位"位选端P0=tab[s%10]; //显示段码delay(100); //延时一段时间P0=0x00; //消隐}void Displayminute(uchar m){P2=0xf7; //"分十位"位选端P0=tab[m/10]; //刚开始显示数字0 delay(100); //延时一段时间P0=0x00; //消隐P2=0xef; //"分个位"位选端P0=tab[m%10]; //显示段码delay(100); //延时一段时间P0=0x00; //消隐P2=0xdf; //分隔符位选端P0=0x40; //显示分隔符delay(100); //延时一段时间P0=0x00; //消隐}void Displayhour(uchar h){P2=0xfe; //"小时十位位选端" P0=tab[h/10]; //显示段码1delay(100);P0=0x00; //消隐P2=0xfd; //小时个位位选端P0=tab[(h)%10]; //显示段码2 delay(100);P0=0x00;//消隐P2=0xfb;P0=0x40;delay(100);P0=0x00;//消隐}void keyscan(){if(S1==0) //S1的功能是秒加1{delay(100);if(S1==0){if(a==0){second++;if(second>=60){minute++;if(minute>=60){minute=0;hour++;if(hour>=24){hour=0;}}second=0;}}if(a==1){minute++;if(minute>=60){hour++;if(hour>=24)hour=0;minute=0;}}if(a==2){hour++;if(hour>=24)hour=0;}while(!S1){Displaysecond(second);delay(100);Displayminute(minute);delay(100);Displayhour(hour);delay(100);}}}if(S2==0) //S2的功能是秒减1 {delay(100);if(S2==0){if(a==0){second--;if(second<=-1){minute--;if(minute<=-1){hour--;if(hour<=-1)hour=23;minute=59;}second=59;}}if(a==1){minute--;if(minute<=-1){minute=59;hour--;if(hour<=-1)hour=23;}}if(a==2){hour--;if(hour<=-1)hour=23;}while(!S2){Displaysecond(second);delay(100);Displayminute(minute);delay(100);Displayhour(hour);delay(100);}}}if(S3==0) //S3的功能是清零{delay(100);if(S3==0){a++;if(a==3)a=0;while(!S3){Displaysecond(second);delay(100);Displayminute(minute);delay(100);Displayhour(hour);delay(100);}}}if(S4==0) //S4的功能是启动或停止计数{while(!S4){Displaysecond(second);delay(100);Displayminute(minute);delay(100);Displayhour(hour);delay(100);}TR0=~TR0;}}void Time0() interrupt 1{T_time++;if(T_time==20){T_time=0;second++;}if(second==60){second=0;minute++;}if(minute==60){minute=0;hour++;}if(hour==24){hour=0;}TH0=(65536-50000)/256;TL0=(65536-50000)%256;}。
AT89C51数码管电子时钟源代码
while(!P32); Delay_ms(10); while(!P32); ring++; if(ring==2)
ring=0; } } if (ring==1) { if((clocksec==sec)&&(clockmin==min)&&(clockhour==hour)) {
flag1=0; sec=0; } } } switch (flag1) { case 0:
if(P30==0) {
Delay_ms(10); if(P30==0) {
while(!P30); Delay_ms(10); while(!P30); sec++; if(sec==60)
sec=0; } }
码 0--9
//数据初始化 void VarInit(void) {
num = 0; sec = 0; min = 0; hour = 0;
//初始化时间
num1=0; sec1=0; min1=0;
//初始化秒表
clocksec=0; clockmin=0; clockhour=0;
//初始化闹钟时间
for(k=123;k>0;k--); }
//计时器初始化 void TimerInit(void) {
EA=1; ET1=1; EX1=1; IT1=1; TMOD=0x10; TH1=0xd8; TL1=0xf0; TR1=1; }
//开总中断
//选择计数器工作方式,选为方式 1,16 位计数器 //装入高八位数 //低八位 //计数器开始工作
} } }
//设置闹钟 void AlarmClock() {
电子时钟C代码
#include<iostream.h>#include<iomanip.h>#include<time.h>class CDateTime{private:int year;int month;int days;int hour;int minute;int second;int wday; //星期private:int YearToDays(int y); int DatesToDays();int MonthToWeek();int YearMonthToDay( int y,int m);public:CDateTime(int y=0,int m=0,int d=0,int h=0,int mt=0,int s=0) {year=y;month=m;days=d;hour=h;minute=mt;second=s;wday=DatesToWeek();}~CDateTime(){}void Now();int DatesToWeek();CDateTime LastMonth();CDateTime NextMonth();void LastTime();void InputYearMonth();void PrintMonth();void InputDates();int Week(){return wday;}CDateTime operator+(const int &n);CDateTime operator-(const int &n);int operator-( CDateTime &dt);friend ostream & operator<< ( ostream & out, const CDateTime &c);};void CDateTime::Now(){time_t te;tm *tp;time(&te);tp=localtime(&te);year=tp->tm_year+1900;month=tp->tm_mon+1;days=tp->tm_mday;hour=tp->tm_hour;minute=tp->tm_min;second=tp->tm_sec;wday=tp->tm_wday;}CDateTime CDateTime:: operator-(const int &n) {int m=n;int mday;CDateTime dt(*this);mday=YearMonthToDay(dt.year,dt.month);while(dt.days+m >mday){dt.month--;if(dt.month>1){dt.year-=1;dt.month=12;m-=mday;mday=YearMonthToDay(dt.year,dt.month); }dt.days+=m;return dt;}CDateTime CDateTime::operator+(const int &n) {int m=n;int mday;CDateTime dt(*this);mday=YearMonthToDay(dt.year,dt.month);while(dt.days+m > mday){dt.month++;if(dt.month>12){dt.year+=1;dt.month=1;m-=mday;mday=YearMonthToDay(dt.year,dt.month); }dt.days+=m;return dt;}int CDateTime::operator-( CDateTime &dt) {int y=dt.year;int m=dt.month;int t1 = dt.DatesToDays ();int t2 = DatesToDays();return (t2 - t1);}int CDateTime::YearToDays(int y){int d=0;int i=(y-1900)*365;while(y>1900)if((y-1)% 4 ==0 && (y-1) % 100 != 0 || (y-1) % 400 == 0)++d;--y;}return(d+i);}int CDateTime::YearMonthToDay(int y,int m ){int mday=0;switch(m){case 1: case 3: case 5: case 7: case 8: case 10: case 12: mday=31;break;case 2:if(y % 4 ==0 && y % 100 != 0 || y % 400 == 0)mday=29;elsemday=28;break;case 4: case 6: case 9: case 11:mday=30;break;default:mday=0;}return mday;}int CDateTime::DatesToDays( ){int total = 0;while( month >1){total += YearMonthToDay(year ,month-1); --month;}int num = YearToDays( year);return (total +num +days);}int CDateTime::DatesToWeek()int w = DatesToDays();return (w%7);}CDateTime CDateTime:: NextMonth( ) {Date tmp( *this);if(month ==12){tmp.month = 1;++tmp.year ;}else++tmp.month;return tmp;}CDateTime CDateTime::LastMonth( ) {CDateTime tmp( *this);if(month ==1)tmp.month = 12;--tmp.year ;}else--tmp.month;return tmp;}void CDateTime::LastTime(){}void CDateTime::InputDates(){cout<<"输入年:"<<endl;cin>>year;while( year <= 1900 || year >=2099) {cout<<"重新输入年:"<<endl;cin>>year;}cout<<"输入月:"<<endl;cin>>month;while( month <= 0 || month >12){cout<<"重新输入月:"<<endl;cin>>month;}cout<<"输入天"<<endl;cin>>days;}void CDateTime::InputYearMonth() {cout<<"输入年:"<<endl;cin>>year;while( year <= 1900 || year >=2099) {cout<<"重新输入年:"<<endl;cin>>year;}cout<<"输入月:"<<endl;cin>>month;while( month <= 0 || month >12){cout<<"重新输入月:"<<endl;cin>>month;}}int CDateTime::MonthToWeek( ){int t = DatesToDays( );return (t-days)%7;}void CDateTime::PrintMonth( ){int y=year;int m=month;cout<<"------------------------------"<<endl;cout<<"******************************"<<endl;cout<<" "<<"日"<<" "<<"一"<<" "<<"二"<<" "<<"三"<<" "<<"四"<<" "<<"五"<<" "<<"六"<<endl;int w = MonthToWeek( );if( w !=6)for( int i =0;i<=w;i++)cout<<" ";int k = YearMonthToDay(y,m);for( int j = 1 ;j<= k; ++j){cout<<setw(2)<<""<<setw(2)<<j;if( (j+w+1)%7 ==0)cout<<endl;}cout<<endl;cout<<"------------------------------"<<endl;cout<<"******************************"<<endl;}ostream & operator<< ( ostream & out, const CDateTime &c) {out<<c.year<<"年"<<c.month<<"月"<<c.days<<"日";out<<c.hour<<":"<<c.minute<<":"<<c.second<<endl;return out;}void main(){CDateTime d1,d2,d3,d4,d5;cout<<"欢迎使用日历"<<endl; cout<<"请选择:"<<endl;cout<<"1.查询月历"<<endl;cout<<"2.查询星期"<<endl;cout<<"3.倒计时"<<endl;cout<<"0退出"<<endl;cin>>i;while(i!=0){switch(i){case 0:break;case 1:d1.InputYearMonth();d1.PrintMonth();cout<<"1.上一个月"<<endl; cout<<"2. 下一个月:"<<endl; cout<<"0.返回:"<<endl;while(j!=0){switch(j){case 0:break;case 1:d2=stMonth();d2.PrintMonth();break;case 2:d3=d1.NextMonth();d3.PrintMonth();break;default:while(j<0||j>2){cout<<"请重新选择"<<endl; cin>>j;}}cout<<"0返回"<<endl;cin>>j;}break;case 2:d4.InputDates();k=d4.DatesToWeek();cout<<"星期"<<k<<endl;break;case 3:d5.InputDates();// stTime();break;default:while(i<0||i>3){cout<<"请重新输入"<<endl; cin>>i;}}cout<<"1.查询月历"<<endl;cout<<"2.查询星期"<<endl; cout<<"3.倒计时"<<endl; cout<<"0退出"<<endl;cin>>i;}}。
电子时钟程序代码
#include<reg52.h>#define uchar unsigned char#define uint unsigned intuchar code weixuan[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; //位选,控制哪个数码管亮。
(从右到左)ucharcodeduanxuan[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf, 0xff}; //0-9,'-','灭'uchar data timedata[3]={0x00,0x00,0x00}; //时间缓冲区,分别为:秒、时、分。
uchar data datetime[8]={0xc0,0xc0,0xbf,0xc0,0xc0,0xbf,0xc0,0xc0}; //时间缓冲区,初始化显示00-00-00。
uchar tt1,tt2,tt,tt0,num=0;uchar flag,flag1,flag2,flash;sbit key1=P1^3;sbit key2=P1^5;sbit key3=P1^7;//**************延时函数*********************************void delay(uint del){uchar i, j;for(i=0; i<del; i++)for(j=0; j<=148; j++);}//********************调用显示************************void display(){datetime[0]=timedata[0]%10; datetime[1]=timedata[0]/10; //秒datetime[3]=timedata[1]%10; datetime[4]=timedata[1]/10; //分 datetime[6]=timedata[2]%10; datetime[7]=timedata[2]/10; //时 if(!flag){P2=0X80; //秒显示P0=duanxuan[datetime[0]];delay(2);P2=0X40;P0=duanxuan[datetime[1]];delay(2);}else{P2=0X80; //秒显示P0=duanxuan[datetime[0]]|flash;delay(2);P2=0X40;P0=duanxuan[datetime[1]]|flash;delay(2);}P2=0X20; //显示'-'P0=duanxuan[10];delay(2);if(!flag1){P2=0X10; //分显示P0=duanxuan[datetime[3]];delay(2);P2=0X08;P0=duanxuan[datetime[4]];delay(2);}else{P2=0X10; //分显示P0=duanxuan[datetime[3]]|flash;delay(2);P2=0X08;P0=duanxuan[datetime[4]]|flash;delay(2);}P2=0X04; //显示'-' P0=duanxuan[10];delay(2);if(!flag2){P2=0X02; //小时P0=duanxuan[datetime[6]];delay(2);P2=0X01;P0=duanxuan[datetime[7]];delay(2);}else{P2=0X02; //小时P0=duanxuan[datetime[6]]|flash;delay(2);P2=0X01;P0=duanxuan[datetime[7]]|flash;delay(2);}}/******************按键调节时间*****************************/ void keyscan(){if(key1==0){delay(10);if(key1==0){num++;while(!key1);while(1){if(num==1){flag=1;flag1=0;flag2=0;if(key2==0){delay(10);if(key2==0){timedata[0]--;if(timedata[0]==-1)timedata[0]=60;while(!key2);}}if(key3==0){delay(10);if(key3==0){timedata[0]++;if(timedata[0]==60)timedata[0]=0;while(!key3);}}}if(key1==0){delay(10);if(key1==0)num++;while(!key1);}if(num==2){flag=0;flag1=1;flag2=0;if(key2==0){delay(10);if(key2==0){timedata[1]--;if(timedata[1]==-1)timedata[1]=60;while(!key2);}}if(key3==0){delay(10);if(key3==0){timedata[1]++;if(timedata[1]==60)timedata[1]=0;while(!key3);}}if(num==3){flag=0;flag1=0;flag2=1;if(key2==0){delay(10);if(key2==0){timedata[2]--;if(timedata[2]==-1)timedata[2]=24;while(!key2);}}if(key3==0){delay(10);if(key3==0){timedata[2]++;if(timedata[2]==25)timedata[2]=0;while(!key3);}}if(num==4){num=0;flag=0;flag1=0;flag2=0;break;}}}}}//主函数void main(){TMOD=0x01;ET0=1;TR0=1;TH0=0x40;TL0=0x00;EA=1;while(1){keyscan();}}//***************定时器函数*************************** void timer1() interrupt 1{TH0=0x40; //50ms自加一次。
电子时钟程序代码
(一)计时模块1. 秒计数是由一个六十进制的计数器构成,生成元器件如下Clk:驱动秒计时器的时钟信号Clr:校准时间时清零的输入端En:使能端Sec0[3..0]sec1[3..0]:秒的高位显示,低位显示Co:进位输出端,作为分的clk输入代码如下:library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity second isport (clk,clr,en:in std_logic;sec0,sec1:out std_logic_vector(3 downto 0);co:outstd_logic);end second;architecture sec of second isSIGNAL cnt1,cnt0:std_logic_vector(3 downto0);beginprocess(clk)beginif(clr='0')thencnt0<="0000";cnt1<="0000";elsif(clk'eventand clk='1')thenif(en='1')thenifcnt1="0101" and cnt0="1000" thenco<='1';cnt0<="1001";elsifcnt0<"1001" thencnt0<=(cnt0+1);elsecnt0<="0000";ifcnt1<"0101"thencnt1<=cnt1+1;elsecnt1<="0000";co<='0';endif;endif;endif;endif;sec1<=cnt1;sec0<=cnt0;endprocess;end sec;2.分计数是由六十进制的计数器构成,生成元器件如下Clk:设置分输入和秒进位的或输入En:使能输入Min1[3..0] min0[3..0]:分的高位显示,低位显示Co:向时的进位输出代码如下:library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity minute isport (clk,en:in std_logic;min1,min0:out std_logic_vector(3 downto 0);co:outstd_logic);end minute;architecture min of minute isSIGNAL cnt1,cnt0:std_logic_vector(3 downto0);beginprocess(clk)beginif(clk'eventand clk='1')thenifen='1' thenifcnt1="0101" and cnt0="1001" thenco<='1';cnt0<="0000";cnt1<="0000";elsifcnt0<"1001" thencnt0<=(cnt0+1);elsecnt0<="0000";cnt1<=cnt1+1;co<='0';endif;endif;endif;min1<=cnt1;min0<=cnt0;endprocess;end min;3.时计数是由二十四进制的计数器构成,生成元器件如下Clk:设置时间输入和分进位输入的或en:使能端h1[3..0] h0[3..0]:时的高位显示和低位显示代码如下:library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity hour isport(clk,en:in std_logic;h1,h0:out std_logic_vector(3 downto 0));end hour;architecture beha of hour issignal cnt1,cnt0:std_logic_vector(3 downto0);beginprocess(clk)beginif(clk'event and clk='1') thenif en='1' thenif cnt1="0010" andcnt0="0011" thencnt1<="0000";cnt0<="0000";elsif cnt0<"1001" thencnt0<=cnt0+1;elsecnt0<="0000";cnt1<=cnt1+1;end if;end if;end if;h1<=cnt1;h0<=cnt0;end process;end beha;。
多功能电子钟设计说明书
多功能电子钟设计说明
书
一.作品硬件原理图
三、程序流程图
Go_dianzhen();
dz_ms=0;
TR1 =1;
}
//TR1=1;
}
五.作品实物图
六.作品演示效果图
七.作品创新性描述
1我们将作品分成两大部分,即单片机基础系统与单片机应用系统,并将其制作成两块板,这样我们可以用这个单片机基础系统来做其他方面的开发。
2.我们采用的是液晶显示,这样比数码管看起来效果会更好。
3.我们通过程序将几大部分功能的器件整合在一起,可以同时运行。
4.采用串口来与计算机通信,因此我们程序可以通过串口下载在线烧写程序,另外还可以与计算机进行其他方面的通信,如将单片机中的容显示在计算机上。
完整51单片机多功能电子钟程序代码
完整51单片机多功能电子钟程序代码#include "regx52.h"#define uint8 unsigned charcode uint8 LED[] = {0xc0, 0xf9, 0xa4, 0xb0,0x99, 0x92, 0x82, 0xf8,0x80, 0x90, 0x88, 0x83,0xc6, 0xa1, 0x86, 0x8e}; unsigned char riqi[8]; static uint8 keyReleas;uint8 disptmp;uint8 pos;uint8 buffer[8];code uint8 BIT[8] = {0xfe, 0xfd, 0xfb, 0xf7,0xef, 0xdf, 0xbf, 0x7f}; uint8 flag2ms;uint8 t2msCnt;uint8 halfSecCnt;uint8 IsTimerRun;uint8 mode;void GlobalvarInit();void T0Init();void TimeRun();unsigned char KeyScan();void Command();void Command();void T0IntSvr() interrupt 1 using 1{TL0 = (65536-2000)%256;TH0 = (65536-2000)/256;switch(mode){case 0:disptmp=LED[buffer[pos]]; switch(pos){case 2:case 4:case 6:disptmp&=~(1<<7); break;}break;case 1:disptmp=LED[riqi[pos]]; switch(pos){case 2:case 4:disptmp&=~(1<<7); break;}break;}P2 = 0xff;P0 = disptmp;P2 = BIT[pos];pos++;if(pos>7)pos = 0;flag2ms = 1;}void main(){GlobalvarInit();T0Init();EA = 1;while(1){TimeRun();Command();}}void TimeRun(){if(IsTimerRun == 0) return; if(flag2ms != 1) return; flag2ms = 0;t2msCnt++;if(t2msCnt < 250) return; t2msCnt = 0; halfSecCnt++;if(halfSecCnt < 2) return; halfSecCnt = 0;buffer[0]++;if(buffer[0] < 10) return; buffer[0] = 0;buffer[1]++;if(buffer[1] < 6) return; buffer[1] = 0;buffer[2]++;if(buffer[2] < 10) return;buffer[2] = 0;buffer[3]++;if(buffer[3] < 6) return;buffer[3] = 0;buffer[4]++;switch(buffer[5]){case 0:case 1:if(buffer[4] < 10) return; buffer[4] = 0;break; case 2: if(buffer[4] < 4) return; buffer[4] = 0;break; }buffer[5]++;if(buffer[5] < 3) return;buffer[5] = 0;riqi[0]++;switch(riqi[1]){case 0:if( riqi[0] < 10) return; riqi[0] = 1;break; case 1: case 2:if( riqi[0] < 10) return; riqi[0] = 0;break; case 3:if( riqi[0] < 2) return; riqi[0] = 0;break;}riqi[1]++;if(riqi[1]<4) return;riqi[1]=0;riqi[2]++;switch(riqi[3]){case 0:if( riqi[2] < 10) return; riqi[2] = 1;break;case 1:if( riqi[2] < 3) return; riqi[2] = 0;break; }riqi[3]++;if(riqi[3]<1) return;riqi[3]=0;riqi[4]++;if(riqi[4]<10) return;riqi[4]=0;riqi[5]++;if(riqi[5]<10) return;riqi[5]=0;riqi[6]++;if(riqi[6]<10) return;riqi[6]=0;riqi[7]++;if(riqi[7]<10) return;riqi[7]=0;}void T0Init(){TMOD = TMOD & 0xf0 | 0x01;// 2msTL0 = (65536-2000)%256;TH0 = (65536-2000)/256;ET0 = 1;TR0 = 1;}// 全局变量初始化void GlobalvarInit(){uint8 i;for (i = 0; i < 8; i++){buffer[i] = 0;}pos = 1;flag2ms = 0;t2msCnt = 0; IsTimerRun = 1;mode = 0;riqi[0]= 0;riqi[1]= 2;riqi[2]=6;riqi[3]=0;riqi[4]=3;riqi[5]=1;riqi[6]=0;riqi[7]=2;}unsigned char KeyScan() { unsigned char key;P3 = 0x0f;if ((P3 & 0x0f) == 0x0f) {return 0;}P3 = 0x7f;key = P3;key = key & 0x0f;if(key != 0x0f){return (key | 0x70);}P3 = 0xbf;key = P3;key = key & 0x0f;if(key != 0x0f){return (key | 0xb0);}P3 = 0xdf;key = P3;key = key & 0x0f;if(key != 0x0f){return (key | 0xd0);}P3 = 0xef;key = P3;key = key & 0x0f;if(key != 0x0f){return (key | 0xe0);}return 0;}static uint8 keyRelease = 1; void Key_bd(){buffer[7] = 0x0b; buffer[6] = 0x0d; }void Key_be() {switch(mode) {case 0:mode = 1; break;case 1:mode = 0; break;}}void Key_0x77() {if(buffer[4]>3) {buffer[5]++;if(buffer[5]>1) buffer[5]=0;}else{buffer[5]++;if(buffer[5]>2) buffer[5]=0;}}void Key_0x7b(){switch(buffer[5]){case 0:case 1:buffer[4]++;if(buffer[4]>9)buffer[4]=0; break;case 2:buffer[4]++;if(buffer[4]>3)buffer[4]=0;break; }}void Key_0xee(){switch(riqi[1]){case 0: riqi[0]++;if(riqi[0]>9) riqi[0]=1;break;case 1:case 2:riqi[0]++;if(riqi[0]>9) riqi[0]=0;break;case 3:riqi[0]++;if(riqi[0]>1)riqi[0]=0;break;}}void Key_0xeb() {switch(riqi[3]){case 0:riqi[2]++;if(riqi[2]>9) riqi[2]=1; break;case 1:riqi[2]++;if(riqi[2]>2) riqi[2]=0; break;}}void Key_0xed() {if(riqi[0]<=1){riqi[1]++;if(riqi[1]>3)riqi[1]=0;}if(riqi[0]>1){riqi[1]++;if(riqi[1]>2)riqi[1]=0;}}void Key_0xe7(){if(riqi[2]>2)riqi[3]=0;else{riqi[3]++;if(riqi[3]>1)riqi[3]=0;}}void Command(){uint8 key;key = KeyScan();if (key == 0){keyRelease = 1;return;}if(keyRelease == 0)return;keyRelease = 0;switch(key){case 0xbe: Key_be(); break; // Key_8 case 0xbd: Key_bd(); break; // Key_9 case 0xbb: IsTimerRun = 1; break;// Key_A case 0xb7: IsTimerRun = 0; break;// Key_Bcase 0x77: Key_0x77();break;// Key_Fcase 0x7b: Key_0x7b();break;// Key_Ecase 0x7d: buffer[3]++;if(buffer[3]>5)buffer[3]=0;;break;// Key_D case 0x7e: buffer[2]++;if(buffer[2]>9)buffer[2]=0;;break;// Key_C case 0xde: riqi[4]++;if(riqi[4]>9)riqi[4]=0; break;// Key_4 case 0xdd: riqi[5]++;if(riqi[5]++>9)riqi[5]=0; break;// Key_5 case 0xdb: riqi[6]++;if(riqi[6]>9)riqi[6]=0; break;// Key_6case 0xd7: riqi[7]++;if(riqi[7]>9)riqi[7]=0; break;// Key_7case 0xee: Key_0xee(); break;// Key_0case 0xed: Key_0xed();break;// Key_1case 0xeb: Key_0xeb(); break;// Key_2case 0xe7: Key_0xe7();break;// Key_3}}。
51单片机电子时钟(C语言)
#include <reg51.h>#include <intrins.h>#define uchar unsigned char //宏定义#define uint unsigned intuchar sec,min,hour,sec50,jishu,dtp2; //sec、min、hour、为显示单元,sec50为60秒计数单元,jishu为扫描数码管计数单元uchar sec1,min1,hour1; //时间中介单元uchar nzmin,nzhour,nzjishu=0,dispjishu=0; //闹钟分、时定义uchar data nzbit=0; //闹钟标志位,闹钟默认为开启uchar data dispbit=0; //显示标志位,默认显示当前时间uchar data disp[8]; //秒、分、时个位与十位存放数组及‘—’uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //数字0-9sbit KEYmin=P3^2; //分加1按钮sbit KEYhour=P3^3; //时加1按钮sbit LABA=P1^0; //闹钟输出I/O口sbit NZdisplay=P3^4; //闹钟显示按钮,按住不放显示闹钟时间,放开则显示当前时间sbit KEYnzmin=P3^5; //闹钟分加1按钮sbit KEYnzhour=P3^6; //闹钟时加1按钮sbit KEYoff=P3^7; //关闭闹钟按钮,按第一次为关闭,第二次为开启void display(); //显示函数声明void keyscan(); //按键扫描函数声明void naozhong(); //闹钟判别函数声明void keynz(); //闹钟按键函数声明void main(){sec=0; //时间初始化为00—00—00min=0;hour=0;sec1=0; //显示单元初始化为00—00—00min1=0;hour1=0;nzmin=01; //闹钟初始时间为01:01nzhour=01;jishu=0;dtp2=0xfe;P0=0xff;TMOD=0x11; //设T0、T1为模式1IE=0x8a;TH0=0xb8; //T0定时20msTL0=0x0;TH1=0xfc; //T1定时1msTL1=0x66;TR0=1;TR1=1;while(1){display(); //调用显示子程序keyscan(); //调用按键子程序keynz(); //调用闹钟按键子程序}}void t0int() interrupt 1 //T0定时中断程序{TH0=0xb8;TL0=0x0;sec50++;if(sec50==50) //对20ms计数50次即1s{sec50=0; //清秒计数器,为下次做准备naozhong(); //调用闹钟判别子程序sec1++; //秒加1}if(sec1==60) //对秒计数60次即1min{sec1=0;min1++; //分加1}if(min1==60) //对分计数60次即1hour{min1=0;hour1++; //时加1}if(hour1==24){hour1=0;}if(dispbit==0) //判断显示标志位是否为0,为0显示当前时间{sec=sec1;min=min1;hour=hour1;}else //显示标志位为1,显示闹钟时间{min=nzmin; //将闹钟时间给显示单元hour=nzhour;}}void t1int() interrupt 3 //T1中断程序{TH1=0xfc;TL1=0x66;P2=0xff; //关闭所有数码管P2=dtp2;dtp2=_crol_(dtp2,1);P0=disp[jishu];jishu++;if(jishu==8) //扫描完8位数码管清0,重新从第一位开始扫描{jishu=0;}}void delay(uint x) //延时函数uchar i;while(x--){for(i=0;i<120;i++);}}void display() //显示子程序{disp[7]=table[sec%10]; //秒个位显示disp[6]=table[sec/10]; //秒十位显示disp[4]=table[min%10]; //分个位显示disp[3]=table[min/10]; //分十位显示disp[1]=table[hour%10]; //时个位显示disp[0]=table[hour/10]; //时十位显示disp[2]=0xbf; //显示‘_’disp[5]=0xbf;}void keyscan() //按键子程序{delay(20); //延时消抖if(KEYmin==0)//时间分加1按钮{min1++; //时间分加1if(min1==60){min1=0;hour1++; //分加到60对时加1}if(hour1==24){hour1=0;}while(!KEYmin); //等待按键放开}delay(20); //延时消抖if(KEYhour==0) //时间时加1按钮{hour1++; //时间时加1if(hour1==24){hour1=0;}while(!KEYhour); //等待按键放开}void naozhong() //闹钟判断{if(nzbit==0) //判断闹钟标志位,0为开启闹钟判断,1为关闭闹钟{if(min1==nzmin) //闹钟与时间分的判别if(hour1==nzhour) //闹钟与时间时的判别LABA=0; //时间分、时与闹钟分、时相等就打开蜂鸣器}elseLABA=1;}void keynz() //闹钟加、减及闹钟关闭、开启按键处理子程序{delay(20); //延时消抖if(KEYnzmin==0) //判别闹钟分加1按键{nzmin++; //闹钟分加1if(nzmin==60){nzmin=0;nzhour++; //闹钟分加到60对闹钟时加1if(nzhour==24)nzhour=0;}while(!KEYnzmin); //等待按键放开}delay(20); //延时消抖if(KEYnzhour==0) //判别闹钟时加1按键{nzhour++; //闹钟时加1if(nzhour==24)nzhour=0;while(!KEYnzhour); //等待按键放开}if(KEYoff==0)//判断关闭闹钟按键是否按下{delay(20); // 延时消抖nzjishu++;if(nzjishu==1) //判断是否为第一次按下{nzbit=1; //第一次按下关闭闹钟if(nzjishu==2) //判断是否为第二次按下{nzjishu=0; //第二次按下清计数单元nzbit=0; // 第二次按下开启闹钟判别}while(!KEYoff); //等待按键放开}if(NZdisplay==0) //判别显示切换闹钟按键是否按下{dispjishu++;if(dispjishu==1) //第一次按下显示闹钟时间{dispbit=1; //第一次按下,把标志位置1,显示切换为闹钟时间}if(dispjishu==2) //第二次按下显示为当前时间{dispjishu=0; //清零,重新计数dispbit=0; //第二次按下清零显示标志位,显示切换为当前时间}while(!NZdisplay); //等待按键放开}}。
LCD电子钟万年历程序代码
源程序代码XTAL218XTAL119ALE 30EA31PSEN 29RST9P0.0/AD039P0.1/AD138P0.2/AD237P0.3/AD336P0.4/AD435P0.5/AD534P0.6/AD633P0.7/AD732P1.01P1.12P1.23P1.34P1.45P1.56P1.67P1.78P3.0/RXD 10P3.1/TXD 11P3.2/INT012P3.3/INT113P3.4/T014P3.7/RD17P3.6/WR 16P3.5/T115P2.7/A1528P2.0/A821P2.1/A922P2.2/A1023P2.3/A1124P2.4/A1225P2.5/A1326P2.6/A1427U1AT89C51D 714D 613D 512D 411D 310D 29D 18D 07E 6R W 5R S 4V S S1V D D 2V E E3LCD1LM016LR11k R21k R31k R41k R51k R61k R71k R81kR91kQ12N5088R10100LS1SPEAKERR111kK1K2X1CRYSTALC1220pC2220p电气04-404010104赵化正/******************************************************************************* *;按K1键,依次进入闹钟功能,闹钟时间,年,月,日和时,分,秒模式,直致退出设置状态* ;按K2键,调整是否起用闹钟和调节闹钟时,分,秒,年,月,日,时间的时,分,秒的数字*;闹钟响时,按K2 即可停止闹钟的声响*;K2和K1需要配合使用;设置状态,LCD 上排最前面显示"P",下排最前面设置闹钟时显示"alarm:",其他显示"time:" * ;闹钟启用时,在LCD 下排中间显示一小喇叭,闹钟禁用时,无此小喇叭*;年代变化2000--2099,星期自动转换*;**************变量的定义*****************RS BIT P2.0 ;LCD 数据/命令选择端(H/L)RW BIT P2.1 ;LCD读/写选择端(H/L)EP BIT P2.2 ;LCD 使能控制PRE BIT P1.7 ;调整键(K1)ADJ BIT P1.6 ;调整键(K2)SPK BIT P3.7 ;闹钟声音输出口YEAR DATA 18H ;年,月,日变量MONTH DATA 19HDATE DATA 1AHWEEK DATA 1BHHOUR DATA 1CH ;时,分,秒,百分之一秒变量MIN DATA 1DHSEC DATA 1EHSEC100 DATA 1FHHOUR_ARM DA TA 20H ;闹钟时,分,秒,变量MIN_ARM DA TA 21HSEC_ARM DATA 22HSTA TE DATA 23HALARM BIT STA TE.0 ;闹钟是否启用标志1--启用,0--禁止LEAP BIT STATE.1 ;是否闰年标志1--闰年,0--平年KEY_S DATA 24H ;当前扫描键值KEY_V DATA 25H ;上次扫描键值DIS_BUF_U0 DATA 26H ;LCD 上排显示缓冲区DIS_BUF_U1 DATA 27HDIS_BUF_U2 DATA 28HDIS_BUF_U3 DATA 29HDIS_BUF_U4 DATA 2AHDIS_BUF_U5 DATA 2BHDIS_BUF_U6 DATA 2CHDIS_BUF_U7 DATA 2DHDIS_BUF_U8 DATA 2EHDIS_BUF_U9 DATA 2FHDIS_BUF_U10 DA TA 30HDIS_BUF_U11 DA TA 31HDIS_BUF_U12 DA TA 32HDIS_BUF_U13 DA TA 33HDIS_BUF_U14 DA TA 34HDIS_BUF_U15 DA TA 35HDIS_BUF_L0 DATA 36H ;LCD 下排显示缓冲区DIS_BUF_L1 DATA 37HDIS_BUF_L2 DATA 38HDIS_BUF_L3 DATA 39HDIS_BUF_L4 DATA 3AHDIS_BUF_L5 DATA 3BHDIS_BUF_L6 DATA 3CHDIS_BUF_L7 DATA 3DHDIS_BUF_L8 DATA 3EHDIS_BUF_L9 DATA 3FHDIS_BUF_L10 DATA 40HDIS_BUF_L11 DA TA 41HDIS_BUF_L12 DATA 42HDIS_BUF_L13 DATA 43HDIS_BUF_L14 DATA 44HDIS_BUF_L15 DATA 45HFLAG DATA 46H ;标识调整状态0-闹钟功能,1-闹钟时,2-闹钟分,3-闹钟秒;4-年,5-月,6-日,7-时,8-分,9-秒,10-退出调整。
电子时钟(python)
电子时钟代码如下:import turtle as tfrom datetime import datetimedef drawGap():t.penup()t.fd(5)def drawLine(draw):drawGap()t.pendown() if draw else t.penup()t.fd(40)drawGap()t.right(90)def drawDigit(digit):drawLine(True) if digit in [2, 3, 4, 5, 6, 8, 9] else drawLine(False)drawLine(True) if digit in [0, 1, 3, 4, 5, 6, 7, 8, 9] else drawLine(False)drawLine(True) if digit in [0, 2, 3, 5, 6, 8, 9] elsedrawLine(False)drawLine(True) if digit in [0, 2, 6, 8] else drawLine(False)t.left(90)drawLine(True) if digit in [0, 4, 5, 6, 8, 9] else drawLine(False) drawLine(True) if digit in [0, 2, 3, 5, 6, 7, 8, 9] elsedrawLine(False)drawLine(True) if digit in [0, 1, 2, 3, 4, 7, 8, 9] elsedrawLine(False)t.left(180)t.penup()t.fd(20)def drawDate(date):t.pencolor('red')for i in date:if i == '-':t.write('时', font=('Arial', 18, 'normal'))t.pencolor('green')t.fd(40)elif i == '=':t.write('分', font=('Arial', 18, 'normal'))t.pencolor('blue')t.fd(40)elif i == '+':t.write('秒', font=('Arial', 18, 'normal'))else:drawDigit(eval(i))def tick():t.clear()t.penup()t.color('red')t.goto(-150, 100)t.pendown()t.write('珍惜每一分善用每一秒', font=('Arial', 18, 'normal')) t.penup()t.home()t.fd(-300)# 获取当前时间drawDate(datetime.now().strftime("%H-%M=%S+"))t.hideturtle()# 定时器,一秒执行一次t.ontimer(tick, 1000)if __name__ == "__main__":t.title("电子时钟")# 设置窗口大小t.setup(800, 350, 200, 200)# 隐藏绘图过程t.tracer(False)t.pensize(8)tick()t.done()实现效果:。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code weixuan[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; //位选,控制哪个数码管亮。
(从右到左)
uchar
code
duanxuan[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf, 0xff}; //0-9,'-','灭'
uchar data timedata[3]={0x00,0x00,0x00}; //时间缓冲区,分别为:秒、时、分。
uchar data datetime[8]={0xc0,0xc0,0xbf,0xc0,0xc0,0xbf,0xc0,0xc0}; //时间缓冲区,初始化显示00-00-00。
uchar tt1,tt2,tt,tt0,num=0;
uchar flag,flag1,flag2,flash;
sbit key1=P1^3;
sbit key2=P1^5;
sbit key3=P1^7;
//**************延时函数*********************************
void delay(uint del)
{
uchar i, j;
for(i=0; i<del; i++)
for(j=0; j<=148; j++);
}
//********************调用显示************************
void display()
{
datetime[0]=timedata[0]%10; datetime[1]=timedata[0]/10; //秒
datetime[3]=timedata[1]%10; datetime[4]=timedata[1]/10; //分 datetime[6]=timedata[2]%10; datetime[7]=timedata[2]/10; //时 if(!flag)
{
P2=0X80; //秒显示
P0=duanxuan[datetime[0]];
delay(2);
P2=0X40;
P0=duanxuan[datetime[1]];
delay(2);
}
else
{
P2=0X80; //秒显示
P0=duanxuan[datetime[0]]|flash;
delay(2);
P2=0X40;
P0=duanxuan[datetime[1]]|flash;
delay(2);
}
P2=0X20; //显示'-'
P0=duanxuan[10];
delay(2);
if(!flag1)
{
P2=0X10; //分显示
P0=duanxuan[datetime[3]];
delay(2);
P2=0X08;
P0=duanxuan[datetime[4]];
delay(2);
}
else
{
P2=0X10; //分显示
P0=duanxuan[datetime[3]]|flash;
delay(2);
P2=0X08;
P0=duanxuan[datetime[4]]|flash;
delay(2);
}
P2=0X04; //显示'-' P0=duanxuan[10];
delay(2);
if(!flag2)
{
P2=0X02; //小时
P0=duanxuan[datetime[6]];
delay(2);
P2=0X01;
P0=duanxuan[datetime[7]];
delay(2);
}
else
{
P2=0X02; //小时
P0=duanxuan[datetime[6]]|flash;
delay(2);
P2=0X01;
P0=duanxuan[datetime[7]]|flash;
delay(2);
}
}
/******************按键调节时间*****************************/ void keyscan()
{
if(key1==0)
{
delay(10);
if(key1==0)
{
num++;
while(!key1);
while(1)
{
if(num==1)
{
flag=1;
flag1=0;
flag2=0;
if(key2==0)
{
delay(10);
if(key2==0)
{
timedata[0]--;
if(timedata[0]==-1)
timedata[0]=60;
while(!key2);
}
}
if(key3==0)
{
delay(10);
if(key3==0)
{
timedata[0]++;
if(timedata[0]==60)
timedata[0]=0;
while(!key3);
}
}
}
if(key1==0)
{
delay(10);
if(key1==0)
num++;
while(!key1);
}
if(num==2)
{
flag=0;
flag1=1;
flag2=0;
if(key2==0)
{
delay(10);
if(key2==0)
{
timedata[1]--;
if(timedata[1]==-1)
timedata[1]=60;
while(!key2);
}
}
if(key3==0)
{
delay(10);
if(key3==0)
{
timedata[1]++;
if(timedata[1]==60)
timedata[1]=0;
while(!key3);
}
}
if(num==3)
{
flag=0;
flag1=0;
flag2=1;
if(key2==0)
{
delay(10);
if(key2==0)
{
timedata[2]--;
if(timedata[2]==-1)
timedata[2]=24;
while(!key2);
}
}
if(key3==0)
{
delay(10);
if(key3==0)
{
timedata[2]++;
if(timedata[2]==25)
timedata[2]=0;
while(!key3);
}
}
if(num==4)
{
num=0;
flag=0;
flag1=0;
flag2=0;
break;
}
}
}
}
}
//主函数
void main()
{
TMOD=0x01;
ET0=1;
TR0=1;
TH0=0x40;
TL0=0x00;
EA=1;
while(1)
{
keyscan();
}
}
//***************定时器函数*************************** void timer1() interrupt 1
{
TH0=0x40; //50ms自加一次。
TL0=0x00;
P3=100;
display();
if((flag||flag1||flag2))
{
tt0++;
if(tt0==10)
{
flash=~flash;
tt0=0;
}
}
//P3=11
else
{
tt++;
if(tt==20)
{
tt=0;
timedata[0]++; //秒加1
if(timedata[0]==60)
{
timedata[0]=0;
timedata[1]++; //分加1
if(timedata[1]==60)
{
timedata[1]=0;
timedata[2]++; //时加1
if(timedata[2]==24)
{
timedata[2]=0 ;
}
}
}
}
}
}。