今天写了一个简单的key按键驱动LED和beep蜂鸣器的程序,源码如下:
/*
* Copyright 2014(C) fulinux <fulinux@sina.com>
*/
#define GPFCON (*(unsigned long volatile *)0x56000050)
#define GPFDAT (*(unsigned long volatile *)0x56000054)
#define GPFUP (*(unsigned long volatile *)0x56000058)
#define GPBCON (*(unsigned long volatile *)0x56000010)
#define GPBDAT (*(unsigned long volatile *)0x56000014)
#define GPBUP (*(unsigned long volatile *)0x56000018)
#define LED0 5 /*LED0 use GPB5*/
#define LED1 6 /*LED1 use GPB6*/
#define LED2 7 /*LED2 use GPB7*/
#define LED3 8 /*LED3 use GPB8*/
#define BEEP 0 /*Buzzer us GPB0 */
#define DELAY_TIME 40000000
static inline void delay (unsigned long loops)
{
__asm__ volatile ("1:\n"
"subs %0, %1, #1\n"
"bne 1b":"=r" (loops):"0" (loops));
}
void key_init(void)
{
/* Set GPF0,GPF1,GPF2,GPF4 as GPIO input mode(00) */
GPFCON &= ~0x33F;
/* Enable pullup resister */
GPFUP &= ~0x17;
/* Set GPF0,GPF1,GPF2,GPF4 as high level */
GPFDAT |= 0x17;
}
void led_init(void)
{
/* Set GPB5,GPB6,GPB7,GPB8 as GPIO output mode(01) */
GPBCON = (GPBCON & ~0x3FC00) | 0x15400;
GPBUP &= ~0x01E0;
/* Set GPB5,GPB6,GPB7,GPB8 as high level, to turn LED0,LED1,LED2,LED3 off */
GPBDAT |= 0x01E0;
}
void beep_init(void)
{
GPBCON = (GPBCON|0x3)&0x1; /* Set GPB0 as GPIO output mode(0x01) */
GPBUP &= ~1; /* Enable pullup resister */
}
void led_off(void)
{
/* Set GPB5,GPB6,GPB7,GPB8 as high level, to turn LED0,LED1,LED2,LED3 off */
GPBDAT |= 0x01E0;
delay(DELAY_TIME);
}
int main(void)
{
beep_init();
key_init();
led_init();
led_off();
while(1)
{
GPBDAT &= ~(1<<BEEP); /* Set Beep GPIO as low level */
while((GPFDAT & 0x17) == 0x17){
delay(DELAY_TIME);
}
GPBDAT ^= (0x0F<<LED0);
GPBDAT &= ~(1<<BEEP); /* Set Beep GPIO as low level */
delay(DELAY_TIME);
GPBDAT |= 1<<BEEP; /* Set Beep GPIO as high level */
delay(DELAY_TIME);
}
}
其他文件与前几天中的内容类似。