Experiment 1 GPIO experiment

1. Purpose of the experiment

  1. Master the embedded GPIO programming process.
  2. Familiar with the basic use of STM32 firmware library.

2. Experimental equipment

Hardware: one PC           

Software: MDK V5.23 set

       Proteus8.7 set

  • Experimental principle

1. The basic structure of GPIO

 

2. GPIO register

Each GPIO port has:

Two configuration registers (GPIOx_CRL, GPIOx_CRH)

Two data registers (GPIOx_IDR and GPIOx_ODR)

A set/reset register (GPIOx_BSRR),

A reset register (GPIOx_BRR)

A lock register (GPIOx_LCKR).

3. GPIO mode

 

4. Speed ​​in GPIO output mode

 5. GPIO library function

   For details on how to use the function, see the STM32 Firmware Library User Manual.pdf

 

6. GPIO configuration steps

 (1) Turn on the clock of the corresponding peripheral, for example:

      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);

                        //Open the clock of peripheral B port

  (2) Set the pin mode, for example :

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;//Select pin 8

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//Push-pull output

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

           //The maximum output speed is 50MHz, if the pin is used for input, this item does not need to be set

  GPIO_Init(GPIOB, &GPIO_InitStructure);//Initialize according to the above pin setting mode

7. Common delay functions

 (1) us level delay function

  void delay_nus(unsigned long n) // n must be greater than 6

{

  unsigned long j;

  while(n--)              

  {

j=12;   

while(j--);

  }

}

 (2) ms-level delay function

void delay_nms(unsigned long n) // ms level delay

{

  while(n--)   

  delay_nus(1030);   

}

4. Experimental content

1. Use Keil5 to build a project. For the method of building a project, see the New Project Template.pdf  file.

2. For how to use Proteus8.7, see the courseware in Chapter 4 or check the information by yourself.

Simple combined application of STM32 MCU compilation environment keil5 and Proteus simulation_哔哩哔哩_bilibili  --- STM32 environment keil5 combined with Proteus simulation application video

In the following questions, LED1, LED2, and LED3 are connected to PA1, PA2, and PA3 respectively, and keyboards K2 and K3 are connected to PC1 and PC2 respectively.

  1. Programming realizes that LED1, LED2, and LED3 flash alternately, and the interval between each LED flashing is 0.2S (once on and off for a total of 0.2S), and lasts for 1S.
  2. Program to realize the function of LED1, LED2, LED3 forward running water lights (that is, light up sequentially, from top to bottom), and the LED interval time is 0.2S.
  3. Program to achieve the following functions:

If the keyboard is not pressed, LED1, LED2, and LED3 will flash alternately, and the interval between each LED flashing is 0.2S (0.2S for one on and one off), and lasts for 1S.

‚If the keyboard K2 is pressed, the LED1, LED2, and LED3 will realize the function of LED1, LED2, and LED3 forward flowing lights (that is, light up in sequence, from top to bottom), and the LED interval time is 0.2S.

ƒIf the keyboard K3 is pressed, the function of LED1, LED2, and LED3 reverse running water lights will be realized (that is, they will light up sequentially, from bottom to top), and the LED interval time is 0.2S.

Submit the content requirements of the experiment report:

  1. Purpose.

(1) Master the embedded GPIO programming process.
(2), familiar with the basic use of STM32 firmware library.

2 . experiment content . Questions 1-3 Programming . (Simulation screenshots, programs. Pay attention to typesetting)

(1) Write the led initialization program

 (2) Define the header file and macro define the corresponding function for use

 ( 3 ) Main function main(): execute the corresponding initialization program first, and execute a pipeline action (1 second)

 ( 4 ) In the dead loop while (1): Use the if function to judge the state of the button to realize the function of switching between forward and reverse flow.

 

3 . Experiment summary.

Through this experiment, I have a better understanding of the embedded GPIO program design process; I have a better understanding of the application of various function configurations of the GPIO port, and I am also more familiar with the basic use of the STM32 firmware library. Through the hardware example and the corresponding control program, the effect of outputting high and low levels on the IO port is analyzed in detail.

Guess you like

Origin blog.csdn.net/weixin_45784275/article/details/125425199