要求:数码管从0开始显示,按一次按键(和内部中断,每隔一秒就自然进入中断一次),进入一次中断,数码管上数字加1,直到999,又从0开始。
连线:P10连L7 电位器连直流电机 脉冲输出连P32 CS1连数码管CS
#include<reg51.h>
typedef unsigned char uchar;
xdata uchar LED_CS _at_ 0x9000;
xdata uchar LED_OUTSEG _at_ 0x9004;
xdata uchar LED_OUTBIT _at_ 0x9002;
sbit P10=P1^0;
uchar n=0;
unsigned int Count=0;
code unsigned char LEDMAP[]={
//八段管显示码
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71
};
code unsigned char LEDWED[]= {0x20,0x10,0x08,0x04,0x02,0x01};
//显示缓冲区
uchar led_buf[]={0,0,0};
void delay(unsigned char ms)
{
unsigned char i;
while(ms--)
{
for(i = 0; i < 50; i++);
}
}
void ADcal()
{
led_buf[0]=Count/100;//百位
led_buf[1]=Count/10%10;//十位
led_buf[2]=Count%10;
}
//开时间中断
void initTime(){
IE=0x83;
TR0=1;
}
//开外部中断
void initOut(){
IE=0x83;
IT0=1;
}
void timer0() interrupt 1
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
n++;
if(n==2)
{
P10=1;
delay(20);
P10=0;
delay(20);
Count++;
if(Count>=999)
{
Count=0;
}
}
}
void out0() interrupt 0
{
Count++;
if(Count>=999)
{
Count=0;
}
}
void main()
{
unsigned int i=0;
initOut(); //打开外部中断'
initTime();
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
delay(10);
while(1)
{
ADcal();
LED_CS=0;
if(i==0)
{
LED_OUTSEG=LEDMAP[led_buf[i]];
}
if(i==1)
{
LED_OUTSEG=LEDMAP[led_buf[i]];
}
if(i==2)
{
LED_OUTSEG=LEDMAP[led_buf[i]];
}
LED_OUTBIT=LEDWED[i];
i++;
i=i%3;
delay(10);
}
}