天天看點

stm32f4系列按鍵控制蜂鳴器,led

如題

先在闆子圖上找到引腳

stm32f4系列按鍵控制蜂鳴器,led

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

注釋看不清楚,是因為我在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<x;i++)
		for(j=0;j<1000;j++);
}


           

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

           

關于位帶映射的部分,我會在我的下一篇部落格中詳細介紹

繼續閱讀