作业需求:
用汇编语言实现LED1/LED2/LED3三盏灯点亮
用C语言实现LED1/LED2/LED3三盏灯点亮 实现过程:
用汇编语言实现LED1/LED2/LED3三盏灯点亮
.text
.global _start
_start:
/**********LED1点灯**************/
/**********RCC章节**************/
@1.设置GPIOE组时钟使能 通过RCC——AHB4ENSETR寄存器设置
ldr r0,=0x50000A28
ldr r1,[r0]
orr r1,r1,#(0x1<<4)
str r1,[r0]
/**********GPIO章节**************/
@1.设置PE10引脚为输出模式
ldr r0,=0x50006000
ldr r1,[r0]
and r1,r1,#(~(0x3<<20))
orr r1,r1,#(0x1<<20)
str r1,[r0]
@2.设置PE10引脚为推挽输出模式
ldr r0,=0x50006004
ldr r1,[r0]
and r1,r1,#(~(0x1<<10))
str r1,[r0]
@3.设置PE10引脚为低速输出
ldr r0,=0x50006008
ldr r1,[r0]
and r1,#(~(0x3<<20))
str r1,[r0]
@4.设置PE10引脚为禁止上下拉电阻
ldr r0,=0x5000600C
ldr r1,[r0]
and r1,#(~(0x3<<20))
str r1,[r0]
/**********LED2点灯**************/
/**********RCC章节**************/
@1.设置GPIOF组时钟使能 通过RCC——AHB4ENSET
ldr r0,=0x50000A28
ldr r1,[r0]
orr r1,r1,#(0x1<<5)
str r1,[r0]
/**********GPIO章节**************/
@1.设置PF10引脚为输出模式
ldr r0,=0x50007000
ldr r1,[r0]
orr r1,r1,#(0x1<<20)
and r1,r1,#(~(0x1<<21))
str r1,[r0]
@2.设置PF10引脚为推挽输出模式
ldr r0,=0x50007004
ldr r1,[r0]
and r1,r1,#(~(0x1<<10))
str r1,[r0]
@3.设置PF10引脚为低速输出
ldr r0,=0x50007008
ldr r1,[r0]
and r1,#(~(0x3<<20))
str r1,[r0]
@4.设置PF10引脚为禁止上下拉电阻
ldr r0,=0x5000700C
ldr r1,[r0]
and r1,#(~(0x3<<20))
str r1,[r0]
/**********LED3点灯**************/
/**********RCC章节**************/
@1.设置GPIOE组时钟使能 通过RCC——AHB4ENSET
ldr r0,=0x50000A28
ldr r1,[r0]
orr r1,r1,#(0x1<<4)
str r1,[r0]
/**********GPIO章节**************/
@1.设置PE8引脚为输出模式
ldr r0,=0x50006000
ldr r1,[r0]
orr r1,r1,#(0x1<<16)
and r1,r1,#(~(0x1<<17))
str r1,[r0]
@2.设置PE8引脚为推挽输出模式
ldr r0,=0x50006004
ldr r1,[r0]
and r1,r1,#(~(0x1<<8))
str r1,[r0]
@3.设置PE8引脚为低速输出
ldr r0,=0x50006008
ldr r1,[r0]
and r1,#(~(0x3<<16))
str r1,[r0]
@4.设置PE8引脚为禁止上下拉电阻
ldr r0,=0x5000600C
ldr r1,[r0]
and r1,r1,#(~(0x3<<16))
str r1,[r0]
loop:
bl LED1_ON
bl delay_1s
bl LED1_OFF
bl delay_1s
b loop
LED1_ON:
ldr r0,=0x50006014
ldr r1,[r0]
orr r1,r1,#(0x1<<10)
str r1,[r0]
ldr r0,=0x50007014
ldr r1,[r0]
orr r1,r1,#(0x1<<10)
str r1,[r0]
ldr r0,=0x50006014
ldr r1,[r0]
orr r1,r1,#(0x1<<8)
str r1,[r0]
mov pc,lr
LED1_OFF:
ldr r0,=0x50006014
ldr r1,[r0]
and r1,r1,#(~(0x1<<10))
str r1,[r0]
ldr r0,=0x50007014
ldr r1,[r0]
and r1,r1,#(~(0x1<<10))
str r1,[r0]
ldr r0,=0x50006014
ldr r1,[r0]
and r1,r1,#(~(0x1<<8))
str r1,[r0]
mov pc,lr
@ 大概1s的延时函数
delay_1s:
mov r3, #0x10000000
mm:
cmp r3, #0
subne r3, r3, #1
bne mm
mov pc, lr
.end
实现结果:
![](https://i-blog.csdnimg.cn/blog_migrate/9fac6dc7e59de6a5274fc8d882983816.jpeg)
实现过程:
用C语言实现LED1/LED2/LED3三盏灯点亮
头文件:
#ifndef __LED_H__
#define __LED_H__
typedef struct
{
volatile unsigned int moder;
volatile unsigned int otyper;
volatile unsigned int ospeedr;
volatile unsigned int pupdr;
volatile unsigned int idr;
volatile unsigned int odr;
}gpio_t;
void led1_init();
void led1_on();
void led1_off();
void led2_init();
void led2_on();
void led2_off();
void led3_init();
void led3_on();
void led3_off();
#endif
源文件:
#include "led.h"
#define GPIOE (*(gpio_t*)0x50006000)
#define GPIOF (*(gpio_t*)0x50007000)
void led1_init()
{
*((volatile unsigned int *) 0x50000A28)=*((volatile unsigned int*) 0x50000A28) | (0x1<<4);
GPIOE.moder=GPIOE.moder & (~(0x3<<20));
GPIOE.moder=GPIOE.moder | (0x1<<20);
GPIOE.otyper=GPIOE.otyper & (~(0x1<<10));
GPIOE.ospeedr=GPIOE.ospeedr & (~(0x3<<20));
GPIOE.pupdr=GPIOE.pupdr & (~(0x3<<20));
}
void led1_on()
{
GPIOE.odr=GPIOE.odr | (0x1<<10);
}
void led1_off()
{
GPIOE.odr=GPIOE.odr & (~(0x1<<10));
}
void led2_init()
{
*((volatile unsigned int *) 0x50000A28)=*((volatile unsigned int*) 0x50000A28) | (0x1<<5);
GPIOF.moder=GPIOF.moder & (~(0x3<<20));
GPIOF.moder=GPIOF.moder | (0x1<<20);
GPIOF.otyper=GPIOF.otyper & (~(0x1<<10));
GPIOF.ospeedr=GPIOF.ospeedr & (~(0x3<<20));
GPIOF.pupdr=GPIOF.pupdr & (~(0x3<<20));
}
void led2_on()
{
GPIOF.odr=GPIOF.odr | (0x1<<10);
}
void led2_off()
{
GPIOF.odr=GPIOF.odr & (~ (0x1<<10));
}
void led3_init()
{
*((volatile unsigned int *) 0x50000A28)=*((volatile unsigned int*) 0x50000A28) | (0x1<<4);
GPIOE.moder=GPIOE.moder & (~(0x3<<16));
GPIOE.moder=GPIOE.moder | (0x1<<16);
GPIOE.otyper=GPIOE.otyper & (~(0x1<<8));
GPIOE.ospeedr=GPIOE.ospeedr & (~(0x3<<16));
GPIOE.pupdr=GPIOE.pupdr & (~(0x3<<16));
}
void led3_on()
{
GPIOE.odr=GPIOE.odr | (0x1<<8);
}
void led3_off()
{
GPIOE.odr=GPIOE.odr & (~(0x1<<8));
}
测试文件:
#include "led.h"
extern void printf(const char *fmt, ...);
void delay_ms(int ms)
{
int i,j;
for(i = 0; i < ms;i++)
for (j = 0; j < 1800; j++);
}
int main()
{
led1_init(); // LED灯初始化
led2_init();
led3_init();
while(1)
{
led1_on();
led2_on();
led3_on();
delay_ms(500);
led1_off();
led2_off();
led3_off();
delay_ms(500);
}
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/7eea1d1c5a4f97055016a31b56e3f45a.jpeg)