Rookie STM32 marquee

WeChat public account : Xiaofan Study is
concerned about common learning, questions or suggestions, please leave a message on the public account !!!

As a programmer, in the initial learning of programming, one of the most basic entry-level examples "Hello World" is unavoidable. So, what is the most basic entry example when learning MCU? That's right, that is, "lighting an LED light" will take you through a classic marquee program to take everyone to the journey of STM32F4. Through this study, you will understand the method of using the IO port of STM32F4 as output .
We will control the two LEDs on the STM32F4 development board through code: DS0 and DS1 flash alternately to achieve the effect similar to a marquee.

Hardware connection

 

 

GPIO working method

  • 4 input modes:
      input floating
      input pull-up
      input pull-down
      analog input

  • 4 output modes:
     open-drain output (with pull-up or pull-down)
     open-drain multiplexing function (with pull-up or
     pull- down) push-pull output (with pull-up or
     pull- down) push-pull multiplexing function (with pull-up or pull-down )

  • 4 kinds of maximum output speed:
      -2MHZ
      -25MHz
      -50MHz
      -100MHz

software design

led.c

#include " led.h "  
//////////////////////////////////////////// ////////////////////////////////////// /      


// initialize output port PF10 and PF9. and Enable the clock of these two ports            
 // LED IO initialization 
void LED_Init ( void ) 
{          
  GPIO_InitTypeDef GPIO_InitStructure; 

  RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOF, ENABLE); // Enable GPIOF clock 

  // GPIOF9, F10 Initialization setting 
  GPIO_IOPin_IOPin_IOPin_IOPin_IOPin_IOPin_IOPin_InitStructure // LED0 and LED1 correspond to the IO port 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; // General output mode 
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;// push-pull output 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; // 100MHz 
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // pull 
  GPIO_Init (GPIOF, & GPIO_InitStructure); // initialize the GPIO 
    
    GPIO_SetBits (GPIOF, GPIO_Pin_9 | GPIO_Pin_10); // GPIOF9, provided the F10 High, light off 

}

 

This code contains a function void LED_Init (void). The function of this function is to configure PF9 and PF10 as push-pull output. It should be noted here that when configuring the STM32 peripheral, the clock of the peripheral must be enabled at all times! GPIO is a peripheral mounted on the AHB1 bus. The peripheral clock mounted on the AHB1 bus in the firmware library is enabled by the function RCC_AHB1PeriphClockCmd ().

After setting the clock, LED_Init calls the GPIO_Init function to complete the initial configuration of PF9 and PF10, and then calls the function GPIO_SetBits to control LED0 and LED1 output 1 (LED off). At this point, the initialization of the two LEDs is complete. This completes the initialization of these two IO ports.

led.h

#ifndef __LED_H
 #define __LED_H 
#include " sys.h " 

////////////////////////////////////// //////////////////////////////////////////// /      
                                  
///// ////////////////////////////////////////////////// ////////////////////////// /      


// the LED port definition 
#define LED0 PFout (. 9)     // the DS0 
#define LEDl PFout (10)     / / DS1      

void LED_Init ( void ); // Initialize #                              
endif

 

Here, bit-band operations are used to operate one bit of an IO port.

Write the following code in the main function:

#include " sys.h " 
#include " delay.h " 
#include " usart.h " 
#include " led.h " 


// Marquee experiment-library function version 

    
int main ( void ) 
{ 
 
    delay_init ( 168 );           // Initialization delay function 
    LED_Init ();                 // Initialize LED port 
  while ( 1 ) 
    { 
     LED0 = 0 ;               // LED0 lights 
       LED1 = 1 ;                // LED1 off 
         delay_ms ( 500 ); 
         LED0 = 1 ;                 // LED0 off 
         LED1 = 0 ;                 // LED1 on 
         delay_ms ( 500 ); 
     } 
}

 

The code includes the sentence #include "led.h", so that LED0, LED1, LED_Init, etc. can be called in the main () function. Here we need to reiterate that in the firmware library, the system will call the function SystemInit () in system_stm32f4xx.c to initialize the system clock when it starts, and call the main () function after the clock initialization is completed. So we don't need to call SystemInit () function in main () function anymore. Of course, if you need to reset the clock system, you can write your own clock setting code. SystemInit () just initializes the clock system to the default state. The main () function is very simple, first call delay_init () to initialize the delay, then call LED_Init () to initialize GPIOF.9 and GPIOF.10 as output. Finally, LED0 and LED1 flash alternately in an infinite loop at 500ms intervals.

then press

Compile the project

You can see that there are no errors and no warnings. From the compilation information, we can see that our code occupies FLASH size: 5672 bytes (5248 + 424), and the SRAM size used is: 1880 bytes (1832 + 48). Here we explain the meaning of several data in the compilation result: Code: indicates the size of FLASH occupied by the program (FLASH). RO-data: Read Only-data, which represents the program-defined constant (FLASH). RW-data: Read Write-data, which means the initialized variable (SRAM) ZI-data: Zero Init-data, which means the uninitialized variable (SRAM) With this, you can know the current flash and The sram size is large, so it is important to note that the size of the program is not the size of the .hex file, but the sum of the compiled Code and RO-data.

Download verification

After downloading, LED0 and LED1 flash cyclically

Guess you like

Origin www.cnblogs.com/fw-qql/p/12740467.html