打印
[STM32F1]

按键控制LED灯以及光敏传感器控制LED

[复制链接]
164|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
tpgf|  楼主 | 2023-8-31 14:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
按键控制 技术点

控制LED的 GPIO 设置为输出
控制按键的GPIO 设置为上拉输入
按键部分代码
Key.c

#include "stm32f10x.h"
#include "Delay.h"
void Key_Init(void){
                RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
                GPIO_InitTypeDef GPIO_InitStructure;
                GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IPU; //ÉÏÀ­ÊäÈë
                GPIO_InitStructure.GPIO_Pin= GPIO_Pin_1;
                GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz ;
                GPIO_Init(GPIOB,&GPIO_InitStructure);
       
}
uint8_t Key_GetNum(void)
{
        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;
               
}




Key.h

#ifndef __KEY_H
#define __KEY_H

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

#endif


Led部分代码
Led.c

#include "stm32f10x.h"

void LED_Init(void){
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP; //ÍÆÃâÊä³ö
        GPIO_InitStructure.GPIO_Pin= GPIO_Pin_1;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz ;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
        GPIO_ResetBits(GPIOA,GPIO_Pin_1);

}

void LED_ON(){ ///chose a on
                GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}

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




Main

#include "stm32f10x.h"
#include "Delay.h"
#include "Led.h"
#include "Key.h"

#define  dm Delay_ms
uint8_t KeyNum;
uint8_t flag=0;

int main(void){
       
        LED_Init();
        Key_Init();
        while(1){
                KeyNum = Key_GetNum();
                if(KeyNum) flag=!flag;
                if(flag) LED_ON();
                else LED_OFF();
        }
       
}




光敏传感部分
光敏传感器设置为上拉输入
无光为亮 有光则暗

Light.c

#include "stm32f10x.h"

void Light_Init(void){

        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IPU; //ÉÏÀ­ÊäÈë
        GPIO_InitStructure.GPIO_Pin= GPIO_Pin_7;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz ;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
       
}

uint8_t get_num(){
        return GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7);
}




Led.c

#include "stm32f10x.h"

void LED_Init(void){
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP; //ÍÆÃâÊä³ö
        GPIO_InitStructure.GPIO_Pin= GPIO_Pin_1;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz ;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
        GPIO_ResetBits(GPIOA,GPIO_Pin_1);

}

void LED_ON(){ ///chose a on
                GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}

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



main

#include "stm32f10x.h"
#include "Delay.h"
#include "Led.h"
#include "Light.h"

#define  dm Delay_ms
uint8_t KeyNum;
uint8_t flag=0;

int main(void){
       
        LED_Init();
        Light_Init();
        while(1){
                if(get_num()==1) LED_ON();
                else LED_OFF();
        }
       
}


————————————————
版权声明:本文为CSDN博主「牛郎恋刘娘,刘娘念牛郎」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_52172364/article/details/132545793

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1536

主题

14520

帖子

9

粉丝