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();
	}
	
}
相关推荐
晶振厂家-晶发电子10 小时前
晶振在5G时代的角色:高精度时钟的核心支撑
单片机·嵌入式硬件·5g·晶振·电子元器件·晶振知识
F1372980155710 小时前
WD5030A 芯片,12V降5V,输出电流12A,电路设计
stm32·单片机·嵌入式硬件·汽车·51单片机
小莞尔10 小时前
【51单片机】【protues仿真】基于51单片机的篮球计时计分器系统
c语言·stm32·单片机·嵌入式硬件·51单片机
三佛科技-1873661339710 小时前
分享机械键盘MCU解决方案
单片机·嵌入式硬件·计算机外设
李永奉10 小时前
51单片机-使用IIC通信协议实现EEPROM模块教程
单片机·嵌入式硬件·51单片机
工大一只猿10 小时前
51单片机学习
嵌入式硬件·学习·51单片机
小莞尔10 小时前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
风_峰10 小时前
Ubuntu Linux SD卡分区操作
嵌入式硬件·ubuntu·fpga开发
bing_feilong10 小时前
STM32精准控制水流
单片机·嵌入式硬件
Hello_Embed17 小时前
STM32HAL 快速入门(二十):UART 中断改进 —— 环形缓冲区解决数据丢失
笔记·stm32·单片机·学习·嵌入式软件