时钟代码及注释

合集下载

数字时钟正确代码

数字时钟正确代码

module clock(CLK,RST,EN,S1,S2,spk,HOURH,HOURL,MINH,MINL,SECH,SECL); input CLK,RST,EN,S1,S2;output spk;output[3:0] HOURH,HOURL,MINH,MINL,SECH,SECL;reg spk;reg[3:0] SECL,SECH,MINL,MINH,HOURL,HOURH;always @(posedge CLK or negedge RST)if(!RST) beginSECL<=0;SECH<=0;MINL<=0;MINH<=0;HOURL<=0;HOURH<=0; end//系统复位else if(EN) //EN为低电平时时钟暂停beginif(!S1) //调节小时beginif(HOURL==9) begin HOURL<=0; HOURH<=HOURH+1; endelsebeginif(HOURH==2&&HOURL==3) begin HOURL<=0;HOURH<=0; endelse HOURL<=HOURL+1;endendelse if(!S2) //调节分钟beginif(MINL==9)beginMINL<=0;if(MINH==5) MINH<=0;else MINH<=MINH+1;endelse MINL<=MINL+1;endelse if(SECL==9) //时钟正常跳动状态 beginSECL<=0;if(SECH==5)beginSECH<=0;if(MINL==9)beginMINL<=0;if(MINH==5)beginMINH<=0;if(HOURL==9) beginHOURL<=0;HOURH<=HOURH+1;endelse if(HOURH==2&&HOURL==3) begin HOURL<=0; HOURH<=0;endelse HOURL<=HOURL+1;endelse MINH<=MINH+1;endelse MINL<=MINL+1;endelse SECH<=SECH+1;endelse SECL<=SECL+1;endelsebeginHOURH<=HOURH;HOURL<=HOURL;MINH<=MINH;MINL<=MINL;SECH<=SECH;SECL<=SECL;endalways @(posedge CLK) //产生报警声音模块beginif(MINH==5&&MINL==9&&SECH==5) begin spk<=CLK;end else if(MINH==0&&MINL==0&&SECH==0) spk<=CLK;else spk<=0;endendmodule各输入、输出信号引脚说明:CLK:时钟信号RST:系统复位信号,低电平有效。

51单片机时钟代码(带秒表闹钟功能).

51单片机时钟代码(带秒表闹钟功能).
write_com(0x80+0x00+7);
}
}
}
if(s6==0)
{
delay(5);
if(s6==0)
{
while(!s6);
di();
if(s4num==1)
{
miao--;
if(miao<0)
miao=59;
write_alarm(10,miao);
write_com(0x80+0x00+13);
ep=0;
}
voidwrite_data(uchardat) //写入字符显示数据到LCD
{
while(lcd_bz()); //等待LCD空闲
rs=1;
rw=0;
ep=0;
P0=dat;
_nop_();
_nop_();
_nop_();
_nop_();
ep=1;
_nop_();
_nop_();
_nop_();
}
if(s1num==3)
{
hour++;
if(hour==24)
hour=0;
write_time(4,hour);
write_com(0x80+0x40+7);delay(5);
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
di();
if(s1num==1)
批注本地保存成功开通会员云端永久保存去开通
#include <reg51.h>
#include <intrins.h>

单片机C语言电子钟显示年月日星期及时间(带注释)

单片机C语言电子钟显示年月日星期及时间(带注释)

#include<reg52.h>#define uchar unsigned char#define uint unsigned intuchar code table[]=" 2011-12-30 SA T";uchar code table1[]=" 23:59:55 ";uchar code week1[][3]={"MON","TUE","WEN","THU","FRI","SA T","SUN"}; /*uchar code week2[]="TUE";uchar code week3[]="WEN";uchar code week4[]="THU";uchar code week5[]="FRI";uchar code week6[]="SA T";uchar code week7[]="SUN"; */uchar count,shi,fen,miao,dnum,year,month,day,week,a=5;sbit rs=P2^0; //数据命令sbit rw=P2^1; //读写sbit en=P2^2; //使能sbit d1=P3^1; //独立键盘sbit d2=P3^2;sbit d3=P3^3;sbit d4=P3^4;void delay(uchar z) //延时{uint x,y;for(x=110;x>0;x--)for(y=z;z>0;z--);}void write_com(uchar com) //写指令{rs=0;rw=0;en=0;P0=com;delay(5);en=1;delay(5);en=0;}void write_date(uchar date) //写数据{rs=1;rw=0;en=0;P0=date;delay(5);en=1;delay(5);en=0;}void write_weekday (uchar a){uchar x;write_com(0x80+13);for(x=0;x<3;x++){write_date(week1[a][x]);}}void weekday(uint i){write_weekday(i%7);}void write_sfm(uchar add,uchar date){uchar shi,ge;shi=date/10;ge=date%10;write_com(0x80+0x40+add); //指向要写的地址write_date(0x30+shi); //0x30代表的数字是0,+shi表示shi的数字write_date(0x30+ge); //ge的数字}void write_ymd(uchar add,uchar date){uchar shi,ge;shi=date/10;ge=date%10;write_com(0x80+add); //指向要写的地址write_date(0x30+shi); //0x30代表的数字是0,+shi表示shi的数字write_date(0x30+ge); //ge的数字}void unit(){uchar num;en=0;write_com(0x38); //显示write_com(0x0c); //光标不显示write_com(0x06); //指针加1,整屏不移动write_com(0x01); //清屏write_com(0x80); //指针指向0x80for(num=0;num<16;num++) //第一行显示table数组{write_date(table[num]);delay(20);}write_com(0x80+0x40);for(num=0;num<16;num++) //第二行显示table1数组{write_date(table1[num]);delay(20);}TMOD=0X01;TH0=(65536-50000)/256; //开启中断TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;shi=23;fen=59;miao=55;year=11;month=12;day=30;}void jishu() //计数{if(count==20){count=0;miao++; //秒加if(miao==60){miao=0;fen++; //分加if(fen==60){fen=0;shi++;if(shi==24){shi=0;day++;a++;weekday(a);//时加}write_sfm(3,shi);write_ymd(9,day);if(day==31){day=0x01;write_ymd(9,day);month++;write_ymd(6,month);if(month==13){month=0x01;write_ymd(6,month);year++;write_ymd(3,year);}}}write_sfm(6,fen);}write_sfm(9,miao);}}void change(){d4=0;if(d1==0) //移光标{delay(10);if(d1==0){while(!d1);dnum++;if(dnum==1){TR0=0;write_com(0x0f);write_com(0x80+0x40+10);}if(dnum==2){write_com(0x80+0x40+7);}if(dnum==3){write_com(0x80+0x40+4);}if(dnum==4){write_com(0x80+10);}if(dnum==5){write_com(0x80+7);}if(dnum==6){write_com(0x80+4);}if(dnum==7){dnum=0;TR0=1;write_com(0x0c);}}}if(d2==0) //加数据{delay(10);if(d2==0){while(!d2);if(dnum==1){miao++;if(miao==60)miao=0;write_sfm(9,miao); //顺序不能换write_com(0x80+0x40+10); //顺序不能换}if(dnum==2){fen++;if(fen==60)fen=0;write_sfm(6,fen);write_com(0x80+0x40+7);}if(dnum==3){shi++;if(shi==24){shi=0;}write_sfm(3,shi);write_com(0x80+0x40+4);}if(dnum==4){day++;a++;weekday(a);if(day==31)day=1;write_ymd(9,day);write_com(0x80+10);}if(dnum==5){month++;if(month==13)month=1;write_ymd(6,month);write_com(0x80+7);}if(dnum==6){year++;write_ymd(3,year%100);write_com(0x80+4);}}}if(d3==0) //减数据{delay(10);if(d3==0){while(!d3);if(dnum==1){miao--;if(miao==-1)miao=59;write_sfm(9,miao);write_com(0x80+0x40+10);}if(dnum==2){fen--;if(fen==-1)fen=59;write_sfm(6,fen);write_com(0x80+0x40+7);}if(dnum==3){shi--;if(shi==-1)shi=23;write_sfm(3,shi);write_com(0x80+0x40+4);}if(dnum==4){day--;a--;weekday(a);if(day==0)day=30;write_ymd(9,day);write_com(0x80+10);}if(dnum==5){month--;if(month==0)month=12;write_ymd(6,month);write_com(0x80+7);}if(dnum==6){year--;write_ymd(3,year%100);write_com(0x80+4);}}}}void main(){unit();while(1){jishu();change();}}void timer0()interrupt 1{TH0=(65536-50000)/256;TL0=(65536-50000)%256;count++;}#include<reg52.h>#define uchar unsigned char //宏定义#define uint unsigned int //宏定义sbit rs=P3^5; //液晶数据/指令选择端:1-数据,0-指令sbit lcden=P3^4; //液晶使能控制端:1-有效,0-无效sbit shift_key=P3^2;//位置移动键sbit up_key=P3^3;//增加键uchar temp=0;//定义定时器溢出计数变量,每隔50ms产生1次溢出,temp加1uint year=2011;//定义年变量并赋初值2011年uchar month=06,day=01,week;//定义月、日、星期变量,并赋初值5月23日uchar hour=23,minute=59,second=58;//定义时、分、秒变量,并赋初值12时00分00秒uchar code week_string[7][4]={"MON","TUE","WED","THU","FRI","SA T","SUN"};//定义星期英文缩写表uchar data month_day[12]={31,0,31,30,31,30,31,31,30,31,30,31};//定义每月天数表/*--定时计数器T0及中断初始化函数--*/void init(void){TMOD=0x01;//设置定时器0为工作方式1TH0=(65536-50000)/256;//16位计数初值除以256得到高8位初值TL0=(65536-50000)%256;//16位计数初值除以256的余数得到低8位初值EA=1;//开总中断ET0=1;//开启定时器0中断EX0=1;//开启外部中断,外部中断用于调整时间PT0=1;//将定时器0中断设置高优先级,调整时间期不停止计时TR0=1;//启动定时器0}/*----------1ms延时函数----------*/void delay(uint n){uint i,j;for(i=n;i>0;i--)for(j=114;j>0;j--);}/*-------LCD1602写指令函数-----*/void LCD1602_write_com(uchar com){rs=0;//rs=0,置指令输入状态P0=com;//输出指令码delay(1);//延时1mslcden=1;//lcden=1,使能端有效delay(1);//延时1mslcden=0;//lcden=0,使能端无效}/*-------LCD1602写数据函数-----*/void LCD1602_write_dat(uchar dat){rs=1;//rs=1,置数据输入状态P0=dat; //输出待显示字符的字符码(ASCII码)delay(1);//延时1mslcden=1;//lcden=1,使能端有效delay(1);//延时1mslcden=0;//lcden=0,使能端无效}/*********************公历平年的2月只有28天,公历闰年的2月有29天。

C语言课程设计 时钟代码

C语言课程设计 时钟代码

/*电子时钟源代码*/#include<graphics.h>#include<stdio.h>#include<math.h>#include<dos.h>#define PI 3.1415926 /*定义常量*/#define UP 0x4800 /*上移↑键:修改时间*/#define DOWN 0x5000 /*下移↓键:修改时间*/#define ESC 0x11b /*ESC键:退出系统*/#define TAB 0xf09 /*TAB键:移动光标*//*函数声明*/int keyhandle(int,int); /*键盘按键判断,并调用相关函数处理*/int timeupchange(int); /*处理上移按键*/int timedownchange(int); /*处理下移按键*/int digithour(double); /*将double型的小时数转换成int型*/int digitmin(double); /*将double型的分钟数转换成int型*/int digitsec(double); /*将double型的秒钟数转换成int型*/void digitclock(int,int,int ); /*在指定位置显示时钟或分钟或秒钟数*/void drawcursor(int); /*绘制一个光标*/ void clearcursor(int);/*消除前一个光标*/void clockhandle(); /*时钟处理*/ double h,m,s; /*全局变量:小时,分,秒*/double x,x1,x2,y,y1,y2; /*全局变量:坐标值*/struct time t[1];/*定义一个time结构类型的数组*/main(){int driver, mode=0,i,j;driver=DETECT; /*自动检测显示设备*/initgraph(&driver, &mode, "");/*初始化图形系统*/setlinestyle(0,0,3); /*设置当前画线宽度和类型:设置三点宽实线*/ setbkcolor(0);/*用调色板设置当前背景颜色*/setcolor(9); /*设置当前画线颜色*/ line(82,430,558,430);line(70,62,70,418);line(82,50,558,50);line(570,62,570,418);line(70,62,570,62);line(76,56,297,56);line(340,56,564,56); /*画主体框架的边直线*//*arc(int x, int y, int stangle, int endangle, int radius)*/arc(82,62,90,180,12);arc(558,62,0,90,12);setlinestyle(0,0,3);arc(82,418,180,279,12);setlinestyle(0,0,3);arc(558,418,270,360,12); /*画主体框架的边角弧线*/setcolor(15);outtextxy(300,53,"CLOCK"); /*显示标题*/setcolor(7);rectangle(342,72,560,360); /*画一个矩形,作为时钟的框架*/setwritemode(0); /*规定画线的方式。

数字时钟代码

数字时钟代码

数字时钟代码1. 介绍数字时钟是一种常见的显示时间的装置,它通过数字显示屏显示当前的小时和分钟。

本文档将介绍如何编写一个简单的数字时钟代码。

2. 代码实现以下是一个基本的数字时钟代码实现示例:import timewhile True:current_time = time.localtime()hour = str(current_time.tm_hour).zfill(2)minute = str(current_time.tm_min).zfill(2)second = str(current_time.tm_sec).zfill(2)clock_display = f"{hour}:{minute}:{second}"print(clock_display, end="\r")time.sleep(1)代码说明:- `time.localtime()` 函数返回当前时间的结构化对象,包括小时、分钟和秒等信息。

- `str(current_time.tm_hour).zfill(2)` 将小时转换为字符串,并使用 `zfill()` 方法填充到两位数。

- `str(current_time.tm_min).zfill(2)` 和`str(current_time.tm_sec).zfill(2)` 同理处理分钟和秒。

- 使用 f-string 格式化字符串 `clock_display`,将小时、分钟和秒显示为 `` 的形式。

- `print(clock_display, end="\r")` 使用 `\r` 实现覆盖打印,使得时钟在同一行连续显示。

- `time.sleep(1)` 让程序每隔一秒更新一次时间。

请注意,上述代码需要在支持 Python 的环境中运行。

3. 结束语通过以上的代码实现,我们可以编写一个简单的数字时钟。

(完整word版)桌面时钟代码

(完整word版)桌面时钟代码

<HTML〉<HEAD〉〈TITLE〉动态数字时钟</TITLE〉〈SCRIPT language=”javascript">function digitalClock(){var today = new Date();digit_clock.innerText = today.toLocaleString();setTimeout(”digitalClock()", 1000 );}〈/SCRIPT〉〈LINK rel="styl esheet” type=”text/css" href="js。

css"〉〈/HEAD><BODY>〈H1 align="center" id=”digit_clock">〈/H1>〈SCRIPT language=”javascript"〉digitalClock();</SCRIPT><SCRIPT LANGUAGE="JavaScript”>var S = '。

..。

.';S = S.split('');//构成秒针的圆点var M = '*****'; M = M.split('');//构成分针的圆点var H = '****’;H = H.split('’);//构成时针的圆点var Ybase = 8; //构成秒、分和时针的圆点之间的间隔距离(Y轴)var Xbase = 8; //构成秒、分和时针的圆点之间的间隔距离(X轴)var dots = 12;//钟盘上的小时刻度数,通常为12,即1,2,3,.。

,12for (var i = 0; i 〈 dots; i++){//将小时刻度值(即1,2,3,。

电子时钟程序代码

电子时钟程序代码

#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自加一次。

时分秒数字钟程序代码

时分秒数字钟程序代码

2.源程序ORG 0000HLJMP STARTORG 000BHLJMP TIME;********初始化*********START: MOV SP, #50HMOV 20H,#00H ;定义秒MOV 21H,#00H ;定义分MOV 22H,#00H ;定义时MOV 23H,#01H ;定义闹钟分钟MOV 24H,#01H ;定义闹钟小时MOV 25H,#00HMOV 26H,#01HMOV 30H,#00H ;定义秒BCDMOV 31H,#00HMOV 32H,#00H ;定义分BCDMOV 33H,#00HMOV 34H,#00H ;定义小时BCDMOV 35H,#00HMOV 36H,#01HMOV 37H,#00HMOV 38H,#01HMOV 39H,#00HMOV 50H,#00H ;按键次数MOV TMOD,#01H ;16位计数器MOV TH0, #03CH ;赋初值MOV TL0, #0B0HMOV IE, #87H ;中断允许SETB TR0 ;启动T0MOV R2,#14HMOV P2,#0FFH;*********主程序**********MAIN: JB P1.4,GBLCALL TIMEPRO ;调用闹钟判断GB: LCALL DISPLAY1 ;调用时间显示JB P1.3,M1 ;P1.3=1时转移 S4没有按下 LCALL SETTIME ;调用SETTIME调时子程序LJMP MAINM1: JB P1.2,M2 ;P1 g.2=1时转移S3LCALL SETATIME ;调用SETATIME子程序LJMP MAINM2: JB P1.0,M4 ;P1.0=1时转移 S1LCALL LOOKATIME ;调用LOOKATIME显示闹钟子程序M4: LJMP MAIN;*********延时子程序********DELAY: MOV R4,#030HDL00: MOV R5,#0FFHDL11: MOV R6,#9HDL12: DJNZ R6,DL12DJNZ R5,DL11DJNZ R4,DL00RET;***********时间调整*******SETTIME: ;设置时间L0: LCALL DISPLAY1MM1: JB P1.3,L1 ;P1.3=1时转移MOV C,P1.3JC MM1LCALL DELAY1 ;延时JC MM1MSTOP1: MOV C,P1.3 ;P1.3为0时转移JNC MSTOP1LCALL DELAY1 ;延时MOV A,50HINC 50HCJNE A,#00H,HJ1LJMP L0HJ1: MOV C,P1.3JNC MSTOP1INC 22H ;小时自加一MOV A,22HCJNE A,#18H,GO12 ;小时计数循环MOV 22H,#00H ;复位MOV 34H,#00HMOV 35H,#00HLJMP L0L1: JB P1.1,L2 ;P1.1=1时转移MOV C,P1.1JC L1LCALL DELAY1 ;延时JC L1MSTOP2: MOV C,P1.1 ;P1.1=0时转移JNC MSTOP2LCALL DELAY1 ;延时MOV C,P1.1JNC MSTOP2INC 21H ;分钟加一MOV A,21HCJNE A,#3CH,GO11 ;分钟计数循环MOV 21H,#00H ; 复位MOV 32H,#00HMOV 33H,#00HLJMP L0GO11: MOV B,#0AHDIV ABMOV 32H,B ; 将A的低4位存入32单元 MOV 33H,A ; 将A的高4位存入33单元 LJMP L0GO12: MOV B,#0AHDIV ABMOV 34H,B ;将A的低4位存入34单元MOV 35H,A ; 将A的高4位存入35单元 LJMP L0L2: JB P1.0,L0 ; P1.0=1时转移MOV C,P1.0JC L2LCALL DELAY1 ;延时MOV C,P1.0JC L2STOP1: MOV C,P1.0 ; P1.0=0时转移JNC STOP1LCALL DELAY1 ;延时MOV C,P1.0JNC STOP1MOV 50H,#00HLJMP MAIN;*******设置闹钟*******SETATIME:LCALL DISPLAY2 ; 调用DISPLAY2显示闹钟N0: LCALL DISPLAY2MM2: JB P1.2,N1 ;P1.2=1时转移MOV C,P1.2JC MM2LCALL DELAY1 ; 延时JC MM2MSTOP3: MOV C,P1.2 ; P1.2=0时转移JNC MSTOP3LCALL DELAY1 ; 延时MOV A,50HINC 50HCJNE A,#00H,HJ2HJ2: MOV C,P1.2JNC MSTOP3INC 24H ;小时加一MOV A,24HCJNE A,#24,GO22 ;小时计数循环MOV 24H,#00H ;复位MOV 38H,#00HMOV 39H,#00HLJMP N0N1: JB P1.1,N2 ;P1.1=1时转移MOV C,P1.1JC N1LCALL DELAY1 ;延时JC N1MSTOP4: MOV C,P1.1 ;P1.1=0时转移JNC MSTOP4LCALL DELAY1 ;延时MOV C,P1.1JNC MSTOP4INC 23H ;分钟加一MOV A,23HCJNE A,#60,GO21 ;分钟计数循环MOV 23H,#00H ;复位MOV 36H,#00HMOV 37H,#00HLJMP N0GO21: MOV B,#0AHDIV ABMOV 36H,B ;将A的低4位存入36单元 MOV 37H,A ;将A的高4位存入37单元 LJMP N0GO22: MOV B,#0AHDIV ABMOV 38H,B ;将A的低4位存入38单元 MOV 39H,A ;将A的高4位存入39单元 LJMP N0N2: JB P1.0 ,N0 ;P1.0=1时转移MOV C,P1.0JC N2LCALL DELAY1 ;延时MOV C,P1.0JC N2STOP2: MOV C,P1.0LCALL DELAY1MOV C,P1.0JNC STOP2MOV 50H,#00HLJMP MAIN;*******闹钟判断*****************TIMEPRO: MOV A,21HMOV B,23HCJNE A,B,BK ;判断定时闹钟的分钟 MOV A,22HMOV B,24HCJNE A,B,BK ;判断定时闹钟的小时 SETB 25H.0MOV C,25H.0LCALL TIMEOUT ;调用TIMEOUTBK:RET;**************喇叭报警***************** TIMEOUT:X1: LCALL BZ ;调用喇叭响应程序CLR 25H.0 ;调用喇叭响应程序结束 LCALL DELAY ;延时CLR 25H.0LJMP DISPLAY1BZ:MOV C,25H.1MOV P1.6,CCLR P1.7MOV R7,#0FFH ;喇叭响应时间T2: MOV R6,#0FFHT3: DJNZ R6,T3DJNZ R7,T2SETB P1.7RET;*************显示闹钟时间************ LOOKATIME:LCALL DISPLAY2MM: JNB P1.0,LOOKATIMELCALL DELAY1LJMP MAINDELAY1: MOV R4,#14H ;时间延时DL001: MOV R5,#0FFHDL111: DJNZ R5,DL111DJNZ R4,DL001RET;***********定时**************TIME: PUSH ACC ;保护现场PUSH PSWMOV TH0,#03CH ;初值MOV TL0,#0B0HDJNZ R2,RET0MOV R2,#14HMOV A,20HCPL 25H.1CLR CINC A ;秒自加一CJNE A,#3CH,GO1 ;秒计数循环MOV 20H,#0 ;复位MOV 30H,#0MOV 31H,#0MOV A,21HINC A ;分钟自加一CJNE A,#3CH,GO2 ;分钟计数循环MOV 21H,#0H ;复位MOV 32H,#0MOV 33H,#0MOV A,22HINC A ;小时自加一CJNE A,#18H,GO3 ;小时计数循环MOV 22H,#00H ;复位MOV 34H,#0MOV 35H,#0AJMP RET0GO1: MOV 20H,AMOV B,#0AHDIV ABMOV 31H,A ;将A的低4位存入31单元 MOV 30H,B ;将A的高4位存入30单元 AJMP RET0GO2: MOV 21H,AMOV B,#0AHDIV ABMOV 33H,A ;将A的低4位存入33单元 MOV 32H,B ;将A的高4位存入32单元AJMP RET0GO3: MOV 22H,AMOV B,#0AHDIV ABMOV 35H,A ;将A的低4位存入35单元 MOV 34H,B ;将A的高4位存入34单元AJMP RET0RET0: POP PSW ;恢复现场POP ACCRETI;********显示子程序**********DISPLAY1: MOV R0,#30HMOV R3,#0FEHMOV A,R3PLAY1: MOV P2,AMOV A,@R0 ;取要显示的数据MOV DPTR,#DSEG1 ;指向字形段码首地址 MOVC A,@A+DPTRCJNE R0,#34H,PAORL A,#80HPA:CJNE R0,#32H,PBORL A,#80HPB:CPL A ;查表取字形段码MOV P0,A ;指向P0口LCALL DL1MOV P2,#0FFHMOV A,R3 ;判断是否显示到最低位 RL A ;左移一位JNB ACC.6,LD1INC R0 ;缓冲器地址加一MOV R3,ALJMP PLAY1LD1: RETDISPLAY2: PUSH ACC ;保护现场PUSH PSWMOV R0,#36HMOV R3,#0FBHMOV A,R3PLAY2: MOV P2,AMOV A,@R0 ;取要显示的数据MOV DPTR,#DSEG1 ;指向字形段码首地址MOVC A,@A+DPTRCJNE R0,#38H,PPORL A,#80HPP:CPL A ;查表取字形段码MOV P0,A ;指向P0口LCALL DL1 ;调用DL1MOV P2,#0FFHMOV A,R3 ;判断是否显示到最低位RL A ;左移一位JNB ACC.6,LD2INC R0 ;缓冲器地址加一 MOV R3,ALJMP PLAY2 ;调用PLAY2 LD2: POP PSWPOP ACC ;恢复现场RET;******DELAY*******DL1: MOV R7,#20HDL: MOV R6,#20HDL6: DJNZ R6,$DJNZ R7,DLRETDSEG1: DB 3FH,06H,5BH,4FH,66H ;七段码表DB 6DH,7DH,07H,7FH,6FH。

钟表源程序代码

钟表源程序代码

钟表源程序代码钟表源程序代码采用了easyx图形库作者:转角梦覃飞絮#include#include#include#include#include#define PI 3.1415926//自定义函数声明void draw_biaozhen(int x,int y,int second,int minute,int hour);//画表针函数void numbertime(int year,int month,int day,char week,int second,int minute,int hour);//显示数字时间void draw_biaopan(int x,int y);//画表盘void huaxian(); //画心形线void main(){initgraph(640,480);srand((unsigned)time(NULL));int x=320,y=240; //设定表盘圆心位置SYSTEMTIME time; //定义变量保存当前时间//画心形线outtextxy(20,40,"稍后进入钟表界面...");huaxian();cleardevice();setwritemode(R2_XORPEN); //设置XOR绘图模式//画表盘draw_biaopan( x, y);while(!kbhit()){ //有用户键就退出GetLocalTime(&time);numbertime(time.wYear ,time.wMonth,time.wDay ,time.wDa yOfWeek ,time.wSecond ,time. wMinute,time.wHour);//画指针draw_biaozhen(x,y,time.wSecond ,time.wMinute ,time.wHou r );Sleep(1000);//擦表针draw_biaozhen(x,y,time.wSecond ,time.wMinute ,time.wHou r );//实现随机色文字setcolor(RGB(rand()%255,rand()%255,rand()%255));outtextxy(285,320," 笨蛋");setfont(80,0,"宋体");outtextxy(40,80,"开");outtextxy(40,330,"心");outtextxy(520,80,"快");outtextxy(520,330,"乐");setfont(0,0,"宋体");}closegraph();}//画表针函数void draw_biaozhen(int x,int y,int second,int minute,int hour) {double a_sec,a_min,a_hour; //定义秒分时表针的弧度int x_sec,y_sec,x_min,y_min,x_hour,y_hour; //定义秒分时表针末端位置//计算表针弧度a_sec=second*2*PI/60;a_min=minute*2*PI/60+a_sec/60;a_hour=hour*2*PI/12+a_min/12;//计算表针末端位置x_sec=(int)(x+120*sin(a_sec));y_sec=(int)(y-120*cos(a_sec));x_min=(int)(x+80*sin(a_min));y_min=(int)(y-80*cos(a_min));x_hour=(int)(x+60*sin(a_hour));y_hour=(int)(y-60*cos(a_hour));//画秒针setlinestyle(PS_SOLID,NULL,1);setcolor(YELLOW);line(x,y,x_sec,y_sec);//画分针setlinestyle(PS_SOLID,NULL,3);setcolor(LIGHTGRAY);line(x,y,x_min,y_min);//画分针setlinestyle(PS_SOLID,NULL,5);setcolor(BLUE);line(x,y,x_hour,y_hour);}//显示数字时间函数void numbertime(int year,int month,int day,char week,int second,int minute,int hour){char str[50];//用来存储时间字符sprintf(str,"当前时间:%d年%d月%d日星期%d %d :%d :%d ",year,month,day,week,hour,minute,second);setcolor(BLUE);outtextxy(20,20,str);outtextxy(20,40,"按任意键退出程序..."); }// 画表盘函数void draw_biaopan(int x,int y){int x1,y1,x2,y2,x3,y3,x4,y4,r=150; setcolor(RED);circle(x,y,38);circle(x,y,2);setlinestyle(PS_SOLID,NULL,2); circle(x,y,150);for(int i=0;i<6;i++) //12个刻度{setcolor(YELLOW);x1=x+(int)(r*cos(2*PI*i/12));y1=y-(int)(r*sin(2*PI*i/12));x2=x+(int)(0.94*r*cos(2*PI*i/12));y2=y-(int)(0.94*r*sin(2*PI*i/12));x3=x+(int)(r*cos(2*PI*i/12+PI));y3=y-(int)(r*sin(2*PI*i/12+PI));x4=x+(int)(0.94*r*cos(2*PI*i/12+PI)); y4=y-(int)(0.94*r*sin(2*PI*i/12+PI)); line(x1,y1,x2,y2);line(x3,y3,x4,y4);}for(i=0;i<30;i++) //其他的小刻度{setcolor(RED);x1=x+(int)(r*cos(2*PI*i/60));y1=y-(int)(r*sin(2*PI*i/60));x2=x+(int)(0.97*r*cos(2*PI*i/60));y2=y-(int)(0.97*r*sin(2*PI*i/60));x3=x+(int)(r*cos(2*PI*i/60+PI));y3=y-(int)(r*sin(2*PI*i/60+PI));x4=x+(int)(0.97*r*cos(2*PI*i/60+PI));y4=y-(int)(0.97*r*sin(2*PI*i/60+PI));line(x1,y1,x2,y2);line(x3,y3,x4,y4);}setcolor(LIGHTRED);outtextxy(316,368,"6");outtextxy(312,98,"12");outtextxy(185,232,"9");outtextxy(450,232,"3");}//画心型线函数void huaxian(){double x1, y1, r1;while(!MouseHit()){for(int k=0;k<=3;k++){setcolor(RGB(rand()%255, rand()%255, rand()%255)); for(int i=0; i<90; i++) {for (int j=0; j<90; j++) {r1 = PI/45 * i * (1 - sin(PI/45 * j)) * 18;x1 = r1 * cos(PI/45 * j) * sin(PI/45 * i) + 320;y1 = -r1 * sin(PI/45 * j) + 120;// 画圆fillcircle((int) x1, (int) y1, 1);}Sleep(30);}// 画完后暂停1秒Sleep(1000);}} }。

单片机课设定时闹钟代码及注解

单片机课设定时闹钟代码及注解

#include<reg52.h>#define uint unsigned int#define uchar unsigned charuintmiao,fen,fen1,shi,shi1,flag1,flag2,flag3,aa,K1num,K2num,K3num,K4num,N;//K1num-K1按键被按下的标记变量,K2num-K2按键被按下的标记变量,K3num=0-K3按键被按下的标记变量uchar code table[]=" TIMING CLOCK"; //定义初始上电时液晶默认显示状态sbit K1=P1^0; //四个按键sbit K2=P1^1;sbit K3=P1^2;sbit K4=P1^3;sbit rs=P2^6;sbit lcden=P2^7;sbit led=P2^3; //发光二极管控制端sbit beep=P1^5; //蜂鸣器控制端sbit relay=P2^5; //继电器控制端void delay(uint z) //延时函数{uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}void write_com(uchar com) //液晶写命令函数{rs=0;lcden=0;P0=com;delay(5);lcden=1;delay(5);lcden=0;}void write_date(uchar date) //液晶写数据函数{rs=1;lcden=0;P0=date;delay(5);lcden=1;delay(5);lcden=0;}void write_sfm(uchar add,uchar date) //液晶写时分秒函数{uchar shi,ge;shi=date/10;ge=date%10;write_com(0x80+0x40+add);write_date(0x30+shi);write_date(0x30+ge);}void init() //初始化函数{uint num; //写液晶的循环控制变量aa=0; //中断次数标志K1num=0; //K1按键被按下的标记变量K2num=0; //K2按键被按下的标记变量K3num=0; //K3按键被按下的标记变量K4num=0;flag1=1; //控制lcd屏刷新的变量flag2=1; //控制闹钟响闹和继电器开启关闭的标记flag3=1; //控制闹钟响起时,按下K4停止闹钟响闹的标记led=1; //发光二极管初始化relay=0; //继电器初始化N=100; //系统启动时,闹钟时间到发出的声音为连续三次发出“哗”的一声miao=0; //系统初始化时间fen=0;shi=0;fen1=1; //初始化闹钟的时间shi1=0;beep=0; //蜂鸣器初始化lcden=0; //液晶使能端初始化//1602液晶初始化,设置16*2显示。

电子时钟程序代码

电子时钟程序代码

(一)计时模块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;。

Verilog--数字钟设计代码

Verilog--数字钟设计代码

数字钟一、简介此数字钟为时分秒可调,带有闹钟设置。

各引脚功能入下:clk: 输入时钟信号,为50mhz;(clk1k:产生闹铃音、报时音的时钟信号,)mode:功能控制信号;为0:计时功能;为1:闹钟功能;为2:手动校时功能;turn:接按键,在手动校时功能时,选择是调整小时,还是分钟;若长时间按住该键,还可使秒信号清零,用于精确调时;change:接按键,手动调整时,每按一次,计数器加1;如果长按,则连续快速加1,用于快速调时和定时;seg:此信号分别输出显示数据;采用BCD码计数,分别驱动6个数码管显示时间;scan:数码管位选择信号输出alert:输出到扬声器的信号,用于产生闹铃音和报时音;闹铃音为持续20秒的急促的“嘀嘀嘀”音,若按住“change”键,则可屏蔽该音;整点报时音为“嘀嘀嘀嘀—嘟”四短一长音;LD_alert:接发光二极管,指示是否设置了闹钟功能;LD_hour:接发光二极管,指示当前调整的是小时信号;LD_min:接发光二极管,指示当前调整的是分钟信号。

二、程序如下moduleshuzizhong(clk,mode,change,turn,alert,seg,scan,LD_alert,LD_hour,LD_min);input clk,mode,change,turn;output alert,LD_alert,LD_hour,LD_min;output[7:0] scan,seg;reg[7:0] scan,seg;reg[7:0] hour,min,sec,hour1,min1,sec1,ahour,amin;reg[1:0] m,fm,num1,num2,num3,num4;reg[1:0] loop1,loop2,loop3,loop4,sound;reg LD_hour,LD_min;reg clk1hz,clk4hz,clk2hz,clk1khz,minclk,hclk;reg alert1,alert2,ear;reg count1,count2,counta,countb;reg [3:0] data;reg [2:0] cnt8;reg [14:0] count3;reg [6:0] count4;wire ct1,ct2,cta,ctb,m_clk,h_clk;//-------------------1khz, 2hz--,1hz------------------------------------ always @(posedge clk)beginif(count3=='d25000)begin clk1khz<=~clk1khz;count3<=0;endelsebegin count3<=count3+1'b1;endendalways @(posedge clk1khz) //4hzbeginif(count4=='d125)begin clk4hz<=~clk4hz;count4<=0;endelsebegin count4<=count4+1'b1;endendalways @(posedge clk4hz) //2hzbeginclk2hz<=~clk2hz;endalways @(posedge clk2hz) //1hzbeginclk1hz<=~clk1hz;end//-----------------------------------------------------------always @(posedge clk4hz)beginif(sound==3)begin sound<=0; ear<=1;endelsebegin sound<=sound+1'b1; ear<=0;endendalways @(negedge mode)beginif(m==2) m<=0;else m<=m+1'b1;endalways @(negedge turn)beginfm<=~fm;end//----------------------------------------------------------------alwaysbegincase(m)2: begin if(fm)begin count1<=change; {LD_min,LD_hour}<=2; endelsebegin counta<=change; {LD_min,LD_hour}<=1; end{count2,countb}<=2'b11;end1: begin if(fm)begin count2<=change; {LD_min,LD_hour}<=2; endelse begin countb<=change; {LD_min,LD_hour}<=1; end{count1,counta}<=2'b11;enddefault: {count1,count2,counta,countb,LD_min,LD_hour}<=6'b111111;endcaseendalways @(negedge clk4hz)beginif(!count2)beginif(loop1==3) num1<=1;elsebegin loop1<=loop1+1'b1; num1<=0;endendelsebegin loop1<=0; num1<=0;endendalways @(negedge clk4hz)beginif(!countb)beginif(loop2==3) num2<=1;elsebegin loop2<=loop2+1'b1; num2<=0;endendelsebegin loop2<=0; num2<=0;endendalways @(negedge clk4hz)beginif(!count1)beginif(loop3==3) num3<=1;elsebegin loop3<=loop3+1'b1; num3<=0;endendelsebegin loop3<=0; num3<=0;endendalways @(negedge clk4hz)beginif(!counta)beginif(loop4==3) num4<=1;elsebegin loop4<=loop4+1'b1; num4<=0;endendelsebegin loop4<=0; num4<=0;endendassign ct1=(num3&clk)|(!num3&m_clk);assign ct2=(num1&clk)|(!num1&(!count2));assign cta=(num4&clk)|(!num4&h_clk);assign ctb=(num2&clk)|(!num2&(!countb));//-----------------------------------------------------------------------------always @(posedge clk1hz)beginif(!(sec1^8'h59)|(!turn)&(!m))begin sec1<=0;if(!((!turn)&(!m)))minclk<=1;endelsebeginif(sec1[3:0]==4'b1001)begin sec1[3:0]<=4'b0000; sec1[7:4]<=sec1[7:4]+1'b1;endelse sec1[3:0]<=sec1[3:0]+1'b1; minclk<=0;endendassign m_clk=minclk||(!count1);always @(posedge ct1)beginif(min1==8'h59)begin min1<=0; hclk<=1;endelsebeginif(min1[3:0]==9)begin min1[3:0]<=0; min1[7:4]<=min1[7:4]+1'b1;endelse min1[3:0]<=min1[3:0]+1'b1; hclk<=0;endendassign h_clk=hclk||(!counta);always @(posedge cta) //Сʱ¼ÆʱºÍСʱµ÷Õû½ø³Ìif(hour1==8'h23) hour1<=0;elseif(hour1[3:0]==9)begin hour1[7:4]<=hour1[7:4]+1'b1; hour1[3:0]<=0;endelse hour1[3:0]<=hour1[3:0]+1'b1;always @(posedge ct2)if(amin==8'h59) amin<=0;elseif(amin[3:0]==9)begin amin[3:0]<=0; amin[7:4]<=amin[7:4]+1'b1;endelse amin[3:0]<=amin[3:0]+1'b1;always @(posedge ctb)if(ahour==8'h23) ahour<=0;else if(ahour[3:0]==9)begin ahour[3:0]<=0; ahour[7:4]<=ahour[7:4]+1'b1;endelse ahour[3:0]<=ahour[3:0]+1'b1;//---------------------------------------------------------------------------alwaysif((min1==amin)&&(hour1==ahour)&&(amin|ahour)&&(change)) if(sec1<8'h20) alert1<=1;else alert1<=0;else alert1<=0;alwaysbegincase(m)3'b00: begin hour<=hour1; min<=min1; sec<=sec1; end3'b01: begin hour<=ahour; min<=amin; sec<=8'bx; end3'b10: begin hour<=hour1; min<=min1; sec<=8'bx; enddefault : begin hour<=8'bx; min<=8'bx; sec<=8'bx; endendcaseendassign LD_alert=(ahour|amin)?1'b1:1'b0;assign alert=((alert1)?clk1khz&clk4hz:1'b0)|alert2;alwaysbeginif((min1==8'h59)&&(sec1>8'h54)||(!(min1|sec1)))if(sec1>8'h54) alert2<=ear&clk1khz;else alert2<=!ear&clk1khz;else alert2<=0;end//-------------------------------------------------------------always @(posedge clk1khz)begincnt8<=cnt8+1'b1;endalwaysbegincase(cnt8[2:0])3'b000 : begin scan<=8'b01111111;data[3:0]<=hour[7:4];end3'b001 : begin scan<=8'b10111111;data[3:0]<=hour[3:0];end3'b010 : begin scan<=8'b11011111;data[3:0]<=min[7:4];end3'b011 : begin scan<=8'b11101111;data[3:0]<=min[3:0];end3'b100 : begin scan<=8'b11110111;data[3:0]<=sec[7:4];end3'b101 : begin scan<=8'b11111011;data[3:0]<=sec[3:0];end//3'b110 : begin scan<='b11111101;data[3:0]<=4'b1011;end//3'b111 : begin scan<='b11111110;data[3:0]<=4'b1010;enddefault : begin scan<=8'b11111111;data[3:0]<=4'bx;endendcasecase(data[3:0])4'b0000 : seg[7:0]<=8'b11000000;4'b0001 : seg[7:0]<=8'b11111001;4'b0010 : seg[7:0]<=8'b10100100;4'b0011 : seg[7:0]<=8'b10110000;4'b0100 : seg[7:0]<=8'b10011001;4'b0101 : seg[7:0]<=8'b10010010;4'b0110 : seg[7:0]<=8'b10000010;4'b0111 : seg[7:0]<=8'b11111000;4'b1000 : seg[7:0]<=8'b10000000;4'b1001 : seg[7:0]<=8'b10010000;4'b1010 : seg[7:0]<=8'b10001000;4'b1011 : seg[7:0]<=8'b10000011;4'b1100 : seg[7:0]<=8'b11000110;4'b1101 : seg[7:0]<=8'b10100001;4'b1110 : seg[7:0]<=8'b10000110;4'b1111 : seg[7:0]<=8'b10001110;default : seg[7:0]<=8'bx;endcaseendendmodule三、框图。

电子时钟设计代码

电子时钟设计代码

#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;}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

HOUR EQU 36HMIN EQU 37HSEC EQU 38HCHOUR1 EQU 39H;定义3个闹钟的时分CMIN1 EQU 40HCHOUR2 EQU 41HCMIN2 EQU 42HCHOUR3 EQU 43HCMIN3 EQU 44HORG 0000H;设定各个中断入口LJMP MIANORG 0003HLJMP W AIZD0ORG 000BHLJMP DINGSHIORG 0030HMIAN:MOV TMOD,#02H;使用定时器模式2 MOV TH0,#06H;设定初值为250usMOV TL0,#06HSETB TR0SETB EA;打开各个中断SETB ET0SETB EX0SETB PT0;定时器0优先级最高MOV HOUR,#0MOV MIN,#0MOV SEC,#0MOV P0,#0FFHMOV CHOUR1,#6;闹钟1设置默认数值MOV CMIN1,#0MOV CHOUR2,#8;闹钟2设置默认数值MOV CMIN2,#0MOV CHOUR3,#14;闹钟3设置默认数值MOV CMIN3,#0D0: ACALL XIANSHISECACALL XIANSHIMINACALL XIANSHIHOURACALL JIANGEACALL CLOCKZAJMP D0;要循环扫描才能动态显示CLOCKZ:;闹钟的调用函数MOV A,HOUR;闹钟一点判断CJNE A,CHOUR1,CN2;判断小时到了没有小时MOV A,MINCJNE A,CMIN1,CN2;判断分钟到时没SETB P3.4;开闹钟ACALL DELAYCLR P3.4CN2: MOV A,HOUR;闹钟2点判断CJNE A,CHOUR2,CN3;判断小时到了没有小时MOV A,MINCJNE A,CMIN2,CN3;判断分钟到时没SETB P3.4;开闹钟ACALL DELAYCLR P3.4CN3: MOV A,HOUR;闹钟2点判断CJNE A,CHOUR3,CNO;判断小时到了没有小时MOV A,MINCJNE A,CMIN3,CNO;判断分钟到时没SETB P3.4;开闹钟ACALL DELAYCLR P3.4CNO: RETSHUJU:INC 31HMOV R0,31HCJNE R0,#40,OUT0;时间达到10ms则执行下一行程序,否则返回OUT0 MOV 31H,#0MOV R0,#0INC 32HMOV R1,32HCJNE R1,#100,OUT0;时间达到1s则执行下一行程序,否则返回OUT0MOV 32H,#0MOV R1,#0INC SECMOV R2,SECCJNE R2,#60,OUT0;时间达到60s则执行下一行程序,否则返回OUT0MOV R2,#0MOV SEC,#0INC MINMOV R3,MINCJNE R3,#60,OUT0;时间达到60min则执行下一行程序,否则返回OUT0 MOV R3,#0MOV MIN,#0INC HOURCJNE R4,#24,OUT0;时间达到24h则执行下一行程序,否则返回OUT0 MOV R4,#0MOV HOUR,#0OUT0:RETJIANGE: MOV DPTR,#TABLE ;-的显示MOV A,#10;11为table编码的序地址MOVC A,@A+DPTRMOV P1,AMOV P2,#24HACALL DELAYMOV P1,#0FFHRETXIANSHISEC:MOV DPTR,#TABLEMOV A,SECMOV B,#10DIV ABMOVC A,@A+DPTRMOV P1,A;这是为位选,就是要显示的数字选?MOV P2,#02H;这是段段选,是控制哪一位,显示第一位ACALL DELAYMOV P1,#0FFHMOV A,BMOVC A,@A+DPTRMOV P1,AMOV P2,#01HACALL DELAYMOV P1,#0FFHRETXIANSHIMIN:MOV DPTR,#TABLEMOV A,MINMOV B,#10DIV ABMOVC A,@A+DPTRMOV P1,AMOV P2,#10HACALL DELAYMOV P1,#0FFHMOV A,BMOVC A,@A+DPTRMOV P1,AACALL DELAYMOV P1,#0FFHRETXIANSHIHOUR:MOV DPTR,#TABLEMOV A,HOURMOV B,#10DIV ABMOVC A,@A+DPTRMOV P1,AMOV P2,#80HACALL DELAYMOV P1,#0FFHMOV A,BMOVC A,@A+DPTRMOV P1,AMOV P2,#40HACALL DELAYMOV P1,#0FFHRETXIANCLOCK1:;闹钟1显示函数MOV DPTR,#TABLEMOV A,CHOUR1;显示闹钟小时MOV B,#10DIV ABMOVC A,@A+DPTR;显示十位MOV P1,AMOV P2,#80HACALL DELAYMOV P1,#0FFHMOV A,BMOVC A,@A+DPTR;显示个位MOV P1,AMOV P2,#40HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE ;间隔-的显示MOV A,#10;11为table编码的序地址MOVC A,@A+DPTRMOV P1,AMOV P2,#24HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE;显示闹钟1的分钟MOV A,CMIN1MOV B,#10DIV ABMOVC A,@A+DPTR;显示十位MOV P1,AMOV P2,#10HACALL DELAYMOV P1,#0FFHMOV A,BMOVC A,@A+DPTR;显示个位MOV P1,AMOV P2,#08HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE ;间隔C的显示MOV A,#11;11为table编码C的地址MOVC A,@A+DPTRMOV P1,AMOV P2,#02HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE ;间隔1的显示MOV A,#1MOVC A,@A+DPTRMOV P1,AMOV P2,#01HACALL DELAYMOV P1,#0FFHRETXIANCLOCK2:;闹钟2显示函数MOV DPTR,#TABLEMOV A,CHOUR2;显示闹钟小时MOV B,#10DIV ABMOVC A,@A+DPTR;显示十位MOV P1,AMOV P2,#80HACALL DELAYMOV P1,#0FFHMOV A,BMOVC A,@A+DPTR;显示个位MOV P1,AMOV P2,#40HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE ;间隔-的显示MOV A,#10;11为table编码的序地址MOVC A,@A+DPTRMOV P1,AMOV P2,#24HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE;显示闹钟3的分钟MOV A,CMIN2MOV B,#10DIV ABMOVC A,@A+DPTR;显示十位MOV P1,AMOV P2,#10HACALL DELAYMOV P1,#0FFHMOV A,BMOVC A,@A+DPTR;显示个位MOV P1,AMOV P2,#08HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE ;间隔C的显示MOV A,#11;11为table编码C的地址MOVC A,@A+DPTRMOV P1,AMOV P2,#02HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE ;间隔2的显示MOV A,#2MOVC A,@A+DPTRMOV P1,AMOV P2,#01HACALL DELAYMOV P1,#0FFHRETXIANCLOCK3:;闹钟1显示函数MOV DPTR,#TABLEMOV A,CHOUR3;显示闹钟小时MOV B,#10DIV ABMOVC A,@A+DPTR;显示十位MOV P1,AMOV P2,#80HACALL DELAYMOV P1,#0FFHMOV A,BMOVC A,@A+DPTR;显示个位MOV P1,AMOV P2,#40HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE ;间隔-的显示MOV A,#10;11为table编码的序地址MOVC A,@A+DPTRMOV P1,AMOV P2,#24HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE;显示闹钟3的分钟MOV A,CMIN3MOV B,#10DIV ABMOVC A,@A+DPTR;显示十位MOV P1,AMOV P2,#10HACALL DELAYMOV P1,#0FFHMOV A,BMOVC A,@A+DPTR;显示个位MOV P1,AMOV P2,#08HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE ;间隔C的显示MOV A,#11;11为table编码C的地址MOVC A,@A+DPTRMOV P1,AMOV P2,#02HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE ;间隔3的显示MOV A,#3MOVC A,@A+DPTRMOV P1,AMOV P2,#01HACALL DELAYMOV P1,#0FFHRETDINGSHI:MOV TH0,#06H;初值为250usMOV TL0,#06HACALL SHUJURETISETKEY:JNB P3.7,$ACALL DELAYSETHOUR:ACALL XIANSHIHOURJB P3.1,HOURJIANJNB P3.1,$INC HOURMOV A,HOURCJNE A,#24,HOURJIANMOV HOUR,#0HOURJIAN:JB P3.0,OUTHJNB P3.0,$ACALL DELAYDEC HOURMOV A,HOURCJNE A,#-1,OUTHMOV HOUR,#23OUTH: JB P3.7,SETHOURJNB P3.7,$ACALL DELAYSETMIN: ACALL XIANSHIMINJB P3.1,MINJIANJNB P3.1,$INC MINMOV A,MINCJNE A,#60,MINJIANMOV MIN,#0MINJIAN:JB P3.0,OUTH0JNB P3.0,$ACALL DELAYDEC MINMOV A,MINCJNE A,#-1,OUTH0MOV MIN,#59OUTH0: JB P3.7,SETMINJNB P3.7,$;ACALL DELAYRET;闹钟子设置程序SETCLOCK:C1SHI: ACALL XIANCLOCK1;调用闹钟一显示函数JB P3.1,C1JIAN;如果被按下则顺序执行,不按下则跳转判断减法按键JNB P3.1,$;松手检测ACALL DELAYINC CHOUR1;按下则闹钟1的时自加1MOV A,CHOUR1CJNE A,#24,C1JIANMOV CHOUR1,#0C1JIAN: JB P3.0,DCCC1;按下则执行自减1JNB P3.0,$DEC CHOUR1;自减1ACALL DELAYMOV A,CHOUR1CJNE A,#-1,DCCC1MOV CHOUR1,#23DCCC1: JB P3.7,C1SHI;切换键按下则执行下面的分钟设置,不按下则跳转回时闹钟时的设置JNB P3.7,$ACALL DELAYC1FEN: ACALL XIANCLOCK1JB P3.1,JIANFEN1;如果被按下则顺序执行,不按下则跳转判断减法按键JNB P3.1,$;松手检测ACALL DELAYINC CMIN1;按下则闹钟1的时自加1MOV A,CMIN1CJNE A,#60,JIANFEN1;判断是否为60MOV CMIN1,#0JIANFEN1:JB P3.0,SETC2;不按下则转到闹钟2设置JNB P3.0,$ACALL DELAYDEC CMIN1MOV A,CMIN1CJNE A,#-1,SETC2MOV CMIN1,#59SETC2: JB P3.7,C1FEN;切换键按下则执行闹钟2JNB P3.7,$ACALL DELAY;闹钟2C2SHI: ACALL XIANCLOCK2;调用闹钟一显示函数JB P3.1,C1JIAN2;如果被按下则顺序执行,不按下则跳转判断减法按键JNB P3.1,$;松手检测ACALL DELAYINC CHOUR2;按下则闹钟2的时自加1MOV A,CHOUR2CJNE A,#24,C1JIAN2MOV CHOUR2,#0C1JIAN2:JB P3.0,DCCC2;按下则执行自减1JNB P3.0,$DEC CHOUR2;自减1ACALL DELAYMOV A,CHOUR2CJNE A,#-1,DCCC2MOV CHOUR2,#23DCCC2: JB P3.7,C2SHI;切换键按下则执行下面的分钟设置,不按下则跳转回时闹钟时的设置JNB P3.7,$ACALL DELAYC2FEN: ACALL XIANCLOCK2JB P3.1,JIANFEN2;如果被按下则顺序执行,不按下则跳转判断减法按键JNB P3.1,$;松手检测ACALL DELAYINC CMIN2;按下则闹钟2的时自加1MOV A,CMIN2CJNE A,#60,JIANFEN2MOV CMIN2,#0JIANFEN2:JB P3.0,SETC22;不按下则转到闹钟2设置JNB P3.0,$ACALL DELAYDEC CMIN2MOV A,CMIN2CJNE A,#-1,SETC22MOV CMIN1,#59SETC22: JB P3.7,C2FEN;切换键按下则向下执行闹钟三。

相关文档
最新文档