void Led_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(Led1_RCC,ENABLE); //all led use GPIOF
#ifdef Degub //在led.h中定义
GPIO_DeInit(Led1_Port); //GPIO外设设置为缺省值
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) //点亮
{
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) //点亮
{
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) //点亮
{
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) //点亮
{
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) //点亮
{
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 点亮 闪烁次数
#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时钟
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发声还是无声
#endif
5.最后,不能忘记了中断服务函数,按键使用了两种方式查询与中断控制
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0)!=RESET) //如果按键1按下,相应中断位就被设置
{
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控制就结束了,完成上面的代码,基本上就掌握了IO口的使用,当然AFIO除外了。这个会在后面的文章多次出现,初学者不要急。