STM32 使用库函数(按键控制led不同的闪烁效果)
下面是main.c
1#include "stm32f10x.h" 2#include "led.h"` 3#include "key.h" 4int main(void) 5 6{ 7 8 9while(1) 10 { 11 12 13 if(!GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)) 14 { 15 16 17 GPIO_WriteBit(GPIOE,GPIO_Pin_5,(BitAction)(1)); 18 } 19 else 20 { 21 22 23 GPIO_WriteBit(GPIOE,GPIO_Pin_5,(BitAction)(0)); 24 } 25 } 26 27}
led.c
1#include "led.h" 2 3 4void LED_Init(void) 5{ 6 7 8GPIO_InitTypeDef GPIO_InitStructure;//构造结构体 9RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); 10RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);//使能其所在时钟 11GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;//定义IO端口 12GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//定义IO端口输出模式 13GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//定义IO端口输出速度 14GPIO_Init(GPIOE, &GPIO_InitStructure);//写入其中 15}
led.h
1#ifndef _led_H 2 3#include "stm32f10x.h" 4#include "stm32f10x_gpio.h" 5 6 7 8 9 10 11void LED_Init(void); 12 13#endif
key.c
1#include "key.h" 2 3void KEY_Init(void) 4 5{ 6 7 8 GPIO_InitTypeDef GPIO_InitStructure; //构造结构体 9 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);//使能其所在时钟 10 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//定义IO端口 11 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入 12 13 GPIO_Init(GPIOE,&GPIO_InitStructure); 14 15}
key.h
1#ifndef _led_H 2#define _led_H 3 4#include "stm32f10x.h" 5#include "stm32f10x_gpio.h" 6void KEY_Init(void); 7#define KEYPORT GPIOE 8#define KEY1 GPIO_Pin_3 9 10 11 12#endif