STM32 learning experience four: GPIO experiment - based register

Record it, read - to facilitate future
test content : 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 : bit operation does not involve related code for beginners friend register 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 (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.
Register relates to:
. 1) registers the RCC --APB2 Peripheral Clock Enable Register (RCC_APB2ENR), Offset Address: 0x18, Reset Value: 0x0000 0000;
Here Insert Picture Description2) the GPIO registers - Port Configuration Register Low (GPIOx_CRL) (x = A ... E), an offset address: 0x00, reset value: 0x4444 4444;
Here Insert Picture Description
Here Insert Picture Description
. 3) port configuration register high (GPIOx_CRH) (x = A ... E), offset: 0x04, reset value: 0x4444 4444;
Here Insert Picture Description
. 4) the GPIO registers - output data register port (GPIOx_ODR) (x = A ... E), the address offset: 0Ch, reset value: 0x0000 0000;
Here Insert Picture Description
. 5) port input data register (GPIOx_IDR) (x = A ... E), offset address: 0x08, reset value: 0x0000 XXXX;
Here Insert Picture Description)
experimental Procedure :
1) New project templates;
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 the corresponding 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
void LED_Init(void);
#endif

5) beep.h header code are as follows:

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

6) key.h header code are as follows:

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

7) led.c file code are as follows:

#include "led.h"
#include "stm32f10x.h"
void LED_Init(void)
{
 RCC->APB2ENR|=1<<3;      //使能PORTB时钟      
 RCC->APB2ENR|=1<<6;      //使能PORTE时钟       
 GPIOB->CRL&=0XFF0FFFFF;  
 GPIOB->CRL|=0X00300000;  //PB5推挽输出,3对应二进制0011,即推挽输出模式,最大速率50MHz,     
  GPIOB->ODR|=1<<5;        //PB.5输出高             
 GPIOE->CRL&=0XFF0FFFFF;
 GPIOE->CRL|=0X00300000;  //PE.5推挽输出,3对应二进制0011,即推挽输出模式,最大速率50MHz,
 GPIOE->ODR|=1<<5;        //PE.5输出高 
}

8) beep.c file code are as follows:

#include "beep.h"
#include "stm32f10x.h"
void BEEP_Init(void)
{
  RCC->APB2ENR|=1<<3;          //使能PORTB时钟   
  GPIOB->CRH&=0XFFFFFFF0; 
   GPIOB->CRH|=0X00000003;      //PB.8推挽输出,3对应二进制0011,即推挽输出模式,最大速率50MHz,     
  GPIOB->ODR&=~(1<<8);         //PB.8输出低 
}

9) key.c file code are as follows:

#include "stm32f10x.h"
#include "key.h"
#include "delay.h"
void KEY_Init(void) //IO初始化
{ 
 RCC->APB2ENR|=1<<2;          //使能PORTA时钟  
 RCC->APB2ENR|=1<<6;          //使能PORTE时钟   
 GPIOA->CRL&=0XFFFFFFF0; 
 GPIOA->CRL|=0X00000008;      //PA0配置为下拉输入,8对应二进制1000,即输入,下拉要在ODR中设置
 GPIOE->CRL&=0XFFF000FF; 
 GPIOE->CRL|=0X00088800;      //PE2,PE3,PE4配置为输入模式,8对应二进制1000,即输入
 GPIOE->ODR|=7<<2;           //PE2,PE3,PE4配置为上拉输入
}
u8 KEY_Scan(u8 mode)
{  
 static u8 key_up=1;//按键按松开标志
 if(mode)key_up=1;  //支持连按    
 if(key_up&&(((GPIOE->IDR&0X0004)==0)||((GPIOE->IDR&0X0008)==0)||((GPIOE->IDR&0X0010)==0)||((GPIOA->IDR&0X0001)==1)))
 {
  delay_ms(10);//去抖动 
  key_up=0;
  if((GPIOE->IDR&0X0004)==0)return 1;
   else if((GPIOE->IDR&0X0008)==0)return 2;
  else if((GPIOE->IDR&0X0010)==0)return 3;
  else if((GPIOA->IDR&0X0001)==1)return 4;
 }else if(((GPIOE->IDR&0X0000)==0)&&((GPIOA->IDR==0X0000)==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(72);
 LED_Init();
  BEEP_Init();
 KEY_Init(); 
 while(1)
 {
    key=KEY_Scan(1);
      if(key)
  {         
   switch(key)
   {     
    case 4:
     GPIOB->ODR|=1<<8;         //PB.8输出高  
     break;
    case 3: 
    GPIOB->ODR&=~(1<<5);        //PB.5输出低       
     break; 
    case 2: 
    GPIOE->ODR&=~(1<<5);        //PE.5输出低
     break;
    case 1:                               
    GPIOB->ODR&=~(1<<5);        //PB.5输出低
    GPIOE->ODR&=~(1<<5);        //PE.5输出低 
     break;
   }
   delay_ms(100);                         //延迟1000ms
   GPIOB->ODR&=~(1<<8);      //PB.8输出低
   GPIOB->ODR|=1<<5;         //PB.5输出高
   GPIOE->ODR|=1<<5;         //PE.5输出高
  }                         
 }
}

13) Upon completion you can run to see results.
Knowledge :
1) Learning Chinese manual STM32 five registers involved;
2) Review key.c file (U8) logic u8 KEY_Scan functions, reference may be STM32 learning experience three: GPIO Experiment - based libraries ;
3) how to review new project templates, reference STM32 learning experience II: New project templates .

Published 24 original articles · won praise 2 · Views 4129

Guess you like

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