【stm32-2】按键控制LED&光敏传感器控制蜂鸣器

1.按键控制LED 

uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);        //读取输入数据寄存器某一个端口的输入值
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);        //读取整个输入数据寄存器
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);        //读取输出数据寄存器某一位
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);        //读取整个输出寄存器

(1)主函数部分

#include "Device/Include/stm32f10x.h"   // Device header
#include "Delay.h"
#include "LED.h"
#include "key.h"
uint8_t KeyNumber;
int main(void)
{
	LED_Init();
	Key_Init();
	while(1)
	{
		KeyNumber=Key_GetNumber();
		if(KeyNumber==1)
		{
			LED1_Turn();
		}
		if(KeyNumber==2)
		{
			LED2_Turn();
		}
	}
}

(2)key.h

#ifndef __KEY_H
#define __KEY_H

void Key_Init(void);
uint8_t Key_GetNumber(void);



#endif

(3)key.c

#include "Device/Include/stm32f10x.h"   // Device header
#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_Pin_11;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
}

uint8_t Key_GetNumber(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;
	}
		if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0)
	{
		Delay_ms(20);
		while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0);
		Delay_ms(20);
		KeyNum=2;
	}
	return KeyNum;
}

(4)LED.h

#ifndef __LED_H
#define __LED_H
void LED_Init(void);
void LED1_ON(void);
void LED1_OFF(void);
void LED2_ON(void);
void LED2_OFF(void);
void LED1_Turn(void);
void LED2_Turn(void);
#endif

(5)LED.c 

#include "Device/Include/stm32f10x.h"   // Device header

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_Pin_2;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	
	GPIO_SetBits(GPIOA,GPIO_Pin_1 | GPIO_Pin_2);
}

void LED1_ON(void)
{
	GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}

void LED1_OFF(void)
{
	GPIO_SetBits(GPIOA,GPIO_Pin_1);
}
void LED1_Turn(void)
{
	if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_1)==0)	//读取当前A口状态,为0,置1.否则,置0。
	{
		GPIO_SetBits(GPIOA,GPIO_Pin_1);//灭(使某个位设置为高电平)
	}
	else
	{
		GPIO_ResetBits(GPIOA,GPIO_Pin_1);//亮
	}
}
void LED2_ON(void)
{
	GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}

void LED2_OFF(void)
{
	GPIO_SetBits(GPIOA,GPIO_Pin_2);
}
void LED2_Turn(void)
{
	if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_2)==0)
	{
		GPIO_SetBits(GPIOA,GPIO_Pin_2);
	}
	else
	{
		GPIO_ResetBits(GPIOA,GPIO_Pin_2);
	}
}

2.光敏传感器控制蜂鸣器 

(1)main.c

#include "Device/Include/stm32f10x.h"   // Device header
#include "Delay.h"
#include "buzzer.h"
#include "lighesensor.h"
int main()
{
	Buzzer_Init();
	Ligntsensor_Init();
	while(1)
	{
		if(Ligntsensor_Get()==1)
		{
			Buzzer_ON();
		}
		else
		{
			Buzzer_OFF();
		}
	}
}

(2)buzzer.c

#include "Device/Include/stm32f10x.h"   // Device header

void Buzzer_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	
	
	GPIO_SetBits(GPIOB,GPIO_Pin_12);
}

void Buzzer_ON(void)
{
	GPIO_ResetBits(GPIOB,GPIO_Pin_12);
}

void Buzzer_OFF(void)
{
	GPIO_SetBits(GPIOB,GPIO_Pin_12);
}
void Buzzer_Turn(void)
{
	if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_12)==0)	//读取当前A口状态,为0,置1.否则,置0。
	{
		GPIO_SetBits(GPIOB,GPIO_Pin_12);//灭(使某个位设置为高电平)
	}
	else
	{
		GPIO_ResetBits(GPIOB,GPIO_Pin_12);//亮
	}
}

(3)buzzer.h

#ifndef __Buzzer_H
#define __Buzzer_H
void Buzzer_Init(void);
void Buzzer_ON(void);
void Buzzer_OFF(void);
void Buzzer_Turn(void);

#endif

(4)ligntsesor.c

#include "Device/Include/stm32f10x.h"   // Device header
void Ligntsensor_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_13;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
}
uint8_t Ligntsensor_Get(void)
{
	return GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13);//读取光敏传感器输入位高低电平
}

(5)lightsensor.h 

#ifndef __LIGHT_SENSOR_H
#define __LIGHT_SENSOR_H
void Ligntsensor_Init(void);
uint8_t Ligntsensor_Get(void);

#endif

最近更新

  1. TCP协议是安全的吗?

    2024-05-11 22:16:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-11 22:16:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-11 22:16:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-11 22:16:03       18 阅读

热门阅读

  1. c++中unrodered_map与unordered_set的基本使用

    2024-05-11 22:16:03       11 阅读
  2. 基于Seata实现分布式事务实现

    2024-05-11 22:16:03       11 阅读
  3. ffmpeg 7.0 + vs2022 +windows编译

    2024-05-11 22:16:03       15 阅读
  4. Idea 核心编程快捷键-简洁版

    2024-05-11 22:16:03       12 阅读
  5. 嵌入式复习提纲

    2024-05-11 22:16:03       12 阅读
  6. 学习笔记:IEEE 1003.13-2003【POSIX PSE53接口列表】

    2024-05-11 22:16:03       13 阅读
  7. Python图形界面(GUI)Tkinter笔记(目录)

    2024-05-11 22:16:03       14 阅读