|
|
幫人幫到底 目前這個(gè)原因閑得蛋疼 給你寫好了,我也是菜雞,見(jiàn)笑了,我用STC給你寫的 用的定時(shí)器實(shí)現(xiàn)你的功能
#include "STC15F104E.H" //單片機(jī)頭文件
#define u16 unsigned int //宏定義無(wú)符號(hào)整型數(shù)據(jù)
#define u8 unsigned char //宏定義無(wú)符號(hào)字符型數(shù)據(jù)
sbit key_sr = P3 ^ 4;
sbit LED_1 = P3 ^ 5;
sbit LED_2 = P3 ^ 0;
sbit LED_3 = P3 ^ 1;
sbit LED_4 = P3 ^ 2;
u8 key_flag = 0;
void SYSTEM_INIT()
{
P3M1 = 0x00;
P3M0 = 0x27; // P3(雙雙推雙雙推推推)
LED_1 = 1;
LED_2 = 1;
LED_3 = 1;
LED_4 = 1;
}
void key_scan()
{
static u8 cnt = 0;
if (key_sr)
{
cnt = 0;
key_flag = 0;
}
else if (!key_flag)
{
cnt++;
if (cnt > 200)
{
key_flag = 1;
}
}
}
void LED_serve()
{
static u8 ms50_cnt = 0;
static u8 min_cnt = 0; //分鐘計(jì)數(shù)
static u8 min2_cnt = 0; // 2分鐘計(jì)時(shí)
static u8 blink_flag = 0;
static u8 blink_cnt = 0;
if (!blink_flag)
{
if (!key_flag) //開(kāi)關(guān)斷開(kāi)
{
LED_1 = 0;
LED_4 = 1;
}
if (!LED_1 && LED_2) //兩秒后點(diǎn)亮LED2
{
if (++min2_cnt >= 40) //兩秒計(jì)時(shí)到
{
min2_cnt = 0;
LED_2 = 0;
}
}
if (!LED_2)
{
if (++ms50_cnt >= 200) // 10秒
{
ms50_cnt = 0;
min_cnt++;
}
if (min_cnt >= 90) // 900秒=15分
{
blink_flag = 1; //閃燈標(biāo)志
}
}
}
if (key_flag) //開(kāi)關(guān)接通
{
ms50_cnt = 0;
min_cnt = 0;
blink_flag = 0;
LED_1 = 1;
LED_2 = 1;
LED_4 = 0;
}
if (blink_flag) //閃燈500毫秒一次
{
LED_1 = 1;
LED_2 = 1;
blink_cnt++;
if (blink_cnt <= 10)
{
LED_3 = 0;
}
else
LED_3 = 1;
if (blink_cnt >= 20)
{
blink_cnt = 0;
}
}
else
{
blink_cnt = 0;
LED_3 = 1;
}
}
void Timer0Init(void) // 50毫秒@11.0592MHz
{
AUXR &= 0x7F; //定時(shí)器時(shí)鐘12T模式
TMOD &= 0xF0; //設(shè)置定時(shí)器模式
TL0 = 0x00; //設(shè)置定時(shí)初始值
TH0 = 0x4C; //設(shè)置定時(shí)初始值
TF0 = 0; //清除TF0標(biāo)志
TR0 = 1; //定時(shí)器0開(kāi)始計(jì)時(shí)
EA = 1;
ET0 = 1;
}
/***************主程序****************/
void main()
{
SYSTEM_INIT();
Timer0Init();
while (1)
{
key_scan();
}
}
void timer0() interrupt 1
{
LED_serve();
}
|
|