『壹』 用AT89C51单片机设计一电子闹钟,C语言程序和仿真
可以用单片机内部定时器来完成,
#include <reg52.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
/* 函数申明 -----------------------------------------------*/
void TransferData(char data1,bit DI);
void display(void);
void display_grapic(void);
void delayms(uint n);
void DisplayLine(uchar line1,uchar line2);
void DisplayGraphic(uchar code *adder);
void delay(uint m);
void lcd_mesg(uchar code *adder1);
unsigned char Convert(unsigned char In_Date);
void initinal(void);
void initina2(void) ;
void lcd_m(uchar adder1,uchar adder2,uchar a);
/* 变量定义 -----------------------------------------------*/
uchar scnt = 0 ;
uchar last_scnt = 0 ;
uchar updat_vision = 0 ;
sbit key1 = P3^5 ;
sbit key2 = P3^7 ;
sbit key3 = P3^6 ;
sbit gnd = P3^0 ;
sbit spk = P1^0 ;
uchar timer_miao = 0 ;
uchar timer_fen = 0 ;
uchar timer_shi = 0 ;
uchar cnt = 0 ;
uchar sw = 0 ;
uchar spk_on = 0 ;
uchar noise_shi = 0 ;
uchar noise_fen = 0 ;
uint count = 0 ;
uint count1 = 0 ;
uint count2 = 0 ;
uchar time_rch = 0 ;
sbit datacs=P2^6;
sbit chipcs=P2^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x07,
0x39,0x5e,0x79,0x71
};
/*
********************************************************************************
** 函数名称 : Init_Timer0(void)
** 函数功能 : 定时器初始化子程序
********************************************************************************
*/
void Init_Timer0(void) //定时器初始化子程序
{
TMOD |= 0x11; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
TH0=0x3c; //给定初值,这里使用定时器最大值从0开始计数一直到65535溢出
TL0=0xaf;
TH1=0x03; //给定初值,这里使用定时器最大值从0开始计数一直到65535溢出
TL1=0xe8;
EA=1; //总中断打开
ET0=1; //定时器中断打开
TR0=1; //定时器开关打开
ET1=1;
TR0=1;
}
/*
********************************************************************************
** 函数名称 : Timer0_isr(void) interrupt 1 using 1
** 函数功能 : 定时器中断程序
********************************************************************************
*/
void Timer0_isr(void) interrupt 1 using 1
{
TH0=0x3c; //重新赋值,方式1是16位计数器,不能硬件重装初始值
TL0=0xaf;
if(cnt<20)
{
cnt ++ ;
}else
{
cnt = 0 ;
if(sw==0)
{
if(timer_miao<59)
{
timer_miao++;
}else
{
timer_miao = 0 ;
if(timer_fen<59)
{
timer_fen++;
}else
{
timer_fen = 0 ;
if(timer_shi<23)
{
timer_shi++;
}else
{
timer_shi = 0 ;
}
}
}
}
}
}
void Timer1_isr(void) interrupt 3
{
TH1=0x03; //给定初值,这里使用定时器最大值从0开始计数一直到65535溢出
TL1=0xe8;
if(scnt<7)
{
scnt ++ ;
}else
{
scnt = 0 ;
}
switch(scnt)
{
case 0 : P0 = 0xfe ;chipcs = 1 ; chipcs = 0 ; P0=table[timer_miao%10] ;datacs=1;datacs=0; break ;
case 1 : P0 = 0xfd ;chipcs = 1 ; chipcs = 0 ; P0=table[timer_miao/10] ;datacs=1;datacs=0; break ;
case 2 : P0 = 0xfb ;chipcs = 1 ; chipcs = 0 ; P0= 0x40 ;datacs=1;datacs=0; break ;
case 3 : P0 = 0xf7 ;chipcs = 1 ; chipcs = 0 ; P0=table[timer_fen%10] ;datacs=1;datacs=0;break ;
case 4 : P0 = 0xef ;chipcs = 1 ; chipcs = 0 ; P0=table[timer_fen/10] ;datacs=1;datacs=0;break ;
case 5 : P0 = 0xdf ;chipcs = 1 ; chipcs = 0 ; P0= 0x40 ;datacs=1;datacs=0; break ;
case 6 : P0 = 0xbf ;chipcs = 1 ; chipcs = 0 ; P0=table[timer_shi%10] ;datacs=1;datacs=0;break ;
case 7 : P0 = 0x7f ;chipcs = 1 ; chipcs = 0 ; P0=table[timer_shi/10] ;datacs=1;datacs=0;break ;
}
}
void key(void)
{
if(key1==0)
{
if(count<8000)
count ++ ;
}else
{
if(count>5000)
{
if(sw<3)
sw++;
else
sw=0;
}
count = 0 ;
}
if(key2==0)
{
if(count1<8000)
count1 ++ ;
}else
{
if(count1>5000)
{
switch(sw)
{
case 0 : spk_on = 1 ; time_rch=0;break ;
case 1 : if(timer_shi<23)timer_shi++;else timer_shi = 0 ; break ;
case 2 : if(timer_fen<59)timer_fen++;else timer_fen = 0 ; break ;
case 3 : if(timer_miao<59)timer_miao++;else timer_miao = 0 ; break ;
}
}
count1 = 0 ;
}
if(key3==0)
{
if(count2<8000)
count2 ++ ;
}else
{
if(count2>5000)
{
switch(sw)
{
case 0 : spk_on = 0 ; time_rch=0; break ;
case 1 : if(timer_shi>0)timer_shi--;else timer_shi = 23 ; break ;
case 2 : if(timer_fen>0)timer_fen--;else timer_fen = 59 ; break ;
case 3 : if(timer_miao>0)timer_miao--;else timer_miao = 59 ; break ;
}
}
count2 = 0 ;
}
}
/********************************************************************
* 名称 : Main()
* 功能 : 主函数
* 输入 : 无
* 输出 : 无
***********************************************************************/
void main(void)
{
sw=0;
noise_shi = 0 ;
noise_fen = 2 ;
Init_Timer0();
while(1)
{
key();
}
}
『贰』 哪位大神能帮我写一个基于单片机C51的电子时钟设计报告
老师给我的
电子钟设计
1、 电路图:
2功能实现:
(1. 把“单片机系统”区域中的P1.0-P1.7端口用8芯排线连接到“动态数码显示”区域中的A-H端口上;
(2. 把“单片机系统:区域中的P3.0-P3.7端口用8芯排线连接到“动态数码显示”区域中的S1-S8端口上;
(3. 把“单片机系统”区域中的P0.0/AD0、P0.1/AD1、P0.2/AD2端口分别用导线连接到“独立式键盘”区域中的SP3、SP2、SP1端口上;
3、程序框图:
6. 汇编源程序
HOURK BIT P1.2
MINITEK BIT P1.1
SECONDK BIT P1.0
SECOND EQU 30H
MINITE EQU 31H
HOUR EQU 32H
ORG 00H
LJMP START
ORG 0BH
LJMP INT_T0
ORG 0030H
START:
MOV SECOND,#00H
MOV MINITE,#00H
MOV HOUR,#12
MOV R0,#00H
MOV TMOD,#01H
MOV TH0,#3CH
MOV TL0,#0B0H
SETB EA
SETB ET0
SETB TR0
NEXT: MOV A,#7FH
MOV P3,A
MOV A,HOUR
MOV B,#10
DIV AB
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P2,A
CALL DELAY
MOV A,#0BFH
MOV P3,A
MOV A,B
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P2,A
CALL DELAY
MOV A,#0DFH
MOV P3,A
mov p2,#40h
call delay
mov a,#0efh
mov p3,a
MOV A,MINITE
MOV B,#10
DIV AB
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P2,A
CALL DELAY
MOV A,#0F7H
MOV P3,A
MOV A,B
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P2,A
CALL DELAY
MOV A,#0FBH
MOV P3,A
mov p2,#40h
call delay
MOV A,#0FDH
MOV P3,A
MOV A,SECOND
MOV B,#10
DIV AB
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P2,A
CALL DELAY
MOV A,#0FEH
MOV P3,A
MOV A,B
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P2,A
CALL DELAY
WT: JB SECONDK,NK1
LCALL DELY10MS
JB SECONDK,NK1
JNB SECONDK,$
INC SECOND
MOV A,SECOND
CJNE A,#60,NK1
MOV SECOND,#00H
NK1: JB MINITEK,NK2
LCALL DELY10MS
JB MINITEK,NK2
JNB MINITEK,$
INC MINITE
MOV A,MINITE
CJNE A,#60,NK2
MOV MINITE,#00H
NK2: JB HOURK,NK3
LCALL DELY10MS
JB HOURK,NK3
JNB HOURK,$
INC HOUR
MOV A,HOUR
CJNE A,#24,NK3
MOV HOUR,#00
NK3:
MOV A,R0
CJNE A,#20,NEXT1
MOV R0,#0
INC SECOND
MOV A,SECOND
CJNE A,#60,NEXT1
MOV SECOND,#0
INC MINITE
MOV A,MINITE
CJNE A,#60,NEXT1
MOV MINITE,#0
INC HOUR
MOV A,HOUR
CJNE A,#24,NEXT1
MOV HOUR,#0
next1:JMP NEXT
TABLE:DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH,40H
INT_T0: CLR TR0
INC R0
MOV TH0,#3CH
MOV TL0,#0B0H
SETB TR0
RETI
DELAY:
MOV R6,#2
D1: MOV R7,#248
DJNZ R7,$
DJNZ R6,D1
RET
DELY10MS:
MOV R6,#10
DD1: MOV R7,#248
DJNZ R7,$
DJNZ R6,DD1
RET
END
『叁』 基于单片机51系列及DS1302时钟芯片数字电子钟的制作。求电路总原理图、PCB图、程序以及元件清单。
元件基本差不多。电路如图
#include<intrins.h>
#defineucharunsignedchar
#defineuintunsignedint
sbitds1302_RST=P2^0;
sbitds1302_IO =P2^1;
sbitds1302_SCLK=P2^2;
sbitACC0=ACC^0;
sbitACC7=ACC^7;
sbitA1=P3^0;
sbitA2=P3^1;
sbitA3=P3^2;
sbitA4=P3^3;
sbitA5=P3^4;
sbitA6=P3^5;
sbitkey1=P3^6;
sbitkey2=P3^7;
ucharnow_time[3],wei,d[3]={0,0,0};
ucharcodes[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e
};
voiddelay(uintx)
{
uchari;
for(x;x>0;x--)
for(i=0;i<100;i++);
}
voiddisp()
{
P1=s[now_time[2]/16];
A1=d[2];
delay(5);
A1=1;
P1=s[now_time[2]%16];
A2=d[2];
delay(5);
A2=1;
P1=s[now_time[1]/16];
A3=d[1];
delay(5);
A3=1;
P1=s[now_time[1]%16];
A4=d[1];
delay(5);
A4=1;
P1=s[now_time[0]/16];
A5=d[0];
delay(5);
A5=1;
P1=s[now_time[0]%16];
A6=d[0];
delay(5);
A6=1;
}
/***********************************************************************/
uchar read_Byte()
{
uchari;
for(i=8;i>0;i--)
{
ACC=ACC>>1;
ACC7=ds1302_IO;
ds1302_SCLK=1;
ds1302_SCLK=0;
}
return(ACC);
}
void write_Byte(uchartdata)
{
uchari;
ACC=tdata;
for(i=8;i>0;i--)
{
ds1302_IO=ACC0;
ds1302_SCLK=1;
ds1302_SCLK=0;
ACC=ACC>>1;
}
}
/***********************************************************************************/
voidwrite_data_ds1302(uchartaddr,uchartdata)
{
ds1302_RST=0;
ds1302_SCLK=0;
ds1302_RST=1;
write_Byte(taddr);
write_Byte(tdata);
ds1302_RST=0;
ds1302_SCLK=1;
}
ucharread_data_ds1302(uchartaddr)
{
uchartdata;
ds1302_RST=0;
ds1302_SCLK=0;
ds1302_RST=1;
write_Byte(taddr);
tdata=read_Byte();
ds1302_RST=0;
ds1302_SCLK=1;
return(tdata);
}
/***********************************************************************************/
voidget_ds1302()
{
uchark;
uchartaddr=0x81;
for(k=0;k<3;k++)
{
now_time[k]=read_data_ds1302(taddr);
taddr+=2;
}
}
/***********************************************************************************/
voidinit_ds1302()
{
ds1302_RST=0;
ds1302_SCLK=0;
A1=1;
A2=1;
A3=1;
A4=1;
A5=1;
A6=1;
write_data_ds1302(0x80,0x00);
}
/***********************************************************************************/
void Time();
/***********************************************************************************/
main()
{
init_ds1302();
while(1)
{
disp();
get_ds1302();
if(key1==0);
{delay(10);
if(key1==0)
Time();
}
}
}
voidtimer()interrupt3
{
uchari;i++;
TH1=(65535-50000)/256;
TL2=(65535-50000)%256;
if(i==50)
{
d[wei]=1;
}
if(i==100)
{i=0;
d[wei]=0;
}
}
/***********************************************************************************/
void Time()
{
uchartemp;
uintr=0,p=1;
wei=2;
TMOD=0x01;
EA=1;
ET1=1;
TH1=(65535-50000)/256;
TL2=(65535-50000)%256;
TR1=1;
for(r=0;r<50;r++)
disp();
while(key1)
{
wei=2;
disp();
if(key2==0)
{
disp();
if(key2==0)
{
temp=now_time[2]/16*10+now_time[2]%16;
temp++;
if(temp>=24)
temp=0;
now_time[2]=temp/10*16+temp%10;
write_data_ds1302(0x84,now_time[2]);
}
while(!key2);
}
}
wei=1;
d[2]=0;
for(r=0;r<50;r++)
disp();
while(key1)
{disp();
if(key2==0)
{
disp();
if(key2==0)
{
temp=now_time[1]/16*10+now_time[1]%16;
temp++;
if(temp>=60)
temp=0;
now_time[1]=temp/10*16+temp%10;
write_data_ds1302(0x82,now_time[1]);
}
while(!key2);
}
}
wei=0;
d[1]=0;
for(r=0;r<50;r++)
disp();
while(key1)
{
disp();
if(key2==0)
{
now_time[0]=0;
write_data_ds1302(0x80,now_time[0]);
}
} TR1=0;
d[0]=0;
for(r=0;r<30;r++)
disp();
}
按图安装即可,祝你成功。