STM32 learning experience five: GPIO experiment - based on the bit operation

Record it, read - to facilitate future
test content : Marquee, buzzer and a key input
official information:
1) "the STM32 Chinese Reference Manual V10" Chapter 8 General and multiplexing functions IO (GPIO and AFIO)
2) "the Cortex- M3 Definitive Guide (Chinese) "Chapter 5-bit operation
characteristics : bit operation
principle of FIG :
1) As shown, marquees, PB5 then LED0, PE5 then LEDl;
Here Insert Picture Description
2) As shown, a buzzer, PB8 then BEEP (buzzer);
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 does not go into detail, be sure to see what peripherals and GPIO pin docking.
GPIO library functions involved have learned :

1void GPIO_Init(GPIO_TypeDef* GPIOx,GPIO_InitTypeDef* GPIO_InitStruct)//初始化作用

New knowledge : bit The address calculation:
Here Insert Picture Description)
is assumed to be a fifth order GPIOA output pins: PAout (5), and stm32f10x.h view sys.h two headers found:
Here Insert Picture Description
Here Insert Picture Description
Accordingly, GPIOA_ODR_Addr = 0x4001080C, n = 5 . Prior to purple formula in the header file key code is unique, the address of the alias can be calculated PAout (5) a.
Here Insert Picture Description
Therefore, bit manipulation can do this:
is defined in led.h header file:

#define LED0 PBout(5);

Led.c defined in the file:

PBout(5)=0; //设GPIOB的第五管脚为低电平。

Finally, mention why is GPIOA_BASE + 12? STM32 Chinese reference manual mentioned, GPIOx_ODR address offsets are 0Ch, and therefore is +12. Corresponding, GPIOx_IDR offset address is 0x08, therefore +8. As for why the GPIOA_BASE equal APB2PERIPH_BASE + 0x0800, did not find the answer, Daniel can explain.
Here Insert Picture Description
Here Insert Picture Description
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 are as follows:

#ifndef __LED_H
#define __LED_H
#include "sys.h"
void LED_Init(void);    //初始化函数
#define LED0 PBout(5)   // PB5
#define LED1 PEout(5)   // PE5 
#endif

5) beep.h header code are as follows:

#ifndef __BEEP_H
#define __BEEP_H
void BEEP_Init(void);   //初始化函数
#define BEEP PBout(8)   // PB8
#endif

6) key.h header code are as follows:

#ifndef __KEY_H
#define __KEY_H
#include "sys.h"
#define KEY0 PEin(4)    //PE4
#define KEY1 PEin(3)   //PE3 
#define KEY2 PEin(2)   //PE2
#define WK_UP PAin(0)  //PA0  WK_UP
void KEY_Init(void);
u8 KEY_Scan(u8);
#endif

7) led.c file code are as follows:

#include "led.h"
#include "sys.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
 LED0=1;                                         //PB.5 输出高,LED0灭
 GPIO_Init(GPIOE, &GPIO_InitStructure);               //根据设定参数初始化GPIOE.5
 LED1=1;                                          //PE.5 输出高,LED1灭  
}

8) beep.c file code are as follows:

#include "beep.h"
#include "sys.h"
void BEEP_Init(void)
{
 GPIO_InitTypeDef  GPIO_InitStructure; 
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);  //使能PB端口时钟
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;           //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
 BEEP=0;                                          //PB8 输出低,LED0灭
} 

9) key.c file code are as follows:

#include "key.h"
#include "sys.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&&(KEY0==0||KEY1==0||KEY2==0||WK_UP==1))
 {
  delay_ms(10);                                                              //去抖动 
  key_up=0;
  if(KEY0==0)return 1;                                                       //KEY0按下,即PE4为低电平
  else if(KEY1==0)return 2;                                                  //KEY1按下,即PE3为低电平
  else if(KEY2==0)return 3;                                                  //KEY2按下,即PE2为低电平
  else if(WK_UP==1)return 4;                                                 //WK_UP按下,即PA0为高电平
 }else if(KEY0==1&&KEY1==1&&KEY2==1&&WK_UP==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 Include
the right of ... Paths, add the directory led.h, beep.h and key.h three header files (ie HARDWARE);
12 ) New main.c main function, as follows:

#include "led.h"
#include "beep.h"
#include "key.h"
#include "sys.h"
#include "delay.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:
       BEEP=1;   //控制蜂鸣器叫   
       break;
      case 3: 
        LED0=0;     //控制LED0亮       
       break; 
      case 2: 
        LED1=0;     //控制LED1亮  
       break;
      case 1:                               
        LED0=0;     //控制LED0亮 
        LED1=0;     //控制LED1亮 
       break;
     }
     delay_ms(300);  //延迟300ms
     BEEP=0;         //控制蜂鸣器不叫
     LED0=1;         //控制LED0灭
     LED1=1;         //控制LED1灭
   }else delay_ms(10); 
  }
 }

13) Upon completion you can run to see results.
Knowledge :
1) Learning Basics bit operation;
2) review GPIO key.c files and libraries u8 KEY_Scan (u8) of logic functions, reference may STM32 learning experience three: GPIO Experiment - based libraries
and STM32 learning experience four : GPIO experiment - based register
how 4) review new project templates, reference STM32 learning experience II: New project template

Published 24 original articles · won praise 2 · Views 4128

Guess you like

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