这个是显示
电子钟与秒表的程序 用数码管显示的
我想按下anj_2这个键能切换秒表与电子钟,再按下再切回来的程序不知道怎么弄求高手指导下
#define _YIZHIBIAO_C_
#include "reg51.h"
#include "yizhibiao.h"
#define uc unsigned char
#define led_f P0
#define led_k P2
uc code led[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xbf};
//uc code led[]={0x3F,0x6,0x5B,0x4F,0x66,0x6D,0x7D,0x7,0x7F,0x6F,0x40};
uc led_M[]={1,2,3,4,5,6,7,8};
uc led_c=0;
//初始表的时间定义
uc miao=57;
uc fen=59;
uc zhong=23;
//初始秒表的时间的定义
uc miao2=0;
uc fen2=0;
uc zhong2=0;
//闪烁的定义
uc led_fs=0x00;
uc led_fc;
uc yansi=100;//闪0.5秒定义
//按键的定义
sbit anj_0=P1^0;
sbit anj_1=P1^1;
sbit anj_2=P1^2;
sbit anj_3=P3^2;
uc jz;
uc yanshi=200;//中断1秒的
uc yanshi2=2;//中断1秒的
bit zdbz=1;//钟的标志
bit mbdbz=1;//秒表的标志
bit sz;
bit up;
bit okj;
void delay20() //20毫秒
{
unsigned char a,b;
for(b=1;b>0;b--)
for(a=7;a>0;a--);
}
void shizhong()
{
led_M[0]=zhong/10;
led_M[1]=zhong%10;
led_M[2]=10;
led_M[3]=fen/10;
led_M[4]=fen%10;
led_M[5]=10;
led_M[6]=miao/10;
led_M[7]=miao%10;
}
void jiashu()
{
if(++miao>59)
{
miao=0;if(++fen>59)
{
fen=0;if(++zhong>23)
{
zhong=0;
}
}
}
}
void miaobiao()
{
led_M[0]=zhong2/10;
led_M[1]=zhong2%10;
led_M[2]=10;
led_M[3]=fen2/10;
led_M[4]=fen2%10;
led_M[5]=10;
led_M[6]=miao2/10;
led_M[7]=miao2%10;
}
void jiashu2()
{
if(++miao2>100)
{
miao2=0;if(++fen2>59)
{
fen2=0;if(++zhong2>99)
{
zhong2=0;
}
}
}
}
void anjian()
{
if(jz==3)
{
okj=1;
}
}
//5000ms的中断程序
void Init
timer0(void)
{
TMOD = 0x01;
TH0 = 0x0EC;
TL0 = 0x00;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void main(void)
{
InitTimer0();
while(1)
{
anjian();
//显示钟的
if(zdbz==0)
{
zdbz=1;
if(okj==1)
{
shizhong();
}
jiashu();
}
//显示秒表的
if(mbdbz==0)
{
mbdbz=1;
if(okj==0)
{
miaobiao();
}
jiashu2();
}
};
}
void Timer0Interrupt() interrupt 1
{
TH0 = 0x0Ec;
TL0 = 0x00;
led_f=0xff;
led_k=0xff;
if(++led_c>7)
led_c=0;
led_f=led[led_M[led_c]];
switch(led_c)
{
case 0:led_k=0xfe|led_fs;break;
case 1:led_k=0xfd|led_fs;break;
case 2:led_k=0xfb|led_fs;break;
case 3:led_k=0xf7|led_fs;break;
case 4:led_k=0xef|led_fs;break;
case 5:led_k=0xdf|led_fs;break;
case 6:led_k=0xbf|led_fs;break;
case 7:led_k=0x7f|led_fs;break;
}
//钟的中断
if(--yanshi==0)
{
yanshi=200;
zdbz=0;
}
//秒表的中断
if(--yanshi2==0)
{
yanshi2=2;
mbdbz=0;
}
//闪烁的中断
if(--yansi==0)
{
yansi=100;
led_fs=led_fs^led_fc;
}
//按键的中断
if(anj_3==0)
{
if(anj_0==0)
{
jz=1;
}
if(anj_1==0)
{
jz=2;
}
if(anj_2==0)
{
jz=3;
}
}
}
实话勿喷
一周热门 更多>