专注电子技术学习与研究
当前位置:单片机教程网 >> MCU设计实例 >> 浏览文章

中断可调数码管程序

作者:佚名   来源:本站原创   点击数:  更新时间:2010年09月06日   【字体:

  按K1数码管加一,按K2数码管减一,按K3打开和停止中断,按K4清零,此程序在自己制作的学习板(资料: http://www.51hei.com/mcu/1019.html)上正常运行.


#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;
   }
   }
关闭窗口

相关文章