89C52單片機之數碼管

數碼管倒計時及蜂鳴器報警


#include<reg52.h>

#define uint unsigned int
#define uchar unsigned char

sbit bell= P0^7;

//unsigned  char dofly_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
                                // 0   1   2     3    4    5   6    7     8   9    
unsigned  char dofly_table[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
                            // 0    1    2    3     4    5   6     7    8    9



  //延遲函數
void  delay_ms(int time)
{
    int i,j;
    for(i=0;i<time;i++)
    {
      for(j=0;j<120;j++)       //執行完一次是1ms
      {
        ;
      }
    }
}

//主函數
void main()
{ 

    int i=50,j=0,a=0,b=0,c=0,k=0;
    while(1)
    {     
        a=i%10;   //個位
        b=(i/10)%10;  //十位
        c=i/100;    //百位
        for(j=0;j<20;j++)
        {    
            P1=0x01;
            P0=dofly_table[a];
            delay_ms(5);
            P1=0x02;
            P0=dofly_table[b];
            delay_ms(5);
            P1=0x04;
            P0=dofly_table[c];
            delay_ms(5);
        }
        i--;
        if(i==0)
        {
            P1=0x07;
            P0=0xbf;
            i=50;
        for(k=0;k<6;k++)
        {
            //bell=!bell;
             P0 ^=0x80;
            delay_ms(500);
        }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章