关于MCS-51单片机、你们懂得!
#include "reg51.h"
unsigned char code disptab[] =
{0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,
0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF};
unsigned char code bittab[] =
{0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};
unsigned char hour=12,min=0,sec=0;//时分秒
unsigned int count;//定时1ms计数次数
unsigned char num=0;//数码管序号
void disp() //显示函数
{
num++;
num=(num%6);
P2=0xff; //数码管消影
P0=0xff;
P2=bittab[num];
switch(num)
{
case 0: P0=disptab[sec%10]; break;//秒个位
case 1: P0=disptab[sec/10]; break;//秒十位
case 2: P0=disptab[min%10]; break;//分个位
case 3: P0=disptab[min/10]; break;//分十位
case 4: P0=disptab[hour%10]; break;//时个位
case 5: P0=disptab[hour/10]; break;//时十位
default: ;
}
//if(num==2||num==4)P0=P0&0x7f;
}
void t0() interrupt 1 //T0中断函数
{
TL0=0x6c;//赋初值,重新定时1ms
TH0=0xfc;
count++;
if(count==1000)
{
count=0;
sec=sec+1;
if(sec==60) {sec=0;min++;}
if(min==60) {min=0;hour++;}
if(hour==24) {hour=0;}
}
disp();
}
void main() //主函数
{
TMOD=0x01; //T0工作方式1,定时
TH0=0xfc; //定时1ms,赋初值
TL0=0x6c;
ET0=1;
EA=1;
TR0=1;
while(1);
}
通过修改源程序设计一个可预置倒计时秒表!按键S1调节十位,按键S2调各位,预置范围为(0-59),按键S3负责启动S4暂停,暂停时现实最后记时值,当倒计时进行时s1和s2不起作用,倒计时到0时蜂鸣器报警1秒,又回到预置值准备倒计时。
提示:通过定时和中断记性数码管动态扫描显示,在主程序中判断按键,通过一个位变量来表示倒计时的运行于暂停状态,在1s计时到的时候判断标识状态以确定是否要对计数值减一 展开
我给你一个电子钟,闹钟功能的程序,你参考一下。
#include"at89x51.h"
unsigned char time0=1,time1=2,time2=0,time3=0;
unsigned char times0=1,times1=2,times2=0,times3=0;
unsigned char SetFlag = 0;
unsigned int a=0;
unsigned int clockflag =0;
unsigned char SetBit = 0;
unsigned int b=1;
unsigned int c=0;
void delay_ms(unsigned int a)
{
unsigned int i;
while(a-->0)
{
i =70;
while(i-->0);
}
}
void disp_time()
{
int i=50;
while(i-->0)
{
P1 = 0x70|time0;
delay_ms(2);
P1 = 0xb0|time1;
delay_ms(2);
P1 = 0xd0|time2;
delay_ms(2);
P1 = 0xe0|time3;
delay_ms(2);
}
}
void disp_settime0()
{
int i=50;
if(P3_4==0)
{
delay_ms(15);
if(P3_4==0)
{
time0++;
if(time0==2)
{
time0 = 0;
}
delay_ms(15);
}
}
while(i-->0)
{
if(i>25)
{
P1 = 0xf0|time0;
}else
{
P1 = 0x70|time0;
}
delay_ms(2);
P1 = 0xb0|time1;
delay_ms(2);
P1 = 0xd0|time2;
delay_ms(2);
P1 = 0xe0|time3;
delay_ms(2);
}
}
void disp_settime1()
{
int i=50;
if(P3_4==0)
{
delay_ms(15);
if(P3_4==0)
{
time1++;
if(time0 == 0)
{
if(time1==10)
{
time1 = 0;
}
}else
{
if(time1==3)
{
time1 = 0;
}
}
delay_ms(15);
}
}
while(i-->0)
{
P1 = 0x70|time0;
delay_ms(2);
if(i>25)
{
P1 = 0xf0|time1;
}else
{
P1 = 0xb0|time1;
}
delay_ms(2);
P1 = 0xd0|time2;
delay_ms(2);
P1 = 0xe0|time3;
delay_ms(2);
}
}
void disp_settime2()
{
int i=50;
if(P3_4==0)
{
delay_ms(15);
if(P3_4==0)
{
time2++;
if(time2==6)
{
time2 = 0;
}
delay_ms(15);
}
}
while(i-->0)
{
P1 = 0x70|time0;
delay_ms(2);
P1 = 0xb0|time1;
delay_ms(2);
if(i>25)
{
P1 = 0xf0|time2;
}else
{
P1 = 0xd0|time2;
}
delay_ms(2);
P1 = 0xe0|time3;
delay_ms(2);
}
}
void disp_settime3()
{
int i=50;
if(P3_4==0)
{
delay_ms(15);
if(P3_4==0)
{
time3++;
if(time3==10)
{
time3 = 0;
}
delay_ms(15);
}
}
while(i-->0)
{
P1 = 0x70|time0;
delay_ms(2);
P1 = 0xb0|time1;
delay_ms(2);
P1 = 0xd0|time2;
delay_ms(2);
if(i>25)
{
P1 = 0xf0|time3;
}else
{
P1 = 0xe0|time3;
}
delay_ms(2);
}
}
void disp_settimes0()
{
int i=50;
if(P3_4==0)
{
delay_ms(15);
if(P3_4==0)
{
times0++;
if(times0==2)
{
times0 = 0;
}
delay_ms(15);
}
}
while(i-->0)
{
if(i>25)
{
P1 = 0xf0|times0;
}else
{
P1 = 0x70|times0;
}
delay_ms(2);
P1 = 0xb0|times1;
delay_ms(2);
P1 = 0xd0|times2;
delay_ms(2);
P1 = 0xe0|times3;
delay_ms(2);
}
}
void disp_settimes1()
{
int i=50;
if(P3_4==0)
{
delay_ms(15);
if(P3_4==0)
{
times1++;
if(times0 == 0)
{
if(times1==10)
{
times1 = 0;
}
}else
{
if(times1==3)
{
times1 = 0;
}
}
delay_ms(15);
}
}
while(i-->0)
{
P1 = 0x70|times0;
delay_ms(2);
if(i>25)
{
P1 = 0xf0|times1;
}else
{
P1 = 0xb0|times1;
}
delay_ms(2);
P1 = 0xd0|times2;
delay_ms(2);
P1 = 0xe0|times3;
delay_ms(2);
}
}
void disp_settimes2()
{
int i=50;
if(P3_4==0)
{
delay_ms(15);
if(P3_4==0)
{
times2++;
if(times2==6)
{
times2 = 0;
}
delay_ms(15);
}
}
while(i-->0)
{
P1 = 0x70|times0;
delay_ms(2);
P1 = 0xb0|times1;
delay_ms(2);
if(i>25)
{
P1 = 0xf0|times2;
}else
{
P1 = 0xd0|times2;
}
delay_ms(2);
P1 = 0xe0|times3;
delay_ms(2);
}
}
void disp_settimes3()
{
int i=50;
if(P3_4==0)
{
delay_ms(15);
if(P3_4==0)
{
times3++;
if(times3==10)
{
times3 = 0;
}
delay_ms(15);
}
}
while(i-->0)
{
P1 = 0x70|times0;
delay_ms(2);
P1 = 0xb0|times1;
delay_ms(2);
P1 = 0xd0|times2;
delay_ms(2);
if(i>25)
{
P1 = 0xf0|times3;
}else
{
P1 = 0xe0|times3;
}
delay_ms(2);
}
}
void rupt1(void) interrupt 0
{
if(SetFlag == 0)
{
SetFlag = 1;
TR0 = 0;
P3_0 = 1;
clockflag = 0;
SetBit = 0;
b = 1;
return;
}
if(SetFlag == 1)
{
SetFlag = 0;
TR0 = 1;
b = 0;
c = 0;
return;
}
}
void rupt2(void) interrupt 2
{
if(SetFlag == 0)
{
SetBit = 0;
SetFlag = 2;
return;
}
if(SetFlag == 2)
{
SetFlag = 0;
b =0;
return;
}
}
void timer(void) interrupt 1
{
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
a++;
if(a%10==0)
{
P3_0 = ~P3_0;
}
if(a==20)
{
a=0;
clockflag++;
}
if(clockflag==60)
{
clockflag = 0;
time3++;
if(time3==10)
{
time3 = 0;
time2++;
if(time2==6)
{
time2 = 0;
time1++;
if((time0==0)&(time1==10))
{
time0 = 1;
time1 = 0;
}
if((time0==1)&&(time1==3))
{
time0 = 0;
time1 = 1;
}
}
}
}
}
void timers(void) interrupt 3
{
TH1 = (65536-50000)/256;
TL1 = (65536-50000)%256;
c++;
if(c==600)
{
TR1=0;
b = 1;
c =0;
}
}
void main()
{
int j = 0;
TMOD = (TMOD&0xf0)|0x01;
TMOD = (TMOD&0x0f)|0x10;
TH1 = (65536-50000)/256;
TL1 = (65536-50000)%256;
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
P3_0 = 1;
EX0 = 1;
EX1 = 1;
ET0 = 1;
ET1 =1;
EA = 1;
IT0 = 1;
IT1 = 1;
TR0 = 1;
while(1)
{
if(SetFlag == 0)
{
disp_time();
}
if(SetFlag == 1)
{
if(P3_5==0)
{
delay_ms(15);
if(P3_5==0)
{
SetBit++;
if(SetBit==4)
{
SetBit = 0;
}
delay_ms(15);
}
}
switch(SetBit)
{
case 0: disp_settime0(); break;
case 1: disp_settime1(); break;
case 2: disp_settime2(); break;
case 3: disp_settime3(); break;
}
}
if(SetFlag == 2)
{
if(P3_5==0)
{
delay_ms(15);
if(P3_5==0)
{
SetBit++;
if(SetBit==4)
{
SetBit = 0;
}
delay_ms(15);
}
}
switch(SetBit)
{
case 0: disp_settimes0(); break;
case 1: disp_settimes1(); break;
case 2: disp_settimes2(); break;
case 3: disp_settimes3(); break;
}
}
if((time0==times0)&&(time1==times1)&&(time2==times2)&&(time3==times3)&&(b==0))
{
TR1 = 1;
for(j=0;j<5;j++)
{
P3_7 = 1;
delay_ms(2);
P3_7 = 0;
delay_ms(2);
}
}
}
}
unsigned char code disptab[] =
{0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,
0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF};
unsigned char code bittab[] =
{0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};
unsigned char hour=12,min=0,sec=0;//时分秒
unsigned int count;//定时1ms计数次数
unsigned char num=0;//数码管序号
sbit K1=P3^4;//按键,接在P3-4567,你随便接,记得改定义!
sbit K2=P3^5;
sbit K3=P3^6;
sbit K4=P3^7;
sbit bee=P3^3;//蜂鸣器
void disp() //显示函数
{
num++;
if(num==2)num=0;//你说是秒表,分就没用了
num=(num%6);
P2=0xff; //数码管消影
P0=0xff;
P2=bittab[num];
switch(num)
{
case 0: P0=disptab[sec%10]; break;//秒个位
case 1: P0=disptab[sec/10]; break;//秒十位
case 2: P0=disptab[min%10]; break;//分个位
case 3: P0=disptab[min/10]; break;//分十位
case 4: P0=disptab[hour%10]; break;//时个位
case 5: P0=disptab[hour/10]; break;//时十位
default: ;
}
//if(num==2||num==4)P0=P0&0x7f;
}
void t0() interrupt 1 //T0中断函数
{
TL0=0x6c;//赋初值,重新定时1ms
TH0=0xfc;
count++;
if(count==1000)
{
count=0;
if(sec==0)sec=1;//防止在自减前就已经是0了
sec=sec-1;//这里改成减!
// if(sec==60) {sec=0;min++;}
// if(min==60) {min=0;hour++;}你要的是秒表,这两个就不要了
// if(hour==24) {hour=0;}
}
//disp();这个函数移动下位置,我要把它放在主函数里
}
void delayms(unsigned char t) //叫delayms其实被改过,并非延迟1ms
{
unsigned char k;
while (t--)
{
for (k = 0; k < 11; k++)
;
}
}
/*开关K4检测函数 *************************************/
uchar j_K4()
{
if(K4==0)
{
delayms(40);
if(K4==0)
{
while(1)
{
if(K4==1)
{
delayms(40);
if(K4==1)break;
}
}
return 1;
}
else return 0;
}
else return 0;
}
/*开关K3检测函数 *************************************/
uchar j_K3()
{
if(K3==0)
{
delayms(40);
if(K3==0)
{
while(1)
{
if(K3==1)
{
delayms(40);
if(K3==1)break;
}
}
return 1;
}
else return 0;
}
else return 0;
}
uchar j_K2()
{
if(K2==0)
{
delayms(40);
if(K2==0)
{
while(1)
{
if(K2==1)
{
delayms(40);
if(K2==1)break;
}
}
return 1;
}
else return 0;
}
else return 0;
}
uchar j_K1()
{
if(K1==0)
{
delayms(40);
if(K1==0)
{
while(1)
{
if(K1==1)
{
delayms(40);
if(K1==1)break;
}
}
return 1;
}
else return 0;
}
else return 0;
}
void beep()
{
unsiged char i,j;
for(i=0;i<250;i++)
{
bee=0;
for(j=0;j<144;j++);
bee=1;
for(j=0;j<84;j++);
}
}
void main() //主函数
{
unsigned char i;
sec=0;
while(1)
{
disp();
for(i=0;i<100;i++);//延迟,可以使数码管更清晰
if(j_K1())
{
sec+=10;
}
if(j_K2())
{
sec++;
}
if(sec>60)sec=60;
if(j_K3())
{
TMOD=0x01; //T0工作方式1,定时
TH0=0xfc; //定时1ms,赋初值
TL0=0x6c;
ET0=1;
EA=1;
TR0=1;
while(1)
{
disp();
for(i=0;i<100;i++);//延迟,可以使数码管更清晰
if(sec==0){beep();beep();sec=0;break;}
if(j_K4())
{
ET0=0;
EA=0;
TR0=0;
break;
}
}
}
}
}
别忘了给分!