02 # 2-bit operation

1. Bit generation operation concept, advantages and disadvantages:

Directly operate the hardware through the port address of the hardware:

Advantages: fast response, can be applied to fields such as military industry that have strict requirements on hardware performance

Disadvantages: The operation is slightly more difficult, and the personnel who need to program must understand and guide how to operate the hardware address, as well as other hardware settings.

 

Second, the conversion technique of register address and alias address:

1. Determine the start address of a port access, for example, the port F access start address is GPIOF_BASE
#define GPIOF               ((GPIO_TypeDef *) GPIOF_BASE)

2. Calculate the offset value according to the register address to be accessed, such as calculation
GPIOF ODR register address = GPIOF_BASE + 0x14;
 
3. Convert according to the following formula
Register bit alias = 0x42000000 + (register address - 0x40000000 ) * 8 * 4 + pin number * 4   

 

3. Calculation formula of bit generation operation:

Method one, set the PF9 pin level code as follows
uint32_t *PF9_BitBand = (uint32_t *)(0x42000000 + (GPIOF_BASE + 0x14 - 0x40000000)*32 + 9*4);
 
Method two, a more optimal solution:
uint32_t *PF9_BitBand   = (uint32_t *)(0x42000000 + ((uint32_t)&GPIOF->ODR - 0x40000000)*32 + 9*4);

The following code uses two methods to operate the GPIOF9 port, mainly to achieve the blinking light function, 

#include " stm32f4xx.h " 
#include <stdio.h> static GPIO_InitTypeDef GPIO_InitStructure; 
uint32_t * p9 = (uint32_t *) ( 0x42000000 + ((uint32_t) & GPIOF-> ODR- 0x40000000 ) * 32 + 9 * 4 );
 // Get the address of the hardware through bit generation operation void delay ( void ) 
{ 
    uint32_t i = 0x2000000 ; while (i-- ); 
} int main ( void ) 
{     // Enable (open) the hardware clock of port F, which is the port F power supply


    

    
    

    
    
    RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOF, the ENABLE); 


    // initialize GPIO pins 
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;         // first pin 9 
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;     // output mode 
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;     // push-pull output, output increase Current capacity. 
    GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_100MHz; // High-speed response 
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;     // The pull-up and pull-down resistors are not enabled 

    GPIO_Init (GPIOF, & GPIO_InitStructure); 
    
    
    while ( 1 ) 
    { 
        // PF9 pin output high level
         /GPIO_SetBits (GPIOF, GPIO_Pin_9); 
        * p9 = 1 ; 
        delay (); 
        
        // PF9 pin output low level 
        GPIO_ResetBits (GPIOF, GPIO_Pin_9); 
    
        delay ();     
    } 



}

4. The schematic diagram is as follows: 

 

Guess you like

Origin www.cnblogs.com/lxuechao/p/12718605.html