您现在的位置是:首页 >技术教程 >DAY 4 ARM网站首页技术教程
DAY 4 ARM
头文件
#ifndef LED_H
#define LED_H
//宏定义一个使能端
#define RCC_AHB4_ENSET (*(volatile unsigned int *)0x50000A28)
//结构体封装
typedef struct{
volatile unsigned int MODER;
volatile unsigned int OTYPER;
volatile unsigned int OSPEED;
volatile unsigned int PUPDR;
volatile unsigned int IDR;
volatile unsigned int ODR;
}gpio_t;
#define GPIOE ((gpio_t *)0x50006000)
#define GPIOF ((gpio_t *)0x50007000)
//封装模式寄存器
typedef enum{
INPUT,
OUTPUT,
ALT,
ANOLOG
}gpio_mode_t;
//封装输出类型寄存器
typedef enum{
push_pull, //推挽模式
open_drain //开路模式
}gpio_otyper_t;
//封装输出速率寄存器
typedef enum{
Low_speed,
Middle_speed,
High_speed,
VeryHigh_speed
}gpio_ospeed_t;
//封装是否使用上下拉电阻
typedef enum{
No_pull,
pullup,
pulldown,
reserved
}gpio_pupdr_t;
//封装高低电平寄存器
typedef enum{
GPIO_RESEET_T,
GPIO_SET_T
}gpio_status_t;
//结构体初始化寄存器
typedef struct{
gpio_mode_t moder; //封装模式寄存器
gpio_otyper_t otyper; //封装输出类型寄存器
gpio_ospeed_t ospeed; //封装输出速率寄存器
gpio_pupdr_t pupdr; //封装上下拉电阻寄存器
}gpio_init_t;
//引脚封装
#define GPIO_PIN_0 0
#define GPIO_PIN_8 8
#define GPIO_PIN_10 10
//初始化函数
void hal_gpio_init(gpio_t gpiox, gpio_init_t init, unsigned int pin);
//操作GPIO借口写函数
void hal_gpio_write(gpio_t* gpiox, gpio_status_t status, unsigned int pin);
#endif
主函数
#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
gpio_init_t Led1;
Led1.moder = OUTPUT;
Led1.otyper = push_pull;
Led1.ospeed = Low_speed;
Led1.pupdr = No_pull;
hal_gpio_init(GPIOE, &Led1, GPIO_PIN_10);
//定义结构体 LED2
gpio_init_t Led2;
Led2.moder = OUTPUT;
Led2.otyper = push_pull;
Led2.ospeed = Low_speed;
Led2.pupdr = No_pull;
hal_gpio_init(GPIOF, &Led2, GPIO_PIN_10);
//定义结构体 LED3
gpio_init_t Led3;
Led2.moder = OUTPUT;
Led2.otyper = push_pull;
Led2.ospeed = Low_speed;
Led2.pupdr = No_pull;
hal_gpio_init(GPIOE, &Led3, GPIO_PIN_8);
while(1)
{
hal_gpio_write(GPIOE, GPIO_SET_T, GPIO_PIN_10);
delay_ms(500);
hal_gpio_write(GPIOE, GPIO_RESEET_T, GPIO_PIN_10);
delay_ms(500);
hal_gpio_write(GPIOF, GPIO_SET_T, GPIO_PIN_10);
delay_ms(500);
hal_gpio_write(GPIOF, GPIO_RESEET_T, GPIO_PIN_10);
delay_ms(500);
hal_gpio_write(GPIOE, GPIO_SET_T, GPIO_PIN_8);
delay_ms(500);
hal_gpio_write(GPIOE, GPIO_RESEET_T, GPIO_PIN_8);
delay_ms(500);
}
return 0;
}
功能函数
#include “led.h”
//初始化 (gpio组号, 初始化值, 引脚编号)
void hal_gpio_init(gpio_t* gpiox, gpio_init_t* init, unsigned int pin)
{
//使能端第4位赋1
if(gpiox == GPIOE)
{
RCC_AHB4_ENSET |= (0x1 << 4);
}
else
{
RCC_AHB4_ENSET |= (0x1 << 5);
}
//设置输出模式
gpiox->MODER &= ~(0x3 << 2*pin);
gpiox->MODER |= (init->moder << 2*pin);
//设置为推挽模式
gpiox->OTYPER &= (~(0x1 << pin));
gpiox->OTYPER |= (init->otyper << pin);
//设置为低俗模式
gpiox->OSPEED &= (~(0x3 << 2*pin));
gpiox->OSPEED |= (init->ospeed << 2*pin);
//禁止上下拉电阻
gpiox->PUPDR &= (~(0x3 << 2*pin));
gpiox->PUPDR |= (init->pupdr << 2*pin);
}
//写入函数(gpio组号, 状态值, 引脚编号)
void hal_gpio_write(gpio_t* gpiox, gpio_status_t status, unsigned int pin)
{
if(status == 0)
{
gpiox->ODR |= (0x1 << pin);
}
else
{
gpiox->ODR &= (~(0x1 << pin));
}
}