2023/4/20 点灯
main.c
#include "head.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()
{
gpio_init_t led1 = {OUTPUT,PUSH_PULL,LOW_SPEED,NO_PULL};
gpio_init_t led2 = {OUTPUT,PUSH_PULL,LOW_SPEED,NO_PULL};
gpio_init_t led3 = {OUTPUT,PUSH_PULL,LOW_SPEED,NO_PULL};
hal_gpio_init(GPIOE,&led1,GPIO_PIN_10);
hal_gpio_init(GPIOF,&led2,GPIO_PIN_10);
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_RESET_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_RESET_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_RESET_T,GPIO_PIN_8);
delay_ms(500);
}
return 0;
}
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define RCC_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;
#define GPIOE ((gpio_t*)0x50006000)
#define GPIOF ((gpio_t*)0x50007000)
typedef enum
{
INPUT,
OUTPUT,
ALT,
ANALOG
}gpio_moder_t;
typedef enum
{
PUSH_PULL,
OPEN_DRAIN
}gpio_otyper_t;
typedef enum
{
LOW_SPEED,
MEDIUM_SPEED,
HIGH_SPEED,
VERY_HIGH_SPEED
}gpio_ospeedr_t;
typedef enum
{
NO_PULL,
PULL_UP,
PULL_DOWN,
RESERVED
}gpio_pupdr_t;
typedef enum
{
GPIO_RESET_T,
GPIO_SET_T
}gpio_status_t;
typedef struct
{
gpio_moder_t moder;
gpio_otyper_t otyper;
gpio_ospeedr_t ospeedr;
gpio_pupdr_t pupdr;
}gpio_init_t;
#define GPIO_PIN_8 8
#define GPIO_PIN_10 10
void hal_gpio_init(gpio_t* gpiox,gpio_init_t* init,unsigned int pin);
void hal_gpio_write(gpio_t* gpiox,gpio_status_t status,unsigned int pin);
#endif
led_do.c
#include "head.h"
void hal_gpio_init(gpio_t* gpiox,gpio_init_t* init,unsigned int pin)
{
if(gpiox == GPIOE)
{
RCC_AHB4_ENSETR |= (0x1 << 4);
}else if(gpiox == GPIOF)
{
RCC_AHB4_ENSETR |= (0x1 << 5);
}
switch(init->moder)
{
case INPUT:
gpiox->MODER &= (~(0x3 << pin*2));
break;
case OUTPUT:
gpiox->MODER &= (~(0x3 << pin*2));
gpiox->MODER |= (0x1 << pin*2);
break;
case ALT:
gpiox->MODER &= (~(0x3 << pin*2));
gpiox->MODER |= (0x1 << (pin*2+1));
break;
case ANALOG:
gpiox->MODER |= (~(0x3 << pin*2));
break;
}
switch(init->otyper)
{
case PUSH_PULL:
gpiox->OTYPER &= (~(0x1 << pin));
break;
case OPEN_DRAIN:
gpiox->OTYPER |= (~(0x1 << pin));
break;
}
switch(init->ospeedr)
{
case LOW_SPEED:
gpiox->OSPEEDR &= (~(0x3 << pin*2));
break;
case MEDIUM_SPEED:
gpiox->OSPEEDR &= (~(0x3 << pin*2));
gpiox->OSPEEDR |= (0x1 << pin*2);
break;
case HIGH_SPEED:
gpiox->OSPEEDR &= (~(0x3 << pin*2));
gpiox->OSPEEDR |= (0x1 << (pin*2+1));
break;
case VERY_HIGH_SPEED:
gpiox->OSPEEDR |= (~(0x3 << pin*2));
break;
}
switch(init->pupdr)
{
case NO_PULL:
gpiox->PUPDR &= (~(0x3 << pin*2));
break;
case PULL_UP:
gpiox->PUPDR &= (~(0x3 << pin*2));
gpiox->PUPDR |= (0x1 << pin*2);
break;
case PULL_DOWN:
gpiox->PUPDR &= (~(0x3 << pin*2));
gpiox->PUPDR |= (0x1 << (pin*2+1));
break;
case RESERVED:
gpiox->PUPDR |= (~(0x3 << pin*2));
break;
}
}
void hal_gpio_write(gpio_t* gpiox,gpio_status_t status,unsigned int pin)
{
if(status == 1)
{
gpiox->ODR |= (0x1 << pin);
}else if(status == 0)
{
gpiox->ODR &= (~(0x1 << pin));
}
}