STM32_GPIO输入
STM32_GPIO输入

1、按键控制LED灯
main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"
uint8_t KeyNum; //定义用于接收按键键码的变量
int main()
{
// 模块初始化
LED_Init();
Key_Init();
while(1)
{
KeyNum = Key_GetNum();
if(KeyNum == 1) // 按键1 按下
{
LED1_Turn();
}
if(KeyNum == 2) // 按键2 按下
{
LED2_Turn();
}
}
}
LED.c
#include "stm32f10x.h" // Device header
void LED_Init(void)
{
// 1、开启GPIOC时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
// 2、GPIO初始化
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
// 3、设置默认为高电平,防止LED上电点亮
GPIO_SetBits(GPIOC, GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
}
void LED1_ON(void)
{
// 点亮
GPIO_ResetBits(GPIOC,GPIO_Pin_13);
}
void LED1_OFF(void)
{
// 熄灭
GPIO_SetBits(GPIOC,GPIO_Pin_13);
}
void LED1_Turn(void)
{
//获取输出寄存器的状态,如果当前引脚输出低电平
if(GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_13) == 0)
{
// 执行为高电平,LED熄灭
GPIO_SetBits(GPIOC,GPIO_Pin_13);
}
else
{
// 执行为低电平,LED点亮
GPIO_ResetBits(GPIOC,GPIO_Pin_13);
}
}
void LED2_ON(void)
{
// 点亮
GPIO_ResetBits(GPIOC,GPIO_Pin_14);
}
void LED2_OFF(void)
{
// 熄灭
GPIO_SetBits(GPIOC,GPIO_Pin_14);
}
void LED2_Turn(void)
{
//获取输出寄存器的状态,如果当前引脚输出低电平
if(GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_14) == 0)
{
// 执行为高电平,LED熄灭
GPIO_SetBits(GPIOC,GPIO_Pin_14);
}
else
{
// 执行为低电平,LED点亮
GPIO_ResetBits(GPIOC,GPIO_Pin_14);
}
}
void LED3_ON(void)
{
// 点亮
GPIO_ResetBits(GPIOC,GPIO_Pin_15);
}
void LED3_OFF(void)
{
// 熄灭
GPIO_SetBits(GPIOC,GPIO_Pin_15);
}
void LED3_Turn(void)
{
//获取输出寄存器的状态,如果当前引脚输出低电平
if(GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_15) == 0)
{
// 执行为高电平,LED熄灭
GPIO_SetBits(GPIOC,GPIO_Pin_15);
}
else
{
// 执行为低电平,LED点亮
GPIO_ResetBits(GPIOC,GPIO_Pin_15);
}
}
LED.h
#ifndef __LED_H
#define __LED_H
void LED_Init(void);
void LED1_ON(void);
void LED1_OFF(void);
void LED1_Turn(void);
void LED2_ON(void);
void LED2_OFF(void);
void LED2_Turn(void);
void LED3_ON(void);
void LED3_OFF(void);
void LED3_Turn(void);
#endif
Key.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
void Key_Init(void)
{
// 1、开启GPIOB时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// 2、GPIO初始化
GPIO_InitTypeDef GPIO_InitStructue;
GPIO_InitStructue.GPIO_Mode = GPIO_Mode_IPD; // 下拉模式
GPIO_InitStructue.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructue.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructue);
}
uint8_t Key_GetNum(void)
{
uint8_t KeyNum = 0; //定义变量,默认键码值为0
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0) == 1) //读PB1输入寄存器的状态,如果为1,则代表按键1按下
{
// 消抖
Delay_ms(20);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0) == 1);
Delay_ms(20);
KeyNum = 1;
}
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 1)
{
Delay_ms(20);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 1);
Delay_ms(20);
KeyNum = 2;
}
return KeyNum;
}
Key.h
#ifndef __KEY_H
#define __KEY_H
void Key_Init(void);
uint8_t Key_GetNum(void);
#endif
2、光敏电阻控制蜂鸣器
main.c
#include "stm32f10x.h" // Device header
#include "LightSensor.h"
#include "Buzzer.h"
uint8_t LightSensor_Num; //定义用于接收光敏电阻状态的变量
int main()
{
// 模块初始化
LightSensor_Init();
Buzzer_Init();
while(1)
{
LightSensor_Num = LightSensor_Get();
if(LightSensor_Num == 1) // 光敏电阻输出低电平
{
Buzzer_ON();
}
else
{
Buzzer_OFF();
}
}
}
Buzzer.c
#include "stm32f10x.h" // Device header
void Buzzer_Init(void)
{
// 1、开启GPIOC时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// 2、GPIO初始化
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 3、设置默认为高电平,防止蜂鸣器上电鸣响
GPIO_SetBits(GPIOA, GPIO_Pin_0);
}
void Buzzer_ON(void)
{
// 鸣响
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
}
void Buzzer_OFF(void)
{
// 静音
GPIO_SetBits(GPIOA,GPIO_Pin_0);
}
void Buzzer_Turn(void)
{
//获取输出寄存器的状态,如果当前引脚输出低电平
if(GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_0) == 0)
{
// 执行为高电平,蜂鸣器静音
GPIO_SetBits(GPIOA,GPIO_Pin_0);
}
else
{
// 执行为低电平,蜂鸣器鸣响
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
}
}
Buzzer.h
#ifndef __BUZZER_H
#define __BUZZER_H
void Buzzer_Init(void);
void Buzzer_ON(void);
void Buzzer_OFF(void);
void Buzzer_Turn(void);
#endif
LightSensor.c
#include "stm32f10x.h" // Device header
void LightSensor_Init(void)
{
// 1、开启GPIOB时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// 2、GPIO初始化
GPIO_InitTypeDef GPIO_InitStructue;
GPIO_InitStructue.GPIO_Mode = GPIO_Mode_IPU; // 上拉模式
GPIO_InitStructue.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructue.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructue);
}
uint8_t LightSensor_Get(void)
{
return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13);
}
LightSensor.h
#ifndef __LIGHT_SENSOR_H
#define __LIGHT_SENSOR_H
void LightSensor_Init(void);
uint8_t LightSensor_Get(void);
#endif

浙公网安备 33010602011771号