#include<stc8g.h>
sbit Key=P3^3;
void Timer_Init(void); //外部中斷初始化
void Key_Check(void); //按鍵掃描函數
unsigned int c=0,Alarm1,Alarm;
void delay1us(unsigned int us)
{
unsigned int i, j;
for(i = us; i > 0; i--)
for(j =700; j > 0; j--); //700
}
void main()
{ P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0xff;
P5M1 = 0x00;
P3PU |= 0x0f;
Timer_Init(); //定時器初始化
// Alarm1=200;
while(1)
{
if(P31==1 )
{
P35=1;
P54=1;
delay1us(Alarm1);
P35=0;
P54=0;
delay1us(Alarm);
// P2=~a;
} }}
void Key_Check(void)
{
static unsigned int press_time=0;
if(Key==0)
{ //掃描按鍵1
if(++press_time<=0) --press_time; //計算按鍵時間,避免數據溢出
if(press_time==1000)
{ //按鍵長按1s時要做的事情
c--; // LED2=~LED2;
} }
else
{
if((20<=press_time)&&(press_time<1000))
{ //大于10ms小于1s,短按
c++; //LED1=~LED1;//長加不能對調
}
press_time=0;
if( c>8)
{
c=0;
}
P1=~c;
if(c==0)
{ Alarm1=999;
Alarm=1; }
if(c==1)
{
Alarm1=1000;
Alarm=3000; } //不能用200 800
if(c==2)
{ Alarm1=1500;
Alarm=4000; }
if(c==3)
{
Alarm1=1500;
Alarm=5000;
}
if(c==4)
{ Alarm1=2000;
Alarm=5000; }
if(c==5)
{
Alarm1=150;
Alarm=1000;}
if(c==6)
{
Alarm1=200;
Alarm=1000; } //16hz
if(c==7)
{ Alarm1=6000;
Alarm=1000; }
if(c==8)
{Alarm1=900; //未加括號,占空比不變,周期變
Alarm=100;}
}}
//定時器初始化
void Timer_Init(void)
{
TMOD|=0x10; //打開定時器01,工作方式1。
TH1=0xfc; //給定時器0賦初值,定時1ms
TL1=0x18;
ET1=1; //打開定時器0中斷允許
TR1=1; //打開定時器
EA=1; //打開總中斷
}
void Timer1() interrupt 3
{
TH1=0xfc; //給定時器1賦初值,定時1ms
TL1=0x18;
Key_Check();//按鍵掃描
}
|