STM32

04GPIO输入-按键控制LED-光敏传感器控制蜂鸣器

2022-12-04  本文已影响0人  MJUNy
按键控制LED

1、学习模块化编程
将LED和Key封装为.c和.h函数。LED.c包含LED初始化,LED_ON,LED_OFF和LED_Turn(用到了GPIO_ReadOutputDataBit()读取引脚电平,再翻转电平);Key.c包含按键初始化以及按键扫描函数(通过GPIO_ReadInputDataBit()函数判断哪个引脚被按下)。
LED.h

#ifndef __LED_H
#define __LED_H

void LED_Init(void);
void LED0_ON(void);
void LED0_OFF(void);
void LED1_ON(void);
void LED1_OFF(void);
void LED0_Turn(void);
void LED1_Turn(void);   
#endif

LED.c

#include "stm32f10x.h"                  // Device header

void LED_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStruct);
    
    GPIO_SetBits(GPIOA, GPIO_Pin_0 | GPIO_Pin_1);
}

void LED0_ON(void)
{
    GPIO_ResetBits(GPIOA, GPIO_Pin_0);
}

void LED0_OFF(void)
{
    GPIO_SetBits(GPIOA, GPIO_Pin_0);
}

void LED0_Turn(void)
{
    if(GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_0) == 0)
    {
        GPIO_SetBits(GPIOA, GPIO_Pin_0);
    }else
    {
        GPIO_ResetBits(GPIOA, GPIO_Pin_0);
    }
}

void LED1_ON(void)
{
    GPIO_ResetBits(GPIOA, GPIO_Pin_1);
}

void LED1_OFF(void)
{
    GPIO_SetBits(GPIOA, GPIO_Pin_1);
}

void LED1_Turn(void)
{
    if(GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_1) == 0)
    {
        GPIO_SetBits(GPIOA, GPIO_Pin_1);
    }else
    {
        GPIO_ResetBits(GPIOA, GPIO_Pin_1);
    }
}

Key.h

#ifndef __KEY_H
#define __KEY_H

void Key_Init(void);
uint8_t Key_GetNum(void);

#endif

Key.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"

void Key_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;//设置为上拉输入,悬空时默认高电平
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_10;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStruct);
}

uint8_t Key_GetNum(void)
{
    uint8_t KeyNum;
    if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) == 0)
    {
        //延时消抖
        Delay_ms(20);
        //确保按键松开
        while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) == 0);
        Delay_ms(20);
        KeyNum = 1;
    }
    if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_10) == 0)
    {
        //延时消抖
        Delay_ms(20);
        //确保按键松开
        while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_10) == 0);
        Delay_ms(20);
        KeyNum = 2;
    }
    return KeyNum;
}

2、在main.c中调用函数
main.c,在接收函数返回值时采用变量的形式接收会比直接在if中调用函数判断更好,在这里如果直接在if()中用函数判断,按键按下不能被清晰的判断,会出现不灵敏的情况,使用变量则避免了这种情况的发生。

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"

uint8_t KeyNum;//接收按键值的变量

int main(void)
{
    LED_Init();
    Key_Init();
    while (1)
    {
        KeyNum = Key_GetNum();
        if(KeyNum == 1)
        {
            LED0_Turn();
        }else if(KeyNum == 2)
        {
            LED1_Turn();
        }
    }
}
光敏传感器控制蜂鸣器

1、光敏传感器知识
当光线较暗时,输出指示灯灭,代表输出高电平;
当光线足够时,输出指示灯亮,代表输出低电平;
电位器可以调节高低电平的判断阈值;
接线采用VCC、GND、DO三个引脚
2、蜂鸣器函数
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

Buzzer.c

#include "stm32f10x.h"                  // Device header

void Buzzer_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStruct);
    
    GPIO_SetBits(GPIOB, GPIO_Pin_12);
}

void Buzzer_ON(void)
{
    GPIO_ResetBits(GPIOB, GPIO_Pin_12);
}

void Buzzer_OFF(void)
{
    GPIO_SetBits(GPIOB, GPIO_Pin_12);
}

void Buzzer_Turn(void)
{
    if(GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_12) == 0)
    {
        GPIO_SetBits(GPIOB, GPIO_Pin_12);
    }else
    {
        GPIO_ResetBits(GPIOB, GPIO_Pin_12);
    }
}

3、光敏传感器函数
LightSensor.h

#ifndef __LIGHTSENSOR_H
#define __LIGHTSENSOR_H

void LightSensor_Init(void);
uint8_t LightSensor_Get(void);

#endif

LightSensor.c

#include "stm32f10x.h"                  // Device header

//光敏传感器初始化 PB13
void LightSensor_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;//上拉下拉效果一样
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStruct);
    
    GPIO_SetBits(GPIOB, GPIO_Pin_13);
}

//
uint8_t LightSensor_Get(void)
{
    //直接返回
    return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13);
}

4、光照强度弱,LED亮,蜂鸣器响;光照足够,LED灭,蜂鸣器停
main.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.h"
#include "Buzzer.h"
#include "LightSensor.h"

int main(void)
{
    LED_Init();
    Buzzer_Init();
    LightSensor_Init();
    while (1)
    {
        if(LightSensor_Get()==1) //光照不足,光敏传感器指示灯灭
        {
            LED0_ON();
            Buzzer_ON();
        }
        else
        {
            LED0_OFF();
            Buzzer_OFF();
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读