Step-by-step LED

Source: Internet
Author: User
Tags call back

1. Understanding Physical Characteristics

The key to lighting LEDs is the voltage difference.

2. Consult the schematic to understand the hardware connection of onboard LEDs

Method: can use the search function of PDF document (search led, search in backplane)

Know: There are 4 positive connection 3.3V, the anode is connected to the SOC above the pin (that is, pin low-power Pingliang), the other one is normally lit.

3 Soc pins are known, and in addition to the core board view PWMTOUT1 The PIN is gpd0_1, such as

The GPD0 represents the port number, and the next 1 is the PIN number

3. Consult the data sheet

When we want to programmatically manipulate the Gpio to operate the LEDs, we need to read through the data sheet on the Gpio section

We want to operate the hardware as LED, through the Gpio indirect control, the actual device to operate is gpio.

3.1. GPJ0 related registers are as follows

Among them, the most important registers are: Gpj0_dat, Gpj0con

3.2, the following is the Gpj0con register:

GPJ0 the first pin of this port gpj0_0 corresponds to the bit0~bit3 of the Register Gpj0_con, the pin should be configured to output mode when the LED is lit.

3.3, Gpj0dat Register Introduction

gpj0dat[7:0] 7:0 for the GPJ0 corresponds to the 0~7 pin, the back of the bit[7:0] represents the Gpj0dat register, it is known from here that each pin of the GPJ0 port corresponds to each bit bit of the Gpjodat register. 3.4, summary According to the above, we know how to light the LED (1) Control Gpj0con Register, check for output mode. (2) control the Gpj0dat register, select the corresponding bit is low level. 4. Step-by-step lighting le4.1. Light LED Light
1 _start:2     //First step: Write 0x11111111 to 0xe0200240 (gpj0con) position3LDR R0, =0x11111111            //from the back of the = can be seen with the LDR pseudo-directive, because the compiler needs to judge the number4LDR R1, =0xe0200240            //whether it is a legitimate immediate number or an illegal immediate number. General Write code is used LDR pseudo-directive5STR r0, [R1]//register Indirect addressing. The function is to write the number in the R0 to the number in the R1 address in memory6 7     //Step Two: Write 0x0 to 0xe0200244 (gpj0dat) position8LDR R0, =0x09LDR R1, =0xe0200244TenSTR r0, [R1]//writes 0 to the Gpj0dat register, the pin is output low, and the LED is lit . One  AFlag//These two lines write a dead loop. Because bare-metal programs run directly on the CPU, the CPU -B Flag//Run the bare metal program row by line until the CPU loses power off. If all the code in our program is -                                 //after executing the CPU will run to fly (after the flight is undefined, so you must not let the CPU the                                 //run to fly), the way to not let the CPU run to fly is to add a dead loop after our entire program has finished executing
View Code

4.2, improve the code 1

1 #defineGpj0con 0xe02002402 #defineGpj0dat 0xe02002443 4.Global_start//Change the _start link property to external so that other files can see _start.5 _start:6     //First step: Set all pins to output mode, the code will not change7LDR R0, =0x11111111            //from the back of the = can be seen with the LDR pseudo-directive, because the compiler needs to judge the number8LDR R1, =gpj0con//whether it is a legitimate immediate number or an illegal immediate number. General Write code is used LDR pseudo-directive9STR r0, [R1]//register Indirect addressing. The function is to write the number in the R0 to the number in the R1 address in memoryTen  One     //The second step: The output of the middle led (Gpj0_4) is set to 0, the remaining two are set to 1, the rest of the other bits regardless ALDR R0, =0x28 -LDR R1, =Gpj0dat -STR r0, [R1]//writes 0 to the Gpj0dat register, the pin is output low, and the LED is lit . the  -B.//represents the address of the current instruction, this is the death cycle on the tall
View Code

The above code improvements:

(1) Replace the number of addresses with a macro definition

(2) Replace the previous dead loop with B.

(3) Add. Global _start (Change _start link property to external link)

Change: The code changes the LED in the middle of the 4.1-point LEDs to light.

4.3, improve the code 2

Defects in 4.2: it is necessary to artificially calculate 0x28 this particular value, and is not readable.

Solution: Use bit arithmetic when writing code to let the compiler help us calculate specific values.
1 #defineGpj0con 0xe02002402 #defineGpj0dat 0xe02002443 4.Global_start//Change the _start link property to external so that other files can see _start.5 _start:6     //First step: Set all pins to output mode, the code will not change7LDR R0, =0x11111111            //from the back of the = can be seen with the LDR pseudo-directive, because the compiler needs to judge the number8LDR R1, =gpj0con//whether it is a legitimate immediate number or an illegal immediate number. General Write code is used LDR pseudo-directive9STR r0, [R1]//register Indirect addressing. The function is to write the number in the R0 to the number in the R1 address in memoryTen  One     //The second step: The output of the middle led (Gpj0_4) is set to 0, the remaining two are set to 1, the rest of the other bits regardless A     //Ldr R0, = ((1<<3) |    (1<<5)) //equivalent to 0b00101000, i.e. 0x28 -Ldr R0, = ((1<<3) | (0<<4) | (1<<5))//clearly see which is out, which is bright -LDR R1, =Gpj0dat theSTR r0, [R1]//writes 0 to the Gpj0dat register, the pin is output low, and the LED is lit . -  -B.//represents the address of the current instruction, this is the death cycle on the tall
View Code

4.4, improve the code 3

Plus the delay function and some functions

1 #defineGpj0con 0xe02002402 #defineGpj0dat 0xe02002443 4.Global_start//Change the _start link property to external so that other files can see _start.5 _start:6     //First step: Set all pins to output mode, the code will not change7LDR R0, =0x11111111            //from the back of the = can be seen with the LDR pseudo-directive, because the compiler needs to judge the number8LDR R1, =gpj0con//whether it is a legitimate immediate number or an illegal immediate number. General Write code is used LDR pseudo-directive9STR r0, [R1]//register Indirect addressing. The function is to write the number in the R0 to the number in the R1 address in memoryTen  One     //to realize the flow lamp, as long as the main loop to achieve the 1-turn flow display effect can be A Flash: -     //1th Step: Light LED1, other off -     //Ldr R0, = ((0<<3) | (1<<4) |    (1<<5)) //clearly see which is out, which is bright theLdr R0, =~ (1<<3) -LDR R1, =Gpj0dat -STR r0, [R1]//writes 0 to the Gpj0dat register, the pin is output low, and the LED is lit . -     //then delay +BL delay//using BL for function calls -      +     //2nd step: Light LED2, other off ALdr R0, =~ (1<<4) atLDR R1, =Gpj0dat -STR r0, [R1]//writes 0 to the Gpj0dat register, the pin is output low, and the LED is lit . -     //then delay -BL delay//using BL for function calls -      -     //3rd Step: Light LED3, other off inLdr R0, =~ (1<<5) -LDR R1, =Gpj0dat toSTR r0, [R1]//writes 0 to the Gpj0dat register, the pin is output low, and the LED is lit . +     //then delay -BL delay//using BL for function calls the      * b Flash $ Panax Notoginseng  - //Delay function: Function name: Delay the Delay: +LDR r2, =9000000 ALDR R3, =0x0 the Delay_loop: +Sub R2, R2, #1                //r2 = r2-1 -CMP R2, R3//The CMP affects the z flag bit, and if R2 equals R3 then Z=1, the EQ will be set up in the next sentence $ bne Delay_loop $mov pc, LR//function call returned
View Code

Key points of knowledge:

(1) b often used for non-return jumps, such as jumping to a label, BL is used for subroutine jump (to return, back to the ground in LR)

(2) in the sub-function generally with MOV pc, LR This sentence is used to call back

(3) in the delay function

CMP R2, R3//CMP affects the z flag bit, and if R2 equals R3 then Z=1, the EQ will be set up in the next sentence

BNE Delay_loop

NE is the conditional suffix, which is set to execute this instruction

/********************************************************

* Above knowledge from teacher Zhu's Internet of Things lecture hall

*******************************************************/

Step-by-step LED

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.