利用定时器来让标志位进行翻转,然后根据标志位让数码管进行亮灭
#include <STC15F2K60S2.H>
unsigned char count;
unsigned smg_bit=1;
void select138(unsigned char n)
{
switch(n)
{
case 4:
P2=(0x1f&P2)|0x80;
break;
case 5:
P2=(0x1f&P2)|0xa0;
break;
case 6:
P2=(0x1f&P2)|0xc0;
break;
case 7:
P2=(0x1f&P2)|0xe0;
break;
case 0:
P2=(0x1f&P2)|0x00;
break;
}
}
void smgshow_bite(unsigned char date,unsigned char pos)
{
select138(6);
P0=0x01<<pos;
select138(7);
P0=date;
}
void delay(unsigned char i)
{
while(i--);
}
void smg()
{
if(smg_bit==1)
{
smgshow_bite(0xc0,0);
delay(500);
}
else
{
select138(6);
P0=0xff;
select138(7);
P0=0xff;
}
}
void Timer0Init()
{
TMOD=0x01;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
EA=1;
TR0=1;
ET0=1;
}
void service () interrupt 1
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
count++;
if(count>=20)
{
smg_bit=~smg_bit;
count=0;
}
}
void main()
{
Timer0Init();
while(1)
{
smg();
}
}