【嵌入式】——arm裸机开发 step by step 之 按键控制 LED 和 蜂鸣器

一、arm-9 TQ2440

 key.h

 1 #ifndef __KEY_H__
 2 #define __KEY_H__
 3 
 4 #define GPFCON (*(volatile unsigned long *)0x56000050)
 5 #define GPFDAT (*(volatile unsigned long *)0x56000054)
 6 
 7 extern void key_init(void);
 8 extern int key1(void);
 9 extern int key2(void);
10 extern int key3(void);
11 extern int key4(void);
12 
13 #endif

 key.c

 1 #include "key.h"
 2 
 3 void key_init(void){
 4     //设置key 1 2 3 4 为输入引脚
 5     GPFCON = ~0x3 & ~(0x3<<4) & ~(0x3<<8) & ~(0x3<<2);    
 6 }
 7 
 8 int key1(void){
 9     if(GPFDAT & (0x1<<1)){
10         return 0;
11     }
12     else{
13         return 1;
14     }
15 }
16 
17 int key2(void){
18     if(GPFDAT & (0x1<<4)){
19         return 0;
20     }
21     else{
22         return 1;
23     }
24 }
25 
26 
27 int key3(void){
28     if(GPFDAT & (0x1<<2)){
29         return 0;
30     }
31     else{
32         return 1;
33     }
34 }
35 
36 int key4(void){
37     if(GPFDAT & (0x1)){
38         return 0;
39     }
40     else{
41         return 1;
42     }
43 }

buzzer.h

 1 #ifndef __BUZZER_H__
 2 #define __BUZZER_H__
 3 
 4 #define GPBCON (*(volatile unsigned long *)0x56000010)
 5 #define GPBDAT (*(volatile unsigned long *)0x56000014)
 6 
 7 extern void buzzer_init(void);
 8 extern void buzzer_on(void);
 9 extern void buzzer_off(void);
10 
11 #endif

 buzzer.c

 1 #include "buzzer.h"
 2 
 3 void buzzer_init(void){
 4     GPBCON &= ~0x3;
 5     GPBCON |= 0x1;
 6 }
 7 
 8 void buzzer_on(void){
 9     GPBDAT |= 1;    
10 }
11 
12 void buzzer_off(void){
13     GPBDAT &= ~1;
14 }

led.h

 1 #ifndef __LED_H__
 2 #define __LED_H__
 3 
 4 #define GPBCON (*(volatile unsigned long*)0x56000010)
 5 #define GPBDAT (*(volatile unsigned long*)0x56000014)
 6 
 7 extern void led_init(void);
 8 extern void led1_on(void);
 9 extern void led1_off(void);
10 extern void led2_on(void);
11 extern void led2_off(void);
12 extern void led3_on(void);
13 extern void led3_off(void);
14 extern void led4_on(void);
15 extern void led4_off(void);
16 
17 #endif

led.c

 1 #include "led.h"
 2 
 3 //初始化led寄存器
 4 void led_init(void){
 5     //设置 led 1 2 3 4 为输出引脚
 6     GPBCON &= ~(0xFF<<10);
 7     GPBCON = (1<<10) | (1<<12) | (1<<14) | (1<<16);
 8 }
 9 
10 void led1_on(void){
11     //GPBDAT &= ~(1<<5);
12     GPBDAT &= ~(1<<5); 
13 }
14 void led1_off(void){
15     GPBDAT |= 1<<5;
16 }
17 
18 void led2_on(void){
19     GPBDAT &= ~(1<<6); 
20 }
21 void led2_off(void){
22     GPBDAT |= (1<<6);
23 }
24 
25 void led3_on(void){
26     GPBDAT &= ~(1<<7);
27 }
28 void led3_off(void){
29     GPBDAT |= (1<<7);
30 }
31 
32 
33 void led4_on(void){
34     GPBDAT &= ~(1<<8);
35 }
36 void led4_off(void){
37     GPBDAT |= (1<<8);
38 }

main.c

 1 #include "led.h"
 2 #include "key.h"
 3 #include "buzzer.h"
 4 
 5 void delay(void);
 6 
 7 int main(void){
 8     led_init();
 9     key_init();
10     //蜂鸣器
11     buzzer_init();
12     while(1){
13         if(key1()){
14             led1_on();
15             led2_on();
16         }
17         else{
18             led1_off();
19             led2_off();
20         }
21         if(key2()){
22             led3_on();
23             led4_on();
24         }
25         else{
26             led3_off();
27             led4_off();
28         }
29         if(key3()){
30             buzzer_on();
31         }
32         else{
33             buzzer_off();
34         }
57     }
58 }
59 
60 void delay(void){
61     int i;
62     for(i = 0; i < 0xf000; i++){
63         ;
64     }
65 }

led.lds

 1 SECTIONS
 2 {
 3     . = 0x30000000;
 4     .text :{
 5        main.o (.text);
 6        *.o (.text);
 7     }
 8     .data :{
 9         *.o (.data);
10     }
11     __bss_start = .;
12     .bss :{
13         *.o (.bss);
14     }
15     __end = .;
16 };

Makefile

 1 CC=arm-linux-gcc
 2 LD=arm-linux-ld
 3 OBJ=arm-linux-objcopy
 4 keyled_c:keyled_c.o
 5     $(LD) -nostartfiles -nostdlib -T led.lds -e main -o keyled_c keyled_c.o
 6     $(OBJ) -O binary keyled_c keyled_c.bin
 7 keyled_c.o:keyled_c.c
 8     $(CC) -march=armv5te -nostdlib -c -o keyled_c.o keyled_c.c
 9 clean:
10     @rm -vf keyled_c.o keyled_c keyled_c.bin

上面的 led.lds 是设置代码和数据段的地址区

 

二、arm-A8 S5PV210

 1 #define GPC0CON (*(volatile unsigned long*)0xE0200060)
 2 #define GPC0DAT (*(volatile unsigned long*)0xE0200064)
 3 
 4 #define GPH0CON (*(volatile unsigned long*)0xE0200C00)
 5 #define GPH0DAT (*(volatile unsigned long*)0xE0200C04)
 6 
 7 #define GPD0CON (*(volatile unsigned long*)0xE02000A0)
 8 #define GPD0DAT (*(volatile unsigned long*)0xE02000A4)
 9 
10 #define GPD01OUT (1<<(4*1))
11 
12 #define GPC03OUT (1<<(3*4))
13 #define GPC04OUT (1<<(4*4))
14 
15 #define GPH00IN ~(0xF<<(4*0))
16 #define GPH01IN ~(0xF<<(4*1))
17 int main(void){
18     //设置led1 2 GPC0CON 为输出引脚
19     GPC0CON = GPC03OUT | GPC04OUT;
20     //设置按键1 2 GPH0CON 为输入引脚
21     GPH0CON = GPH00IN & GPH01IN;
22     //设置蜂鸣器 GPD0CON 为输出引脚
23     GPD0CON = GPD01OUT;
24 
25     while(1){
26         if(GPH0DAT & 0x1){
27             GPC0DAT = 0x00;
28             GPD0DAT = 0x00;
29         }
30         else{
31             GPC0DAT = 0x08;
32             GPD0DAT = 0x02;
33         }
34         if(GPH0DAT & 0x02){
35             GPC0DAT = 0x00;
36         }
37         else{
38             GPC0DAT = 0x10;
39         }
40     }
41 }

  以上的宏定义都是根据开发板电路图找到CPU相对应的管脚,然后设置管脚就可以实现相应的功能。

Makefile:

 1 CC=arm-linux-gcc
 2 LD=arm-linux-ld
 3 OBJ=arm-linux-objcopy
 4 keyled_c:keyled_c.o
 5     $(LD) -nostartfiles -nostdlib -Ttext=0x20008000 -e main -o keyled_c keyled_c.o
 6     $(OBJ) -O binary keyled_c keyled_c.bin
 7 keyled_c.o:keyled_c.c
 8     $(CC) -march=armv5te -nostdlib -c -o keyled_c.o keyled_c.c
 9 clean:
10     @rm -vf keyled_c.o keyled_c keyled_c.bin

 

你可能感兴趣的:(嵌入式)