The C language of GPIO experiment

Source: Internet
Author: User
Tags volatile

In the previous chapter, the experiment was programmed using a compilation, and the experiment was written in C language.

(1) Light an LED light

1) Boot file:

Crt. S

. text

. Global _start

_start:

LDR r0,=0x53000000

mov R1, #0x00000000

STR R1,[R0]

LDR Sp,=1024*4

BL Main

Halt_loop:

      b halt_loop

2) led.c

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

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

int main ()

{

Gpfcon= 0x00000100; may have an impact on other bits

Gpfdat= 0x00000000;

return 0;

}

3) Makefile

Led.bin:crt. S LED.C

Arm-linux-gcc-g-c-o CRT.O CRT. S

Arm-linux-gcc-g-c-o LED.O LED.C

Arm-linux-ld-ttext 0x00000000-g LED.O Crt.o-o led_elf

Arm-linux-objcopy-o binary-s led_elf Led.bin

arm-linux-objdump-d-M arm led_elf > Led.dis

Clean

Rm-f led_elf Led.dis Led.bin *.o

(2) Running lights experiment

Startup file: Crt. S

Source file: LEDS.C

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

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

#define Gpf4_reset (3<< (4*2))

#define Gpf5_reset (3<< (5*2))

#define Gpf6_reset (3<< (6*2))

#define Gpf4_out (1<< (4*2))

#define Gpf5_out (1<< (5*2))

#define Gpf6_out (1<< (6*2))

void Delay_ms (volatile unsigned long ms)//delay

{

for (; ms>0; ms--);

}

int main ()

{

Gpfcon &=~ (Gpf4_reset | Gpf5_reset | Gpf6_reset);

Gpfcon |= Gpf4_out | Gpf5_out |  Gpf6_out; Output

while (1)

{

Gpfdat &=~ (1<<4);

Delay_ms (30000);

Gpfdat |= (1<<4);

Gpfdat &=~ (1<<5);

Delay_ms (30000);

Gpfdat |= (1<<5);

Gpfdat &=~ (1<<6);

Delay_ms (30000);

Gpfdat |= (1<<6);

}

}

Makefile:

Leds.bin:crt. S LEDS.C

Arm-linux-gcc-g-c-o CRT.O CRT. S

Arm-linux-gcc-g-c-o LEDS.O LEDS.C

Arm-linux-ld-ttext-g CRT.O Leds.o-o leds_elf

Arm-linux-objcopy-o binary-s leds_elf Leds.bin

arm-linux-objdump-d-M arm leds_elf >leds.dis

Clean

Rm-f leds_elf leds.bin Leds.dis *.o

(3) Key control LED

The keys are connected in the form of an external pull-up, where the S2,S3,S4 is connected to the GPF0,GPF2,GPG3 pin of 2440 respectively.

Startup file: Crt. S

Source file: led_key.c

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

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

#define GPGCON (* (volatile unsigned long*) 0x56000060)

#define GPGDAT (* (volatile unsigned long*) 0x56000064)

#define Gpf4_reset (3<< (4*2))

#define Gpf5_reset (3<< (5*2))

#define Gpf6_reset (3<< (6*2))

#define Gpf4_out (1<< (4*2))

#define Gpf5_out (1<< (5*2))

#define Gpf6_out (1<< (6*2))

#define Gpf0_reset (3<< (0*2))

#define Gpf2_reset (3<< (2*2))

#define Gpg3_reset (3<< (3*3))

#define GPF0_IN (0<< (0*2))

#define GPF2_IN (0<< (2*2))

#define GPG3_IN (0<< (3*2))

int main ()

{

Gpfcon &=~ (Gpf4_reset | Gpf5_reset | Gpf6_reset);//Output

Gpfcon |= Gpf_out4 | GPF_OUT5 | GPF_OUT6;

Gpfcon &=~ (Gpf0_rest | Gpf2_reset);

Gpfcon |= gpf0_in | gpf2_in;//the place can not be, because [x:x]=00, through the above statement has been implemented

Gpgcon &=~GPG3_reset;

Gpfcon |= gpg3_in; The service can save

while (1)

{

if (Gpfdat & (1<<0))//Key not pressed

Gpfdat |= (1<<4); Led1 out

Else

gpfdat &=~ (1<<4); Led1 lights up.

if (Gpfdat & (1<<2))

Gpfdat |= (1<<5);

Else

Gpfdat &=~ (1<<5);

if (Gpgdat & (1<<3))

Gpgdat |= (1<<6);

Else

Gpgdat &=~ (1<<6);

}

}

Makefile:

Led_key.bin:crt. S led_key.c

Arm-linux-gcc-g-c-o CRT.O CRT. S

Arm-linux-gcc-g-c-o led_key.o led_key.c

Arm-linux-ld-ttext 0x00000000-g CRT.O led_key.o led_key_elf

Arm-linux-objcopy-o binary-s led_key_elf Led_key.bin

arm-linux-objdump-d-M arm led_key_elf > Led_key.dis

Clean

Rm-f led_key_elf Led_key.dis Led_key.bin *.o

The C language of GPIO experiment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.