小弟用的
单片机是STC15L104E,学着用一个按键控制LED的不同模式,并且想用他的掉电模式,程序总是改不对,请各位前辈指点迷津!
#include <reg51.h>
#include <intrins.h>
#define COUNT 1000//50000 //以us为单位 (1ms)
unsigned int Tmp;
unsigned int cnt_s = 0;
unsigned int count = 0;
unsigned char L_cnt = 0;
unsigned char PD = 0;
sbit key = P3^2;
sbit led1 = P3^0;
void
timer0_Timer1_Init()
{
TMOD|=0x00; //定时器工作在模式1
TH0 = (65536-COUNT)/256; //装初值
TL0 = (65536-COUNT)%256;
TR0 = 1;
ET0 = 1;
EA = 1;
count = 0;
}
void INT_System_init()
{
IT0 = 1;
EX0 = 1;
EA = 1;
}
void main(void)
{
P3=0xFF;
L_cnt = 0;
INT_System_init();
Timer0_Timer1_Init();
while(1)
{
if(!key)
{
count ++;
if( count >= 1500 )
{
PD = 1;
PCON = 0x02; //掉电模式开启
}
else if( count >=10 )
{
L_cnt++;
if(L_cnt == 6)
L_cnt = 0;
}
}
switch(L_cnt)
{
case 1:TR0 = 1; Tmp = 456 ;break; //1Hz
case 2:Tmp = 221 ;break; //2Hz
case 3:Tmp = 111 ;break; //4Hz
case 4:Tmp = 57 ;break; //8Hz
case 5:TR0 = 0; led1 = 1; break; //全亮
default :TR0 = 0; led1 = 0;break; //全灭
}
}
}
void INT0_Routine () interrupt 0
{
if(PD)
{
PD = 0;
while( key == 0 )
{
}
PCON = 0x00; //掉电模式关闭
}
}
void Timer0_Server(void) interrupt 1 //T0做时钟源10ms中断一次
{
TH0=(65536-COUNT)/256; //装初值
TL0=(65536-COUNT)%256;
cnt_s++; //0-100 = (10ms~1000ms)
if(cnt_s >= Tmp)
{
cnt_s = 0;
led1 = ~ led1;
}
}
补充内容 (2016-4-13 21:52):
出现的问题是,按键按一下闪烁,2Hz,再按,熄灭,只有这两种状态
2、应该加松手判断,松手再进行相应的判断工作
if(!key)
{
count ++;
}
else
{
if( count >= 1500 )
{
PD = 1;
PCON = 0x02; //掉电模式开启
}
else if( count >=10 )
{
L_cnt++;
if(L_cnt == 6)
L_cnt = 0;
}
count = 0;
} 最佳答案
谢谢前辈提醒,我把帖子改一下
一周热门 更多>