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();
	}
	
}
相关推荐
点灯小铭1 小时前
基于单片机的多模式自动洗衣机设计与实现
数据库·单片机·嵌入式硬件·毕业设计·课程设计
La Pulga1 小时前
【STM32】RTC实时时钟
c语言·stm32·单片机·嵌入式硬件·mcu·实时音视频
m0_555762901 小时前
STM32产品程序测试完整指南
stm32·单片机·嵌入式硬件
普中科技2 小时前
【普中Hi3861开发攻略--基于鸿蒙OS】-- 第 26 章 WIFI实验-AP 建立网络
单片机·嵌入式硬件·wifi·liteos·hi3861·普中科技
普中科技2 小时前
【普中Hi3861开发攻略--基于鸿蒙OS】-- 第 24 章 OLED液晶显示实验
单片机·嵌入式硬件·oled·liteos·hi3861·普中科技
冻结的鱼3 小时前
STM32H5 的 PB14 引脚被意外拉低的问题解析
stm32·单片机·嵌入式硬件
小莞尔3 小时前
【51单片机】【protues仿真】基于51单片机彩灯控制器系统
单片机·嵌入式硬件
文火冰糖的硅基工坊3 小时前
[嵌入式系统-146]:五次工业革命对应的机器人形态的演进、主要功能的演进以及操作系统的演进
前端·网络·人工智能·嵌入式硬件·机器人
老六哥_AI助理指南4 小时前
为什么AI会改变单片机的未来?
人工智能·单片机·嵌入式硬件
点灯小铭4 小时前
基于单片机的智能家居多参数环境监测与联动报警系统设计
单片机·mongodb·毕业设计·智能家居·课程设计·期末大作业