C语言实现LED1闪烁
led.h
#ifndef __LED_H__
#define __LED_H__
//RCC寄存器封装
#define RCC_MP_AHB4_ENSETR (*(volatile unsigned int*)0x50000A28) //寄存器封装
//GPIO寄存器封装
typedef struct{
volatile unsigned int MODER; //00
volatile unsigned int OTYPER; //04
volatile unsigned int OSPEEDR; //08
volatile unsigned int PUPDR; //0C
volatile unsigned int IDR; //10
volatile unsigned int ODR; //14
}gpio_t;
#define GPIOE (*(gpio_t*)0x50006000) //GPIOE基地址 0x50006000
#define GPIOF (*(gpio_t*)0x50007000) //GPIOF基地址 0x50007000
//LED1灯初始化
void led1_rcc_init();
//LED1灯点亮
void led1_on();
//LED1灯熄灭
void led1_off();
#endif
led.c
#include "led.h"
//LED1灯初始化
void led1_rcc_init()
{
//设置GPIOE组时钟使能 通过RCC_MP_AHB4ENSETR寄存器设置 0x50000A28[4] = 1
RCC_MP_AHB4_ENSETR |=(0x1<<4);
//1.设置PE10引脚为输出模式 通过GPIOE_MODER寄存器设置 0x50006000[21:20] = 01
GPIOE.MODER&=~(0x3<<20);
GPIOE.MODER|=(0x1<<20);
//2.设置PE10引脚为推挽输出类型 通过GPIOE_OTYPER寄存器设置 0x50006004[10] = 0
GPIOE.OTYPER&=~(0x1<<10);
//3.设置PE10引脚为低速输出 通过GPIOE_OSPEEDR寄存器设置 0x50006008[21:20] =00
GPIOE.OSPEEDR&=~(0x3<<20);
//4.设置PE10引脚为禁止上下拉电阻 通过GPIOE_PUPDR寄存器设置 0x5000600C[21:20]= 00
GPIOE.PUPDR&=~(0x3<<20);
}
//LED1灯点亮
void led1_on()
{
//1.设置PE10引脚为输出高电平 通过GPIOE_ODR寄存器设置 0x50006014[10] = 1
GPIOE.ODR|=(0x1<<10);
}
//LED1灯熄灭
void led1_off()
{
//1.设置PE10引脚为输出低电平 通过GPIOE_ODR寄存器设置 0x50006014[10] = 0
GPIOE.ODR&=~(0x1<<10);
}
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灯初始化
led1_rcc_init();
while(1)
{
//LED1灯点亮
led1_on();
delay_ms(500);
//LED1灯熄灭
led1_off();
delay_ms(500);
}
return 0;
}