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();
		}
		
	}
}
相关推荐
西岸行者3 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
悠哉悠哉愿意3 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
别催小唐敲代码3 天前
嵌入式学习路线
学习
Lester_11013 天前
STM32霍尔传感器输入口设置为复用功能输入口时,还能用GPIO函数直接读取IO的状态吗
stm32·单片机·嵌入式硬件·电机控制
LCG元3 天前
低功耗显示方案:STM32L0驱动OLED,动态波形绘制与优化
stm32·嵌入式硬件·信息可视化
三佛科技-187366133973 天前
120W小体积碳化硅电源方案(LP8841SC极简方案12V10A/24V5A输出)
单片机·嵌入式硬件
z20348315203 天前
STM32F103系列单片机定时器介绍(二)
stm32·单片机·嵌入式硬件
毛小茛3 天前
计算机系统概论——校验码
学习
babe小鑫3 天前
大专经济信息管理专业学习数据分析的必要性
学习·数据挖掘·数据分析
古译汉书3 天前
【IoT死磕系列】Day 7:只传8字节怎么控机械臂?学习工业控制 CANopen 的“对象字典”(附企业级源码)
数据结构·stm32·物联网·http