#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();//按键扫描
}
|