求一个51单片机设计的电子时钟程序及电路图。
- 提问者网友:了了无期
- 2021-05-05 08:19
- 五星知识达人网友:旧脸谱
- 2021-05-05 08:28
- 1楼网友:撞了怀
- 2021-05-05 11:44
#include<reg52.h> #define uint unsigned int #define uchar unsigned char sbit dula=P2^6; sbit wela=P2^7; sbit s1=P3^0; sbit s2=P3^1; sbit s3=P3^2; sbit s4=P3^3;
uchar code table[]={ 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x40,0x00}; uchar aa,slnum=0,temp,temp1,temp2, gee,shi,ge,shii,shiii,geee,cc,bb,flag,tt,ss; void display(uchar shiii,uchar geee,uchar cc, uchar shii,uchar gee,uchar bb,uchar shi,uchar ge); void delay(uint z); void keyscan(); void init(); void main() { init();//初始化子程序 while(1) { keyscan(); if(aa<=10) display(shiii,geee,10,shii,gee,10,shi,ge); else { display(shiii,geee,11,shii,gee,11,shi,ge); } } }
void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void display(uchar shiii,uchar geee,uchar ee,uchar shii, // 显示子函数 uchar gee,uchar dd,uchar shi,uchar ge) { dula=1; P0=table[shiii]; dula=0; P0=0x00; wela=1; P0=0xfe; wela=0; delay(1);
dula=1; P0=table[geee]; dula=0; P0=0x00; wela=1; P0=0xfd; wela=0; delay(1);
dula=1; P0=table[ee]; dula=0; P0=0x00; wela=1; P0=0xfb; wela=0; delay(1);
dula=1; P0=table[shii]; dula=0; P0=0x00; wela=1; P0=0xf7; wela=0; delay(1);
dula=1; P0=table[gee]; dula=0; P0=0x00; wela=1; P0=0xef; wela=0; delay(1);
dula=1; P0=table[dd]; dula=0; P0=0x00; wela=1; P0=0xdf; wela=0; delay(1);
dula=1; P0=table[shi]; dula=0; P0=0x00; wela=1; P0=0xbf; wela=0; delay(1);
dula=1; P0=table[ge]; dula=0; P0=0x20; wela=1; P0=0x7f; wela=0; delay(1); } void init() //初始化子函数 { wela=0; dula=0; temp=0; temp1=0; temp2=0; TMOD=0x11; TH0=(65536-50000)/256; TL0=(65536-50000)%256; ET0=1; TR0=1; EA=1; } void keyscan() //调时按键子函数 { if(s1==0) delay(5); if(s1==0) { TR0=0; while(!s1); TR0=1; temp2++; if(temp2==24) temp2=0; } if(s2==0) delay(5); if(s2==0) { TR0=0; while(!s2); TR0=1; if(temp2==0) temp2=23; else temp2--; } if(s3==0) delay(5); if(s3==0) { TR0=0; while(!s3); TR0=1; temp1++; if(temp1==60) temp1=0; } if(s4==0) delay(5); if(s4==0) { TR0=0; while(!s4); TR0=1; if(temp1==0) temp1=59; else temp1--; } }
void timer0() interrupt 1 //中断 { ET0=0; TR0=0; TH0=(65536-50000)/256; TL0=(65536-50000)%256; TR0=1; aa++; if(aa==20) { aa=0; temp++; if(temp==60) { temp=0; temp1++; if(temp1==60) { temp1=0; temp2++; } if(temp2==24) { temp2=0; } } shiii=temp2/10; geee=temp2%10; shii=temp1/10; gee=temp1%10; shi=temp/10; ge=temp%10; } ET0=1; }//你自己接四个按键再就是加两个数码管就OK了,端口在程序里都有,希望对你有所帮助
- 2楼网友:往事隔山水
- 2021-05-05 11:03
给图及程序你自己研究吧:
(1. 开机时,显示12:00:00的时间开始计时;
(2. P0.0/AD0控制“秒”的调整,每按一次加1秒;
(3. P0.1/AD1控制“分”的调整,每按一次加1分;
(4. P0.2/AD2控制“时”的调整,每按一次加1个小时;
汇编源程序
SECOND EQU 30H
MINITE EQU 31H
HOUR EQU 32H
HOURK BIT P0.0
MINITEK BIT P0.1
SECONDK BIT P0.2
DISPBUF EQU 40H
DISPBIT EQU 48H
T2SCNTA EQU 49H
T2SCNTB EQU 4AH
TEMP EQU 4BH
ORG 00H
LJMP START
ORG 0BH
LJMP INT_T0
START: MOV SECOND,#00H
MOV MINITE,#00H
MOV HOUR,#12
MOV DISPBIT,#00H
MOV T2SCNTA,#00H
MOV T2SCNTB,#00H
MOV TEMP,#0FEH
LCALL DISP
MOV TMOD,#01H
MOV TH0,#(65536-2000) / 256
MOV TL0,#(65536-2000) MOD 256
SETB TR0
SETB ET0
SETB EA
WT: JB SECONDK,NK1
LCALL DELY10MS
JB SECONDK,NK1
INC SECOND
MOV A,SECOND
CJNE A,#60,NS60
MOV SECOND,#00H
NS60: LCALL DISP
JNB SECONDK,$
NK1: JB MINITEK,NK2
LCALL DELY10MS
JB MINITEK,NK2
INC MINITE
MOV A,MINITE
CJNE A,#60,NM60
MOV MINITE,#00H
NM60: LCALL DISP
JNB MINITEK,$
NK2: JB HOURK,NK3
LCALL DELY10MS
JB HOURK,NK3
INC HOUR
MOV A,HOUR
CJNE A,#24,NH24
MOV HOUR,#00H
NH24: LCALL DISP
JNB HOURK,$
NK3: LJMP WT
DELY10MS:
MOV R6,#10
D1: MOV R7,#248
DJNZ R7,$
DJNZ R6,D1
RET
DISP:
MOV A,#DISPBUF
ADD A,#8
DEC A
MOV R1,A
MOV A,HOUR
MOV B,#10
DIV AB
MOV @R1,A
DEC R1
MOV A,B
MOV @R1,A
DEC R1
MOV A,#10
MOV@R1,A
DEC R1
MOV A,MINITE
MOV B,#10
DIV AB
MOV @R1,A
DEC R1
MOV A,B
MOV @R1,A
DEC R1
MOV A,#10
MOV@R1,A
DEC R1
MOV A,SECOND
MOV B,#10
DIV AB
MOV @R1,A
DEC R1
MOV A,B
MOV @R1,A
DEC R1
RET
INT_T0:
MOV TH0,#(65536-2000) / 256
MOV TL0,#(65536-2000) MOD 256
MOV A,#DISPBUF
ADD A,DISPBIT
MOV R0,A
MOV A,@R0
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P1,A
MOV A,DISPBIT
MOV DPTR,#TAB
MOVC A,@A+DPTR
MOV P3,A
INC DISPBIT
MOV A,DISPBIT
CJNE A,#08H,KNA
MOV DISPBIT,#00H
KNA: INC T2SCNTA
MOV A,T2SCNTA
CJNE A,#100,DONE
MOV T2SCNTA,#00H
INC T2SCNTB
MOV A,T2SCNTB
CJNE A,#05H,DONE
MOV T2SCNTB,#00H
INC SECOND
MOV A,SECOND
CJNE A,#60,NEXT
MOV SECOND,#00H
INC MINITE
MOV A,MINITE
CJNE A,#60,NEXT
MOV MINITE,#00H
INC HOUR
MOV A,HOUR
CJNE A,#24,NEXT
MOV HOUR,#00H
NEXT: LCALL DISP
DONE: RETI
TABLE: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH,40H
TAB: DB 0FEH,0FDH,0FBH,0F7H,0EFH,0DFH,0BFH,07FH
END
- 3楼网友:拜訪者
- 2021-05-05 10:32
使用定时器做时间比较不准,可以考虑使用实时时钟芯片。
- 4楼网友:想偏头吻你
- 2021-05-05 09:18
用汇编还是用C的