数字时钟C语言程序

合集下载

数字钟c语言程序

数字钟c语言程序
void timer0() interrupt 1
{
//TH0=0x4b;
// TL0=0x0ff;
s--;
if(s==0){
sec++;
s=20; //注意S的初始值
}
if(sec==60){
sec=0;
min++;
}
if(min==60){
min=0;
sec=0;
}
}
///**************************算法1******************************//
void calculate(void)
{//24小时制
time[0]=min/10;//十分
time[1]=min%10;//分
time[2]=sec/10;//十秒
time[3]=sec%10;//秒
}
void LED_display() //显示子程序
{
P0=0xfe; //P0.0=0,选通第一位
{
LED_display();//显示子程序
calculate();//算法
}
}
unchar time[4];
unsigned char min,sec;
unsigned int a=0;
unsigned char s=20;
unsigned char i,j,m,n,f,s;
void delay1ms() //1毫秒延时子程序
{
unsigned char i;
for(i=124;i>0;i--); //延时124*8+10=1002us
}
/*------------------------------------------------------------------------

51-数字时钟-89C52-单片机C语言程序

51-数字时钟-89C52-单片机C语言程序

51-数字时钟-89C52-单片机C语言程序数字时钟89C52 单片机C语言程序STC89C52|/************** 【数字时钟】****************//****【功能】1、时间显示2、秒表3、闹钟4、日期显示都可以设置****/#include /*包含器件配置文件*/#define uchar unsigned char /*宏定义字符型数据整型数据*/ #define uint unsigned intuchar code H[] = {0x0f, 0x07, 0x0b, 0x0d, 0x0e}; /*按键【P3】端口断码用于按键*/ char Code[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d,0x7d, 0x07, 0x7f, 0x6f}; /*显示【0 1 2 3 4 5 6 7 8 9】数字的数码管的段码*/ uchar code C[] = {0x0, 0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F};/*列扫描控制LED1位2位3位4位5位6位7位8位*/uchar MON[]={0,31,28,31,30,31,30,31,31,30,31,30,31};uchar A;uchar BIN=0; /* 【BIN】作为倒计时开始的标志*/uchar hour = 0; /* 定义[时][分][秒] */uchar min = 0;uchar sec = 0;uint shi=12;uint fen=30;uchar Mmin=0;uchar Msec=30;uchar M0=0;uchar m=0;uchar year=9;uchar month=7;uchar month2;uchar day=19;uchar set1 = 1; /* set1=1 是调节时分秒set1=2时时调节年月日set=3时事调节闹钟*/uchar set2 = 1; /* set2=1时是调节【时】位set2=2时事调节【分】位*/uint x = 0; /* x 每【0.01s】自加一*/void Delay(uint k);void Timer0() /*定时器T0 5mS初始化*/{TMOD = 0x01; /*设置*定时器0**计数方式一* (每12个时钟脉冲计数一次)或IT0=1 第二种计数方式*/TH0 = (65536 - 10000) / 256; /*给定计时器高位赋予初值=15536/256*/TL0 = (65536 - 10000) % 256; /*给定计时器低位赋予初值=15536%256 */ET0 = 1; /*打开定时器外部终断0允许ET1是中断器1的开关*/ EA = 1; /*打开总中断允许*/TR0 = 1;}void T0_srv(void) interrupt 1 /*5mS定时中断服务函数秒分时的值*/{TH0 = (65536 - 10000) / 256; /*给定计时器高位赋予初值=15536/256*/TL0 = (65536 - 10000) % 256; /*给定计时器低位赋予初值=15536%256 */x++;if(x==100){sec++;x=0; /*没每一秒【sec】加一*/if(sec==60){min++;sec=0;if(min==60) /*每一分钟【min】加一*/ {min = 0;hour++;if(hour==24){hour = 0; /*每一小时【hour】加一*/ day++;if(year%4==0) /*是否闰年的判断*/ MON[2]=29;else MON[2]=28;if(day==MON[month]){day=0;month++;}}}}}if(BIN==1)if(Mmin!=0||Msec!=0){M0 = 100-x;if(M0==100){ if(Mmin>0){Msec--;if(Msec==0){Msec=60;Mmin-- ;}if(Msec==0) {Mmin=0;Msec=0;M0=0;} }if(Mmin==0){if(Msec!=0)Msec--;if(Msec==0){Msec=0;M0=0;}}}}if(Mmin==0&&Msec==0){M0=0;}} /*秒表分秒M0的赋值*/}void Scan_key() /*按键扫描函数【Scan_key()】*/ {P3=H[0];if(P3!=H[0])Delay(1);if(P3==H[4]){set1++;if(set1>4) set1 = 1;er1: if(P3==H[4]) goto er1; /*按下键后未释放,在此等候*/ }Delay(1);if(P3==H[3]){set2++;if(set2>3) set2 = 1;er2: if(P3==H[3]) goto er2; /*按下键后未释放,在此等候*/ }Delay(1);if(P3==H[1]){BIN++;if(BIN>1) BIN=0;er3: if(P3==H[1]) goto er3; /*按下键后未释放,在此等候*/ }}}void Time_show() /*时间的显示变量为【时】【分】【秒】*/ { P0 = Code[hour/10]; /*第一位数字显示【时】的【十】位*/ P2 = C[1];Delay(1);P0 = Code[hour%10]; /*第二位数字显示【时】的【个】位*/P2 = C[2];Delay(1);if(x/50==0)P0 = 0x40; /*第三位符号【-】的显示*/elseP0 = 0x00;P2 = C[3];Delay(1);P0 = Code[min/10]; /*第四位的数字的显示【分】的【十】位*/ P2 = C[4];Delay(1);P0 = Code[min%10]; /*第五位的数字显示【分】的【个】位*/ P2 = C[5];Delay(1);Delay(1);if(x/50==0)P0 = 0x40; /*第六位符号【-】的显示*/elseP0 = 0x00;P2 = C[6];Delay(1);P0 = Code[sec/10]; /*第七位数字的显示【秒】的【十】位显示*/P2 = C[7];Delay(1);P0 = Code[sec%10]; /*第八位数字的显示【秒】的【个】位显示*/ P2 = C[8];Delay(1);}void Year_show(){P0 = Code[year/10]; /*第一位数字显示【年】的【十】位*/P2 = C[1];Delay(1);P0 = Code[year%10]; /*第二位数字显示【年】的【个】位*/P2 = C[2];Delay(1);P0 = 0x40; /*第三位符号【-】的显示*/P2 = C[3];Delay(1);P0 = Code[month/10]; /*第四位的数字的显示【月】的【十】位*/P2 = C[4];Delay(1);P0 = Code[month%10]; /*第五位的数字显示【月】的【个】位*/P2 = C[5];Delay(1);Delay(1);P0 = 0x40; /*第六位符号【-】的显示*/P2 = C[6];Delay(1);P0 = Code[day/10]; /*第七位数字的显示【天】的【十】位显示*/P2 = C[7];Delay(1);P0 = Code[day%10]; /*第八位数字的显示【天】的【个】位显示*/ P2 = C[8];Delay(1);}void Clock_show(){P0 = Code[shi/10]; /*第一位数字显示【时】的【十】位*/P2 = C[4];P0 = Code[shi%10]; /*第二位数字显示【时】的【个】位*/P2 = C[5];Delay(1);P0 = 0x40; /*第三位符号【-】的显示*/P2 = C[6];Delay(1);P0 = Code[fen/10]; /*第四位的数字的显示【分】的【十】位*/ P2 = C[7];Delay(1);P0 = Code[fen%10]; /*第五位的数字显示【分】的【个】位*/P2 = C[8];Delay(1);}void Miao_show() /*时间的显示变量为【时】【分】【秒】*/ { P0 = Code[Mmin/10]; /*第一位数字显示【时】的【十】位*/P2 = C[1];Delay(1);P0 = Code[Mmin%10]; /*第二位数字显示【时】的【个】位*/ P2 = C[2];Delay(1);P0 = 0x40; /*第三位符号【-】的显示*/P2 = C[3];Delay(1);P0 = Code[Msec/10]; /*第四位的数字的显示【分】的【十】位*/ P2 = C[4];Delay(1);P0 = Code[Msec%10]; /*第五位的数字显示【分】的【个】位*/ P2 = C[5];Delay(1);P0 = 0x40; /*第六位符号【-】的显示*/P2 = C[6];Delay(1);P0 = Code[M0/10]; /*第七位数字的显示【秒】的【十】位显示*/P2 = C[7];Delay(1);P0 = Code[M0%10]; /*第八位数字的显示【秒】的【个】位显示*/ P2 = C[8];Delay(1);}void Time_set() /*调整时间的函数time_set() */{Scan_key();if(set1==1){if(set2==1) /*当set1=1 set2=1时位时的设置*/{if(P3==H[2]) hour++; /*按一下第三行的键hour 就加一*/Delay(1);if(hour>24) hour=0;while(P3==H[2]); /*按键未释放,在此等候*/}if(set2==2) /*当set1=1 set2=1时位时的设置*/{if(P3==H[2]) min++; /*按一下第三行的键hour 就加一*/Delay(1);if(min>60){ min=0; hour++;}while(P3==H[2]); /*按键未释放,在此等候*/}}}void Year_set() /*调整时间的函数time_set() */{Scan_key();if(set1==2){if(set2==1) /*当set1=1 set2=1时位时的设置*/{if(P3==H[2]) year++; /*按一下第三行的键hour 就加一*/ Delay(1);if(year>100) year=0;while(P3==H[2]);}if(set2==2) /*当set1=1 set2=1时位时的设置*/{if(P3==H[2]) month++; /*按一下第三行的键hour 就加一*/ Delay(1);if(month>12){ month=0; year++;}while(P3==H[2]);}if(set2==3){if(P3==H[2]) day++; /*按一下第三行的键hour 就加一*/ Delay(1);if(day>MON[month]){ day=0; month++;}while(P3==H[2]); /*按键未释放,在此等候*/}}}Clock_set(){Scan_key();if(set1==3){if(set2==1) /*当set1=1 set2=1时位时的设置*/{if(P3==H[2]) shi++; /*按一下第三行的键hour 就加一*/ Delay(1);if(shi>24) shi=0;while(P3==H[2]); /*按键未释放,在此等候*/}if(set2==2) /*当set1=1 set2=1时位时的设置*/{if(P3==H[2]) fen++; /*按一下第三行的键hour 就加一*/ Delay(1);if(fen>60){ fen=0; shi++;}while(P3==H[2]); /*按键未释放,在此等候*/}}}void Miao_set() /*调整时间的函数time_set() */{Scan_key();if(set1==4){if(set2==1) /*当set1=1 set2=1时位时的设置*/{if(P3==H[2]) Mmin++; /*按一下第三行的键hour 就加一*/Delay(1);if(Mmin>24) Mmin=0;while(P3==H[2]); /*按键未释放,在此等候*/}if(set2==2) /*当set1=1 set2=1时位时的设置*/{if(P3==H[2]) Msec++; /*按一下第三行的键hour 就加一*/ Delay(1);。

电子钟显示时分秒c语言程序

电子钟显示时分秒c语言程序
second++;//秒数加1
if (second>=60)//是否达到60秒
{second=0;//秒数归零
minute++;//分数加1
if (minute>=60)//是否达到60分
{minute=0;//分数归零
hour++;//时数加1
if (hour == 13)//是否达到13小时
hour=1;//时数改为1
void write_inst(char);//写入指令函数
void write_char(char);//写入字元函数
void write_pat(void);//写入自建字型函数
void check_BF(void);//检查忙碌函数
void init_LCM(void);//声明LCM初始设定函数
write_inst(0x01);//清除显示幕(填0x20,I/D=1)
write_inst(0x06);//输入模式-位址递增-关显示幕
write_inst(0x0c);//显示功能-开显示幕-无游标-游标不闪
}
//LCD写入指令函数
void write_inst(char inst)
{
E=0;//写入指令至LCM
for (i=0;i<11;i++)
write_char(time[i]);//显示时间
}
}
//转换函数
void transfer(void)
{time[0]= hour/10 + 0x30;//时数之十位数显示资料
time[1]= hour%10 + 0x30;//时数之个位数显示资料

数字时钟C语言源程序

数字时钟C语言源程序
#include<reg52.h>//头文件
#define uchar unsigned char
#define uint unsigned int //宏定义
uchar count;
uint hour,minute,second;
uint hour1=0,minute1=0;
sbit dula=P2^6;
{
count=0;
second++;
if(second==60)
{
second=0;
minute++;
}
if(minute==60)
{
minute=0;
hour++;
}
if(hour==24)
hour=0;
}
}
second=0;
while(!key5);
}
if(key2==0)
{
delay(10);
if(key2==0)
if(hour>0)
hour--;
else
hour=23;
while(!key2);
}
if(key4==0)
{
delay(10);
while(!key6);
}
if(key7==0)//复位功能
{
delay(10);
if(key7==0)
{
hour=0;
minute=0;
second=0;
}
while(!key7);
}
}
void zhengdianbaoshi()//整点报时(从50秒开始)

C语言编写高精度 电子时钟

C语言编写高精度 电子时钟

#include<reg51.h>#include "public.h"#define uchar unsigned char#define uint unsigned int#define write_addr 0xa0#define read_addr 0xa1extern number_list[10]={0xed,0x28,0xb5,0xb9,0x78,0xd9,0xdd,0xa8,0xfd,0xf9}; extern number_list1[10]={0xef,0x2A,0xb7,0xbB,0x7A,0xdB,0xdF,0xaA,0xfF,0xfB}; extern x_list[12]={0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b}; void int_clock(void);void delay(ms);void ds_time();void ds_day();void set_time();uchar Sec;uchar Min;uchar Hour;uchar Week;uchar day;uchar month;uchar year;uchar Sec_H;uchar Sec_L;uchar Min_H;uchar Min_L;uchar Hour_H;uchar Hour_L;uchar timer_count;uchar dsplay_count;uchar key_week;uchar key_h;uchar key_m;uchar key_s;uchar set_flag;uchar ds_flag;void main (){uchar i,day_sum;timer_count=0;dsplay_count=0xAA;key_h=0;key_m=0;key_s=0;key_week=0;set_flag=0;ds_flag=0xBB;LCD_EN=1;TMOD=0X01;TCON=0x10;TH0=0X63;TL0=0XC0;EA=1;ET0=1;int_clock();while(1){ WDT_RST=0xA1;//时钟计算、进位if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12)) {day_sum=31;}if(month==2) //暂不区分闰年{day_sum=28;}if((month==4)||(month==6)||(month==9)||(month==11)){day_sum=30;}if((timer_count==10)&&(set_flag==0)){timer_count=0;Sec++;if(Sec==60){Sec=0;Min++;if(Min==60){Min=0;Hour++;if(Hour==24){Hour=0;Week++;day++;if(Week==8){Week=1;}if(day==day_sum){day=1;month++;if(month==13){month=1;year++;}}}}}}if(ds_flag==0xbb){ds_time();}if(ds_flag==0xaa){ds_day();}if (ds_flag==0xcc){for(i=0;i<=12;i++){EXT_ADR=x_list[i];EXT_DAT=0x00;}}set_time();}}void ds_time(void){EXT_ADR=0x1b; // 显示时间EXT_DAT=0x10;//时钟显示Sec_H=Sec/10;Sec_L=Sec%10;EXT_ADR=0x17;EXT_DAT=number_list[Sec_H];EXT_ADR=0x18;if(Sec_L%2==0){EXT_DAT=number_list1[Sec_L];}else{EXT_DAT=number_list[Sec_L];}Min_H=Min/10;Min_L=Min%10;EXT_ADR=0x15;EXT_DAT=number_list[Min_H];EXT_ADR=0x16;EXT_DAT=number_list[Min_L];Hour_H=Hour/10;Hour_L=Hour%10;EXT_ADR=0x13;EXT_DAT=number_list1[Hour_H];EXT_ADR=0x14;EXT_DAT=number_list1[Hour_L];EXT_ADR=0x11;EXT_DAT=number_list[Week];}void ds_day (void){EXT_ADR=0x1b; // 显示日期EXT_DAT=0x40;// 日期显示EXT_ADR=0x17;EXT_DAT=number_list[day/10];EXT_ADR=0x18;EXT_DAT=number_list1[day%10];//monthEXT_ADR=0x15;EXT_DAT=number_list[month/10];EXT_ADR=0x16;EXT_DAT=number_list[month%10];//yearEXT_ADR=0x11;EXT_DAT=number_list[2];EXT_ADR=0x12;EXT_DAT=number_list[0];EXT_ADR=0x13;EXT_DAT=number_list1[1];EXT_ADR=0x14;EXT_DAT=number_list1[1];}void set_time(){//set_sif(key_s==1){if(set_s==0)key_s=0;}else{ if(set_s==1){delay(20000);if(set_s==1){set_flag=1;key_s=1;Sec++;if(Sec==60){Sec=0;}}}}set_flag=0;//set_hif(key_h==1){if(set_h==0)key_h=0;}else{ if(set_h==1){delay(20000);if(set_h==1){set_flag=1;key_h=1;Hour++;if(Hour==24){Hour=0;}}}}set_flag=0;//set_mif(key_m==1){if(set_m==0)key_m=0;}else{ if(set_m==1){delay(20000);if(set_m==1){set_flag=1;key_m=1;Min++;if(Min==60){Min=0;}}}}set_flag=0;//set_weekif(key_week==1){if(set_week==0)key_week=0;}else{if (set_week==1){delay(20000);if(set_week==1){key_week=1;Week++;if(Week==8){Week=1;}}}}}void timer0_int(void) interrupt 1 //定时100ms {TMOD=0X01;TCON=0x10;TH0=0X63;TL0=0XC0;WDT_RST=0xA1;timer_count++;dsplay_count++;if(dsplay_count<30){ds_flag=0xaa;}if(((dsplay_count)>30)&&((dsplay_count)<32)) {ds_flag=0xcc;}if(((dsplay_count)>32)&&((dsplay_count)<60)){ds_flag=0xbb;}if(dsplay_count>60){dsplay_count=0;}}/*void I2C_sart(){SDA=1;SCL=1;delay();//延时4毫秒SDA=0;SCL=0;}void I2C_stop(){SDA=0;SCL=1;delay();//延时4毫秒SDA=1;SCL=0;}*/void int_clock(void){uchar i;Sec=0;Min=41;Hour=7;Week=2;day=23;month=3;for(i=0;i<4;i++){EXT_ADR=x_list[i+5];EXT_DAT=number_list[0];}EXT_ADR=x_list[3];EXT_DAT=number_list[1];EXT_ADR=0X14;EXT_DAT=0Xb7;EXT_ADR=0X18;EXT_DAT=0XEF;}void delay (ms){uint i;for(i=0;i<ms;i++){;;;;;;}}。

数字时钟程序(完整版)

数字时钟程序(完整版)

数字时钟程序/**************************************************程序名称:数字时钟程序全局变量:tt,shi,fen,miao等参数说明:无返回说明:无版本:1.0功能说明:通过数码实现时钟的显示,通过键盘实现时间的调整其中k1是开始调整,k2是增加,k3是减小,k5是实现定时屏幕的切换,k4是定时设置的开始。

带闹铃功能。

作者邮箱:****************(欢迎交流)**************************************************/#include<reg52.h>#define uint unsigned int#define uchar unsigned charsbit beep=P2^3;sbit dula=P2^6;sbit wela=P2^7;sbit k1=P3^0;sbit k2=P3^1;sbit k3=P3^2;sbit k4=P3^3;sbit k5=P3^4;sbit rd=P3^7;uchartt,shi_s,shi_g,shi1_s,shi1_g,fen1_s,fen1_g,miao1_s,miao1_g,fen_s,fen_g,miao_s,miao_g,k1num,k 4num,flag;char shi,fen,miao=20,shi1,fen1,miao1;/*********************************函数名称:延时程序设计全局变量:无参数说明:z传递给内部,是实现75*z条空指令延迟返回说明:无版本:1.0功能说明:约Z*75us延时程序设计作者邮箱:****************(欢迎交流)*********************************/void delay(uint z){uchar x;uint y;for(x=z;x>0;x--)for(y=75;y>0;y--);}/********八段数码管编码**********/uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};/*********************************函数名称:初始化函数全局变量:无参数说明:无返回说明:无版本:1.0功能说明:定时器装初值,开总中断,定时中断,并写定时中断函数作者邮箱:****************(欢迎交流)*********************************/void init(){TMOD=0x01;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;}void timer0() interrupt 1{TH0=(65536-50000)/256;TL0=(65536-50000)%256;tt++;if(tt==20){tt=0;miao++;if(miao==60){miao=0;fen++;if(fen==60){fen=0;shi++;if(shi==24){shi=0;}}}}}/*********************************函数名称:6个数码管显示函数全局变量:shi fen miao 的个十位分离参数说明:将个十位的分离分别传递到各个数码管中并显示返回说明:无版本:1.0功能说明:实现时钟的数码管显示,动态扫描。

数字钟51单片机程序c语言

数字钟51单片机程序c语言
F0:if(P3_4==0)
goto F0; //*按键未释放,在此等候*//
}
//*延时子函数*/
unsigned char i,j;
for(i=5;i>0;i--)
for(j=248;j>0;j--);
}
//*5mS定时中断服务 子函数*//
void zd(void) interrupt 1
{
TH0=-(4800//256);
TL0=-(4800%256);
deda++;
}
//*调整走时时间*//
{
t_dirve();
}
if(P3_7==0)
delay(1);
if(P3_7==0)
t_min++;
if(t_min==60)
{
if(deda<=100)
d_05s=0;
else d_05s=1;
if(deda>=200)
{sec++;deda=0;}
if(sec==60)
{min++;sec=0;}
if(min==60)
{hour++;min=0;}
if(hour==24)
{hour=0;}
p_out(); //*判别闹时到 子函数*// 否
}
//*定时器 T0 5mS初始化*//
void init_timer()
{
TMOD=0x01;
TH0=-(4800//256);
TL0=-(4800%256);
IE=0x82;

C语言绘图界面编写的数字时钟程序【原来C语言也可以这么炫】

C语言绘图界面编写的数字时钟程序【原来C语言也可以这么炫】

/*C语言绘图界面编写的数字时钟程序*/#include<graphics.h>#include<math.h>#include<dos.h>#define pi 3.1415926#define X(a,b) x=a*cos(b*pi/180)+300#define Y(a,b) y=a*sin(b*pi/180)+240void colorZZ(int Angel,int length,int color)/*绘制指针,Angel :指针角度,length :指针长度,color :指针颜色*/ {int x,y;Angel-=90;setcolor(color);line(300,240,X(length,Angel),Y(length,Angel));circle(X(length,Angel),Y(length,Angel),4);}void drawClock()/*绘制表盘*/{int i,l,x1,x2,y1,y2;int j=0,x,y;char s[10];setcolor(8);circle(300,240,200);circle(300,240,205);for(i=0,j=0;i<60;i++){if(j%5==0){if(j%90==0){setcolor(7);line(X(200,j),Y(200,j),X(170,j),Y(170,j));sprintf(s,"%2d",(j/90+1)*3);setcolor(7);settextstyle(0,0,2);outtextxy(X(150,j)-20,Y(150,j),s);}else{setcolor(7);line(X(200,j),Y(200,j),X(180,j),Y(180,j));}}else{setcolor(8);line(X(200,j),Y(200,j),X(190,j),Y(190,j));}j+=6;}}void init()/*时钟初始化*/{int driver,mode;driver=VGA;mode=VGAHI;initgraph(&driver,&mode,"");setbkcolor(WHITE);drawClock();}int lastsec=0;void changeTime()/*改变时钟显示时间*/{struct time tim;int x,y,i,j;char s[10];gettime(&tim);if(lastsec!=tim.ti_sec){colorZZ((tim.ti_hour-1)*30,120,WHITE);colorZZ((tim.ti_min-1)*6,140,WHITE);colorZZ((tim.ti_sec-1)*6,160,WHITE);setcolor(8);circle(300,240,5);sound(5000);delay(10000);nosound();lastsec=tim.ti_sec;colorZZ(tim.ti_hour*30,120,8);colorZZ(tim.ti_min*6,140,8);colorZZ(tim.ti_sec*6,160,8);}elsedelay(10000);}void clockTime()/*检查是否更改时钟显示时间*/{int i;while(!kbhit()){changeTime();for(i=0;i<4;i++)delay(10000);}closegraph();}int main()/*主程序*/{init();clockTime();}。

数字时钟c程序

数字时钟c程序

#include < reg52.h >//#include < intrins.h >#define uchar unsigned char#define uint unsigned intsbit dula=P2^6;sbit wela=P2^7;sbit rs=P3^5;sbit lcden=P3^4;sbit s1=P1^0;//功能键sbit s2=P1^1;//增加键sbit s3=P1^2;//减小键sbit s4=P1^3;//闹钟查看键sbit rd=P3^7;sbit beep=P2^3;//蜂鸣器sbit dscs=P1^4;sbit dsas=P1^5;sbit dsrw=P1^6;sbit dsds=P1^7;sbit dsirq=P3^3;bit flag1,flag_ri;//定义两个位变量uchar s1num,flag;//其它变量定义char miao,shi,fen,year,month,day,week,amiao,afen,ashi; uchar code table[]=" 20 - - ";//液晶固定显示内容uchar code table1[]=" : : ";void write_ds(uchar,uchar);//函数申明void set_alarm(uchar,uchar,uchar);void read_alarm(); uchar read_ds(uchar);void set_time();sbit DQ = P2^2 ; //定义DS18B20端口DQ//sbit BEEP=P2^4 ; //蜂鸣器驱动线bit presence ;//sbit LCD_RS = P3^5 ;//sbit LCD_RW = P2^0 ;//sbit LCD_EN = P3^4 ;/* uchar code cdis1[ ] = {" DS18B20 OK "} ;*/ uchar code cdis2[ ] = {" . C "} ;//uchar code cdis3[ ] = {" DS18B20 ERR0R "} ;//uchar code cdis4[ ] = {" PLEASE CHECK "} ;unsigned char data temp_data[2] = {0x00,0x00} ;unsigned char data display[5] = {0x00,0x00,0x00,0x00,0x00} ;unsigned char code ditab[16] = {0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09} ;//void beep() ;unsigned char code mytab[8] = {0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0x00} ;#define delayNOP() ; {_nop_() ;_nop_() ;_nop_() ;_nop_() ;} ;void write_com(uchar );void write_date(uchar );/*******************************************************************/ /*void delay1(int ms){unsigned char y ;while(ms--){for(y = 0 ; y<250 ; y++){_nop_() ;_nop_() ;_nop_() ;_nop_() ;}}}*//******************************************************************//*检查LCD忙状态*/ /*lcd_busy为1时,忙,等待。

单片机数字钟万年历(c语言)

单片机数字钟万年历(c语言)

#include <REGX51.H>#include <ABSACC.h>#define Font_code XBYTE[0xefff] //字符码写地址#define reg_code XBYTE[0xdfff] //字位码写地址#define s_1_con_key P3_2#define m_1_con_key P3_3#define h_1_con_key P3_4#define w_1_con_key P3_5#define p1 P1sbit BELL=P1^0;//sbit p10=P1^0;//sbit p20=P1^1;//sbit p30=P1^2;//sbit p40=P1^3;//sbit p50=P1^4;//sbit p60=P1^5;//sbit p70=P1^6;//sbit p80=P1^7;//字型码const unsigned char seg_design[11]={0x3F,/*0*/0x06,/*1*/0x5B,/*2*/0x4F,/*3*/0x66,/*4*/0x6D,/*5*/0x7D,/*6*/0x07,/*7*/0x7F,/*8*/0x6F,/*9*/0x40,/*-*/};//字位码const unsigned char bit_design[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};code unsigned char sszymmh[]={ 6,2,3, 5,2,1, 3,2,2, 5,2,2, 1,3,2, 6,2,1, 5,2,1,6,2,4, 3,2,2, 5,2,1, 6,2,1, 5,2,2, 3,2,2, 1,2,1,6,1,1, 5,2,1, 3,2,1, 2,2,4, 2,2,3, 3,2,1, 5,2,2,5,2,1, 6,2,1, 3,2,2, 2,2,2, 1,2,4, 5,2,3,3,2,1,2,2,1, 1,2,1, 6,1,1, 1,2,1, 5,1,6, 0,0,0} ;// 音阶频率表高八位code unsigned char FREQH[]={0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC,//1,2,3,4,5,6,7,8,i0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,} ;// 音阶频率表低八位code unsigned char FREQL[]={0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F,//1,2,3,4,5,6,7,8,i0xEE,0x44, 0x6B,0xB4,0xF4,0x2D,0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,};unsigned char bit_con;unsigned int ms_1_con; //毫秒计数器unsigned char s_1_con; //秒计数器unsigned char m_1_con; //分计数器unsigned char h_1_con=12;//时计数器unsigned char w_1_con=6; //周计时器unsigned char d_1_con=1;unsigned char mon_1_con=1;unsigned int y_1_con=2011;unsigned char s1; //秒计数器unsigned char m1; //分计数器unsigned char h1; //时计数器unsigned char Display_Buffer[8]; //显示缓冲区unsigned int k1=0;unsigned int k2=0;unsigned char timer0h,timer0l,time,led=1,j=0;unsigned char flagd=0;void delay_nms(unsigned int n);void Calculation_display(void);void time_set(void);void time_set1(void);void time_set2(void);void InitTimer0(void){TMOD = 0x01; // 方式1,16位计数器方式TH0 = 0xFC; //定时初值=65536-n n=1000,定时周期是1ms TL0 = 0x18;EA = 1; //开总中断使能ET0 = 1; //允许定时器T0溢出中断TR0 = 1; //T0 运行}void delay(unsigned char t){unsigned char t1;unsigned long t2;for(t1=0;t1<t;t1++){for(t2=0;t2<4000;t2++){;}}TR0=0;}void song(){TH0=timer0h;TL0=timer0l;TR0=1;delay(time);}void sing(){unsigned char k,i=0;time=1;while(time){if(k1==0) break;InitTimer0();Calculation_display();if(k2==0)time_set();else if(k2==1)time_set1();elsetime_set2();p1=~seg_design[8];k=sszymmh[i]+7*sszymmh[i+1]-1;timer0h=FREQH[k];timer0l=FREQL[k];time=sszymmh[i+2];i=i+3;song();}}void main(void){InitTimer0();while(1){Calculation_display();delay_nms(100);if(k2==0)time_set();else if(k2==1)time_set1();elsetime_set2();sing();k1=0;InitTimer0();}}void Timer0Interrupt(void) interrupt 1 using 1 {TH0 = 0xFC;TL0 = 0x18;ms_1_con++;//采用定时扫描方式bit_con++;if(bit_con > 7) bit_con = 0;reg_code = 0x00; //先消隐显示Font_code = Display_Buffer[bit_con];reg_code = bit_design[bit_con];if(k1==1){TR0=0;BELL=!BELL;TH0=timer0h;TL0=timer0l;TR0=1;}}void delay_nms(unsigned int n) //延时N ms{unsigned char a,b;unsigned int c;for(c=n;c>0;c--)for(b=142;b>0;b--)for(a=2;a>0;a--);}void Calculation_display(void){unsigned char day;if(ms_1_con > 999) //1000ms定时到{ms_1_con = 0;s_1_con++;if(s_1_con > 59){s_1_con = 0;m_1_con++;if(m_1_con > 59){m_1_con = 0;h_1_con++;k1=1;if(h_1_con > 23){h_1_con = 0;w_1_con++;if(w_1_con>7)w_1_con=1;switch(mon_1_con){case 1:case 3:case 5:case 7:case 8:case 10:case 12:day=31;break;case 2:day=28;break;case 4:case 6:case 9:case 11:day=30;break;}d_1_con++;if(d_1_con>day){mon_1_con++;d_1_con=1;if(mon_1_con>12){y_1_con++;mon_1_con=1;}}}}}}p1=~seg_design[w_1_con];if(k2==0){if(ms_1_con<500){Display_Buffer[2] = seg_design[10];Display_Buffer[5] = seg_design[10];}else{Display_Buffer[2] = !seg_design[10];Display_Buffer[5] = !seg_design[10];}Display_Buffer[6] = seg_design[s_1_con/10];//秒十位Display_Buffer[7] = seg_design[s_1_con%10]; //秒个位Display_Buffer[3] = seg_design[m_1_con/10]; //分十位Display_Buffer[4] = seg_design[m_1_con%10]; //分个位Display_Buffer[0] = seg_design[h_1_con/10]; //时十位Display_Buffer[1] = seg_design[h_1_con%10]; //时个位}if(k2==1){Display_Buffer[3] = seg_design[y_1_con%10]; //年个位Display_Buffer[2] = seg_design[(y_1_con/10)%10]; //年十位Display_Buffer[1] = seg_design[((y_1_con/100)%10)]; //年百位Display_Buffer[0] = seg_design[y_1_con/1000]; //年千位Display_Buffer[6] = seg_design[d_1_con/10]; //日十位Display_Buffer[7] = seg_design[d_1_con%10]; //日个位Display_Buffer[4] = seg_design[mon_1_con/10]; //月十位Display_Buffer[5] = seg_design[mon_1_con%10]; //月个位}if(k2==2){Display_Buffer[2] = seg_design[10];Display_Buffer[5] = seg_design[10];Display_Buffer[6] = seg_design[s1/10]; //秒十位Display_Buffer[7] = seg_design[s1%10]; //秒个位Display_Buffer[3] = seg_design[m1/10]; //分十位Display_Buffer[4] = seg_design[m1%10]; //分个位Display_Buffer[0] = seg_design[h1/10]; //时十位Display_Buffer[1] = seg_design[h1%10]; //时个位}if(s1==s_1_con&&m1==m_1_con&&h1==h_1_con)k1=1;}void time_set(void){unsigned char day;if(!s_1_con_key){s_1_con++;if(s_1_con > 59){s_1_con = 0;m_1_con++;if(m_1_con > 59){m_1_con = 0;h_1_con++;if(h_1_con > 23){h_1_con = 0;w_1_con++;if(w_1_con>7)w_1_con=1;d_1_con++;switch(mon_1_con){case 1:case 3:case 5:case 7:case 8:case 10:case 12:day=31;break;case 2:day=28;break;case 4:case 6:case 9:case 11:day=30;break;}if(d_1_con>day){mon_1_con++;d_1_con=1;if(mon_1_con>12){y_1_con++;mon_1_con=1;}}}}}}if(!m_1_con_key){m_1_con++;if(m_1_con > 59){m_1_con = 0;h_1_con++;if(h_1_con > 23){h_1_con = 0;w_1_con++;if(w_1_con>7)w_1_con=1;d_1_con++;switch(mon_1_con){case 1:case 3:case 5:case 7:case 8:case 10:case 12:day=31;break;case 2:day=28;break;case 4:case 6:case 9:case 11:day=30;break;}if(d_1_con>day){mon_1_con++;d_1_con=1;if(mon_1_con>12){y_1_con++;mon_1_con=1;}}}}}if(!h_1_con_key){h_1_con++;if(h_1_con > 23){h_1_con = 0;w_1_con++;if(w_1_con>7)w_1_con=1;d_1_con++;switch(mon_1_con){case 1:case 3:case 5:case 7:case 8:case 10:case 12:day=31;break;case 2:day=28;break;case 4:case 6:case 9:case 11:day=30;break;}if(d_1_con>day){mon_1_con++;d_1_con=1;if(mon_1_con>12){y_1_con++;mon_1_con=1;}}}}if(!w_1_con_key){k2=1;}}void time_set1(void){unsigned char day;switch(mon_1_con){case 1:case 3:case 5:case 7:case 8:case 10:case 12:day=31;break;case 2:day=28;break;case 4:case 6:case 9:case 11:day=30;break;}if(!s_1_con_key){d_1_con++;w_1_con++;if(w_1_con>7)w_1_con=1;if(d_1_con>day){mon_1_con++;d_1_con=1;if(mon_1_con>12){y_1_con++;mon_1_con=1;}}}if(!m_1_con_key){mon_1_con++;w_1_con=(w_1_con+day%7)%7;if(mon_1_con>12){y_1_con++;mon_1_con=1;}}if(!h_1_con_key){y_1_con++;if(y_1_con>9999){w_1_con=(w_1_con+365%7)%7;y_1_con=0;}}if(!w_1_con_key){k2=2;}}void time_set2(void){if(!s_1_con_key){s1++;if(s1>59)s1=0;}if(!m_1_con_key){m1++;if(m1>59)m1=0;}if(!h_1_con_key){h1++;if(h1>23)h1=0;}if(!w_1_con_key){k2=0;}}。

c语言数字时钟设计实例

c语言数字时钟设计实例

#include <reg51.h>#include <absacc.h>#define uchar unsigned char#define PA XBYTE[0x0fefc]#define PB XBYTE[0x0fefd]#define PC XBYTE[0x0fefe]#define CTL XBYTE[0x0feff]code unsigned char pan[]={0x0e7,10,0x0eb,3,0x0ed,2,0x0ee,1, //键盘0x0d7,11,0x0db,6,0x0dd,5,0x0de,4,0x0b7,12,0x0bb,9,0x0bd,8,0x0be,7,0x77,13,0x7b,14,0x7d,0,0x7e,15};unsigned char buf[]={0,0,0,0,0,0,0,0},now,old=0xff;code char led[]={0x0c0,0x0f9,0x0a4,0x0b0, 0x99,0x92,0x82,0x0f8,0x80,0x90,0x0bf, 0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x078,0x00,0x10,0x88,0x0c8,0x8e};//字段码0~9,-,0.~9.,n,fchar time[8]={0,0,0,0,0,0,0,0}; //用于放时间char date[8]={0,0,0,0,0,0,0,0}; //用于放日期int show_state;code int MAX=150;//用于设置时,无键按下的最多时间unsigned char clock_hour=6,clock_minute=30;//设置闹钟的时间char ms50,mx50,hour=5,minute=59,second=55;int year=2008;unsigned char month=10,day=1;// XXXXXXXXXXXXXXXX延时1毫秒XXXXXXXXXXXXXXXX //void delay(void){uchar m;for(m=0;m<150;m++);}//XXXXXXXXXXXXXXXX LED显示子程序1 XXXXXXXXXXXXXXXXX // void scan1(){unsigned char m,tc=0x80;int j;for(m=0;m<8;m++){PA=0x00;PB=tc; tc>>=1;for(j=0;j<1000;j++) delay();PB=0x00;for(j=0;j<100;j++) delay();}}//XXXXXXXXXXX LED显示XXXXXXXXXXX//void scan2(unsigned char x[],char a) {unsigned char m,tb,tc=0x80; for(m=0;m<8;m++){tb=x[m];PA=led[tb];if(m==a){PB=0x00;//灭}else{PB=tc;}//亮tc>>=1;delay();}}void time1() //时间存放数组{time[7]=second%10;time[6]=second/10;time[5]=10;time[4]=minute%10;time[3]=minute/10;time[2]=10;time[1]=hour%10;time[0]=hour/10;}void date1() //日期存放数组{date[7]=day%10;date[6]=day/10;date[5]=month%10+11;date[4]=month/10;date[3]=year%10+11;date[2]=year%100/10;date[1]=year%1000/100;date[0]=year/1000;}//XXXXXXXXXXXX 判断闰年程序XXXXXXXXXXXXXXXXXXX//char runnian(){if((year%400==0)||(year%4==0&&year%100!=0)){return 1;}return 0;}void timechange(void) interrupt 1 using 2 //定时器0中断程序{ int m;unsigned char xday;TF0=0;TH0=0x4B;TL0=0x0ff; //0.05sTR0=1;ms50++;if(ms50>=20)//50毫秒中断20次为1秒{{ms50=0;second++;}if(second>=60){second=0;minute++;}if(minute>=60){minute=0;hour++;}if(hour>=24){hour=0;day++;}if(month==1,3,5,7,8,10,12){xday=31;}if(month==4,6,9,11){xday=30;}if(month==2){if(runnian()==1){xday=29;} else {xday=28;}}//判断闰年if(day>xday){month++;day=1;}if(month>12){year++;month=1;}}//XXXXXXXXXXXXXXXXXXX计算星期几XXXXXXXXXXXXXXXXXXX// char week(){ unsigned char y,w;y=month;if(y==1||y==2) y=y+12;w=(day+1+2*y+3*(y+1)/5+year+year/4-year/100+year/400)%7;//基姆拉尔森计算公式return w;}//XXXXXXXXXXXXXXXXXXX显示星期几XXXXXXXXXXXXXXXXXXX// void show_week(){char n,w,key;show_state=0;w=week();for(n=0;n<8;n++)time[n]=w;while(1){show_state++;if(show_state==MAX) break;key=jian(time);if(key==13)break;scan(time);}}//闹钟响if(hour==clock_hour&&minute==clock_minute){ P3=P3&0x0fb;for(m=0;m<5;m++) delay();P3=P3|0x04;}}//XXXXXXXXXXXXXXXX键盘子程序XXXXXXXXXXXXXXXXXX// unsigned char jianpan(){unsigned char m,row,line,k_value;PC=0xf0;if(PC&0xf0==0xf0) return 0x0ff; //无键按下line=0x0FE;for (m=0;m<4;m++){ PC= line; //输出扫描信号row=PC; //读键盘口if ((row & 0xf0) != 0xf0)break;line=(line<<1)+1;//下一行}if (m==4) return 0xff;k_value = (row & 0xf0) | (line & 0x0f);//给各键赋键值for(m=0;m<32;m=m+2){if(pan[m]==k_value)return pan[m+1];}return 0x0ff;}unsigned char jian(unsigned char a[]){//读键盘5次,每次间隔10ms,判断键值是否有变,起到消抖动作用unsigned char jj[5]={0,0,0,0,0};char n;for(n=0;n<5;n++){ h[n]=jianpan(); scan(a);}if((h[0]==h[1])&&(h[1]==h[2])&&(h[2]==h[3])&&(h[3]==h[4])){now=jj[0];if(now!=old)//键值前后不等{old=now;return now;}return 0xff;}elsereturn 0xff;}//XXXXXXXXXXXXXXXXXXX设置时间XXXXXXXXXXXXXXXXXXX//void set_time(){unsigned char m=0,key=0xff,team=0,j;show_state=0;time[6]=10;//--用于区别时间设置与时间显示time[7]=10;while(1){show_state++;if(show_state==MAX) {team=1;break;}//5s无按键自动退出for(j=0;j<12;j++)scan2(time,m);key=jian(time);//获取键值if(key!=0x0ff){ show_state=0;if(key==13&&(time[0]*10+time[1])<24&&(time[3]*10+time[4])<60)//确定,退出break;else if(key==15) {team=1;break;}//取消,退出else if(key<=9){if(m==0&&key<=2) //控制时、分钟的个位和及十位输入的有效的键值{ time[m]=key;m++; }else if(m==1&&time[0]<2&&key<=9){ time[m]=key;m++; }else if(m==1&&key<=3){ time[m]=key;m++; }else if(m==3&&key<=5){ time[m]=key;m++; }else if(m==4){ time[m]=key;m++; }m=m%5;}}else if(m==2)m++;}if(team==0){hour=time[0]*10+time[1];minute=time[3]*10+time[4];}}//XXXXXXXXXXXXXXXXXXX设置日期XXXXXXXXXXXXXXXXXXX// void set_date(){unsigned char m=0,key,j,team=0;int y=year;show_state=0;date1();date[6]=0;date[7]=1;while(1){show_state++;if(show_state==MAX) {team=1;break;}//5s无按键自动退出for(j=0;j<12;j++)scan2(date,m);key=jian(date);if(key!=0x0ff){ show_state=0;if(key==13&&(date[4]*10+date[5]-11)<13&&(date[4]*10+date[5]-11)>0) //确认退出{break;}else if(key==15) {team=1;break;}{if(m==3) {date[m]=key+11;m++;} //年份的最后一位else if(m==4&&key<=1) {date[m]=key;m++;} //月份的高位只能为0或1else if(m==5){if(date[4]==1&&key<=2) {date[m]=key+11;m++;}//如果月份高位大于0,那月份的低位只能小于3else{date[m]=key+11;m++;}}else if(m==6&&key<=2&&date[4]==0&&date[5]==13) {date[m]=key;m++;}//如果月份是2月,天数的高位要小于3else if(m==6&&key<=3&&date[4]!=0&&date[5]!=13) {date[m]=key;m++;}//不是2月,天数的高位可以为3elseif(m==7&&key<=9&&date[6]==2&&date[4]==0&&date[5]==13)//二月且天数高位为2{year=date[0]*1000+date[1]*100+date[2]*10+date[3]-11;if(runnian()!=1&&key<=8)//不是闰年,低位只能0-8{date[m]=key;m++;}else if(runnian()==1)//是闰年,低位0-9{date[m]=key;m++;}year=y;}else if(m==7&&key<=9&&date[6]==2){date[m]=key;m++;} //不是二月且天数的高位为2,天数的低位0-8else if(m==7&&key<=1&&date[6]==3) //天数的高位为3{y=date[4]*10+date[5]-11;if((y==2||y==4,y==6||y==9||y==11)&&key==0){date[m]=key;m++;} //是2,4,6,9,11月,天数的低位只能为0,否则为1else{date[m]=key;m++;}}else if((m==0||m==1||m==2)&&key<=9) {date[m]=key;m++;} m=m%8;}}}if(team==0){year=date[0]*1000+date[1]*100+date[2]*10+date[3]-11;month=date[4]*10+date[5]-11;day=date[6]*10+date[7];}}//XXXXXXXXXXXXXX开机自检子程序XXXXXXXXXXXXXXXX //void zijian(){int m,j;scan1();//检查数码管p_jiance();for(j=0;j<3;j++){P3=P3&0x0fb;for(m=0;m<1000;m++) delay();P3=P3|0x04;for(m=0;m<1000;m++)delay();}}//XXXXXXXXXXXX LED显示子程序2 XXXXXXXXXXXX //void scan(char buf[]){unsigned char m,tb,tc=0x80;for(m=0;m<8;m++){tb=buf[m];PA=led[tb];PB=tc;tc>>=1;//右移一位delay();}}//XXXXXXXXXXXXXXXXXXX指示灯检测XXXXXXXXXXXXXXXXXXX// void p_jiance(){unsigned char team,m;int j;team=0x80;for(m=0;m<8;m++){P1=~team;team>>=1;for(j=0;j<100;j++)scan(time);}P1=0xff;}//XXXXXXXXXXXXXXXXXXX主程序XXXXXXXXXXXXXXXXXXX // void main(){uchar j,key=0;for(j=0;j<255;j++)delay();CTL=0x88; //设置8255工作方式zijian(); //开机自检TMOD=0x01;TH0=0x4B;TL0=0x0ff; //0.05sTR0=1;IE=0x82;ms50=0;//这两个为50毫秒计数mx50=0;for(j=0;j<255;j++)scan(time);while(1){if(key==10)//时间、日期显示切换{if(mx50>=100)mx50=0;else mx50=101;}else if(key==11)set_time();//时间设置else if(key==12)set_date();//日期设置show_week();//显示星期if(mx50<100)//5秒时间显示{time1();key=jian(time);}else //5秒日期显示{date1();key=jian(date);if(mx50>=200)//重新计数mx50=0;}}}。

c语言单片机数字钟程序

c语言单片机数字钟程序

#include<reg52.h>sbit ksec=P3^0; //定义端口,秒的调整按键对应p3.0sbit kmin=P3^1; //分的调整按键对应p3.1sbit khour=P3^2; //时的调整按键对应p3.2unsigned char secshi=0,secge=0,minshi=0,minge=0,hourshi=0,hourge=0;unsigned int num=0,sec=0,min=0,hour=0;unsigned char code table[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//定义数码管显示的0-9的代码void delay(unsigned int z){ //延时程序unsigned int x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);//如果是12MHz晶振,就是110us对应z=1}void keyscan();void display();void main(){TMOD=0x01;//T0定时器,模式1定时,13位定时TH0=(65536-50000)/256; //定时1S的数据设置,定时50000个机器周期后中断一次TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;//开启总中断,允许T0中断,开启T0中断while(1){display();//显示keyscan();//读取按键}}void time0() interrupt 1{num++;//T0中断服务程序,定时中断一次后num加一TH0=(65536-50000)/256;TL0=(65536-50000)%256;//重装计数值,因为模式1是非自动重装的}void display(){if(num==20){num=0; //定时20次后sec加一,那应该是6MHz的晶振,机器周期为2us,50000*2us*20=1ssec++;if(sec==60){sec=0;min++;//到60s,min加一,sec清零if(min==60){min=0;hour++;//到60分,hour加一,分清零if(hour==24){hour=0;min=0;//到24小时则所有时间清零,新的一天从零时开始计时sec=0;}}}}secge=sec%10;//秒的个位,秒除以10的余数secshi=sec/10;//十位,秒整除10的商minge=min%10;minshi=min/10;hourge=hour%10;hourshi=hour/10;P2=0xfe;//1111 1110,0选通一位数码管,P0=table[secge];//送秒的各位显示delay(5);//延时一会儿P2=0xfd;//选通下一位数码管,1111 1101P0=table[secshi];//送秒的十位显示delay(5);P2=0xfb;//1111 1011,选通再下一位P0=0x40;//显示分与秒之间的那个点delay(5);P2=0xf7;//1111 0111,再下一位P0=table[minge];//分的个位delay(5);P2=0xef;//1110 1111P0=table[minshi];//分的十位delay(5);P2=0xdf;//1101 1111P0=0x40;//分与时间的点delay(5);P2=0xbf;//1011 1111P0=table[hourge];//小时的个位delay(5);P2=0x7f;//0111 1111P0=table[hourshi];//小时的十位delay(5);}void keyscan(){if(ksec==0){delay(10);//延时防抖动if(ksec==0)//这里应该按键上接地的,有按键按下就是0{sec++;//有秒对应的按键按下,则秒加一if(sec>=60)//秒数到60则清零sec=0;}while(ksec==0)//等待按键释放,display();}//不做别的操作,就一直显示,等待按键释放,防止一次按键多次操作if(kmin==0){delay(10);//分对应的按键if(kmin==0){min++;if(min>=60)min=0;}while(kmin==0)display();}if(khour==0){delay(10);//小时对应的按键if(khour==0){hour++;if(hour>=24)hour=0;}while(khour==0)display();}}。

数字时钟C程序及仿真

数字时钟C程序及仿真

数字时钟C程序及仿真-CAL-FENGHAI-(2020YEAR-YICAI)_JINGBIAN**************************************/#include <>unsigned char code SEG7[12]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xbf,0xff,};unsigned short int tcount;//sbit set=P1^0;//sbit up=P1^1;//sbit down=P1^2;unsigned charDisSecondL,DisSecondH,DisMinutL,DisMinutH,DisHourL,DisHourH;//结构体变量struct time{unsigned char second;unsigned char minute;unsigned char hour;}time1;//延时子程序void delay(k)unsigned int k;{unsigned char j;while((k--)!=0)for(j=0;j<125;j++);}//时钟调整子程序TimeUpDown(n,x,y)unsigned char n,x,y;{if(up==0){delay(50); //消除抖动if(up==0){n++;if(n==x)//上限越界显示为0n=0;while(up==0);}}if(down==0){delay(50);if(down==0){if(n==0)//下限越界显示为最大可显示n=y;elsen--;while(down==0);}}return n;//返回调整后的值} *///显示子程序void display(DisSecondL,DisSecondH,DisMinutL,DisMinutH,DisHourL,DisHourH) {// 日日日日日日// 1 2 3 4 5 6// hH hL mH mL sH sL// 01 02 04 08 10 20P2=0x00;P0=SEG7[DisSecondL]; //显示秒钟的个位P2=0x20;delay(3);P2=0x00;P0=SEG7[DisSecondH]; //显示秒钟的十位P2=0x10;delay(3);P2=0x00;P0=SEG7[DisMinutL]; //显示分钟的个位P2=0x08;delay(3);P2=0x00;P0=SEG7[DisMinutH]; //显示分钟的十位P2=0x04;delay(3);P2=0x00;P0=SEG7[DisHourL]; //显示小时的个位P2=0x02;delay(3);P2=0x00;if(DisHourH==0){;}else{P0=SEG7[DisHourH]; //显示小时的十位P2=0x01;delay(3);}}void main(){//unsigned char Flag;=40;=03;=05;TMOD=0x01;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;while(1){display(DisSecondL,DisSecondH,DisMinutL,DisMinutH,DisHourL,DisHourH);if(set == 0) //设置标志位 Flag{delay(50);if(set == 0){TR0 = 0;Flag++;if(Flag > 3) //0:显示时间,1:小时调整,2:分钟调整,3:秒调整Flag = 0;}while(set == 0);}switch(Flag){case 0:TR0=1;display%10,10,%10,10,%10,10);break;case 1: //时钟小时调整TR0=0;=TimeUpDown,24,23);display(10,10,10,10,%10,10);break;case 2: //时钟分钟调整=TimeUpDown,60,59);display(10,10,%10,10,10,10);break;case 3: //时钟秒调整=TimeUpDown,60,59);display%10,10,10,10,10,10);break;//default:// break;}}}void t0(void) interrupt 1{TH0=(65536-50000)/256; TL0=(65536-50000)%256; tcount++;if(tcount==20){tcount=0;++;}if >=60){=0;++;}if >=60){=0;++;}if>=12){=1;}DisSecondL=%10;DisSecondH=10;DisMinutL=%10;DisMinutH=10;DisHourL=%10;DisHourH=10;}。

基于C51单片机的数字时钟课程设计(C语言带闹钟)

基于C51单片机的数字时钟课程设计(C语言带闹钟)

单片机技术课程设计数字电子钟学院:班级:姓名:学号:教师:摘要电子钟在生活中应用非常广泛,而一种简单方便的数字电子钟则更能受到人们的欢迎。

所以设计一个简易数字电子钟很有必要。

本电子钟采用AT89C52单片机为核心,使用12MHz 晶振与单片机AT89C52 相连接,通过软件编程的方法实现以24小时为一个周期,同时8位7段LED数码管(两个四位一体数码管)显示小时、分钟和秒的要求,并在计时过程中具有定时功能,当时间到达提前定好的时间进行蜂鸣报时。

该电子钟设有四个按键KEY1、KEY2、KEY3、KEY4和KEY5键,进行相应的操作就可实现校时、定时、复位功能。

具有时间显示、整点报时、校正等功能。

走时准确、显示直观、运行稳定等优点。

具有极高的推广应用价值。

关键词:电子钟 AT89C52 硬件设计软件设计目录NO TABLE OF CONTENTS ENTRIES FOUND.一、数字电子钟设计任务、功能要求说明及方案介绍1.1 设计课题设计任务设计一个具有特定功能的电子钟。

具有时间显示,并有时间设定,时间调整功能。

1.2 设计课题的功能要求说明设计一个具有特定功能的电子钟。

该电子钟上电或按键复位后能自动显示系统提示符“d.1004-22”,进入时钟准备状态;第一次按电子钟启动/调整键,电子钟从12时59分0秒开始运行,进入时钟运行状态;按电子钟S5键,则电子钟进入时钟调整状态,此时可利用各调整键调整时间,调整结束后可按S5键再次进入时钟运行状态。

1.3 设计课的设计总体方案介绍及工作原理说明本电子钟主要由单片机、键盘、显示接口电路和复位电路构成,设计课题的总体方案如图1所示:图1-1总体设计方案图本电子钟的所有的软件、参数均存放在AT89C52的Flash ROM和内部RAM 中,减少了芯片的使用数量简化了整体电路也降低了整机的工作电流。

键盘采用动态扫描方式。

利用单片机定时器及计数器产生定时效果通过编程形成数字钟效果,再利用数码管动态扫描显示单片机内部处理的数据,同时通过端口读入当前外部控制状态来改变程序的不同状态,实现不同功能。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
delay(10); if(key7==0)
{ hour=0; minute=0;
second=0; }
while(!key7); }
} void zhengdianbaoshi()//整点报时(从 50 秒 开始) {
if(hour==hour&&minute==59&&second>5 0)
{BEEP=!BEEP; }
if(second==60) { second=0; minute++; }
if(minute==60) { minute=0;
hour++; }
if(hour==24)
hour=0;
}
} void display() {
P0=0xff;//消影 wela=1; //位打开 P0=wei[0];//显示第一位 wela=0;//锁存 dula=1;//段打开 P0=duan[hour/10];//段赋值 dula=0;//段锁存 delay(10);
7f}; //扫描
void delay(uint z)//延时函数
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void init()//初始化中断
{
TMOD=0x10; //定时器 1 以方式 1 定时
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
if(second<59) second++;
else second=0;
while(!key5);
} if(key2==0)
{ delay(10); if(key2==0)
if(hour>0) hour--;
else hour=23;
while(!key2); } if(key4==0) {
delay(10); if(key4==0)
P0=0xff; wela=1; P0=wei[7]; wela=0; dula=1;
P0=duan[second%10]; dula=0; delay(10); } void key_scan()//按键+和{ if(key1==0) { delay(10); if(key1==0)
if(hour<23) hour++;
P0=0xff; wela=1; P0=wei[1]; wela=0; dula=1; P0=duan[hour%10]; dula=0; delay(10);
P0=0xff; wela=1; P0=wei[2]; wela=0; dula=1; P0=0x40; dula=0; delay(10);
P0=0xff; wela=1; P0=wei[5];
wela=0; dula=1; P0=0x40; dula=0; delay(10);
P0=0xff; wela=1; P0=wei[6]; wela=0; dula=1; P0=duan[second/10]; dula=0; delay(10);
else hour=0;
while(!key1); } if(key3==0) {
delay(10); if(key3==0)
if(minute<59) minute++;
else minute=0;
while(!key3); } if(key5==0) {
delay(10); if(key5==0)
sbit key6=P3^5;
sbit key7=P3^6;
sbit BEEP=P1^2;
uchar
code
duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x40};//0-9,横杆Fra bibliotekuchar
code
wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x
} void naozhong()//闹钟七点半,就响 30 秒 {
if(hour==7&&minute==30&&second<30) BEEP=!BEEP;
}
void main()//主函数 {
init(); while(1) {
key_scan(); display(); naozhong(); zhengdianbaoshi(); }
}
本人为单片机初学者,这是本人制作的 一个简单的数字时钟程序,程序比较繁琐, 只适合初学者学习参考使用,因为程序都是 一步步来的,没有跳跃。只要将此程序复制 到 keil,然后编译生成 hex 文件,连到单片 机(实物)就可以直接显示了。希望对有些 人有帮助
#include<reg52.h>//头文件 #define uchar unsigned char #define uint unsigned int//宏定义 uchar count; uint hour,minute,second; uint hour1=0,minute1=0; sbit dula=P2^6; sbit wela=P2^7; sbit key1=P3^0; sbit key2=P3^1; sbit key3=P3^2; sbit key4=P3^3; sbit key5=P3^4;
P0=0xff;
wela=1; P0=wei[3]; wela=0; dula=1; P0=duan[minute/10]; dula=0; delay(10);
P0=0xff; wela=1; P0=wei[4]; wela=0; dula=1; P0=duan[minute%10]; dula=0; delay(10);
if(minute>0) minute--;
else minute=59;
while(!key4);
} if(key6==0) {
delay(10); if(key6==0)
if(second>0) second--;
else second=59;
while(!key6);
} if(key7==0)//复位功能 {
EA=1;//总中断打开
ET1=1;//打开定时中断
TR1=1;//打开定时器 } void time1() interrupt 3 {
TH1=(65536-50000)/256; TL1=(65536-50000)%256; count++;
if(count==20)//1 秒累加一次 { count=0; second++;
相关文档
最新文档