按键点亮led灯

原理图:


K0这个按键按下时,开发板D1这个灯亮,松开,灯灭


代码如下:

复制代码
#include "stm32f4xx.h"  


void LED_Init(void)
{
	//1.定义一个GPIO外设的结构体变量  
	GPIO_InitTypeDef GPIO_InitStructure;
	
	//RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOF, ENABLE);
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
	//3.对结构体变量的成员进行赋值
	GPIO_InitStructure.GPIO_Mode 	= GPIO_Mode_OUT;				//输出模式
	GPIO_InitStructure.GPIO_OType 	= GPIO_OType_PP;				//推挽输出
	GPIO_InitStructure.GPIO_Speed 	= GPIO_Speed_100MHz;			//输出速率100MHZ
	GPIO_InitStructure.GPIO_PuPd 	= GPIO_PuPd_UP;					//上拉输出
	
	//4.初始化GPIO
	//GPIO_InitStructure.GPIO_Pin 	= GPIO_Pin_9|GPIO_Pin_10;		//引脚
	GPIO_InitStructure.GPIO_Pin 	= GPIO_Pin_9;		//引脚
	GPIO_Init(GPIOF, &GPIO_InitStructure);
		
	//GPIO_InitStructure.GPIO_Pin 	= GPIO_Pin_13|GPIO_Pin_14;		//引脚
	//GPIO_Init(GPIOE, &GPIO_InitStructure);

	GPIO_SetBits(GPIOF,GPIO_Pin_9);		//设置高电平  LED灭
	//	GPIO_SetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10);		//设置高电平  LED灭
	//GPIO_SetBits(GPIOE,GPIO_Pin_13|GPIO_Pin_14);	//设置高电平  LED灭	
}


void KEY_Init(void)
{
	//1.定义一个GPIO外设的结构体变量  
	GPIO_InitTypeDef GPIO_InitStructure;
	
	//2.打开外设时钟  GPIOA  PA0
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

	//3.对结构体变量的成员进行赋值
	GPIO_InitStructure.GPIO_Mode 	= GPIO_Mode_IN;					//输入模式
	GPIO_InitStructure.GPIO_PuPd 	= GPIO_PuPd_UP;					//上拉输出
	
	//4.初始化GPIO
	GPIO_InitStructure.GPIO_Pin 	= GPIO_Pin_9;					//引脚
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
}


int main()
{
	KEY_Init();  //按键的初始化
	LED_Init();	 //LED的初始化
	
	while(1)
	{
		if( GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9) == RESET )	//说明被按下
		{
			GPIO_ResetBits(GPIOF,GPIO_Pin_9);	//设置低电平  LED亮
		}
		else
		{
			GPIO_SetBits(GPIOF,GPIO_Pin_9);		//设置高电平  LED灭	
		}
	}
}
相关推荐
风逸尘_lz4 分钟前
STM32标准工程目录文件解释
stm32·单片机·嵌入式硬件
小白学电子_5 分钟前
51 单片机 拖尾流水灯
单片机·嵌入式硬件
酸奶乳酪2 小时前
IIC学习笔记
笔记·单片机·学习
进击的横打2 小时前
【车载开发系列】系统时钟与定时器
stm32·单片机·fpga开发
LCG元3 小时前
STM32实战案例:基于STM32F103的智能插座(电量计量+远程控制)
stm32·单片机·嵌入式硬件
至为芯3 小时前
PY32F005至为芯支持32位ARM内核的高主频MCU微控制器
单片机·集成电路·芯片
somi73 小时前
ARM-06-时钟系统配置
arm开发·单片机·嵌入式硬件·时钟配置
爱喝纯牛奶的柠檬3 小时前
基于STM32和HAL库的大夏龙雀BT311-10C02S蓝牙模块驱动
stm32·单片机·嵌入式硬件
小谦32513 小时前
NTC热敏电阻分压测量电路的数学特性与应用选择研究
stm32·嵌入式硬件
Xueqian E4 小时前
驱动策略和效率的整理
stm32·单片机·嵌入式硬件