//按键按下,对应的LED灯亮,并且蜂鸣器鸣叫,按键松开,全部停止
#include "config.h"
const uint32 BEEP = (1<<7);
const uint32 KEY1 = (1<<21);
const uint32 KEY2 = (1<<22);
const uint32 KEY3 = (1<<29);
const uint32 KEY4 = (1<<30);
const uint32 LEDS4 = (0x0F << 16); // P1[19:16]控制LED4~LED1
void DelayNS(uint32 dly)
{
uint32 i;
for( ; dly>0; dly--)
for(i=0; i<50000; i++);
}
int main (void)
{
PINSEL0 = 0x00000000; //设置管脚连接GPIO
PINSEL1 = 0x00000000;
IO1DIR = LEDS4; //设置LED控制口为输出
IO0DIR = BEEP; //设置BEEP口为输出
while(1)
{
IO0SET = BEEP; //关闭蜂鸣器
if((IO0PIN&KEY1) == 0) //当KEY1按下,LED1亮,并且蜂鸣器鸣叫
{
IO1CLR = LEDS4 & (1 << 16);
IO0CLR = BEEP;
DelayNS(25);
IO0SET = BEEP;
}
else IO1SET = LEDS4 & (1 << 16);
if((IO0PIN&KEY2) == 0) //当KEY2按下,LED2亮,并且蜂鸣器鸣叫
{
IO1CLR = LEDS4 & (1 << 17);
IO0CLR = BEEP;
DelayNS(25);
IO0SET = BEEP;
}
else IO1SET = LEDS4 & (1 << 17);
if((IO0PIN&KEY3) == 0) //当KEY3按下,LED3亮,并且蜂鸣器鸣叫
{
IO1CLR = LEDS4 & (1 << 18);
IO0CLR = BEEP;
DelayNS(25);
IO0SET = BEEP;
}
else IO1SET = LEDS4 & (1 << 18);
if((IO0PIN&KEY4) == 0) //当KEY4按下,LED4亮,并且蜂鸣器鸣叫
{
IO1CLR = LEDS4 & (1 << 19);
IO0CLR = BEEP;
DelayNS(25);
IO0SET = BEEP;
}
else IO1SET = LEDS4 & (1 << 19);
}
return 0;
} |