利用外部中斷和時(shí)間中斷計(jì)數(shù)0-999顯示在數(shù)碼管上
掃描二維碼
隨時(shí)隨地手機(jī)看文章
要求:數(shù)碼管從0開(kāi)始顯示,按一次按鍵(和內(nèi)部中斷,每隔一秒就自然進(jìn)入中斷一次),進(jìn)入一次中斷,數(shù)碼管上數(shù)字加1,直到999,又從0開(kāi)始。
連線:P10連L7 電位器連直流電機(jī) 脈沖輸出連P32 CS1連數(shù)碼管CS
#include
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};
//顯示緩沖區(qū)
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;
}
//開(kāi)時(shí)間中斷
void initTime(){
IE=0x83;
TR0=1;
}
//開(kāi)外部中斷
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(); //打開(kāi)外部中斷'
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);
}
}