幫人幫到底 目前這個原因閑得蛋疼 給你寫好了,我也是菜雞,見笑了,我用STC給你寫的 用的定時器實現你的功能
#include "STC15F104E.H" //單片機頭文件
#define u16 unsigned int //宏定義無符號整型數據
#define u8 unsigned char //宏定義無符號字符型數據
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; //分鐘計數
static u8 min2_cnt = 0; // 2分鐘計時
static u8 blink_flag = 0;
static u8 blink_cnt = 0;
if (!blink_flag)
{
if (!key_flag) //開關斷開
{
LED_1 = 0;
LED_4 = 1;
}
if (!LED_1 && LED_2) //兩秒后點亮LED2
{
if (++min2_cnt >= 40) //兩秒計時到
{
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; //閃燈標志
}
}
}
if (key_flag) //開關接通
{
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; //定時器時鐘12T模式
TMOD &= 0xF0; //設置定時器模式
TL0 = 0x00; //設置定時初始值
TH0 = 0x4C; //設置定時初始值
TF0 = 0; //清除TF0標志
TR0 = 1; //定時器0開始計時
EA = 1;
ET0 = 1;
}
/***************主程序****************/
void main()
{
SYSTEM_INIT();
Timer0Init();
while (1)
{
key_scan();
}
}
void timer0() interrupt 1
{
LED_serve();
}
|