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

用STC15F104E單片機(jī)制作閃燈

作者:佚名   來(lái)源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2012年12月20日   【字體:
用STC15F104E單片機(jī),就是一個(gè)定時(shí)器,上電開(kāi)始計(jì)時(shí),LED每秒閃一次,9小時(shí)后繼電器吸合3秒,然后釋放,計(jì)時(shí)器重新計(jì)時(shí)。如此循環(huán)。
#include "reg51.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define SYSclk 6000000L

#define MODE1T        //Timer clock mode, commendt this line is 12T mode, uncomment is 1T mode
#ifdef MODE1T
#define T1MS (65536-SYSclk/1000)        //1ms timer calculation method in 1T mode, 1000=1000Hz
#else
#define T1MS (65526-SYSclk/12/1000)        //1ms timer calculation method in 12T mode
#endif

sfr AUXR=0x8e;                    //Auxiliary register
sbit TEST_LED=P3^3;
sbit relay = P3^4;

WORD count;                        //1000 times counter
BYTE sec;
BYTE min;
BYTE hour;
BYTE S3cnt;
BYTE S3cnt_Stat;
void clock_cnt();

/*Timer0 interrupt routine */
void tm0_isr() interrupt 1 using 1
{
    if (count--==0)
    {
         count=500;            //500ms 
        TEST_LED=!TEST_LED;
        if (TEST_LED) clock_cnt();
    }
}

void main()
{
#ifdef MODE1T
    AUXR=0x80;         //timer0 work in 1T mode
#endif
    TMOD=0x00;        //set timer0 as mode0 (16-bit auto-reload)
    TL0=T1MS;        //initial timer0 low byte
    TH0=T1MS>>8;    //initial timer0 high byte
    TR0=1;            //timer0 start running
    ET0=1;            //enable timer0 interrupt
    EA=1;            //open global interrupt switch

    sec  = 0;
    min  = 0; 
    hour = 0;
    S3cnt_Stat = 0;

    while(1);
}

void clock_cnt()
{
    if (S3cnt_Stat)
    {
        S3cnt--;
        if (S3cnt==0) 
        {
            S3cnt_Stat = 0;
            relay = 0;      //繼電器釋放
        }
    }
    else
    {
      sec++;
     if (sec>=60)
     {
         sec=0;
        min++;
        if (min>=60);
        {
            min=0;
            hour++;
            if (hour>=9)         //到了9小時(shí)
            {
                relay = 1;        //繼電器吸合
                sec = 0;
                min = 0;
                hour = 0;
                S3cnt_Stat = 1;
                S3cnt = 3;        //計(jì)數(shù)3秒
            }
        }
     }
    }
} 
關(guān)閉窗口

相關(guān)文章