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

數(shù)碼管顯示HELLO的單片機(jī)程序

作者:huqin   來(lái)源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2014年04月08日   【字體:
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar code hello[]={0x76,0x79,0x38,0x38,0x3f};
uchar code LED_W[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar code table[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar temp,t0,t1,hundred,ten,bit,flag,flag1;
uint num;
void init();
void display(uchar,uchar,uchar);
void display1();
void delay();
void main()
{
 init();
 while(1)
 {
 if(flag1!=1)
  display(hundred,ten,bit);
 else
  display1();
 }
}
void init()
{
 t0=0;
 t1=0;
 flag=0;
 flag1=0;
 num=432;
 temp=0xfe;
 P1=temp;
 TMOD=0x11;
 TH0=(65536-50000)/256;
 TL0=(65536-50000)%256;
 TH1=(65536-50000)/256;
 TL1=(65536-50000)%256;
 EA=1;
 ET0=1;
 ET1=1;
 TR0=1;
 TR1=1;
}
void timer0() interrupt 1
{
   TH0=(65536-50000)/256;
 TL0=(65536-50000)%256;
 t0++;
 if(flag==0)
 {
  if(t0==10)
  {
   t0=0;
   temp=_crol_(temp,1);
   P1=temp;
  }
 }
 else
 {
  if(t0%4==0)
  P1=~P1;
  if(t0==60)
  {
   TR0=0;
   P1=0xff;
   flag1=1;
  }
 }
}
void timer1() interrupt 3
{
   TH1=(65536-50000)/256;
 TL1=(65536-50000)%256;
 t1++;
 if(t1==2)
 {
  t1=0;
  num--;
  hundred=num/100;
  ten=num%100/10;
  bit=num%10;
  if(num==398)
  {
   TR0=0;
   TH0=(65536-50000)/256;
   TL0=(65536-50000)%256;
   TR0=1;
   flag=1;
   t0=0;
   P1=0xff;
   TR1=0;
  }
 }
}
void display(uchar h,uchar t,uchar b)
{
 P2=LED_W[0];
 P0=table[b];
 delay();
 P2=LED_W[1];
 P0=table[t];
 delay();
 P2=LED_W[2];
 P0=table[h];
 delay();
 P2=LED_W[3];
 P0=table[5];
 delay();
 P2=LED_W[4];
 P0=table[6];
 delay();
 P2=LED_W[5];
 P0=table[7];
 delay();
}
void display1()
{
 P2=LED_W[4];
 P0=hello[0];
 delay();
 P2=LED_W[3];
 P0=hello[1];
 delay();
 P2=LED_W[2];
 P0=hello[2];
 delay();
 P2=LED_W[1];
 P0=hello[3];
 delay();
 P2=LED_W[0];
 P0=hello[4];
 delay();
}
void delay()
{
 uchar x,y;
 for(x=0;x<148;x++)
  for(y=0;y<10;y++);
}
關(guān)閉窗口

相關(guān)文章