STM32f103入门(3)按键控制LED灯以及光敏传感器控制LED

按键控制 技术点

  • 控制LED的 GPIO 设置为输出
  • 控制按键的GPIO 设置为上拉输入

按键部分代码

Key.c

cpp 复制代码
#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

cpp 复制代码
#ifndef __KEY_H
#define __KEY_H

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

#endif

Led部分代码

Led.c

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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

cpp 复制代码
#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();
	}
	
}
相关推荐
SRA.25 分钟前
STM32——HAL库开发笔记22(定时器3—呼吸灯实验)(参考来源:b站铁头山羊)
笔记·stm32·嵌入式硬件
SRA.26 分钟前
STM32——HAL库开发笔记21(定时器2—输出比较)(参考来源:b站铁头山羊)
笔记·stm32·嵌入式硬件
日记成书3 小时前
详细介绍嵌入式硬件设计
嵌入式硬件·深度学习·学习
wenchm3 小时前
细说STM32F407单片机1个ADC使用DMA同时采集3个输入通道的方法
stm32·单片机·嵌入式硬件
SRA.3 小时前
STM32——HAL库开发笔记23(定时器4—输入捕获)(参考来源:b站铁头山羊)
笔记·stm32·嵌入式硬件
打酱油的工程师4 小时前
w803|联盛德|WM IoT SDK2.X测试|window11|TOML 文件|外设|TFT_LCD|测试任务|(5):TFT_LCD_LVGL示例
单片机·物联网·lcd·tft·w80x
广药门徒4 小时前
(200): error: #29: expected an expression error: #40: expected an identifier
嵌入式硬件
沐欣工作室_lvyiyi5 小时前
基于物联网的家庭版防疫面罩设计与实现(论文+源码)
人工智能·stm32·单片机·物联网·目标跟踪
苏慕TRYACE8 小时前
RT-Thread+STM32L475VET6——USB鼠标模拟
stm32·单片机·嵌入式硬件·计算机外设·rt_thread
楼台的春风14 小时前
【MCU驱动开发概述】
c语言·驱动开发·单片机·嵌入式硬件·mcu·自动驾驶·嵌入式