ARM9---LED和按键





//寄存器设置

#define  GPBCON   (*(volatile unsigned long *)0x56000010)
#define  GPBDAT   (*(volatile unsigned long *)0x56000014)
#define  GPBUP    (*(volatile unsigned long *)0x56000018)


#define  GPFCON   (*(volatile unsigned long *)0x56000050)
#define  GPFDAT   (*(volatile unsigned long *)0x56000054)

#define  GPFUP    (*(volatile unsigned long *)0x56000058)


//配置引脚功能,对应LED1-LED4
#define GPB5_out  (1<<(2*5))  
#define GPB6_out  (1<<(2*6))
#define GPB7_out  (1<<(2*7))

#define GPB8_out  (1<<(2*8))


//配置引脚,对应key1-key6

#define GPF1_in  ~(3<<(2*1))
#define GPF2_in  ~(3<<(2*2))
#define GPF3_in  ~(3<<(2*3))
#define GPF4_in  ~(3<<(2*4))
#define GPF5_in  ~(3<<(2*5))
#define GPF7_in  ~(3<<(2*6))


int i=0;//定义全局变量,用来存储按键值,全局的目的是在delay中,可以用来缓存按键值



//引脚初始化

void init()

{

//GPB[8:5]配置输出,且初始化为高电平

GPBCON|=GPB5_out|GPB6_out|GPB7_out|GPB8_out;
GPBDAT|=(1<<5)|(1<<6)|(1<<7)|(1<<8);

//GPF[6:0]配置为输入
GPFCON&= GPF1_in&GPF2_in&GPF3_in&GPF4_in&GPF5_in&GPF7_in;
}



//扫描按键
int key()

{

//key1按下

if((GPFDAT & (1<<7))==0)
{
while((GPFDAT&(1<<7))==1);  
return 1;
}
//key2按下
if((GPFDAT&(1<<1))==0)
{
while((GPFDAT&(1<<1))==1);
return 2;
}
//key3按下
if((GPFDAT&(1<<2))==0)
{
while((GPFDAT&(1<<2))==1);
return 3;
}
//key4按下
if((GPFDAT&(1<<3))==0)
{
while((GPFDAT&(1<<3))==1);
return 4;
}
//key5按下
if((GPFDAT&(1<<4))==0)
{
while((GPFDAT&(1<<4))==1);
return 5;
}
//key6按下
if((GPFDAT&(1<<5))==0)
{
while((GPFDAT&(1<<5))==1);
return 6;
}


return i;
}


void delay()
{
unsigned int t=163554;
while(--t) i=key();
}
void key1()
{


int count;
GPBDAT|=(1<<5)|(1<<6)|(1<<7)|(1<<8);
for(count=5;count<9;count++)
{
GPBDAT&=~(1<<count);
delay();
GPBDAT|=(1<<count);
delay();
}
}


void key2()
{
int count;
GPBDAT|=(1<<5)|(1<<6)|(1<<7)|(1<<8);
for(count=8;count>4;count--)
{
GPBDAT&=~(1<<count);
delay();
GPBDAT|=(1<<count);
delay();
}
}


void key3()
{
unsigned int count;
for(count=0;count<2;count++)
{
GPBDAT&=(~(1<<(5+count)))&(~(1<<(8-count)));
delay();
GPBDAT|=(1<<(5+count))|(1<<(8-count));
delay();
}
}


void key4()
{
int count;
for(count=1;count>=0;count--)
{
GPBDAT&=(~(1<<(5+count)))&(~(1<<(8-count)));
delay();
GPBDAT|=(1<<(5+count))|(1<<(8-count));
delay();
}
}




void key5()
{
GPBDAT&=~(1<<5)&~(1<<6)&~(1<<7)&~(1<<8);
delay();
GPBDAT|=(1<<5)|(1<<6)|(1<<7)|(1<<8);
delay();
}


void key6()
{
key1();
delay();
key2();
delay();
key3();
delay();
key4();
delay();
key5();
delay();
}




int main()
{

init();
while(1)
{


i=key(); //判断那个键按下
switch(i)
{
case 1:key1();break;
case 2:key2();break;
case 3:key3();break;
case 4:key4();break;
case 5:key5();break;
case 6:key6();break;
default:GPBDAT|=(1<<5)|(1<<6)|(1<<7)|(1<<8);break;
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值