⑴ 下面是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,这个其实很简单的,我才大三,单片机才学了四个星期,我都会做,相信你能行的。。。