微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 用STC15F104E单片机制作闪灯

用STC15F104E单片机制作闪灯

时间:11-30 来源:互联网 点击:
STC15F104E单片机,就是一个定时器,上电开始计时,LED每秒闪一次,9小时后继电器吸合3秒,然后释放,计时器重新计时。如此循环。

#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小时

{

relay = 1; //继电器吸合

sec = 0;

min = 0;

hour = 0;

S3cnt_Stat = 1;

S3cnt = 3; //计数3秒

}

}

}

}

}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top