|
/*----------------------------------------------------------
單片機(jī)采用stc8h1k28 32PIN 按鍵接P37,閃爍LED接P03
功能:按下按鍵,LED閃爍4次,然后熄滅。
------------------------------------------------------------*/
#include "stc8h.h"
//#include "Led.h"
sbit Led1=P0^3;
sbit Led2=P0^2;
sbit Key=P2^3;
sbit Key1=P3^7;
unsigned char B_400ms;
unsigned char Key_lock=0;
unsigned char Key_cnt=0;
unsigned char Key_Num=0;
void Timer0Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x80; //定時(shí)器時(shí)鐘1T模式
TMOD &= 0xF0; //設(shè)置定時(shí)器模式
TL0 = 0xCD; //設(shè)置定時(shí)初始值
TH0 = 0xD4; //設(shè)置定時(shí)初始值
// TF0 = 0; //清除TF0標(biāo)志
ET0=1;
TR0 = 1; //定時(shí)器0開(kāi)始計(jì)時(shí)
EA=1;
}
void delay_ms(unsigned int t)
{
while(t--)
{
;
}
}
void led_flash(unsigned char i) //LED閃爍間隔400/2 ms
{
// unsigned char m;
// for(m=0;m<=i;m++)
while(i--)
{
if(B_400ms==1)
{
B_400ms=0;
Led1=~Led1;
}
}
}
void KeyScan(void) //按鍵掃描
{
if(Key1 == 1)
{
Key_cnt=0;
Key_lock = 0; //設(shè)置按鍵狀態(tài),防止重復(fù)觸發(fā)
}
else if(Key_lock==0)
{
Key_cnt++;
if(Key_cnt>=20)
{
Key_cnt=0;
Key_lock=1;
Key_Num=1;
}
}
}
void Key_service() //按鍵服務(wù)函數(shù)
{
if(Key_Num==1)
{
Key_Num=0; //添加這行LED常亮,去掉LED閃爍不停
led_flash(4);
}
}
void main()
{
P_SW2|=0x80;
P0M1=0;
P0M0=0;
P2M1=0;
P2M0=0;
P3M1=0;
P3M0=0;
P3PU=0x80; //P37內(nèi)部電阻上拉
Timer0Init();
delay_ms(1000);
while(1)
{
Key_service();
Led2=0;
}
}
void TIM0_ISR() interrupt 1
{
static unsigned int cnt;
KeyScan();
cnt++;
if(cnt>=400)
{
cnt=0;
B_400ms=1;
}
TL0 = 0xCD; //設(shè)置定時(shí)初始值
TH0 = 0xD4; //設(shè)置定時(shí)初始值
}
|
|