Class 1 lights up the first LED light

Source: Internet
Author: User
8.1 light LED

Hardware connection diagram,

 

According to the hardware diagram, when the output of the 175-pin gpio [0] is 0, the LED is on. When the output of gpio [0] is 1, the LED is off, the base address of the gpio register is 0x8000d000, which is the offset address of the used registers.

 

First, select the gpio pin,

* (Unsigned short *) (0x8000d000 + 0x00) = 0x5555;

* (Unsigned short *) (0x8000d000 + 0x04) = 0x5555;

Then, gpio [0] is used as the output, and other pins are set as input.

* (Unsigned short *) (0x8000d000 + 0x08) = 0 xfffe;

Read the current gpio status to avoid changing the gpio status in subsequent operations, leading to other problems,

Unsigned short val;

Val = * (unsigned short *) (0x8000d000 + 0x10 ));

Finally, in the output register, set gpio [0] to 0. Specifically, the gpio [0] is set to 0 by the current state of gpio and the 0xfffe phase, other pins remain unchanged. And write the changed value to the gpio output register.

* (Unsigned short *) (0x8000d000 + 0x0c) = (Val & 0 xfffe );

To turn off the LED, Replace the last sentence:

* (Unsigned short *) (0x8000d000 + 0x0c) = (Val | 0x0001 );

The complete program for LED lighting is as follows:

 

Compile a MAKEFILE file for the program. You can use the MAKEFILE file of the hello program in Lesson 2 to modify it,

 

Next, download to the Development Board and run it,

 

 

8.2 key control LED light off

The hardware diagram shows that when the key is pressed, The gpio [1] pin status is 1. when the key is released, the gpio [1] pin status is 0, by reading the status of the gpio [1] pin, you can know whether the key is pressed, and continuously check the status of the key through a while loop. If the button is pressed, the current led status is changed once. The specific procedure is as follows:

# Include <stdio. h>

 

Void gpio_init (void );

Void led_on (unsigned short Val );

Void led_off (unsigned short Val );

 

Void main ()

{

Unsigned short val;

Unsigned short flag = 0;

Gpio_init ();

While (1)

{

Val = * (unsigned short *) (0x8000d000 + 0x10 ));

If (Val & 0x02) = 0x02)

{

If (flag = 0)

{

Led_on (VAL );

Flag = 1;

}

Else

{

Led_off (VAL );

Flag = 0;

}

}

}

 

}

 

Void gpio_init (void)

{

* (Unsigned short *) (0x8000d000 + 0x00) = 0x5555;

* (Unsigned short *) (0x8000d000 + 0x04) = 0x5555;

* (Unsigned short *) (0x8000d000 + 0x08) = 0 xfffe;

}

 

Void led_on (unsigned short Val)

{

* (Unsigned short *) (0x8000d000 + 0x0c) = (Val & 0 xfffe );

}

 

Void led_off (unsigned short Val)

{

* (Unsigned short *) (0x8000d000 + 0x0c) = (Val | 0x0001 );

}

8.3 conclusion

This course is mainly about programming. The problem is not difficult. It is generally the first program of embedded entry-light emitting diode, I have a general understanding of the status of the operation register to control the pin. At the same time, we also learned some differences between the Embedded C language and the Standard C language.

Class 1 lights up the first LED light

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.