当前位置:网站首页>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
}
}
}
边栏推荐
- Sword *offer04 rebuild binary tree
- RPM build has installed the dependent package, but it still reports an error. There is no module provided in the package
- URL query parameter encoding problem (golang)
- HCIP网络类型,ppp会话,数据链路层协议
- RIP(第二天笔记)
- Create database table db.create in flask project_ all()
- Description of TCP packet sticking problem code
- Socket basic knowledge and various usage scenarios
- Data warehouse construction - ods floor
- OSI、TCP/IP(A1)
猜你喜欢

Concept, key points and summary of postgraduate entrance examination in Polymer Physics

Explanation and induction of polymer physics terms

2022全球开发者薪资曝光:中国排第19名,平均年薪23,790美元

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

Basic exercises of C language for beginners

Matlab extracts the original data in the illustrations of the paper - fig2data tool

kubernetes 部署 dashboard(可視化界面)

IDEA设置 自动导包删无用包

HCIP第三天笔记

HCIP第七天笔记
随机推荐
HCIP第一天笔记
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?
cnpm 执行时卡住应该怎么解决?
Openresty模板实时渲染 lua-resty-template
[untitled]
Create database table db.create in flask project_ all()
General method of C language supporting yaml configuration file
High voltage technology learning summary
Review questions of polymer synthesis technology
2022 global developer salary exposure: China ranks 19th, with an average annual salary of $23790
Redis - configuration and Application
Several states during SCI review
Preprocessing instruction define, do you really understand?
RIP(第二天笔记)
HCIP中OSPF详解
OSPF(第四天笔记)
HCIP中的MGRE GRE OSPF过程
Source code installation and use of APIs IX
Broadcast, multicast, unicast
Sublime text 3 汉化+添加常用插件