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();
		}
		
	}
}
相关推荐
FreakStudio8 天前
W55MH32L-EVB 上手测评:硬件 TCP/IP 加持的以太网单片机,MicroPython 零门槛开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
通信小呆呆13 天前
当算法有了“五感”:多模态数据融合如何向人体感官协同学习?
人工智能·学习·算法·机器学习·机器人
H__Rick13 天前
自动对焦学习-3
人工智能·学习·计算机视觉
✎ ﹏梦醒͜ღ҉繁华落℘13 天前
单片机基础知识---stm32单片机的优先级
stm32·单片机·mongodb
Daisy Lee13 天前
量化学习-第1章-什么是量化金融
学习·金融·datawhale
Alsn8613 天前
等待学习-学习目录:Docker 容器安全攻防
学习·安全·docker
YM52e13 天前
买菜计算器小应用 - HarmonyOS ArkUI 开发实战-PC版本
学习·华为·harmonyos·鸿蒙·鸿蒙系统
小雨下雨的雨13 天前
HarmonyOS ArkUI训练营入门-组件掌握系列-Animation 动画效果实现-PC版本
学习·华为·harmonyos·鸿蒙
zd84510150013 天前
RS485 总线详解
单片机·嵌入式硬件
cqbzcsq13 天前
CellFlow虚拟细胞论文阅读
论文阅读·人工智能·笔记·学习·生物信息