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();
		}
		
	}
}
相关推荐
狐狐生风12 分钟前
LangChain 向量存储:Chroma、FAISS
人工智能·python·学习·langchain·faiss·agentai
狐狐生风20 分钟前
LangChain RAG 基础
人工智能·python·学习·langchain·rag·agentai
LCG元2 小时前
STM32项目实战:基于STM32F103的智能农业监控系统
stm32·单片机·嵌入式硬件
努力努力再努力FFF3 小时前
医生对AI辅助诊断感兴趣,作为临床人员该怎么了解和学习?
人工智能·学习
sakiko_4 小时前
UIKit学习笔记5-使用UITableView制作聊天页面
笔记·学习·swift·uikit
Truffle7电子5 小时前
STM32CubeIDE/Programmer/Touch GFX 应用
stm32·单片机·嵌入式硬件
Alice-YUE5 小时前
【js高频八股】防抖与节流
开发语言·前端·javascript·笔记·学习·ecmascript
constant_LDX5 小时前
步进电机开发(一、硬件设计)
单片机·嵌入式硬件
北山有鸟6 小时前
修改源码法和插件法
嵌入式硬件·学习
richxu202510016 小时前
嵌入式学习之路->stm32篇->(14)通用定时器(上)
stm32·单片机·嵌入式硬件·学习