⑴ 下面是51單片機30秒倒計時程序,怎麼把它改成24小時倒計時程序,別的不變,十分感謝
#include "reg51.h"
sbit start=P3^2; //外部中斷0引腳 開始
sbit rst=P3^3; //外部中斷1引腳 復位
sbit breakk=P3^4;
sbit led=P3^5;
sbit beep=P3^7;
sbit P30=P3^0;
sbit P31=P3^1;
unsigned char code anma[]=
{
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff
};
char time=24;//【原來的30s改為24小時】
char flag_t=0; // 1秒輔助計時
char now=0; // 0停止 1 計時 2報警 3復位
unsigned int temp=0;//【添加此行】// 【定義一個16位臨時變數】
unsigned char g=0,s=3,i=0;
void delay(unsigned int n)
{
while(--n);
}
void t0_srv() interrupt 1 using 1
{
TH0=0X3C;
TL0=0XB0;
flag_t++;
if(flag_t>=20){ //20*50ms=1s
flag_t=0;
temp++; //【添加此行】
if(temp>=3600)//1小時=3600s //【添加此行】
{
temp=0;//【添加此行】
time--;
if(time<=0){
beep=0;
led=0;
TR0=0;
now=2;
time=0;
}
}
}
}
void button_start() interrupt 0 using 1 //外部中斷0
{
delay(1500);
if(start==0) {
now=1;
TR0=1;
}
else return;
while(start==0);
delay(1500);
}
void button_rst() interrupt 2 using 1
{
delay(1500);
if(rst==0) {
TR0=0;
time=24;//【原為time=30;】
i=0;
led=1;
beep=1;
now=0;
}
else return;
while(rst==0);
delay(1500);
}
void disp()
{
g=time%10;
s=time/10;
g=anma[g];
s=anma[s];
P1=g;
P31=0;
delay(250);
P31=1;
P1=s;
P30=0;
delay(250);
P30=1;
//
}
void key()
{
if(breakk==0)delay(1500);
else return;
if(breakk==0) {
if(now==1){
i++;
if(i%2)TR0=0;
if(!(i%2))TR0=1;
}
}
else return;
while(breakk==0);
delay(1500);
}
main()
{
TMOD=0X01;//定時器0工作在方式1,
EA=1;
IT0=1;
ET0=1;
TH0=0X3C;//在12M 晶振下,定時時間為50ms
TL0=0XB0;
EX0=1;
EX1=1;
//TR0=1;
while(1){
disp();
key();
}
}
⑵ 誰能告訴我51單片機簡單的led數碼管時鍾程序 24小時制的(c語言版的)
#include "reg52.h"
#define uint unsigned int
#define uchar unsigned char
uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar shi,fen,miao;
uchar time;
void delay(uint x)
{
uint y;
for(;x>0;x--)
{
for(y=0;y<124;y++);
}
}
void display(uchar shi,uchar fen,uchar miao)
{
P2=0; //位碼
P0=(tab[shi/10]); //段碼
delay(2);
P2=1;
P0=(tab[shi%10]);
delay(2);
P2=2; //位碼
P0=0x40; //段碼
delay(2);
P2=3; //位碼
P0=(tab[fen/10]); //段碼
delay(2);
P2=4;
P0=(tab[fen%10]);
delay(2);
P2=5; //位碼
P0=0x40; //段碼
delay(2);
P2=6; //位碼
P0=(tab[miao/10]); //段碼
delay(2);
P2=7;
P0=(tab[miao%10]);
delay(2);
}
void main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
if(time==20)
{
time=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
shi=0;
}
}
}
display(shi,fen,miao);
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
time++;
}
/*還有什麼不明白繼續追加*/
⑶ 單片機時鍾程序 24小時制
C語言的:
#include <reg51.h>
#include<stdio.h>
unsigned char ledbuf[8];
code unsigned char ledmap[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99,
0x92, 0x82, 0xf8, 0x80, 0x90}; //8段顯示
void delay(unsigned char cnt)
{
unsigned char i;
while(cnt--!=0)
for(i=100;i!=0;i--);
}
#define tick 7554
#define T100us (256-134)
unsigned char hour,minute,second,ankey;
unsigned int c100us;
void sees()
{
ledbuf[0]=ledmap[hour/10];
ledbuf[1]=ledmap[hour%10];
ledbuf[2]=0xbf;
ledbuf[3]=ledmap[minute/10];
ledbuf[4]=ledmap[minute%10];
ledbuf[5]=0xbf;
ledbuf[6]=ledmap[second/10];
ledbuf[7]=ledmap[second%10];
}
void displayled()
{
char i;
unsigned char pos;
pos=0x01;
for(i=7;i>=0;i--)
{
P2=0x255;
P0=ledbuf[i];
P2=~pos;
delay(2);
P2=0x255;
pos<<=1;
}
}
void key()
{
if(P32==0||P33==0||P35==0)
delay(2);
if(P32==0||P33==0||P35==0)
{
if(P32==0)
{
hour++;
if(hour==24)
hour=0;
while(P32==0)
{
sees();
displayled();
}
}
if(P33==0)
{
minute++;
if(minute==60)
minute=0;
while(P33==0)
{
sees();
displayled();
}
}
if(P35==0)
{
second++;
if(second==60)
second=0;
while(P35==0)
{
sees();
displayled();
}
}
}
}
void t0int()interrupt 1
{
c100us--;
if(c100us==0)
{
c100us=tick;
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
if(hour==24)hour=0;
}
}
}
}
void main()
{
TMOD=0x02;
TH0=T100us;
TL0=T100us;
IE=0x82;
hour=0;
minute=0;
second=0;
c100us=tick;
TR0=1;
while(1)
{
sees();
displayled();
key();
}
}
匯編的:
ORG 0000H
AJMP MAIN
ORG 000BH
AJMP TIME
ORG 0300H
MAIN:
MOV P3,#0FFH;
MOV 25H,#00H;
MOV R0,#40H;
MOV 20H,#00H;
MOV 21H,#00H;
MOV 22H,#00H;
MOV 23H,#00H;
MOV P2,#0FFH
MOV IP,#02H;
MOV IE,#82H;
MOV TMOD,#01H;
MOV TL0,#0B0H;
MOV TH0,#3CH;
SETB TR0;
MOV SP,#50H;
NEXT:
LCALL DISP;
LCALL KEY;
JZ NEXT;
LCALL ANKEY;
SJMP NEXT;
NOP
NOP;
NOP;
TIME:
PUSH ACC;
PUSH PSW;
MOV TL0,#0B4H;
MOV TH0,#3CH;
INC 20H;
MOV A,20H;
CJNE A,#20,RETI1;
MOV 20H,#00H;
MOV A,21H;
INC A;
MOV 21H,A;
CJNE A,#3CH,RETI1;
MOV 21H,#00H;
MOV A,22H;
ADD A,#01H;
MOV 22H,A;
CJNE A,#3CH,RETI1;
MOV 22H,#00H;
MOV A,23H;
ADD A,#01H;
MOV 23H,A;
CJNE A,#18H,RETI1;
MOV 23H,#00H;
RETI1:
POP PSW;
POP ACC;
RETI;
NOP
NOP
DISP:
MOV A,21H;
MOV B,#10;
DIV AB;
MOV 41H,A;
MOV 40H,B;
MOV A,22H;
MOV B,#10;
DIV AB;
MOV 43H,A;
MOV 42H,B;
MOV A,23H;
MOV B,#10;
DIV AB;
MOV 45H,A;
MOV 44H,B;
MOV R0,#40H
LCALL DISP2; 1
CLR P2.0;
LCALL DELAY;
SETB P2.0;
INC R0;
LCALL DISP2;
CLR P2.1;
LCALL DELAY;
SETB P2.1;
MOV P0,#0bfh;
CLR P2.2 ;
LCALL DELAY;
SETB P2.2;
INC R0;
LCALL DISP2;
CLR P2.3;
LCALL DELAY;
SETB P2.3;
INC R0;
LCALL DISP2;
CLR P2.4;
LCALL DELAY;
SETB P2.4;
MOV P0,#0BFH;
CLR P2.5;
LCALL DELAY;
SETB P2.5;
INC R0;
LCALL DISP2;
CLR P2.6;
LCALL DELAY;
SETB P2.6;
INC R0;
LCALL DISP2;
CLR P2.7;
LCALL DELAY;
SETB P2.7;
MOV R0,#40H;
DISP2:
MOV DPTR,#TABLE;
MOV A,@R0;
MOVC A,@A+DPTR;
MOV P0,A;
RET
DELAY:
MOV R6,#255;
D2:DJNZ R6,D2;
RET;
TABLE:DB 0c0h,0f9h,0a4h,0b0h,99h,92h,82h,0f8h,80h,90h
; 0 1 2 3 4 5 6 7 8 9
;按鍵判斷程序
KEY:
MOV A,P3;
CPL A;
ANL A,#3CH;
JZ RETX ;無鍵按下則返回
LCALL DISP ;
LCALL DELAY;
MOV A,P3;
CPL A;
ANL A,#3CH;
JZ RETX ;無鍵按下則返回
MOV R6,A ;將鍵值存入R6。
LOOP2: LCALL DISP ;
MOV A,P3;
CPL A;
ANL A,#3CH;
JNZ LOOP2 ;等待鍵釋放
MOV A,R6;
RETX:
MOV P3,#0FFH;
RET;
NOP
NOP
;按鍵處理子程序
ANKEY: CLR EA;關中斷
LX: MOV A,R6;
JB ACC.2,L1;是功能鍵轉L1
JB ACC.3,L2;是確認鍵轉L2
JB ACC.4,L3;是減1鍵轉L3
JNB ACC.5,L12;不是增1鍵,轉L12
JB 2BH.4,L6;判斷使哪一位(時、分、秒)的值加1
JB 2DH.4,L8;
JB 2FH.4,L9;
L12: LCALL DISP;
LCALL DISP;
LCALL KEY;判斷有無鍵按下。
JZ L12;
LJMP LX;
L2: MOV 25H,#00H ;確認鍵處理程序
CLR 2BH.4;
CLR 2DH.4;
CLR 2FH.4;
SETB EA;
RET;
L3: JB 2BH.4,L61;減一鍵處理程序
JB 2DH.4,L81;
JB 2FH.4,L91;
AJMP L12;
L1: MOV A,25H;功能鍵處理程序
JZ LB1;
JB ACC.0,LB2;
JB ACC.1,LB3;
JNB ACC.2,L12;
LB1: MOV 25H,#01H;25H單元是標志位,(25H)=01H調節時單元的值
SETB 2BH.4;
CLR 2DH.4;
CLR 2FH.4;
AJMP L12;
LB3: MOV 25H,#04H;25H單元是標志位,(25H)=04H調節秒單元的值
SETB 2FH.4;
CLR 2DH.4;
CLR 2BH.4;
AJMP L12;
LB2: MOV 25H,#02H;25H單元是標志位,(25H)=02H調節分單元的值
SETB 2DH.4;
CLR 2BH.4;
CLR 2FH.4;
AJMP L12;
L61: AJMP L611;
L81: AJMP L811;
L91: AJMP L911;
L112:AJMP L12;
L6: MOV A,23H;時加一
INC A;
MOV 23H,A;
CJNE A,#24H,L12;
MOV 23H,#00H;
AJMP L12;
L8: MOV A,22H;分加一
INC A;
MOV 22H,A;
CJNE A,#60H,L12;
MOV 22H,#00H;
AJMP L12;
L9: MOV A,21H;秒加一
INC A;
MOV 21H,A;
CJNE A,#60H,L12;
MOV 21H,#00H;
AJMP L12;
L611: MOV A,23H;時減一
DEC A;
MOV 23H,A;
CJNE A,#00H,L112;
MOV 23H,#23H;
AJMP L12;
L811: MOV A,22H;分減一
DEC A;
MOV 22H,A;
CJNE A,#00H,L112;
MOV 22H,#59H
AJMP L12;
L911: MOV A,21H;秒減一
DEC A;
MOV 21H,A;
CJNE A,#00H,L112;
MOV 21H,#59H;
AJMP L12;
NOP
NOP
END
⑷ 用單片機設計一個時鍾,可顯示時和分,可以調時間,也要有鬧鍾功能,要有設計的電路圖
其實不用定時中斷也能實現功能:
#include<reg51.h> 主函數
unsigned char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};定義0-9數組
unsigned int tmp;定義變數
void delay(unsigned int xms)定義延時函數
{unsigned int j,i;
for(i=0;i<xms;i++)
for(j=0;j<100;j++);
}
void disp()定義子函數
{
P1=tmp;
delay(1);
P2=0xff;
tmp=tmp<<1;
}
void main( )
{
unsigned char z,s=00,m=00,h=00;給時鍾初始值
while(1)
{
for(z=0;z<100;z++)
{
tmp=0x01;
P2=tab[h/10];小時顯示
disp();
P2=tab[h%10];
disp();
P2=tab[m/10];分鍾顯示
disp();
P2=tab[m%10];
disp();
P2=tab[s/10];秒顯示
disp();
P2=tab[s%10];
disp();
}
s++;
while(s==60)秒進一位,到60清0
{
m++;
s=00;
}
while(m==60)分鍾進一位,到60清0
{
h++;
m=00;
}
while(h==24)小時進一位,到24清0
{
h=00;
}
}
}
⑸ 請問如何使用單片機定時器製作一個以00-00-00為初始的24小時制時鍾
程序比較長,可用定時器定時50mS,累積20次,即1S,秒加1,60S後分加1,秒清0,60分後,小時加1,分清0 ,24小時後小時清0,這些都在定時器中斷程序中完成,主程序只管顯示時 分 秒即可。
⑹ 單片機課程設計 設計製作一個24小時制多功能數字鍾
額。。。。。這個東西就是零碎的東西加起來變成一個一個整體的啊,講個思路,寫個中斷服務程序,裡面的是led燈亮的程序,和1秒的計時程序,寫個beep子程序控制蜂鳴器,寫個display子程序,控制icl0809,主程序循環控制,顯示數碼管,顯示am,過了12,進入中斷,顯示pm,這個其實很簡單的,我才大三,單片機才學了四個星期,我都會做,相信你能行的。。。