新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32 基于庫函數(shù)控制按鍵 蜂鳴器 LED顯示

STM32 基于庫函數(shù)控制按鍵 蜂鳴器 LED顯示

作者: 時(shí)間:2016-11-25 來源:網(wǎng)絡(luò) 收藏


void Led_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

本文引用地址:http://butianyuan.cn/article/201611/321297.htm

RCC_APB2PeriphClockCmd(Led1_RCC,ENABLE); //all led use GPIOF

#ifdef Degub //在led.h中定義
GPIO_DeInit(Led1_Port); //GPIO外設(shè)設(shè)置為缺省值
GPIO_StructInit(&GPIO_InitStructure); //將GPIO_InitStructure按缺省值填入
GPIO_PinLockConfig(Led1_Port,Led1_Pin); //鎖定LED1使用的IO引腳
#endif

#if 1

GPIO_InitStructure.GPIO_Pin=Led1_Pin|Led2_Pin|Led3_Pin|Led4_Pin;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(Led1_Port,&GPIO_InitStructure); //all led use GPIOF

LedAll_On;
Delay(0x6fffff);
LedAll_Off;
#endif

#if 0

GPIO_InitStructure.GPIO_Pin=Led1_Pin;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(Led1_Port,&GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin=Led2_Pin;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(Led2_Port,&GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin=Led3_Pin;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(Led3_Port,&GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin=Led4_Pin;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(Led4_Port,&GPIO_InitStructure);

LedAll_On;
Delay(0x6fffff);
LedAll_Off;
#endif
}



void Led_Spark(LedOrder Led_Order,u8 TurnOn_Time,LedState Led_State)
{
u8 i;
switch(Led_Order)
{
case LedAll: //選中所有LED
if(Led_State==LedOn) //點(diǎn)亮
{
for(i=0;i
{
LedAll_On;
Delay(0x5fffff);
LedAll_Off;
}
}
else if(Led_State==LedOff) //熄滅
{
LedAll_Off;
}
break;

case Led1: //選中LED1
if(Led_State==LedOn) //點(diǎn)亮
{
for(i=0;i
{
Led1_On;
Delay(0x5fffff);
Led1_Off;
}
}
else if(Led_State==LedOff) //熄滅
{
Led1_Off;
}
break;

case Led2: //選中LED2
if(Led_State==LedOn) //點(diǎn)亮
{
for(i=0;i
{
Led2_On;
Delay(0x5fffff);
Led2_Off;
}
}
else if(Led_State==LedOff) //熄滅
{
Led2_Off;
}
break;

case Led3: //選中LED3
if(Led_State==LedOn) //點(diǎn)亮
{
for(i=0;i
{
Led3_On;
Delay(0x5fffff);
Led3_Off;
}
}
else if(Led_State==LedOff) //熄滅
{
Led3_Off;
}
break;

case Led4: //選中LED4
if(Led_State==LedOn) //點(diǎn)亮
{
for(i=0;i
{
Led4_On;
Delay(0x5fffff);
Led4_Off;
}
}
else if(Led_State==LedOff) //熄滅
{
Led4_Off;
}
break;

default:
break;
}
}


#ifndef _LED_H
#define _LED_H

#include"stm32f10x.h"


#define Debug

#define Led1_RCC RCC_APB2Periph_GPIOF
#define Led1_Port GPIOF
#define Led1_Pin GPIO_Pin_7
#define Led1_On GPIO_ResetBits(Led1_Port,Led1_Pin)
#define Led1_Off GPIO_SetBits(Led1_Port,Led1_Pin)

#define Led2_RCC RCC_APB2Periph_GPIOF
#define Led2_Port GPIOF
#define Led2_Pin GPIO_Pin_8
#define Led2_On GPIO_ResetBits(Led2_Port,Led2_Pin)
#define Led2_Off GPIO_SetBits(Led2_Port,Led2_Pin)

#define Led3_RCC RCC_APB2Periph_GPIOF
#define Led3_Port GPIOF
#define Led3_Pin GPIO_Pin_9
#define Led3_On GPIO_ResetBits(Led3_Port,Led3_Pin)
#define Led3_Off GPIO_SetBits(Led3_Port,Led3_Pin)

#define Led4_RCC RCC_APB2Periph_GPIOF
#define Led4_Port GPIOF
#define Led4_Pin GPIO_Pin_10
#define Led4_On GPIO_ResetBits(Led4_Port,Led4_Pin)
#define Led4_Off GPIO_SetBits(Led4_Port,Led4_Pin)


#define LedAll_On GPIO_ResetBits(Led1_Port,Led1_Pin|Led2_Pin|Led3_Pin|Led4_Pin)
#define LedAll_Off GPIO_SetBits(Led1_Port,Led1_Pin|Led2_Pin|Led3_Pin|Led4_Pin)

typedef enum{LedAll=0,Led1=1,Led2=2,Led3=3,Led4=4}LedOrder;
typedef enum{LedOff=0,LedOn=!LedOff}LedState;


void Led_Init(void);

void Led_Spark(LedOrder Led_Order,u8 TurnOn_Time,LedState Led_State);//選中LED 點(diǎn)亮 閃爍次數(shù)

#endif

4.輪到蜂鳴器了beep.c與beep.h 代碼

#include"stm32f10x.h"
#include"beep.h"




static void Delay(vu32 Time)
{
for(;Time!=0;Time--);
}



void Beep_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(Beep_RCC,ENABLE);//Beep使用的GPIO時(shí)鐘

GPIO_InitStructure.GPIO_Pin=Beep_Pin;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(Beep_Port,&GPIO_InitStructure);

Beep_On;
Delay(0x5fffff);
Beep_Off;
}



void Beep_State(vu32 TurnOn_Time,BeepState Beep_State)
{
vu32 Time=0x5fffff;
switch(Beep_State)
{
case BeepOff:
Beep_Off;
break;

case BeepOn:
Beep_On;
Delay(TurnOn_Time*Time);
Beep_Off;
break;

default:
break;
}
}


#ifndef _BEEP_H
#define _BEEP_H

#include"stm32f10x.h"

#define Beep_RCC RCC_APB2Periph_GPIOF
#define Beep_Port GPIOF
#define Beep_Pin GPIO_Pin_6

#define Beep_On GPIO_ResetBits(Beep_Port,Beep_Pin)
#define Beep_Off GPIO_SetBits(Beep_Port,Beep_Pin)

typedef enum{BeepOff=0,BeepOn=!BeepOff}BeepState;


void Beep_Init(void);

void Beep_State(vu32 TurnOn_Time,BeepState Beep_State);//選中Beep發(fā)聲還是無聲

#endif

5.最后,不能忘記了中斷服務(wù)函數(shù),按鍵使用了兩種方式查詢與中斷控制


void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0)!=RESET) //如果按鍵1按下,相應(yīng)中斷位就被設(shè)置
{
Key_Number=Key1;
Led_Spark(Led1,1,LedOn);
Beep_State(1,BeepOn);
Key_Number=KeyNone;
EXTI_ClearITPendingBit(EXTI_Line0);
}
}


void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line13)!=RESET)
{
Key_Number=Key2;
Led_Spark(Led2,1,LedOn);
Beep_State(1,BeepOn);
Key_Number=KeyNone;
EXTI_ClearITPendingBit(EXTI_Line13);
}
}


void EXTI1_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line1)!=RESET)
{
Key_Number=Key3;
Led_Spark(Led3,1,LedOn);
Beep_State(1,BeepOn);
Key_Number=KeyNone;
EXTI_ClearITPendingBit(EXTI_Line1);
}
}


void EXTI3_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line3)!=RESET)
{
Key_Number=Key4;
Led_Spark(Led4,1,LedOn);
Beep_State(1,BeepOn);
Key_Number=KeyNone;
EXTI_ClearITPendingBit(EXTI_Line3);
}
}

以上,基于STM32的IO控制就結(jié)束了,完成上面的代碼,基本上就掌握了IO口的使用,當(dāng)然AFIO除外了。這個(gè)會在后面的文章多次出現(xiàn),初學(xué)者不要急。


上一頁 1 2 下一頁

評論


技術(shù)專區(qū)

關(guān)閉