STM32单片机学习篇6

光敏传感器与蜂鸣器

1.LigntSensor.c

复制代码
#include "stm32f10x.h"                  // Device header


void LightSensor_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 LightSensor_Get(void){
	return GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13);
}

2.LightSensor.h

复制代码
#ifndef _Lightsensor_H
#define _LightSensor_H


void LightSensor_Init(void);
uint8_t LightSensor_Get(void);
#endif

3.Buzzer.c

复制代码
//封装Buzzer的驱动程序
#include "stm32f10x.h"                  // Device header

//初始化Buzzer的函数

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 Buzzer1_ON(void)
	{
	
	GPIO_ResetBits(GPIOB,GPIO_Pin_12);
}
void Buzzer1_OFF(void)
	{
	GPIO_SetBits(GPIOB,GPIO_Pin_12);
	
}

//实现翻转功能
void Buzzer1_Turn(void){
	
	if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_12) == 0){
		
		GPIO_SetBits(GPIOB,GPIO_Pin_12);
	}
	else
		GPIO_ResetBits(GPIOB,GPIO_Pin_12);
}

void Buzzer2_ON(void)
	{
	
	GPIO_ResetBits(GPIOB,GPIO_Pin_12);
}
void Buzzer2_OFF(void)
	{
	GPIO_SetBits(GPIOB,GPIO_Pin_12);
	
}
	
void Buzzer2_Turn(void){
	
	if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_12) == 0){
		
		GPIO_SetBits(GPIOB,GPIO_Pin_12);
	}
	else
		GPIO_ResetBits(GPIOB,GPIO_Pin_12);
}

Buzzer.h

复制代码
#ifndef _Buzzer_H
#define _Buzzer_H

void Buzzer_Init(void);
void Buzzer1_ON(void);
void Buzzer1_OFF(void);
void Buzzer2_ON(void);
void Buzzer2_OFF(void);
void Buzzer1_Turn(void);
void Buzzer2_Turn(void);

#endif

4.main.c

复制代码
#include "stm32f10x.h"                  // Device header
#include "Delay.h"						//使用延时函数
#include "Buzzer.h"

int main(void)
{
	Buzzer_Init();
	LightSensor_Init();
	
	while(1)
	{
	
		if(LightSensor_Get() == 1){
			Buzzer1_ON();
			
		}else{
			Buzzer1_OFF();
		}
		
	}
}
相关推荐
深蓝海拓几秒前
PySide6,QCoreApplication::aboutToQuit与QtQore.qAddPostRoutine:退出前后的清理工作
笔记·python·qt·学习·pyqt
酒鼎2 分钟前
学习笔记(3)HTML5新特性(第2章)
笔记·学习·html5
L***一17 分钟前
2026届大专跨境电商专业毕业生就业能力提升路径探析
学习
.小墨迹29 分钟前
apollo学习之借道超车的速度规划
linux·c++·学习·算法·ubuntu
ZH154558913139 分钟前
Flutter for OpenHarmony Python学习助手实战:模块与包管理的实现
python·学习·flutter
Gain_chance1 小时前
33-学习笔记尚硅谷数仓搭建-DWS层交易域用户粒度订单表分析及设计代码
数据库·数据仓库·hive·笔记·学习·datagrip
全栈游侠1 小时前
STM32F103XX 02-电源与备份寄存器
stm32·单片机·嵌入式硬件
hqyjzsb1 小时前
盲目用AI提效?当心陷入“工具奴”陷阱,效率不增反降
人工智能·学习·职场和发展·创业创新·学习方法·业界资讯·远程工作
承渊政道1 小时前
Linux系统学习【Linux系统的进度条实现、版本控制器git和调试器gdb介绍】
linux·开发语言·笔记·git·学习·gitee
野犬寒鸦3 小时前
从零起步学习并发编程 || 第七章:ThreadLocal深层解析及常见问题解决方案
java·服务器·开发语言·jvm·后端·学习