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();
	}
	
}
相关推荐
平凡灵感码头30 分钟前
AT 指令详解:基于 MCU 的通信控制实战指南AT 指令详解
单片机·嵌入式硬件
猪猪童鞋1 小时前
基于正点原子阿波罗F429开发板的LWIP应用(1)——网络ping通
单片机·嵌入式硬件
0xCC说逆向1 小时前
Windows逆向工程提升之二进制分析工具:HEX查看与对比技术
汇编·windows·单片机·嵌入式硬件·安全·pe结构·pe文件
长流小哥2 小时前
STM32实战指南:DHT11温湿度传感器驱动开发与避坑指南
stm32·单片机·嵌入式硬件·keil5
Dev_XH2 小时前
【成品设计】基于Arduino的自动化农业灌溉系统
单片机·智能家居
Wythzhfrey3 小时前
单片机Day11
c语言·单片机·嵌入式硬件·学习·51单片机
HHONGQI1234 小时前
STM32 片上资源之串口
stm32·单片机·嵌入式硬件
搬砖的小码农_Sky4 小时前
低功耗:XILINX FPGA如何优化功耗?
嵌入式硬件·fpga开发·硬件架构·硬件工程
芯眼4 小时前
正点原子STM32新建工程
开发语言·c++·stm32·单片机·软件工程
芯动力小子7 小时前
MCU开发学习记录17* - RTC学习与实践(HAL库) - 日历、闹钟、RTC备份寄存器 -STM32CubeMX
单片机·学习·实时音视频