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();
		}
		
	}
}
相关推荐
xier_ran7 分钟前
【infra之路】AI 编译器学习笔记(八):向量化——把小块变成 SIMD 指令
人工智能·笔记·学习
kmomo..8 分钟前
Linux 驱动开发学习笔记:I2C 子系统从入门到实践
linux·驱动开发·学习
zhangdaliangmvp39 分钟前
四元数的学习理解
学习
硅基手札41 分钟前
【Linux 内核专栏 18】进程间通信 IPC(下)— Socket·Netlink·fd 三件套·futex·线程同步
linux·运维·驱动开发·单片机·嵌入式硬件·mcu·架构
遇印记11 小时前
传输层,网络层
运维·服务器·网络·windows·学习
clorinda12 小时前
PyQt5学习:从界面搭建到与机器学习实战
学习
欣欣之王来了14 小时前
Vue3项目脚手架:Vite vs Vue CLI怎么选?
前端·学习·架构·项目·vue3教程
kaixin_啊啊14 小时前
【零基础学AI】第 7 章——让 AI 辅助学习和办公
人工智能·学习
信奥卷王14 小时前
[GESP202506 六级] 学习小组(含视频)
学习
宛 禾15 小时前
Qt 控件入门:QLabel 从拖拽到对象树,一篇讲透
c++·qt·学习