51 단편기 타이머 디지털관은 1부터 100까지 (세 개의 버튼 제어로 시작, 정지, 리셋)

8034 단어

 
 
#include 
#define uint unsigned int
#define uchar unsigned char
sbit wei=P2^7;
sbit duan=P2^6;
sbit key1=P3^4;
sbit key2=P3^5;
sbit key3=P3^6;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//1~9    //0~9,0x77,0x7c,0x39,0x5e,0x79,0x71
uint number;
void delay(uint xms)//    
{
   uint i;uchar j;
   for(i=xms;i>0;i--)
   for(j=110;j>0;j--);
}
void show(uint ge,uint shi)
{
    ge=number%10;
    shi=number/10;   
      wei=1;
    P0 = 0xfe;
    wei=0;
    duan=1;
    P0=table[shi];
    duan=0;
    P0=0xff;
    delay(5);

    wei=1;
    P0=0xfd;
    wei=0;
    duan=1;
    P0=table[ge];
    duan=0;
    P0=0xff;
    delay(5); 
}
void initial_mcu()
{                                                   
    TMOD=0x01; //   0 1   16   
    TH0=(65536-45872)/256;
    TL0=(65536-45872)%256;
    EA=1;
    ET0=1;
    //TR0=1;
}

void buttons()
{
         if(key1==0)
         {
            delay(10); //    
            if(key1==0)
            {                                       
               number=0; 
            }                
          } 
          
          if(key2==0)
         {
            delay(10); //    
            if(key2==0)
            {                                       
               TR0=1;
            }                
          } 
          
          if(key3==0)
         {
            delay(10); //    
            if(key3==0)
            {                                       
               TR0=0;
            }                
          }                                 
}
void main ()
{ 
     initial_mcu();//                  
         while(1)
         {
            buttons();
              show();                         
         }     
}
void timer0() interrupt 1 
{     
    uint num;    
    TH0=(65536-45872)/256;
    TL0=(65536-45872)%256;
    num++;    
    if(num==20)
    {
         num=0;
         number++;
         if(number==100)
         {
            number=0;
         }
    }
}

 
전재 대상:https://www.cnblogs.com/enduo/p/7819729.html

좋은 웹페이지 즐겨찾기