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

單片機(jī)鬧鐘程序設(shè)計(jì)

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

這個(gè)是在之前的電子時(shí)鐘上功能的擴(kuò)展,主要加入鬧鐘模塊。即到達(dá)預(yù)定時(shí)間響鈴一分鐘,一分鐘后自動(dòng)停止,也可以按下已設(shè)定的鍵實(shí)現(xiàn)手動(dòng)停止。

數(shù)碼管方面的學(xué)習(xí)就到此為此吧,下一步是對液晶顯示的學(xué)習(xí)。

單片機(jī)鬧鐘程序設(shè)計(jì)如下:

#i nclude<reg51.h>
#define uchar unsigned char
unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82, 0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};//數(shù)碼管數(shù)字編碼
uchar i,j,k,second,tcount,minute,hour,tminute,thour,ring;
sbit beep=P2^6;sbit S2=P3^4;
//---------------------------------------------------延時(shí)子程序,大約延時(shí) n MS
delay(uchar n)
{
 for(j=n;j>0;j--)
  for(k=125;k>0;k--);
}
//---------------------------------------------------中斷子程序
void timer0() interrupt 1 using 0
{
 TH0=(65536-50000)/256;           //中斷設(shè)置初始化                  
 TL0=(65536-50000)%256;      
 tcount++;                               
 if(tcount==20)              //滿1秒
 {tcount=0;second++;beep=0;       //秒數(shù)加1
  if(second==60)             //滿1分
    {second=0;minute++;      //分?jǐn)?shù)加1,秒數(shù)歸零
   if(minute==60)         //滿1小時(shí)
    {
     minute=0;hour++;     //小時(shí)數(shù)加1,分?jǐn)?shù)歸零
   if(hour==24)
    {hour=0;}
    }
    }
  }
}
void timer1() interrupt 3 using 1
{
 TH1=0x3c;           //中斷設(shè)置初始化                  
 TL1=0xb0;      
 if(minute==tminute & hour==thour & ring==1) {beep=0;}//到預(yù)設(shè)時(shí)間自動(dòng)響鈴,持續(xù)一分鐘后自己關(guān)閉
 if(S2==0)
   {
     ring=0;                 //鈴聲中斷
 }
}
//---------------------------------------------------
void main()
{tminute=1;thour=0;         //預(yù)置響鈴時(shí)間
 second=55;minute=0;hour=0;   //給電子時(shí)鐘賦初值,即啟動(dòng)時(shí)顯示的時(shí)間
 ring=1;                      //啟動(dòng)響鈴功能
 TH0=(65536-50000)/256;      //中斷設(shè)置初始化
 TL0=(65536-50000)%256;
 EA=1;ET0=1;TMOD=0x21;TR0=1; //開中斷總開關(guān),計(jì)數(shù)器0允許中斷,設(shè)置中斷模式,啟動(dòng)計(jì)數(shù)器0
 ET1=1;TR1=1;
 while(1)                    //死循環(huán),進(jìn)入顯示,主要是動(dòng)態(tài)顯示原理
 {
  P0=table[(second%10)];
  P2=0xdf;
  delay(5);
  P0=table[(second/10)];
  P2=0xef;
  delay(5);
  P0=table[(minute%10)];
  P2=0xf7;
  delay(5);
  P0=table[(minute/10)];
  P2=0xfb;
  delay(5);
  P0=table[(hour%10)];
  P2=0xfd;
  delay(5);
  P0=table[(hour/10)];
  P2=0xfe;
  delay(5);
  }
}

關(guān)閉窗口

相關(guān)文章