当前位置:网站首页>Arm architecture and programming 3 -- key control LED (based on Baiwen arm architecture and programming tutorial video)
Arm architecture and programming 3 -- key control LED (based on Baiwen arm architecture and programming tutorial video)
2022-07-24 01:22:00 【Mountains】
1、 Assembly implementation LED flashing
According to the previous compilation knowledge , You can know how to use assembly operation register and realize function call and parameter transfer . The following is the assembly code implementation written by myself LED flashing .



delay_A ; No parameter , No return value delay function
ldr r1,=0x30d40 ; Function local variable 200000
loop_delay_A
subs r1,r1,#1 ; Local variable minus 1, The calculation results affect PSR
bne loop_delay_A ;r1 != 0, Continue to cycle
bx lr ;r1 = 0, The loop ends , return while loop
delay_B ; With parameters , Delay function with return value , Using stack operations
push {r0,lr} ; Put parameters 5000 And return address pushed onto the stack
nop
loop_delay_B
ldr r1,[sp,#0] ; Read the data in the stack , That is to say 200000
subs r2,r1,#1 ; Local variable minus 1 Save in R2, The calculation results affect PSR
str r2,[sp,#0] ; Save the changed variables on the stack
bne loop_delay_B ;r1 != 0, Continue to cycle
mov r0,#0xff ; The return value is stored in r0
pop {r3,pc} ; Out of the stack , hold lr The address in is assigned to PC, The program jumps to the return address to continue execution
main
; Can make GPIO The clock
ldr r1,=0x40021000
add r1,r1,#0x18 ; Set the real address = Base address + offset
ldr r2,[r1] ; Read register
orr r2,r2,#8 ; Bit operation , Third place 1
str r2,[r1] ; Write register
; Set up GPIO The output mode
ldr r1,=0x40010C00
add r1,r1,#0
ldr r2,[r1]
orr r2,r2,#1
str r2,[r1]
; Set up GPIO Output high level , The light goes out
ldr r1,=0x40010C00
add r1,r1,#0X0c
ldr r2,[r1]
orr r2,r2,#1 ;bit0 = 1
str r2,[r1]
;while loop
loop
; Call delay function delay_A-- No parameters
bl delay_A ; No parameters , No use R0 Save parameters , Direct jump
;GPIO Output low level , Light on
ldr r1,=0x40010C00
add r1,r1,#0X0c
ldr r2,[r1]
bic r2,r2,#1 ; The lowest bit is cleared ,bit0=0
str r2,[r1]
; Call delay function delay_B-- With parameters
ldr r0,=0x30d40 ;0x30d40=200000, Save the parameters in R0 in
bl delay_B ; Jump
;GPIO Output high level , The light goes out
ldr r1,=0x40010C00
add r1,r1,#0X0c
ldr r2,[r1]
orr r2,r2,#1 ;bit0 = 1
str r2,[r1]
b loop ; Jump to loop Realize the cycle
Reset_Handler PROC
LDR SP, =0x20000000+0x100
BL main
ENDP
END
In order to understand CPU Functions of internal registers , Two delay functions are used , One has no parameters , no return value , Use it directly CPU Internal registers read and write data ; The other has parameters , There is a return value , Use stack to read and write data .
matters needing attention :
1、 When a function is called , Jump command should use BL, The return address is saved to LR in , Otherwise, the function cannot return to the original address after execution .
2、 The parameters of the function are saved in R0-R3 in , Save the parameters in R0 in .
3、 For simple functions , The compiler will optimize , Data is stored directly in internal registers ,CPU Direct operation of internal registers , Efficient . For complex functions , The data will be put on the stack , Data is taken out of the stack , Put it back on the stack .
4、ldr,str Such as instruction , Square brackets indicate that the operation is the data of the address stored in the internal register in square brackets . The change is R0 The data pointed to by the saved address , instead of R0 Data in .
2、C Language to achieve key control LED
Just like before LED The experiment is similar , The same is true for keys . Here key connection PA0 Pin , Press the key ,PA0 Low level ; Release the button ,PA0 High level .
1、 Can make GPIOA.
2、 Set up PA0 The pin is in input mode .
3、 Read GPIOA Input data register , obtain PA0 Pin status of .
GPIO Before the clock enable register and the working mode configuration register LED It was introduced in the experiment , There is no explanation here , So let's talk about that GPIO Data register GPIOX_IDR .
You can see , This is a 32 Bit register , low 16 Bit effective , preservation PIN0-15 Current state of each pin , above-mentioned , This register can only read all values , Cannot read by bit .
Here is the procedure , First, define the pointer to all the registers used , Then through bit operation , Modify the value of the register , stay while In circulation , Read GPIOA_IDR Value , And to 0 Bit to judge , if bit0=1, explain PA0 This is the high level , Release the button , The light goes out ; if bit0=0, explain PA0 At this time, it is low level , Press the key , Light on .
int main(void)
{
unsigned int * pAPB2En = ( unsigned int * )(0x40021000 + 0x18);
unsigned int * pGPIOBCrl = ( unsigned int * )(0x40010C00 + 0);
unsigned int * pGPIOACrl = ( unsigned int * )(0x40010800 + 0x00);
unsigned int * pGPIOOdr = ( unsigned int * )(0x40010C00 + 0X0c);
unsigned int * pGPIOIdr = ( unsigned int * )(0x40010800 + 0x08);
*pAPB2En |= (3<<2); // Can make GPIO_A_B
* pGPIOBCrl |= (1<<0); //PB0 Set the output mode
* pGPIOACrl |= (1<<2); //PA0 Set to input mode
* pGPIOOdr |= (1<<0); //PB0=1, The light goes out
while(1)
{
if((* pGPIOIdr) & (1<<0) == 1) //PA0=1, Release the button
{
* pGPIOOdr |= (1<<0); //PB0=1, The light goes out
}
else //PA0=0, Press the key
{
* pGPIOOdr &= ~(1<<0); //PB0=0, Light on
}
}
}
边栏推荐
- 1000 okaleido tiger launched binance NFT, triggering a rush to buy
- Review questions of polymer synthesis technology
- Why can't HMI panels of botu V17 and below connect with CPUs of 1500 firmware version 2.9 or 1200 firmware version 4.5?
- Notes to Chapter 2 of kubernetes in action
- HCIP中的MGRE GRE OSPF过程
- SCM learning notes 1 -- data download and environment construction (based on Baiwen STM32F103 series tutorials)
- Parsing yaml configuration files using C language and libcyaml Library
- HCIP第六天笔记
- OSPF(第六天笔记)
- 2022 global developer salary exposure: China ranks 19th, with an average annual salary of $23790
猜你喜欢

Skywalking distributed system application performance monitoring tool - upper

1000 okaleido tiger launched binance NFT, triggering a rush to buy

Navicat for MySQL installation tutorial

HCIA的复习

Group chat room based on UDP

Answer to the short answer question of polymer synthesis technology
![[untitled]](/img/84/f6ee2fba096cd31190f14436196535.png)
[untitled]

Why can't HMI panels of botu V17 and below connect with CPUs of 1500 firmware version 2.9 or 1200 firmware version 4.5?

好大夫问诊-俞驰-口腔信息

Axure implements addition, deletion, modification and query
随机推荐
Redis - configuration and Application
Domestic MCU and SOC are rising, but they are still lagging behind in special fields
Answer to the short answer question of polymer synthesis technology
Preprocessing instruction define, do you really understand?
HCIP第十二天笔记
爬虫requests模块的基本使用
网络类型(第三天笔记)
OSI open system interconnection model and tcp/ip model
OSPF(第五天笔记)
High voltage technical examination questions with answers
数字化转型时代的企业数据新基建 | 爱分析报告
LVS load balancing scheduling principle and configuration method
Navicat for MySQL installation tutorial
HCIP第十一天笔记
Kotlin foundation from introduction to advanced series explanation (basic chapter) keyword: suspend
罗克韦尔AB PLC RSLogix5000中的位指令使用方法介绍
Redis - basic concept
C language: student management system based on sequence table, super detailed, all with notes, if you don't understand it after reading, please slap me.
SCM learning notes 1 -- data download and environment construction (based on Baiwen STM32F103 series tutorials)
Basic use of crawler requests module