stm32f4系列按键控制蜂鸣器,led

如题

先在板子图上找到引脚
stm32f4系列按键控制蜂鸣器,led_第1张图片
beep引脚PF8,LED0 PF9

我们编写程序使用的官方提供的库函数,我们需要完成beep.c,led.c,key.c的编写,这些用于按键,led灯,以及beep的初始化,以及位带映射的宏定义

主函数

#include "stm32f4xx.h"
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "beep.h"

int main()
{

	led_init();//
	key_init();//
	beep_init();
	while(1)
	{
		if(PAin(0) == 0)
		{
			PFout(9) ^= 1;
			PFout(8) ^= 1;
		}		
		delay(5000);
	}
	
	
}

主函数解释:先对ledkey,beep进行初始化,然后进入循环(我们需要持续观察到现象,所以需要死循环),然后判断按键是否按下过,未按下则执行 亦或1运算,运算后灯亮蜂鸣,

led.c

#include "led.h"

void led_init(void)
{
	//³õʼ»¯GPIOFµÄ9ºÅÒý½ÅΪÍÆÍìÊä³ö£¬¸ßËÙÎÞÉÏÏÂÀ­µÄģʽ
	GPIO_InitTypeDef LED;
	
	//¿ªÆôGPIOF£¬GPIOEµÄʱÖÓ£¬
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOF,ENABLE);

	LED.GPIO_Mode = GPIO_Mode_OUT;//ÅäÖÃΪÊä³öģʽ
	LED.GPIO_OType = GPIO_OType_PP;//ÅäÖÃΪÍÆÍìÊä³ö
	LED.GPIO_Pin = GPIO_Pin_9 |GPIO_Pin_10 ;//Ñ¡ÖÐ9ºÅºÍ10ºÅÒý½Å
	LED.GPIO_PuPd = GPIO_PuPd_NOPULL;//²»ÒªÉÏÏÂÀ­Ä£Ê½
	LED.GPIO_Speed = GPIO_High_Speed;//¸ßËÙ
	GPIO_Init(GPIOF,&LED);
	
	LED.GPIO_Pin = GPIO_Pin_13 |GPIO_Pin_14 ;//Ñ¡ÖÐ13ºÅºÍ14ºÅÒý½Å
	GPIO_Init(GPIOE,&LED);
	
	//°ÑLEDµÆ¶¼¹Ø±Õ
	GPIO_SetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);
	GPIO_SetBits(GPIOE,GPIO_Pin_13| GPIO_Pin_14);
	
}


stm32f4系列按键控制蜂鸣器,led_第2张图片
注释看不清楚,是因为我在keil5中编写的代码,你将代码复制到keil5中的环境中去就可以了

led.h

#ifndef _LED_H_
#define _LED_H_

#include "stm32f4xx.h"

void led_init(void);

#endif

key.c

#include "key.h"


void key_init(void)
{
	GPIO_InitTypeDef KEY;

	//¿ªÆôGPIOAµÄʱÖÓ£¬
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);

	//³õʼ»¯°´¼üPA0
	KEY.GPIO_Mode = GPIO_Mode_IN;//ÅäÖÃΪÊäÈëģʽ
	KEY.GPIO_Pin = GPIO_Pin_0 ;//Ñ¡ÖÐ0ºÅÒý½Å
	KEY.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ­Ä£Ê½---½«IOÒý½ÅǶÌ×ÔÚÒ»¸ö±È½Ï¸ßµÄµçƽÀïÃæ
	KEY.GPIO_Speed = GPIO_High_Speed;//¸ßËÙ
	GPIO_Init(GPIOA,&KEY);
	
	//¿ªÆôGPIOEµÄʱÖÓ£¬
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);
	
	//³õʼ»¯°´¼üPE2,PE3,PE4
	KEY.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
	GPIO_Init(GPIOE,&KEY);
	
}



key.h

#ifndef _KEY_H_
#define _KEY_H_

#include "stm32f4xx.h"

void key_init(void);

#endif

beep.c

#include "beep.h"

void beep_init(void)
{
	GPIO_InitTypeDef BEEP;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
	
	BEEP.GPIO_Mode = GPIO_Mode_OUT;//ÅäÖÃΪÊäÈëģʽ
	BEEP.GPIO_Pin = GPIO_Pin_8 ;//Ñ¡ÖÐ0ºÅÒý½Å
	BEEP.GPIO_Mode = GPIO_Mode_OUT;
  BEEP.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOF,&BEEP);
	
}

beep.h

#ifndef _BEEP_H_
#define _BEEP_H_

#include "stm32f4xx.h"

void beep_init(void);

#endif

delay.c

#include "delay.h"

void delay(int x)
{
	int i,j;
	for(i=0;i

delay.h

#ifndef _DELAY_H_
#define _DELAY_H_

#include "stm32f4xx.h"

void delay(int x);


#endif

sys.h(位带映射)

#ifndef __SYS_H
#define __SYS_H	
#include "stm32f4xx.h"

//ÐèÒª²Ù×÷µÄÒý½Å±àºÅ
#define BITBAND(addr, bitnum) ((0x42000000+((addr &0x000FFFFF)<<5)+(bitnum<<2)))
#define MEM_ADDR(addr)  *((volatile unsigned long  *)(addr)) 
#define BIT_ADDR(addr, bitnum)   MEM_ADDR(BITBAND(addr, bitnum)) 

//IO¿ÚµØÖ·Ó³Éä
//Êä³ö¼Ä´æÆ÷Ó³Éä
#define GPIOA_ODR_Addr    (GPIOA_BASE+0x14) //0x40020014
#define GPIOB_ODR_Addr    (GPIOB_BASE+0x14) //0x40020414
#define GPIOC_ODR_Addr    (GPIOC_BASE+0x14) //0x40020814
#define GPIOD_ODR_Addr    (GPIOD_BASE+0x14) //0x40020C14
#define GPIOE_ODR_Addr    (GPIOE_BASE+0x14) //0x40021014
#define GPIOF_ODR_Addr    (GPIOF_BASE+0x14) //0x40021414 
#define GPIOG_ODR_Addr    (GPIOG_BASE+0x14) //0x40021814 

//ÊäÈë¼Ä´æÆ÷Ó³Éä
#define GPIOA_IDR_Addr    (GPIOA_BASE+0x10) //0x40020010
#define GPIOB_IDR_Addr    (GPIOB_BASE+0x10) //0x40020410
#define GPIOC_IDR_Addr    (GPIOC_BASE+0x10) //0x40020810
#define GPIOD_IDR_Addr    (GPIOD_BASE+0x10) //0x40020C10
#define GPIOE_IDR_Addr    (GPIOE_BASE+0x10) //0x40021010
#define GPIOF_IDR_Addr    (GPIOF_BASE+0x10) //0x40021410
#define GPIOG_IDR_Addr    (GPIOG_BASE+0x10) //0x40021810
 

#define PAout(n)   BIT_ADDR(GPIOA_ODR_Addr,n)  //Êä³ö 
#define PAin(n)    BIT_ADDR(GPIOA_IDR_Addr,n)  //ÊäÈë 

#define PBout(n)   BIT_ADDR(GPIOB_ODR_Addr,n)  //Êä³ö 
#define PBin(n)    BIT_ADDR(GPIOB_IDR_Addr,n)  //ÊäÈë 

#define PCout(n)   BIT_ADDR(GPIOC_ODR_Addr,n)  //Êä³ö 
#define PCin(n)    BIT_ADDR(GPIOC_IDR_Addr,n)  //ÊäÈë 

#define PDout(n)   BIT_ADDR(GPIOD_ODR_Addr,n)  //Êä³ö 
#define PDin(n)    BIT_ADDR(GPIOD_IDR_Addr,n)  //ÊäÈë 

#define PEout(n)   BIT_ADDR(GPIOE_ODR_Addr,n)  //Êä³ö 
#define PEin(n)    BIT_ADDR(GPIOE_IDR_Addr,n)  //ÊäÈë

#define PFout(n)   BIT_ADDR(GPIOF_ODR_Addr,n)  //Êä³ö 
#define PFin(n)    BIT_ADDR(GPIOF_IDR_Addr,n)  //ÊäÈë

#define PGout(n)   BIT_ADDR(GPIOG_ODR_Addr,n)  //Êä³ö 
#define PGin(n)    BIT_ADDR(GPIOG_IDR_Addr,n)  //ÊäÈë

#endif

关于位带映射的部分,我会在我的下一篇博客中详细介绍

你可能感兴趣的:(stm32)