STM32按键控制蜂鸣器

发布时间:2023年12月24日

key.c

#include"key.h"
void Key_Init(void)
{
	GPIO_InitTypeDef GPIO_VALUE; 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_VALUE.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_VALUE.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_8;
	GPIO_Init(GPIOC,&GPIO_VALUE);	
	
	GPIO_VALUE.GPIO_Mode=GPIO_Mode_IPD;
	GPIO_VALUE.GPIO_Pin=GPIO_Pin_0;
	GPIO_Init(GPIOC,&GPIO_VALUE);	
}
int Key_status(int nu)
{
	int ret=0;
	switch(nu)
	{
		case 0:ret=GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9);break;
		case 1:ret=GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8);break;
		case 2:ret=GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0);ret=!ret;break;
	}
	return !ret;
	
}

key.h

#ifndef __KEY_H
#define __KEY_H
#include "stm32f10x_conf.h"
extern void Key_Init(void);
extern int Key_status(int nu);

#endif 

main.c

#include"fmq.h"
#include"key.h"
int main(void)
{
	int i=0,j=0;
	Fmq_Init();
	Key_Init();
	for(i=0;i<3;i=(i+1)%3)
	{
	  if(i==0)  
	  Key_status(i)?Fmq_On():Fmq_Off();
  }
	return 0;
}

文章来源:https://blog.csdn.net/qq_63507404/article/details/135173134
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。