1、静态
/*
#include
unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
void delayms(int ms)
{
int i,j;
for(i=ms;i>0;i--)
for(j=845;j>0;j--);
}
void main()
{
int i;
P2=0xa0;P0=0x00;P2=0xc0;P0=0xff;P2=0xff;
while(1)
{
for(i=0;i<12;i++)
{
P0 = tab[i];
delayms(1000);
}
}
}
*/
/*系统上电之后数码管显示0,按下S4用定时器以间隔500ms在数码管上依次显示0,1,2,3,……,C,D,E,F,重复。
按下S5用定时器以间隔1s在数码管上依次显示0,1,2,3,……,A,b,C,d,E,F,重复。
按下S6用定时器以间隔2s在数码管上依次显示0,1,2,3,……,C,D,E,F,重复。
按下S7用定时器以间隔5s在数码管上依次显示0,1,2,3,……,A,b,C,d,E,F,重复。*/
#include
#include
#define uchar unsigned char
#define uint unsigned int
sfr AUXR = 0x8e;
sbit s7 = P3^0;
sbit s6 = P3^1;
sbit s5 = P3^2;
sbit s4 = P3^3;
uchar code tab1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e};
uchar code tab2[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa0,0x86,0x8e};
uint tt=0;
void Delay1ms();
void delayms(int ms);
void Timer0Init();
void keyscan4();
void main()
{
P2=0xa0;P0=0x00;P2=0x80;P0=0xff;P2=0xc0;P0=0xff;P2=0xff;//关蜂鸣器,开数码管
Timer0Init();
EA = 1;
while(1)
{
keyscan4();
}
}
//独立按键
void keyscan4()
{
uchar i;
if(s4 == 0)
{
delayms(20); //延时消抖
if(s4 == 0)
{
ET0 = 1;
for(i=0;i<16;i++)
{
P0 = tab1[i];
while(tt != 100); //用定时器0来延时500ms
tt = 0;
}
ET0 = 0;
}
while(!s4); //确保每次按键执行一次
}
if(s5 == 0)
{
delayms(20); //延时消抖
if(s5 == 0)
{
ET0 = 1;
for(i=0;i<16;i++)
{
P0 = tab2[i];
while(tt/2 != 100); //用定时器0来延时500ms
tt = 0;
}
ET0 = 0;
}
while(!s5); //确保每次按键执行一次
}
if(s6 == 0)
{
delayms(20); //延时消抖
if(s6 == 0)
{
ET0 = 1;
for(i=0;i<16;i++)
{
P0 = tab1[i];
while(tt/4 != 100); //用定时器0来延时500ms
tt = 0;
}
ET0 = 0;
}
while(!s6); //确保每次按键执行一次
}
if(s7 == 0)
{
delayms(20); //延时消抖
if(s7 == 0)
{
ET0 = 1;
for(i=0;i<16;i++)
{
P0 = tab2[i];
while(tt/10 != 100); //用定时器0来延时500ms
tt = 0;
}
ET0 = 0;
}
while(!s7); //确保每次按键执行一次
}
}
void Timer0Init(void) //5毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x00; //设置定时初值
TH0 = 0x28; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void Timer0() interrupt 1
{
tt++;
}
void Delay1ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
void delayms(int ms)
{
int i;
for(i=ms;i>0;i--)
Delay1ms();
}