STM32单片机按键点亮LED(无定时器)

线上接线图,用的是面包板和最小系统板:

这次的实验是用左边的按钮控制左边LED的亮灭,右边的按钮控制右边LED的亮灭,可以看到LED负极接到的是单片机的引脚,因此在初始化引脚时要选择低电平有驱动能力的模式,我们选择推挽输出模式。再看按钮,如图我们希望按钮按下时是低电平,松开时是高电平,那我们在初始化他们引脚的时候就要给他们配置成上拉输入模式,这样在按钮松开时默认为高电平,按下时接地,变成低电平。

LED初始化:

 

#include "stm32f10x.h"                  // Device header
void LED_Init()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_InitTypeDef GPIO_LED_InitStructure;
	GPIO_LED_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_LED_InitStructure.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_1;
	GPIO_LED_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_LED_InitStructure);
}
void LED1_Turn()
{
	if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_2)==0)
		GPIO_SetBits(GPIOA,GPIO_Pin_2);
	else
		GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}
void LED2_Turn()
{
	if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_1)==0)
		GPIO_SetBits(GPIOA,GPIO_Pin_1);
	else
		GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}

按钮初始化:

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
void Key_Init()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	GPIO_InitTypeDef GPIO_Key_InitStructure;
	GPIO_Key_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_Key_InitStructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_11;
	GPIO_Key_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_Key_InitStructure);
}
uint8_t Get_Key1Num()
{
	uint8_t KeyNum=0;
	if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0)
	{
		Delay_ms(20);
		while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0);
		Delay_ms(20);
		KeyNum=1;
	}
	return KeyNum;
}
uint8_t Get_Key2Num()
{
	uint8_t KeyNum=0;
	if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0)
	{
		Delay_ms(20);
		while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0);
		Delay_ms(20);
		KeyNum=1;
	}
	return KeyNum;
}

按钮初始化时按下按钮会有抖动,我们需要Delay()函数消抖,这个函数直接拿就行:

#include "stm32f10x.h"

/**
  * @brief  微秒级延时
  * @param  xus 延时时长,范围:0~233015
  * @retval 无
  */
void Delay_us(uint32_t xus)
{
	SysTick->LOAD = 72 * xus;				//设置定时器重装值
	SysTick->VAL = 0x00;					//清空当前计数值
	SysTick->CTRL = 0x00000005;				//设置时钟源为HCLK,启动定时器
	while(!(SysTick->CTRL & 0x00010000));	//等待计数到0
	SysTick->CTRL = 0x00000004;				//关闭定时器
}

/**
  * @brief  毫秒级延时
  * @param  xms 延时时长,范围:0~4294967295
  * @retval 无
  */
void Delay_ms(uint32_t xms)
{
	while(xms--)
	{
		Delay_us(1000);
	}
}
 
/**
  * @brief  秒级延时
  * @param  xs 延时时长,范围:0~4294967295
  * @retval 无
  */
void Delay_s(uint32_t xs)
{
	while(xs--)
	{
		Delay_ms(1000);
	}
} 

都初始化完毕就剩下主函数部分了主函数有个while()循环,我们可以在循环前将按钮和LED初始化,在循环函数内不断返回Get_Key1Num()和Get_Key2Num()的返回值,分别用两个变量接受,用不同的接受到的值控制LED亮灭。

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.H"
#include "Key.h"
uint8_t Key1,Key2;
int main()
{
	Key_Init();
	LED_Init();
	while(1)
	{
		Key1=Get_Key1Num();
		Key2=Get_Key2Num();
		if(Key1==1)
		{
			LED1_Turn();
		}
		if(Key2==1)
		{
			LED2_Turn();
		}
	}
}

记得再对以上的.c文件创建对应的头文件哟!

那么以上就是按键点亮LED了,以上内容经供参考,如果有更好的想法也可以去试试。

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值