#include <reg51.h>
#define uchar unsigned char //宏定义?
#define uint unsigned int
void delay (uchar n );
sfr p0 = 0x80;
sfr p2 = 0xA0;
sfr p1 = 0x90;
sbit dula = P2^6; //段选,申明U1锁存器的锁存端,锁存器使用的是74HC573?
sbit wela = P2^7; //位选,申明U2锁存器的锁存端,锁存器使用的是74HC573?
sbit beep = p2^3;
sbit led1=p1^0;
uchar DisBuf[4];
uchar Minute,Second,Counter;
uint Count=0;
int num=0;
int flag=1;
int frq;
int i,j ;
uchar code SegCode[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //数组,用于数码管显示数字的数字集,从0
uchar code BitCode[]=
{0xfe,0xfd,0xfb,0xf7};
void BufToSeg(void)
{
uchar i;
for(i=0;i<4;i++) {
if(i==2)
{
dula = 1;
P0 = SegCode[DisBuf[ i]];
dula = 0;
}
dula = 1; //打开U1锁存器,用于控制显示的数据?
P0 = SegCode[DisBuf[ i]]; //送入数据到锁存器U1,送入段选信号,显示什么数字?
dula = 0; //关闭U1锁存器?
P0 = 0xff; //消影,因为P0口任然保持上次的数据?
wela = 1; //打开U2锁存器,用于控制哪个数码管亮?
P0 = BitCode[ i];
wela = 0; //关闭U1锁存器?
delay(3); //延时五个毫秒?
}
}
void delay (uchar n )
{
uchar j;
while(n--)
{
for(j=0;j<113;j++);
}
}
void NumToBuf(void)
{
DisBuf[0]=Minute/10;
DisBuf[1]=Minute%10;
DisBuf[2]=Second/10;
DisBuf[3]=Second%10;
}
void main()
{
if(flag==1)
{
TMOD = 0x01; //设置定时器0在工作方式1?
TH0 = (65536-45872)/256; //装初值,50ms申请一次中断?
TL0 = (65536-45872)%256;
EA = 1; //开总中断?
ET0 = 1; //开定时器0中断?
TR0 = 1; //启动定时器0?
}
while(1)
{
NumToBuf();
BufToSeg();
delay(1);
led1=1;
beep=1;
if(Minute==10)
{
TR0=0;
delay(30);
led1=~led1; 结果是灯和倒计时的前三位一起闪烁,希望大神可以指点,
我想只让灯闪,或者就让灯和倒计时的四位一起闪,小白求指点
delay(30);
beep=~beep;
}
}
}
void T0_time() interrupt 1 //1号中断,定时器0的中断号?
{
TH0 = (65536-45872)/256; //装初值,50ms申请一次中断?
TL0 = (65536-45872)%256;
Counter++; //申请一次中断num加1一次?
if(Counter==1) //num加到20时,50*20=1000ms,说明一秒钟到了?
{
Counter=0;
Second++;
if(Second==60)
{
Second=0;
Minute++;
num++;
}
}
}
|