STM32 learning experience three: GPIO experiment - based library functions

Record it, easy to read - after
experiment contents : Marquee, buzzer and a key input
official information: "the STM32 Chinese Reference Manual V10" Chapter 8 General and multiplexing functions IO (GPIO and AFIO)
Features : does not involve register operations, friends relevant code bit operation, suitable for beginners library function operation
principle of FIG:
1) As shown, marquees, PB5 then LED0, PE5 then LEDl;
Here Insert Picture Description2) As shown, a buzzer, then PB8 bEEP (beep unit);
Here Insert Picture Description
3) As shown, PE4 then KEY0, PE3 then KEY1, PE2 then KEY2, PA0 then WK_UP;
Here Insert Picture Description
works do not go into detail, must see the GPIO pins with which the peripheral docking.
Involving four GPIO library functions :

1void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)//初始化作用
2void GPIO_SetBits(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin)//设置输出电平为高
3void GPIO_ResetBits(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin)//设置输出电平为低
4)uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef*GPIOx, uint16_t GPIO_Pin);    //读取某个GPIO端口的输入电平

In stm32f10x_gpio.h file , the following code, you need to keep in mind:

typedef enum
{ GPIO_Mode_AIN = 0x0,            //模拟输入//
  GPIO_Mode_IN_FLOATING = 0x04,   //输入浮空//
  GPIO_Mode_IPD = 0x28,           //输入下拉//
  GPIO_Mode_IPU = 0x48,           //输入上拉//
  GPIO_Mode_Out_OD = 0x14,        //开漏输出//
  GPIO_Mode_Out_PP = 0x10,        //推挽输出//
  GPIO_Mode_AF_OD = 0x1C,         //复用开漏输出//
  GPIO_Mode_AF_PP = 0x18          //复用推挽输出//
}GPIOMode_TypeDef;

How these eight modes corresponding to the address is to modify the register, I do not know, are interested can be carefully interpreted stm32f10x_gpio.c file void GPIO_Init (GPIO_TypeDef * GPIOx, GPIO_InitTypeDef * GPIO_InitStruct) function.
Experimental procedure :
1) new construction template;
2) New HARDWARE empty folder (the folder USER tied);
3) in MDK software, the new led.c, beep.c and key.c three .c files and correspondence the led.h, beep.h key.h and header files are saved in the folder HARDWARE;
. 4) led.h header code as follows:

#ifndef __LED_H
#define __LED_H
void LED_Init(void);
#endif

5) beep.c header code as follows:

#ifndef __BEEP_H
#define __BEEP_H
void BEEP_Init(void);
#endif

6) key.h header code as follows:

#ifndef __KEY_H
#define __KEY_H
#include "stm32f10x.h"
void KEY_Init(void);
u8 KEY_Scan(u8);
#endif

7) led.c document code is as follows:

#include "led.h"
#include "stm32f10x.h"
void LED_Init(void)
{
 GPIO_InitTypeDef  GPIO_InitStructure; 
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);  //使能PB端口时钟
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);  //使能PE端口时钟
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;              //LED0-->PB.5 端口配置
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      //推挽输出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     //IO口速度为50MHz
 GPIO_Init(GPIOB, &GPIO_InitStructure);               //根据设定参数初始化GPIOB.5
 GPIO_SetBits(GPIOB,GPIO_Pin_5);                      //PB.5 输出高,LED0灭
 GPIO_Init(GPIOE, &GPIO_InitStructure);              //根据设定参数初始化GPIOE.5
 GPIO_SetBits(GPIOE,GPIO_Pin_5);                     //PE.5 输出高,LED1灭 
}

8) beep.c document code is as follows:

#include "stm32f10x.h"
#include "beep.h"
void BEEP_Init(void)
{ 
 GPIO_InitTypeDef  GPIO_InitStructure;  
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);  //使能PB端口时钟 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;           //蜂鸣器-->PB.8 端口配置
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      //推挽输出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     //IO口速度为50MHz
 GPIO_Init(GPIOB, &GPIO_InitStructure);              //根据设定参数初始化GPIOB.8
 GPIO_ResetBits(GPIOB,GPIO_Pin_8);                 //PB.8 输出低,蜂鸣器不叫 
}

9) key.c document code is as follows:

#include "stm32f10x.h"
#include "key.h"
#include "delay.h"
void KEY_Init(void) //IO初始化
{ 
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE,ENABLE);    //使能PA,PE端口时钟
 GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;             //KEY0-KEY2
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;                                //设置成上拉输入
  GPIO_Init(GPIOE, &GPIO_InitStructure);                                       //初始化GPIOE2,3,4
 GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;                                //PA0设置成输入,默认下拉   
 GPIO_Init(GPIOA, &GPIO_InitStructure);                                       //初始化GPIOA.0
}
u8 KEY_Scan(u8 mode)
{  
 static u8 key_up=1;//按键按松开标志
 if(mode)key_up=1;  //支持连按    
 if(key_up&&(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)==0||GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)==0||GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2)==0||GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==1))
 {
  delay_ms(10);//去抖动 
  key_up=0;
  if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)==0)return 1;
  else if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)==0)return 2;
  else if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2)==0)return 3;
  else if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==1)return 4;
 }else if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)==1&&GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)==1&&GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2)==1&&GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==0)key_up=1;      
  return 0;// 无按键按下
}

10) In the MDK software, right Target1, click the Manage Project Items ..., in Project Items interface, Groups column, the new HARDWARE, and led.c, beep.c and key.c three .c files are added to come in;
11) in the MDK software, click the magic wand in C / C ++ option, click the Include Paths of ... the right to add directory led.h, beep.h and key.h three header files (ie HARDWARE);
12 ) New main.c main function, as follows:

#include "stm32f10x.h"
#include "delay.h"
#include "led.h"
#include "beep.h"
#include "key.h"
int main(void)
{
  vu8 key=0;
 delay_init();
 LED_Init();
  BEEP_Init();
 KEY_Init(); 
 while(1)
 {
  key=KEY_Scan(1);
      if(key)
  {         
   switch(key)
   {     
    case 4:
     GPIO_SetBits(GPIOB,GPIO_Pin_8);   //控制蜂鸣器叫   
     break;
    case 3: 
    GPIO_ResetBits(GPIOB,GPIO_Pin_5);     //控制LED0亮       
     break; 
    case 2: 
    GPIO_ResetBits(GPIOE,GPIO_Pin_5);     //控制LED1亮  
     break;
    case 1:                               
    GPIO_ResetBits(GPIOB,GPIO_Pin_5);     //控制LED0亮 
    GPIO_ResetBits(GPIOE,GPIO_Pin_5);     //控制LED1亮 
     break;
   }
   delay_ms(1000);                         //延迟1000ms
   GPIO_ResetBits(GPIOB,GPIO_Pin_8);       //控制蜂鸣器不叫
   GPIO_SetBits(GPIOB,GPIO_Pin_5);         //控制LED0灭
   GPIO_SetBits(GPIOE,GPIO_Pin_5);         //控制LED1灭
  }
 }
}

13) Upon completion you can run to see results.
Knowledge points :
1) to learn the meaning of four GPIO library functions involved;
2) understanding key.c file (u8) u8 KEY_Scan logic functions;
3) review how to create a project template - based library functions, refer to the STM32 learning experience two : New project template

Published 24 original articles · won praise 2 · Views 4130

Guess you like

Origin blog.csdn.net/Leisure_ksj/article/details/105049793