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();
	}
	
}
相关推荐
fengfuyao9857 小时前
利用 STM32 和 ADS1256 进行高精度数据采集
stm32·单片机·嵌入式硬件
黑白园8 小时前
ADC读取XY二轴操纵杆数据通过I2C_GPIO模拟 控制0.96寸OLED显示
stm32·单片机·嵌入式硬件
一个平凡而乐于分享的小比特9 小时前
还在手动挡写单片机?MicroPython 一脚油门踩进 Python 硬件世界
单片机·嵌入式硬件·micropython
FreakStudio9 小时前
WIZnet-EVB-Pico2开始,用MicroPython玩转以太网开发
python·单片机·嵌入式·大学生·面向对象·技术栈·并行计算·电子diy·电子计算机
LCG元10 小时前
STM32实战:基于STM32F103的工业仪表数据采集(多路ADC)
stm32·单片机·嵌入式硬件
BT-BOX10 小时前
Stm32CubeMX+Proteus仿真--STM32外部中断
stm32·单片机·proteus
Wallystech-Linda11 小时前
DR9574 vs DR9574S — Choosing the Right IPQ9574 WiFi 7 Platform for Your Network
嵌入式硬件
森利威尔电子-11 小时前
森利威尔SL8700 降压型大功率 LED 恒流驱动器:5A/95%效率,支持 PWM/模拟调光
单片机·嵌入式硬件·集成电路·芯片·电源芯片
三佛科技-1873661339712 小时前
GP8892SEH贴片SOP7省外围5V2A隔离型原边反馈芯片直接替代MT3723
单片机·嵌入式硬件
Quinn2712 小时前
正点原子 STM32MP257 修复异核 FreeRTOS 例程 osDelay() 函数比 HAL_Delay() 延时快的问题
stm32·单片机·嵌入式硬件