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_AHB4_ENSETR (*(volatile unsigned int*)0x50000A28)
void LED1_open();
void LED1_close();
void LED2_open();
void LED2_close();
void LED3_open();
void LED3_close();
//led灯初始化
void gpio_e();
#endif
led.c
#include "led.h"
void gpio_e()
{
//RCC使能
RCC_AHB4_ENSETR = RCC_AHB4_ENSETR |(0x1<<4);
//设置E组IO引脚模式为输出模式
GPIOE->MODER = GPIOE->MODER &(~(0x3<<20));
GPIOE->MODER = GPIOE->MODER |(0x1<<20);
GPIOE->MODER = GPIOE->MODER &(~(0x3<<16));
GPIOE->MODER = GPIOE->MODER |(0x1<<16);
//设置F组IO引脚模式为输出模式
GPIOF->MODER = GPIOF->MODER &(~(0x3<<20));
GPIOF->MODER = GPIOF->MODER |(0x1<<20);
//设置E组IO引脚输出类型为推挽输出
GPIOE->OTYPER = GPIOE->OTYPER &(~(0x1<<10));
//设置E组IO引脚输出类型为推挽输出
GPIOE->OTYPER = GPIOE->OTYPER &(~(0x1<<8));
//设置F组IO引脚输出类型为推挽输出
GPIOF->OTYPER = GPIOF->OTYPER &(~(0x1<<10));
//设置E组IO引脚输出速率为低速
GPIOE->OSPEEDR = GPIOE->OSPEEDR &(~(0x3<<20));
GPIOE->OSPEEDR = GPIOE->OSPEEDR &(~(0x3<<16));
//设置F组IO引脚输出速率为低速
GPIOF->OSPEEDR = GPIOF->OSPEEDR &(~(0x3<<20));
//设置E组IO引脚禁止上下拉
GPIOE->PUPDR = GPIOE->PUPDR &(~(0x3<<20));
GPIOE->PUPDR = GPIOE->PUPDR &(~(0x1<<16));
GPIOF->PUPDR = GPIOF->PUPDR &(~(0x3<<20));
}
//亮灯
void LED1_open()
{
GPIOE->ODR = GPIOE->ODR |(0x1<<10);
}
//灭灯
void LED1_close()
{
GPIOE->ODR = GPIOE->ODR &(~(0x1<<10));
}
void LED2_open()
{
GPIOF->ODR = GPIOF->ODR |(0x1<<10);
}
void LED2_close()
{
GPIOF->ODR = GPIOF->ODR &(~(0x1<<10));
}
void LED3_open()
{
GPIOE->ODR = GPIOE->ODR |(0x1<<8);
}
void LED3_close()
{
GPIOE->ODR = 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()
{
gpio_e();
while(1)
{
LED1_open();
delay_ms(500);
LED1_close();
delay_ms(500);
LED2_open();
delay_ms(500);
LED2_close();
delay_ms(500);
LED3_open();
delay_ms(500);
LED3_close();
delay_ms(500);
}
return 0;
}
该代码实现了一个STM32的LED控制函数库,包括LED初始化、开启和关闭。通过配置GPIO寄存器设置E和F组的IO口为推挽输出,低速模式,并禁用上下拉电阻。在主函数中,LED1、LED2和LED3交替点亮和熄灭,间隔500毫秒。
2892

被折叠的 条评论
为什么被折叠?



