天天看點

利用外部中斷和時間中斷計數0-999顯示在數位管上(考題)

#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;

    Count++;

    if(Count>=999)

        Count=0;

    }

void out0() interrupt 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)

        if(i==2)

        LED_OUTBIT=LEDWED[i];

        i++;

        i=i%3;

        delay(10);

     }

本文轉自蓬萊仙羽51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366777,如需轉載請自行聯系原作者