检测到一个按键后LED灯亮下,现在无法实现,求助怎么解决!!!
#include<reg51.h>
#include<intrins.h>
sbit led_dr=P2^0;
sbit hc165_si_dr=P1^1;
sbit hc165_clk_dr=P1^3;
sbit hc165_qh_sr=P1^0;
#define cnt_key_delay 40
#define cnt_led_
time 150
void key_scan();
void key_service();
unsigned char key_lock1=0;
unsigned char key_lock2=0;
unsigned char key_lock3=0;
unsigned char key_lock4=0;
unsigned int key_delay_cnt=0;
unsigned int led_time_cnt=0;
unsigned char key_sec=0;
unsigned char key_status=0;
void main()
{
hc165_qh_sr=1;
hc165_clk_dr=1;
hc165_qh_sr=1;
led_dr=1;
TMOD=0X00;
TH0=(8192-1000)/32;
TL0=(8192-1000)%32;
EA=1;
ET0=1;
TR0=1;
while(1)
{
key_service();
}
}
void key_scan()
{
unsigned char i;
key_status=0x00;
hc165_si_dr=0;
_nop_();
_nop_();
hc165_si_dr=1;
_nop_();
_nop_();
for(i=0;i<8;i++)
{
hc165_clk_dr=0;
_nop_();
_nop_();
key_status=key_status<<1;
key_status=key_status|hc165_qh_sr;
hc165_clk_dr=1;
_nop_();
_nop_();
}
if((key_status&&0x01)==0x01)
{
key_lock1=0;
key_delay_cnt=0;
}
else if(key_lock1==0)
{
++key_delay_cnt;
if(key_delay_cnt>cnt_key_delay)
{
key_delay_cnt=0;
key_lock1=1;
key_sec=1;
}
}
if((key_status&&0x02)==0x02)
{
key_lock2=0;
key_delay_cnt=0;
}
else if(key_lock2==0)
{
++key_delay_cnt;
if(key_delay_cnt>cnt_key_delay)
{
key_delay_cnt=0;
key_lock2=1;
key_sec=2;
}
}
if((key_status&&0x04)==0x04)
{
key_lock3=0;
key_delay_cnt=0;
}
else if(key_lock3==0)
{
++key_delay_cnt;
if(key_delay_cnt>cnt_key_delay)
{
key_delay_cnt=0;
key_lock3=1;
key_sec=3;
}
}
if((key_status&&0x08)==0x08)
{
key_lock4=0;
key_delay_cnt=0;
}
else if(key_lock4==0)
{
++key_delay_cnt;
if(key_delay_cnt>cnt_key_delay)
{
key_delay_cnt=0;
key_lock4=1;
key_sec=4;
}
}
}
void key_service()
{
switch(key_sec)
{
case 1:
led_time_cnt=cnt_led_time;
key_sec=0;
break;
case 2:
led_time_cnt=cnt_led_time;
key_sec=0;
break;
case 3:
led_time_cnt=cnt_led_time;
key_sec=0;
break;
case 4:
led_time_cnt=cnt_led_time;
key_sec=0;
break;
}
}
void time0() interrupt 1
{
TR0=0;
key_scan();
if(led_time_cnt)
{
led_dr=0;
--led_time_cnt;
}
else
{
led_dr=1;
}
TH0=(8192-1000)/32;
TL0=(8192-1000)%32;
TR0=1;
}
-
电路图
一周热门 更多>