asm-led.S
.text
.global _start
_start:
/**********LED1点灯**********/
/**********RCC章节初始化**********/
RCC_INIT:
@通过RCC_MP_AHB4ENSETR寄存器使能GPIOE组控制器 0x50000A28[4] = 1
ldr r0,=0x50000A28 @LED1 GPIOE10 LED3 GPIOE8
ldr r1,[r0]
orr r1,r1,#(0x1 << 4)
str r1,[r0]
@通过RCC_MP_AHB4ENSETR寄存器使能GPIOF组控制器 0x50000A28[5] = 1
ldr r0,=0x50000A28
ldr r1,[r0]
orr r1,r1,#(0x1 << 5)
str r1,[r0]
/**********GPIO节初始化**********/
LED1_INIT:
@1.通过GPIOE_MODER寄存器设置PE10引脚为输出模式 0x50006000[21:20] = 01
ldr r0,=0x50006000
ldr r1,[r0]
and r1,r1,#(~(0x3 << 20))
orr r1,r1,#(0x1 << 20)
str r1,[r0]
@2.通过GPIOE_OTYPER寄存器设置PE10引脚为推挽输出类型 0x50006004[10] = 0
ldr r0,=0x50006004
ldr r1,[r0]
and r1,r1,#(~(0x1 << 10))
str r1,[r0]
@3.通过GPIOE_OSPEEDR寄存器设置PE10引脚为低速输出模式 0x50006008[21:20] = 00
ldr r0,=0x50006008
ldr r1,[r0]
and r1,r1,#(~(0x3 << 20))
str r1,[r0]
@4.通过GPIOE_PUPDR寄存器设置PE10引脚禁止上下拉电阻 0x5000600c[21:20] = 00
ldr r0,=0x5000600c
ldr r1,[r0]
and r1,r1,#(~(0x3 << 20))
str r1,[r0]
LED2_INIT:
@1.通过GPIOE_MODER寄存器设置PF10引脚为输出模式 0x50007000[21:20] = 01
ldr r0,=0x50007000
ldr r1,[r0]
and r1,r1,#(~(0x3 << 20))
orr r1,r1,#(0x1 << 20)
str r1,[r0]
@2.通过GPIOE_OTYPER寄存器设置PF10引脚为推挽输出类型 0x50007004[10] = 0
ldr r0,=0x50007004
ldr r1,[r0]
and r1,r1,#(~(0x1 << 10))
str r1,[r0]
@3.通过GPIOE_OSPEEDR寄存器设置PF10引脚为低速输出模式 0x50007008[21:20] = 00
ldr r0,=0x50007008
ldr r1,[r0]
and r1,r1,#(~(0x3 << 20))
str r1,[r0]
@4.通过GPIOE_PUPDR寄存器设置PF10引脚禁止上下拉电阻 0x5000700c[21:20] = 00
ldr r0,=0x5000700c
ldr r1,[r0]
and r1,r1,#(~(0x3 << 20))
str r1,[r0]
LED3_INIT:
@1.通过GPIOE_MODER寄存器设置PE8引脚为输出模式 0x50006000[17:16] = 01
ldr r0,=0x50006000
ldr r1,[r0]
and r1,r1,#(~(0x3 << 16))
orr r1,r1,#(0x1 << 16)
str r1,[r0]
@2.通过GPIOE_OTYPER寄存器设置PE8引脚为推挽输出类型 0x50006004[8] = 0
ldr r0,=0x50006004
ldr r1,[r0]
and r1,r1,#(~(0x1 << 8))
str r1,[r0]
@3.通过GPIOE_OSPEEDR寄存器设置PE8引脚为低速输出模式 0x50006008[17:16] = 00
ldr r0,=0x50006008
ldr r1,[r0]
and r1,r1,#(~(0x3 << 16))
str r1,[r0]
@4.通过GPIOE_PUPDR寄存器设置PE8引脚禁止上下拉电阻 0x5000600c[17:16] = 00
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
bl LED2_ON
bl delay_1s
bl LED2_OFF
bl delay_1s
bl LED3_ON
bl delay_1s
bl LED3_OFF
bl delay_1s
b loop
LED1_ON:
@通过GPIOE_ODR寄存器设置PE10引脚输出高电平 0x50006014[10] = 1
ldr r0,=0x50006014
ldr r1,[r0]
orr r1,r1,#((0x1 << 10))
str r1,[r0]
mov pc,lr
LED1_OFF:
@通过GPIOE_ODR寄存器设置PE10引脚为低电平 0x50006014[10] = 0
ldr r0,=0x50006014
ldr r1,[r0]
bic r1,r1,#(0x1 << 10)
str r1,[r0]
mov pc,lr
LED2_ON:
@通过GPIOE_ODR寄存器设置PF10引脚输出高电平 0x50007014[10] = 1
ldr r0,=0x50007014
ldr r1,[r0]
orr r1,r1,#((0x1 << 10))
str r1,[r0]
mov pc,lr
LED2_OFF:
@通过GPIOE_ODR寄存器设置PF10引脚为低电平 0x50007014[10] = 0
ldr r0,=0x50007014
ldr r1,[r0]
bic r1,r1,#(0x1 << 10)
str r1,[r0]
mov pc,lr
LED3_ON:
@通过GPIOE_ODR寄存器设置PE8引脚输出高电平 0x50006014[8] = 1
ldr r0,=0x50006014
ldr r1,[r0]
orr r1,r1,#((0x1 << 8))
str r1,[r0]
mov pc,lr
LED3_OFF:
@通过GPIOE_ODR寄存器设置PE8引脚为低电平 0x50006014[8] = 0
ldr r0,=0x50006014
ldr r1,[r0]
bic 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
C语言工程项目代码
led.h
#ifndef __LED_H__
#define __LED_H__
//声明一个结构体
typedef struct{
unsigned int MODER;//00
unsigned int OTYPER;//04
unsigned int OSPEEDR;//08
unsigned int PUPDR;//0c
unsigned int IDR;//10
unsigned int ODR;//14
}gpio_t;
#define GPIOE ((volatile gpio_t*)0x50006000)
#define GPIOF ((volatile gpio_t*)0x50007000)
//rcc 初始化
void rcc_init();
//led1 初始化
void led1_init();
//led2 初始化
void led2_init();
//led3 初始化
void led3_init();
//led1 灯打开
void led1_on();
//led1灯关闭
void led1_off();
//led2 灯打开
void led2_on();
//led2灯关闭
void led2_off();
//led3 灯打开
void led3_on();
//led3灯关闭
void led3_off();
#endif
main.c
#include"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()
{
rcc_init();
led1_init(); // LED1灯初始化
led2_init(); // LED2灯初始化
led3_init(); // LED3灯初始化
while(1)
{
led1_on();
delay_ms(500);
led1_off();
delay_ms(500);
led2_on();
delay_ms(500);
led2_off();
delay_ms(500);
led3_on();
delay_ms(500);
led3_off();
delay_ms(500);
led1_on();
led2_on();
led3_on();
delay_ms(500);
led1_off();
led2_off();
led3_off();
delay_ms(500);
led1_on();
led3_on();
delay_ms(500);
led1_off();
led3_off();
delay_ms(500);
led3_on();
delay_ms(500);
led3_off();
delay_ms(500);
led2_on();
delay_ms(500);
led2_off();
delay_ms(500);
led1_on();
delay_ms(500);
led1_off();
delay_ms(500);
}
return 0;
}
led.c
#include"../include/led.h"
/*
*led1 引脚pe10
*led2 引脚pf10
*led3 引脚pe8
*/
/*rcc初始化
* function: rcc_init pe10,pf10,pe8
* @param [ in]
* @param [out]
* @return
*/
void rcc_init(){
*(volatile int *)0x50000A28 |=(0x3 << 4);
}
/*led1灯初始化
* function: led1_init pe10
* @param [ in]
* @param [out]
* @return
*/
void led1_init(){
GPIOE->MODER &= (~(0x3 << 20));
GPIOE->MODER |= (0x1 << 20);
GPIOE->OTYPER &= (~(0x1 << 10));
GPIOE->OSPEEDR &= (~(0x3 << 20));
GPIOE->PUPDR &= (~(0x3 << 20));
}
/*led2灯初始化
* function: led2_init pf10
* @param [ in]
* @param [out]
* @return
*/
void led2_init(){
GPIOF->MODER &= (~(0x3 << 20));
GPIOF->MODER |= (0x1 << 20);
GPIOF->OTYPER &= (~(0x1 << 10));
GPIOF->OSPEEDR &= (~(0x3 << 20));
GPIOF->PUPDR &= (~(0x3 << 20));
}
/*led3灯初始化
* function: led3_init pe8
* @param [ in]
* @param [out]
* @return
*/
void led3_init(){
GPIOE->MODER &= (~(0x3 << 16));
GPIOE->MODER |= (0x1 << 16);
GPIOE->OTYPER &= (~(0x1 << 8));
GPIOE->OSPEEDR &= (~(0x3 << 16));
GPIOE->PUPDR &= (~(0x3 << 16));
}
/*led1 灯打开
* function: led1_on
* @param [ in]
* @param [out]
* @return
*/
void led1_on(){
GPIOE->ODR |= (0x1 << 10);
}
/*led1 灯关闭
* function: led1_off
* @param [ in]
* @param [out]
* @return
*/
void led1_off(){
GPIOE->ODR &= (~(0x1 << 10));
}
/*led2 灯打开
* function: led2_on
* @param [ in]
* @param [out]
* @return
*/
void led2_on(){
GPIOF->ODR |= (0x1 << 10);
}
/*led2 灯关闭
* function: led2_off
* @param [ in]
* @param [out]
* @return
*/
void led2_off(){
GPIOF->ODR &= (~(0x1 << 10));
}
/*led3 灯打开
* function: led3_on
* @param [ in]
* @param [out]
* @return
*/
void led3_on(){
GPIOE->ODR |= (0x1 << 8);
}
/* led3 灯关闭
* function: led3_off
* @param [ in]
* @param [out]
* @return
*/
void led3_off(){
GPIOE->ODR &= (~(0x1 << 8));
}