專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

中斷可調(diào)數(shù)碼管程序

作者:佚名   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2010年09月06日   【字體:

  按K1數(shù)碼管加一,按K2數(shù)碼管減一,按K3打開和停止中斷,按K4清零,此程序在自己制作的學(xué)習(xí)板(資料: http://www.torrancerestoration.com/mcu/1019.html)上正常運(yùn)行.


#include"reg51.h"
#define uchar unsigned char 
uchar code smg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar shi,ge,num,numt0;

void delayms(uchar aa)
  {
   uchar i,j;
   for(i=aa;i>0;i--)
   for(j=110;j>0;j--);
   }

 sbit K1=P3^0;
 sbit K2=P3^1;
 sbit K3=P3^2;
 sbit K4=P3^3;

 sbit wx1=P1^0;
 sbit wx2=P1^1;

void display(num)
  {
  shi=num/10;
  ge=num%10;

  wx1=1;
  P2=smg[shi];
  delayms(10);
  wx1=0;

  wx2=1;
  P2=smg[ge];
  delayms(10);
  wx2=0;
  }

void init()
  {
   TMOD=0x01;
   TH0=(65536-45872)/256;
   TL0=(65536-45872)%256;
   EA=1;
   ET0=1;
   }


void Kscan()
   {
  if(!K1)
 {
   delayms(10);
     if(!K1)
    {
      num++;
        if(num==60) num=0;          
            while(!K1);
          }
           }
  if(!K2)
 {
   delayms(10);
      if(!K2)
       {
         if(num==0) num=60;
           num--;
             while(!K2);
              }
               }

   if(!K3)
 {
   delayms(10);
    if(!K3)
    {
     while(!K3);
      TR0=~TR0;
   }
  }

   if(!K4)
 {
   delayms(10);
 if(!K4)
 {
      num=0;
   while(!K4);
   }
  }
 }

void main()
 {
     init();
   while(1)
     {
       Kscan();
        display(num);
       }
      }


void T0_timer() interrupt 1
  {
   TH0=(65536-45872)/256;
   TL0=(65536-45872)%256;
   numt0++;
   if(numt0==20)
   {
   numt0=0;
   num++;
   if(num==60)
   num=0;
   }
   }
關(guān)閉窗口

相關(guān)文章