2.1按键控制LED
接线图:
LED低电平点亮
主函数:
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"
uint16_t KeyNum=0;
int main()
{
LED_Init();
Key_Init();
while(1)
{
KeyNum=Key_GetNum();
if(KeyNum==1)
{ LED_Turn(0);}
if(KeyNum==2)
{ LED_Turn(1);}
}
}
Key文件:
#include "stm32f10x.h" // Device header
#include "Delay.h"
/*
* @brief 按键初始化
* @param 无
* @retval 无
*/
void Key_Init()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); //开启时钟
GPIO_InitTypeDef GPIO_InitKey;
GPIO_InitKey.GPIO_Mode=GPIO_Mode_IPU; //上拉输入
GPIO_InitKey.GPIO_Pin=GPIO_Pin_1 | GPIO_Pin_11; //仅开启了1、11口
GPIO_InitKey.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitKey);
}
/*
* @brief 读取按键是否按下
* @param 无
* @retval 按下按键号
*/
uint8_t Key_GetNum()
{
uint8_t KeyNum=0;
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0)
{
Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0);
Delay_ms(10);
KeyNum=1;
}
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0)
{
Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0);
Delay_ms(10);
KeyNum=2;
}
return KeyNum;
}
#ifndef __KEY_H__
#define __KEY_H__
void Key_Init();
uint8_t Key_GetNum();
#endif
备注
- ReadInputDataBit:读取输入模式下IO 口电平状态值
- Delay+while+Delay软件消抖
- 按键的B口为上拉输入,按键按下时输入电平为0
LED文件:
#include "stm32f10x.h" // Device header
/*
* @brief LED初始化
* @param 无
* @retval 无
*/
void LED_Init()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitLED;
GPIO_InitLED.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitLED.GPIO_Pin=GPIO_Pin_0 | GPIO_Pin_1; //仅开启了1、2口
GPIO_InitLED.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitLED);
GPIO_SetBits(GPIOA,GPIO_Pin_All);
}
/*
* @brief 单独设置一个LED亮灭
* @param x:A口号 ;y=1亮 y=0灭
* @retval 无
*/
void LED_Set(unsigned char x,unsigned char y)
{
if(y==1){ GPIO_ResetBits(GPIOA,GPIO_Pin_0<<x); }
else { GPIO_SetBits(GPIOA,GPIO_Pin_0<<x); }
}
/*
* @brief LED翻转
* @param x:A口号
* @retval 无
*/
void LED_Turn(unsigned char x)
{
if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_0<<x)==0) //若当前开
{ GPIO_SetBits(GPIOA,GPIO_Pin_0<<x);}
else
{ GPIO_ResetBits(GPIOA,GPIO_Pin_0<<x);}
}
#ifndef __LED_H__
#define __LED_H__
void LED_Init();
void LED_Set(unsigned char x,unsigned char y);
void LED_Turn(unsigned char x);
#endif
备注
- ReadOutputDataBit:读取输出模式下IO 口电平状态值
- GPIO_SetBits 拉高引脚输出电平
GPIO_ResetBits 拉低引脚输出电平 - GPIO_Pin_0左移x位减少后续移植代码工作量
2.2光敏控制蜂鸣器
接线图
光敏模块:光强高于阈值输出1
主函数:
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"
#include "Buzzer.h"
#include "LightSenser.h"
uint16_t KeyNum=0;
int main()
{
Buzzer_Init();
LightSenser_Init();
while(1)
{
if(LightSenser_Get()==1) Buzzer_Set(1); //亮:不响
else Buzzer_Set(0);
}
}
LightSenser文件:
#include "stm32f10x.h" // Device header
/*
* @brief 光敏模块初始化
* @param 无
* @retval 无
*/
void LightSenser_Init()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIO_InitLightSenser;
GPIO_InitLightSenser.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitLightSenser.GPIO_Pin=GPIO_Pin_9; //仅开启了12口
GPIO_InitLightSenser.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitLightSenser);
GPIO_SetBits(GPIOB,GPIO_Pin_9);
}
/*
* @brief 读取光敏模块输出值
* @param 无
* @retval 光敏模块输出值,1:光强大于阈值,0:低于阈值
*/
uint16_t LightSenser_Get()
{
return GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9);
}
Buzzer文件:
#include "stm32f10x.h" // Device header
/*
* @brief 蜂鸣器初始化
* @param 无
* @retval 无
*/
void Buzzer_Init()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIO_InitBuzzer;
GPIO_InitBuzzer.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitBuzzer.GPIO_Pin=GPIO_Pin_12; //仅开启了12口
GPIO_InitBuzzer.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitBuzzer);
GPIO_SetBits(GPIOB,GPIO_Pin_12); //低电平触发
}
/*
* @brief 设置蜂鸣器
* @param y=0熄 y=1响
* @retval 无
*/
void Buzzer_Set(unsigned char y)
{
if(y==1){ GPIO_ResetBits(GPIOB,GPIO_Pin_12); }
else { GPIO_SetBits(GPIOB,GPIO_Pin_12); }
}
/*
* @brief 蜂鸣器翻转
* @param 无
* @retval 无
*/
void Buzzer_Turn()
{
if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_12)==0) //若当前开
{ GPIO_SetBits(GPIOB,GPIO_Pin_12);}
else
{ GPIO_ResetBits(GPIOB,GPIO_Pin_12);}
}