关于野火mini 蜂鸣器和按键检测的学习

学习目标:

关于野火mini 蜂鸣器和按键检测的学习


学习内容:

1.关于野火mini 蜂鸣器的学习
2.按键检测的学习


学习时间:

1、 周五上午 8点—上午9点


学习产出:

1、CSDN 技术博客 1 篇

一、关于野火mini beep的学习
在这里插入图片描述

beep.h

#ifndef _BEEP_H
#define _BEEP_H
#include "stm32f10x.h"

void BEEP_GPIO_Config(void);  //定义配置蜂鸣器的GPIO配置函数

#endif

beep.c

#include "./beep/beep.h"
void BEEP_GPIO_Config(void)
{
	GPIO_InitTypeDef GPIO_InitTStruct;                     //GPIO_InitTypeDef结构体
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);   //打开对应的时钟
	GPIO_InitTStruct.GPIO_Pin=GPIO_Pin_1;                  //选择引脚
	GPIO_InitTStruct.GPIO_Mode=GPIO_Mode_Out_PP;           //推挽输出
	GPIO_InitTStruct.GPIO_Speed=GPIO_Speed_10MHz;          //端口频率为10Hz
	GPIO_Init(GPIOC,&GPIO_InitTStruct);                    //库函数调用,初始化GPIO
	
}

main.c

#include "stm32f10x.h"
#include "./beep/beep.h"
//延时函数
void delay(uint32_t count)
{
	for(;count!=0;count--);
}
int main(void)
{
	BEEP_GPIO_Config();     //调用关于配置蜂鸣器GPIO口的函数
	while(1)
 {
   GPIO_SetBits(GPIOC,GPIO_Pin_1);	    //beep发声
   delay(0xffffff);                     //延时
   GPIO_ResetBits(GPIOC,GPIO_Pin_1);	//beep不发声
   delay(0xffffff);   
 }
}

二、关于野火mini 按键检测的学习
在这里插入图片描述

key.h

#ifndef __KEY_H
#define	__KEY_H
#include "stm32f10x.h"
//定义端口
#define    KEY1_GPIO_CLK     RCC_APB2Periph_GPIOA
#define    KEY1_GPIO_PORT    GPIOA			   
#define    KEY1_GPIO_PIN	GPIO_Pin_0

#define    KEY2_GPIO_CLK     RCC_APB2Periph_GPIOC
#define    KEY2_GPIO_PORT    GPIOC		   
#define    KEY2_GPIO_PIN	GPIO_Pin_13

#define KEY_ON	1
#define KEY_OFF	0

void Key_GPIO_Config(void);
uint8_t Key_Scan(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin);      //定义关于按键检测的函数

#endif /* __KEY_H */

key.c

#include "./key/key.h"  
void Key_GPIO_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;     //定义GPIO_InitTypeDef结构体
	
	/*打开端口时钟*/
	RCC_APB2PeriphClockCmd(KEY1_GPIO_CLK|KEY2_GPIO_CLK,ENABLE);
	
	//选择GPIO口
	GPIO_InitStructure.GPIO_Pin = KEY1_GPIO_PIN; 
	//浮空输入:浮空最大的特点就是电压的不确定性,它可能是0V,可能是VCC,还可能是介于两者之间的某个值. 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
	//使用结构体初始化端口
	GPIO_Init(KEY1_GPIO_PORT, &GPIO_InitStructure);
	//选择GPIO口
	GPIO_InitStructure.GPIO_Pin = KEY2_GPIO_PIN; 
	//浮空输入
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
	//使用结构体初始化端口
	GPIO_Init(KEY2_GPIO_PORT, &GPIO_InitStructure);	
}

//按键检测函数
uint8_t Key_Scan(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin)
{			
	/*是否有按键按下*/
	if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_ON )  
	{	 
		/*等待按键松开*/
		while(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_ON);   
		return 	KEY_ON;	 
	}
	else
		return KEY_OFF;
}
/*********************************************END OF FILE**********************/

main.c(配置led代码参照之前的博客“关于野火mini led的学习”)

#include "stm32f10x.h"
#include "./led/led.h"  
#include "./key/key.h" 

int main(void)
{	
	/*配置LED端口*/
	LED_GPIO_Config();
	LED1_ON;//LED1打开,根据led.h中的宏定义

	/*配置按键的GPIO口 */
	Key_GPIO_Config();
	
	/*检测哪个按键按下*/
	while(1)                            
	{	   
		if( Key_Scan(KEY1_GPIO_PORT,KEY1_GPIO_PIN) == KEY_ON  )
		{
			/*LED1反转,根据led.h中宏定义*/
			LED1_TOGGLE;
		} 

		if( Key_Scan(KEY2_GPIO_PORT,KEY2_GPIO_PIN) == KEY_ON  )
		{
			/*LED2反转*/
			LED2_TOGGLE;
		}		
	}
}
/*********************************************END OF FILE**********************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

visual_eagle

欢迎交流学习

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值