1 汇编实现
text
global _start
start:
************** LED1点灯 ---> PE10 **************/
************** RCC章节初始化 **************/
CC_INIT:
@1.使能GPIOE组控制器,通过RCC_MP_AHB4ENSETR寄存器设置GPIOE组使能0x50000A28[4]= 1
ldr r0,=0x50000A28 @准备一块地址空间0x50000A28
ldr r1,[r0] @将0x50000A28这块地址中的内容,读到目标寄存器r1中
orr r1,r1,#(0x3 << 4) @将r1寄存器第[5:4] = 11,保证其他位不变
str r1,[r0] @将r1寄存器中的值,写回0x50000A28这块地址空间中
************** LED1对应GPIO章节初始化 **************/
ED1_INIT:
@1.通过GPIOE_MODER寄存器,设置PE10引脚为 输出模式 0x50006000[21:20] = 01
ldr r0,=0x50006000
ldr r1,[r0]
orr r1,r1,#(0x3 << 20)
and r1,r1,#(~(0x1 << 21))
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对应GPIO章节初始化 **************/
ED2_INIT:
@1.通过GPIOE_MODER寄存器,设置PF10引脚为 输出模式 0x50007000[21:20] = 01
ldr r0,=0x50007000
ldr r1,[r0]
orr r1,r1,#(0x3 << 20)
and r1,r1,#(~(0x1 << 21))
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对应GPIO章节初始化 **************/
ED3_INIT:
@1.通过GPIOE_MODER寄存器,设置PE8引脚为 输出模式 0x50006000[17:16] = 01
ldr r0,=0x50006000
ldr r1,[r0]
orr r1,r1,#(0x3 << 16)
and r1,r1,#(~(0x1 << 17))
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]
oop:
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点亮 **************/
ED1_ON:
@1.通过GPIOE_ODR寄存器,设置PE10引脚输出高电平 0x50006014[10] = 1
ldr r0,=0x50006014
ldr r1,[r0]
orr r1,r1,#(0x1 << 10)
str r1,[r0]
mov pc,lr
/************** LED1熄灭 **************/
ED1_OFF:
@1.通过GPIOE_ODR寄存器,设置PE10引脚输出低电平 0x50006014[10] = 0
ldr r0,=0x50006014
ldr r1,[r0]
bic r1,r1,#(0x1 << 10)
str r1,[r0]
mov pc,lr
/************** LED2点亮 **************/
ED2_ON:
@1.通过GPIOF_ODR寄存器,设置PE10引脚输出高电平 0x50007014[10] = 1
ldr r0,=0x50007014
ldr r1,[r0]
orr r1,r1,#(0x1 << 10)
str r1,[r0]
mov pc,lr
/************** LED2熄灭 **************/
ED2_OFF:
@1.通过GPIOF_ODR寄存器,设置PE10引脚输出低电平 0x50006014[10] = 0
ldr r0,=0x50007014
ldr r1,[r0]
bic r1,r1,#(0x1 << 10)
str r1,[r0]
mov pc,lr
/************** LED3点亮 **************/
ED3_ON:
@1.通过GPIOF_ODR寄存器,设置PE10引脚输出高电平 0x50006014[8] = 1
ldr r0,=0x50006014
ldr r1,[r0]
orr r1,r1,#(0x1 << 8)
str r1,[r0]
mov pc,lr
/************** LED3熄灭 **************/
ED3_OFF:
@1.通过GPIOE_ODR寄存器,设置PE10引脚输出低电平 0x50006014[8] = 0
ldr r0,=0x50006014
ldr r1,[r0]
bic r1,r1,#(0x1 << 8)
str r1,[r0]
mov pc,lr
大概1s的延时函数
elay_1s:
mov r3, #0x10000000
mm:
cmp r3, #0
subne r3, r3, #1
bne mm
mov pc, lr
end
2 C语言实现
led.h
#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;
#define GPIOE ((gpio_t*)0x50006000)
#define GPIOF ((gpio_t*)0x50007000)
#define RCC_MP_AHB4_ENSETR (*(volatile unsigned int*)0x50000A28)
//LED1 ===> PE10
//RCC/GPIO章节初始化
void led1_rcc_gpio_init();
//LED1点亮
void led1_on();
//LED1熄灭
void led1_off();
//LED1 ===> PE10
//LED2 ===> PF10
//RCC/GPIO章节初始化
void led2_rcc_gpio_init();
//LED2点亮
void led2_on();
//LED2熄灭
void led2_off();
//LED3 ===> PE8
//RCC/GPIO章节初始化
void led3_rcc_gpio_init();
//LED3点亮
void led3_on();
//LED3熄灭
void led3_off();
#endif
led.c
#include "led.h"
//LED1 ---> PE10
//RCC/GPIO章节初始化
void led1_rcc_gpio_init()
{
//0、使能GPIOE组控制器,通过RCC_MP_AHB4_ENSETR寄存器设置GPIOE组使能0x50000A28[4] = 1
RCC_MP_AHB4_ENSETR |= (0X1 << 4);
//1、通过GPIOE_MODER寄存器,设置PE10引脚为输出模式 0x50006000[21:20] = 01
GPIOE->MODER &= (~(0x1 << 21)); //21位清0
GPIOE->MODER |= (0x1 << 20); //20位置1
//2、通过GPIOE_OTYPER寄存器,设置PE10引脚为推挽输出类型 0x50006004[10] = 0
GPIOE->OTYPER &= (~(0x1 << 10));
//3、通过GPIOE_OSPEEDR寄存器,设置PE10引脚为低速输出 0x50006008[21:20] = 00
GPIOE->OSPEEDR &= (~(0x3 << 20));
//4、通过GPIOE_PUPDR寄存器,设置PE10引脚禁止上下拉电阻 0x5000600C[21:20] = 00
GPIOE->PUPDR &= (~(0x3 << 20));
}
//LED1点亮
void led1_on()
{
//通过GPIOE_ODR寄存器,设置PE10引脚输出高电平 0x50006014[10] = 1
GPIOE->ODR |= (0x1 << 10);
}
//LED1熄灭
void led1_off()
{
//通过GPIOE_ODR寄存器,设置PE10引脚输出低电平 0x50006014[10] = 0
GPIOE->ODR &= (~(0x1) << 10);
}
//LED2 ---> PF10
//RCC/GPIO章节初始化
void led2_rcc_gpio_init()
{
//0、使能GPIOE组控制器,通过RCC_MP_AHB4_ENSETR寄存器设置GPIOE组使能0x50000A28[5] = 1
RCC_MP_AHB4_ENSETR |= (0X1 << 5);
//1、通过GPIOF_MODER寄存器,设置PF10引脚为输出模式 0x50007000[21:20] = 01
GPIOF->MODER &= (~(0x1 << 21)); //21位清0
GPIOF->MODER |= (0x1 << 20); //20位置1
//2、通过GPIOE_OTYPER寄存器,设置PF10引脚为推挽输出类型 0x50007004[10] = 0
GPIOF->OTYPER &= (~(0x1 << 10));
//3、通过GPIOE_OSPEEDR寄存器,设置PF10引脚为低速输出 0x50007008[21:20] = 00
GPIOF->OSPEEDR &= (~(0x3 << 20));
//4、通过GPIOE_PUPDR寄存器,设置PF10引脚禁止上下拉电阻 0x5000700C[21:20] = 00
GPIOF->PUPDR &= (~(0x3 << 20));
}
//LED2点亮
void led2_on()
{
//通过GPIOF_ODR寄存器,设置PF10引脚输出高电平 0x50007014[10] = 1
GPIOF->ODR |= (0x1 << 10);
}
//LED2熄灭
void led2_off()
{
//通过GPIOF_ODR寄存器,设置PF10引脚输出低电平 0x50007014[10] = 0
GPIOF->ODR &= (~(0x1) << 10);
}
//LED3 ---> PE8
//RCC/GPIO章节初始化
void led3_rcc_gpio_init()
{
//0、使能GPIOE组控制器,通过RCC_MP_AHB4_ENSETR寄存器设置GPIOE组使能0x50000A28[4] = 1
RCC_MP_AHB4_ENSETR |= (0X1 << 4);
//1、通过GPIOE_MODER寄存器,设置PE10引脚为输出模式 0x50006000[17:16] = 01
GPIOE->MODER &= (~(0x1 << 17)); //17位清0
GPIOE->MODER |= (0x1 << 16); //16位置1
//2、通过GPIOE_OTYPER寄存器,设置PE10引脚为推挽输出类型 0x50006004[8] = 0
GPIOE->OTYPER &= (~(0x1 << 8));
//3、通过GPIOE_OSPEEDR寄存器,设置PE10引脚为低速输出 0x50006008[17:16] = 00
GPIOE->OSPEEDR &= (~(0x3 << 16));
//4、通过GPIOE_PUPDR寄存器,设置PE10引脚禁止上下拉电阻 0x5000600C[17:16] = 00
GPIOE->PUPDR &= (~(0x3 << 16));
}
//LED1点亮
void led3_on()
{
//通过GPIOE_ODR寄存器,设置PE10引脚输出高电平 0x50006014[8] = 1
GPIOE->ODR |= (0x1 << 8);
}
//LED1熄灭
void led3_off()
{
//通过GPIOE_ODR寄存器,设置PE10引脚输出低电平 0x50006014[8] = 0
GPIOE->ODR &= (~(0x1) << 8);
}
main.c
#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_rcc_gpio_init(); // LED灯初始化
led2_rcc_gpio_init(); // LED灯初始化
led3_rcc_gpio_init(); // LED灯初始化
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);
}
return 0;
}
3 循环实现
led.h
#ifndef __LED_H__
#define __LED_H__
#define RCC_MP_AHB4_ENSETR (*(volatile unsigned int*)0x50000A28)
//结构体封装
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;
typedef enum{
LED_OFF=0,
LED_ON,
}LED_C;
typedef enum{
LED1=0,
LED2,
LED3
}LED;
#define GPIOE ((gpio_t*)0x50006000)
#define GPIOF ((gpio_t*)0x50007000)
void led_init(LED l);
void led_con(LED l,LED_C c);
#endif
led.c
#include "led.h"
//初始化灯
void led_init(LED led)
{
switch(led)
{
case LED1:
RCC_MP_AHB4_ENSETR |= (0X1 << 4);
GPIOE->MODER &= (~(0x1 << 21)); //21位清0
GPIOE->MODER |= (0x1 << 20); //20位置1
GPIOE->OTYPER &= (~(0x1 << 10));
GPIOE->OSPEEDR &= (~(0x3 << 20));
GPIOE->PUPDR &= (~(0x3 << 20));
break;
case LED2:
RCC_MP_AHB4_ENSETR |= (0X1 << 5);
GPIOF->MODER &= (~(0x1 << 21)); //21位清0
GPIOF->MODER |= (0x1 << 20); //20位置1
GPIOF->OTYPER &= (~(0x1 << 10));
GPIOF->OSPEEDR &= (~(0x3 << 20));
GPIOF->PUPDR &= (~(0x3 << 20));
break;
case LED3:
RCC_MP_AHB4_ENSETR |= (0X1 << 4);
GPIOE->MODER &= (~(0x1 << 17)); //17位清0
GPIOE->MODER |= (0x1 << 16); //16位置1
GPIOE->OTYPER &= (~(0x1 << 8));
GPIOE->OSPEEDR &= (~(0x3 << 16));
GPIOE->PUPDR &= (~(0x3 << 16));
break;
}
}
//控制灯
void led_con(LED led,LED_C c)
{
switch(led)
{
case LED1:
switch(c)
{
case LED_OFF:
GPIOE->ODR &= (~(0X1 << 10));
break;
case LED_ON:
GPIOE->ODR |= (0X1 << 10);
break;
}
break;
case LED2:
switch(c)
{
case LED_OFF:
GPIOF->ODR &= (~(0X1 << 10));
break;
case LED_ON:
GPIOF->ODR |= (0X1 << 10);
break;
}
break;
case LED3:
switch(c)
{
case LED_OFF:
GPIOE->ODR &= (~(0X1 << 8));
break;
case LED_ON:
GPIOE->ODR |= (0X1 << 8);
break;
}
break;
}
}
main.c
#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()
{
led_init(LED1);
led_init(LED2);
led_init(LED3);
while(1)
{
led_con(LED1,LED_ON);
delay_ms(200);
led_con(LED1,LED_OFF);
delay_ms(200);
led_con(LED2,LED_ON);
delay_ms(200);
led_con(LED2,LED_OFF);
delay_ms(200);
led_con(LED3,LED_ON);
delay_ms(200);
led_con(LED3,LED_OFF);
delay_ms(200);
}
return 0;
}
4 封装函数实现【重要】【常用】
初始化函数函数===>函数参数、函数实现文章来源地址https://www.toymoban.com/news/detail-718035.html
led.h
#ifndef __LED_H__
#define __LED_H__
//LED1 ---> PE10
//LED2 ---> PF10
//LED3 ---> PE8
//结构体封装
typedef struct{
volatile unsigned int MODER; //0x00
volatile unsigned int OTYPER; //0x04
volatile unsigned int OSPEEDR; //0x08
volatile unsigned int PUPDR; //0x0c
volatile unsigned int IDR; //0x10
volatile unsigned int ODR; //0x14
}gpio_t;
//GPIOE组基地址 0x50006000
#define GPIOE ((gpio_t*)0x50006000)
//GPIOF组基地址 0x50007000
#define GPIOF ((gpio_t*)0x50007000)
#define RCC_MP_AHB4ENSETR (*(volatile unsigned int*)0x50000A28)
//引脚编号封装
#define GPIO_PIN_0 0
#define GPIO_PIN_1 1
#define GPIO_PIN_2 2
#define GPIO_PIN_3 3
#define GPIO_PIN_4 4
#define GPIO_PIN_5 5
#define GPIO_PIN_6 6
#define GPIO_PIN_7 7
#define GPIO_PIN_8 8
#define GPIO_PIN_9 9
#define GPIO_PIN_10 10
#define GPIO_PIN_11 11
#define GPIO_PIN_12 12
#define GPIO_PIN_13 13
#define GPIO_PIN_14 14
#define GPIO_PIN_15 15
//模式寄存器封装
typedef enum{
INPUT, //输入模式
OUTPUT, //输出模式
ALT, //复用功能模式
ANALOG, //模拟功能模式
}gpio_moder_t;
//输出类型寄存器封装
typedef enum{
PP, //推挽
OD, //开漏
}gpio_otyper_t;
//输出速率寄存器封装
typedef enum{
LOW, //低速
MED, //中速
HIGH, //高速
VERY_HIGH, //快速
}gpio_ospeedr_t;
//是否需要上下拉电阻进行封装
typedef enum{
NO_PU_PD, //禁止上下拉电阻
PU, //上拉
PD, //下拉
}gpio_pupdr_t;
//封装初始化结构体
typedef struct{
gpio_moder_t moder; //模式相关寄存器
gpio_otyper_t otyper; //输出类型寄存器
gpio_ospeedr_t ospeedr; //输出速率寄存器
gpio_pupdr_t pupdr; //是否需要上下拉电阻寄存器
}gpio_init_t;
//输出高低电平
typedef enum{
GPIO_RESET_T, //低电平
GPIO_SET_T, //高电平
}gpio_status_t;
//函数功能:gpio相关初始化工作
//参数1:GPIO组编号
//参数2:GPIO引脚编号
//参数3:初始化相关内容
//返回值:无
void hal_gpio_init(gpio_t * gpiox,unsigned int pin,gpio_init_t* init);
//函数功能:gpio写相关操作
//参数1:GPIO组编号
//参数2:GPIO引脚编号
//参数3:写值 写1高电平 写0低电平
//返回值:无
void hal_gpio_write(gpio_t* gpiox,unsigned int pin,gpio_status_t status);
#endif
led.c
#include "led.h"
void hal_gpio_init(gpio_t * gpiox,unsigned int pin,gpio_init_t* init)
{
//设置模式
gpiox->MODER &= (~(0x3 << (pin * 2)));
gpiox->MODER |= (init->moder << (pin * 2));
//设置输出类型
gpiox->OTYPER &= (~(0x1 << pin));
gpiox->OTYPER |= (init->otyper << pin);
//设置输出速率
gpiox->OSPEEDR &= (~(0x3 << pin));
gpiox->OSPEEDR |= (init->ospeedr << pin);
//设置是否需要上下拉电阻
gpiox->PUPDR &= (~(0x3 << pin));
gpiox->PUPDR |= (init->pupdr << pin);
}
void hal_gpio_write(gpio_t* gpiox,unsigned int pin,gpio_status_t status)
{
if(status == GPIO_RESET_T)
{
//熄灭
gpiox->ODR &= (~(0X1 << pin));
}
else
{
//点亮
gpiox->ODR |= (0x1 << pin);
}
}
main.c
#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++);
}
void led_init()
{
//GPIOE组 / GPIOF组使能
RCC_MP_AHB4ENSETR |= (0x3 << 4);
//结构体初始化
gpio_init_t init = {OUTPUT,PP,LOW,NO_PU_PD};
hal_gpio_init(GPIOE,GPIO_PIN_10,&init);
hal_gpio_init(GPIOF,GPIO_PIN_10,&init);
hal_gpio_init(GPIOE,GPIO_PIN_8,&init);
}
int main()
{
led_init(); //LED灯初始化
while(1)
{
hal_gpio_write(GPIOE,GPIO_PIN_10,GPIO_SET_T);
delay_ms(200);
hal_gpio_write(GPIOE,GPIO_PIN_10,GPIO_RESET_T);
delay_ms(200);
hal_gpio_write(GPIOF,GPIO_PIN_10,GPIO_SET_T);
delay_ms(200);
hal_gpio_write(GPIOF,GPIO_PIN_10,GPIO_RESET_T);
delay_ms(200);
hal_gpio_write(GPIOE,GPIO_PIN_8,GPIO_SET_T);
delay_ms(200);
hal_gpio_write(GPIOE,GPIO_PIN_8,GPIO_RESET_T);
delay_ms(200);
}
return 0;
}
文章来源:https://www.toymoban.com/news/detail-718035.html
到了这里,关于C语言 cortex-A7核 点LED灯 (附 汇编实现、使用C语言 循环实现、使用C语言 封装函数实现【重要、常用】)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!