CT107D-按键的使用(查询法)
#include <reg52.h>
sbit S4=P3^3;
sbit S5=P3^2;
sbit S6=P3^1;
sbit S7=P3^0;
sbit buzzer=P0^6; //蜂鸣器
void key_get();
void Led_print(int key);
void stop_buzzer();
int main()
{
stop_buzzer();
P2=(P2&0x1f)|0x80;
P0=0xff;
while(1)
key_get();
return 0;
}
void key_get()
{
if(S4==0)
Led_print(1);
if(S5==0)
Led_print(2);
if(S6==0)
Led_print(3);
if(S7==0)
Led_print(4);
}
void Led_print(int key)
{
P2 = ((P2&0x1f)|0x80);
P0=0xff;
P0=0xfe<<(key-1);
P2 &= 0x1f;
}
void stop_buzzer()//关蜂鸣器
{
P2=0xa0;
buzzer=0;
P2=0x00;
}