- With the previous explanation of the LED, follow-up related to the bare-Ben is more convenient, of course, code will not be as simple as LED ~ now began to explain the key keys related code
- First look at the schematic diagram to find the key related pin, in my tq2440 board, the following distribution:
Key1–int1–gpf1
Key2–int4–gpf4
Key3–int2–gpf2
Key4–int0–gpf0
From the above can be seen GPF0124 in addition to the normal pin input and output IO function also has an additional role –> interrupt, this we will talk about, and then explain, from the schematic can be seen, the pin default is high, when the button is pressed down to low level.
The next is to write code, you can write the previous C-led direct copy to change the name can be satisfied with the framework, now the code is posted out:
crt0. S key_loop.c Makefile
Crt0. S
1.text 2.Global_start3_start:4LDR R0, =0x53000000@ Close Watch dog,or itwould reboot again andAgain5LDR R1, =0x0 6STR R1, [R0]7 8Ldr sp, =4096@SetStack forCall C function9 TenBL Main @ call C Main, that' s why theUser Code Entry isMain and itWouldreturn OneLoop AB Loop @ Call but not return
Key_loop.c
1#define Gpbcon(* (volatile unsigned long *)0x56000010)2#define Gpbdat(* (volatile unsigned long *)0X56000014)3#define Gpfcon(* (volatile unsigned long *)0x56000050)4#define Gpfdat(* (volatile unsigned long *)0x56000054)5 6void Delay_ms (int ms)7 {8 int x, y; 9 for (x=ms; x>0; x--) ten for (y=100; y>0; y--) 11 {12; do nothing -} theint main () - {unsigned char key_value=0; Gpbcon |= (1<<10) | (1<<12) | (1<<14) | (1<<16)); Set led1~4 output Gpfcon = 0x0; Set GPF input gpbdat = ~0x0; Set led off default (1) @ key_value = Gpfdat; (0 = = (Key_value & (1& lt;<0))//int0 Key4-Gpbdat = ~ (1<<8);//led4 on in - if(0= = (Key_value & (1<<1)))//int1 Key1 to {Gpbdat = ~ (1<<5);//led1 on the * if(0= = (Key_value & (1<<2)))//int2 Key3 $ {PNs Gpbdat = ~ (1<<7);//led3 on the + if(0= = (Key_value & (1<<4)))//int4 Key2 A {Gpbdat = ~ (1<<6);//led2 on - $Delay_ms ( -); $} - -Return0; the}
Makefile
1Key.Bin:crt0.S Key_loop.C2Arm-linux-GCC - CCrt0.S- oCrt0.O3Arm-linux-GCC - CKey_loop.C- oKey_loop.O4Arm-linux-ld -ttext 0x0Crt0.o Key_loop.O- oKey.Elf5Arm-linux-objcopy - oBinary- SKey.Elf Key.Bin6Arm-linux-objdump - D - SKey.Elf>Key.Dis7 8Clean9Rm- F *.O Key.Elf Key.Bin Key.Dis
Crt0. s or the same old, close the watchdog, set the stack, and finally jump to C's main function, in Key_ LOOP.C, set the key pin to input, we use a very low efficiency polling query method, read the value once every time and make a judgment of which key is pressed, and then light the corresponding led. This example will also be used when the interruption is reached, except that this is not a polling method.
Embedded Learning note 003-Keys