汇编实现点灯:
.text
.global _start
_start:
bl LED_INIT
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
LED_INIT:
/**********LED1点灯**************/
/*******RCC章节*******/
@ 1.设置GPIOE组时钟使能,通过RCC_AHB4ENSETR寄存器设置 0x50000A28[4]=1
ldr r0,=0x50000A28 @准备一块地址空间r0 =0x50000a28
ldr r1,[r0]
orr r1,r1,#(0x1<<4)
str r1,[r0]
@ 2.设置GPIOF组时钟使能,通过RCC_AHB4ENSETR寄存器设置 0x50000A28[5]=1
ldr r0,=0x50000A28 @准备一块地址空间r0 =0x50000a28
ldr r1,[r0]
orr r1,r1,#(0x1<<5)
str r1,[r0]
/*****LED1->PE10 LED2->PF10 LED3->PE8*********/
/********GPIO章节**********/
@设置led1_IO口MODER模式 PE10
ldr r0,=0x50006000
ldr r1,[r0]
and r1,r1,#(~(0x3<<20))
orr r1,r1,#(0x1<<20)
str r1,[r0]
@设置led1_IO口输出类型OTYPER PE10
ldr r0,=0x50006004
ldr r1,[r0]
and r1,r1,#(~(0x1<<10))
str r1,[r0]
@设置led1_GPIO引脚输出速率 PE10
ldr r0,=0x50006008
ldr r1,[r0]
and r1,r1,#(~(0x3<<20))
str r1,[r0]
@设置led1_GPIO引脚禁止上下拉电阻 PE10
ldr r0,=0x5000600C
ldr r1,[r0]
and r1,r1,#(~(0x3<<20))
str r1,[r0]
@设置led2_IO口MODER模式 PF10
ldr r0,=0x50007000
ldr r1,[r0]
and r1,r1,#(~(0x3<<20))
orr r1,r1,#(0x1<<20)
str r1,[r0]
@设置led2_IO口输出类型OTYPER PF10
ldr r0,=0x50007004
ldr r1,[r0]
and r1,r1,#(~(0x1<<10))
str r1,[r0]
@设置led2_GPIO引脚输出速率 PF10
ldr r0,=0x50007008
ldr r1,[r0]
and r1,r1,#(~(0x3<<20))
str r1,[r0]
@设置led2_GPIO引脚禁止上下拉电阻 PF10
ldr r0,=0x5000700C
ldr r1,[r0]
and r1,r1,#(~(0x3<<20))
str r1,[r0]
@设置led3_IO口MODER模式 PE8
ldr r0,=0x50006000
ldr r1,[r0]
and r1,r1,#(~(0x3<<16))
orr r1,r1,#(0x1<<16)
str r1,[r0]
@设置led3_IO口输出类型OTYPER PE8
ldr r0,=0x50006004
ldr r1,[r0]
and r1,r1,#(~(0x1<<8))
str r1,[r0]
@设置led3_GPIO引脚输出速率 PE8
ldr r0,=0x50006008
ldr r1,[r0]
and r1,r1,#(~(0x3<<16))
str r1,[r0]
@设置led1_GPIO引脚禁止上下拉电阻 PE8
ldr r0,=0x5000600C
ldr r1,[r0]
and r1,r1,#(~(0x3<<16))
str r1,[r0]
mov pc,lr
led1_on:
@设置GPIO引脚输出高电平
ldr r0,=0x50006014
ldr r1,[r0]
orr r1,r1,#(0x1<<10)
str r1,[r0]
mov pc,lr
led1_off:
@设置GPIO引脚输出低电平
ldr r0,=0x50006014
ldr r1,[r0]
and r1,r1,#(~(0x1<<10))
str r1,[r0]
mov pc,lr
led2_on:
@设置GPIO引脚输出高电平
ldr r0,=0x50007014
ldr r1,[r0]
orr r1,r1,#(0x1<<10)
str r1,[r0]
mov pc,lr
led2_off:
@设置GPIO引脚输出低电平
ldr r0,=0x50007014
ldr r1,[r0]
and r1,r1,#(~(0x1<<10))
str r1,[r0]
mov pc,lr
led3_on:
@设置GPIO引脚输出高电平
ldr r0,=0x50006014
ldr r1,[r0]
orr r1,r1,#(0x1<<8)
str r1,[r0]
mov pc,lr
led3_off:
@设置GPIO引脚输出低电平
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
C语言实现点灯:
gpio.h:
#ifndef __GPIO_H__
#define __GPIO_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;
#define GPIOE ((gpio_t*)0x50006000)
#define GPIOF ((gpio_t*)0x50007000)
#define RCC_AHB4_ENSETR (*(volatile unsigned int*)0x50000A28)
enum{
LED1=1,
LED2,
LED3
};
void hal_gpio_init(gpio_t *gpiox,unsigned int pin);//GPIO章节初始化
void hal_gpio_output_high(unsigned int ledx);//点亮
void hal_gpio_output_low(unsigned int ledx);//熄灭
#endif
gpio.c:
#include "../include/gpio.h"
void hal_gpio_init(gpio_t* gpiox,unsigned int pin)
{
gpiox->MODER&=(~(0x3<<pin*2));
gpiox->MODER|=(0x1<<pin*2);
gpiox->OTYPER&=(~(0x1<<pin));
gpiox->OSPEEDR&=(~(0x3<<pin*2));
gpiox->PUPDR&=(~(0x3<<pin*2));
}
void hal_gpio_output_high(unsigned int ledx)
{
switch (ledx)
{
case LED1:
GPIOE->ODR|=(0x1<<10);
break;
case LED2:
GPIOF->ODR|=(0x1<<10);
break;
case LED3:
GPIOE->ODR|=(0x1<<8);
break;
default:
break;
}
}
void hal_gpio_output_low(unsigned int ledx)
{
switch (ledx)
{
case LED1:
GPIOE->ODR&=(~(0x1<<10));
break;
case LED2:
GPIOF->ODR&=(~(0x1<<10));
break;
case LED3:
GPIOE->ODR&=(~(0x1<<8));
break;
default:
break;
}
}
main.c:
#include "../include/gpio.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_AHB4_ENSETR|=(0x1<<4);//初始化GPIOE组RCC寄存器
RCC_AHB4_ENSETR|=(0x1<<5);//初始化GPIOF组RCC寄存器
hal_gpio_init(GPIOE,10); // LED1灯初始化
hal_gpio_init(GPIOF,10); // LED2灯初始化
hal_gpio_init(GPIOE,8); // LED3灯初始化
while(1)
{
hal_gpio_output_high(LED1);//LED1亮
delay_ms(500);
hal_gpio_output_low(LED1);//LED1灭
delay_ms(500);
hal_gpio_output_high(LED2);
delay_ms(500);
hal_gpio_output_low(LED2);
delay_ms(500);
hal_gpio_output_high(LED3);
delay_ms(500);
hal_gpio_output_low(LED3);
delay_ms(500);
}
return 0;
}