步骤
- 编写LED驱动程序
- 编写头文件:宏定义连接LED的端口、端口引脚、端口时钟,函数声明
- 编写驱动文件:
- LED初始化函数:开启端口时钟,定义GPIO_InitTypeDef结构体变量并初始化,拉高引脚电平
- 主函数点亮LED
代码
//led.h
#ifndef _led_H
#define _led_H
#include "stm32f10x.h" //定义了各种地址
#define LED1_PORT GPIOB
#define LED1_PIN GPIO_Pin_5
#define LED1_PORT_RCC RCC_APB2Periph_GPIOB
#define LED2_PORT GPIOE
#define LED2_PIN GPIO_Pin_5
#define LED2_PORT_RCC RCC_APB2Periph_GPIOE
void LED_Init(void);
#endif
//led.c
#include "led.h"
void LED_Init(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(LED1_PORT_RCC|LED2_PORT_RCC, ENABLE);
GPIO_InitStructure.GPIO_Pin = LED1_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED1_PORT, &am