『壹』 用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();
}
按圖安裝即可,祝你成功。