当前位置:网站首页>Bit segment operation of STM32, LED lighting based on stm32f103xxx multiple methods

Bit segment operation of STM32, LED lighting based on stm32f103xxx multiple methods

2022-06-21 09:38:00 ah_ yl

First, let me talk about the steps of bit segment operation :

( direct cp The original text of the manual )

The following mapping formula shows how each word in the alias area corresponds to the corresponding bit in the bit band area :
bit_word_addr = bit_band_base + (byte_offset×32) + (bit_number×4)
among :
bit_word_addr Is the address of the word in the alias memory area , It maps to a target bit .
bit_band_base Is the starting address of the alias area .
byte_offset Is the sequence number in the bit segment of the byte containing the target bit
bit_number Is the location of the target bit (0-31)
Example :
The following example shows how to map in the alias area SRAM The address is 0x20000300 Bits in bytes of 2:
0x22006008 = 0x22000000 + (0x300×32) + (2×4).
Yes 0x22006008 Address write operation and pair SRAM Middle address 0x20000300 Bits of bytes 2 Carry out reading - Change - Writes have the same effect
 

We need to know that bit segment operations also apply to mapped addresses of peripherals

The starting address of the peripheral is 0x40000000;

 

We use stm32f103RC6 Chip as an example :

I bought this board pd2 Pin connected led The lamp , Therefore, some key information to know includes the following

//GPIOD Mapping address of

#define  GPIOD               ((GPIO_TypeDef *)GPIOD_BASE)        //0x4001 1400

//GPIO The register of is defined as follows , among __IO Namely volatile keyword , about GPIOD for , We will directly GPIOD The address of the contained register is marked with , Convenient view

typedef struct
{
  __IO uint32_t CRL;     //0x40011400
  __IO uint32_t CRH;    //0x40011404
  __IO uint32_t IDR;    //0x40011408
  __IO uint32_t ODR;   //0x4001140c
  __IO uint32_t BSRR;   //0x40011410
  __IO uint32_t BRR;   //0x40011414
  __IO uint32_t LCKR;   //0x40011418
} GPIO_TypeDef;     

 

How to light led The lamp ?

We can GPIOD Of ODR Register or BSSR Register operation ;

1、 Yes ODR The register of   bit2(0~31) Write 0, be PD2 Output low level , Yes ODR The register of   bit2(0~31) Write 1, be PD2 Output high level

2、 Yes BSSR Of bit2(0~31) Write 1,PD2 Output high level , Yes BSSR Of bit(2+16)(0~31) Write 1,PD2 Output low level ,      //16 Representative pin is   0~15 ,16 One pin   . Yes BSSR Register write 0 , Without any action

 

So we can use the following methods to control LED The light goes on and off .

1、 Control with bit segment operation ODR perhaps BSSR register

2、 Write data in the direct peripheral address register

So we can follow the following method

① The first is right BSSR Perform bit segment operation , Let's look at the previous one BSSR The address for   0x40011410, Then the bit segment operation address ( Yes PD2 Pin , So it is bit2) by   0x4200 0000 + 0x11410*32 + 2*4 Pin output high level

Pin needs to output low level   Then the bit segment operation is ( Yes BSSR The register of 18bit To operate )    The address is  0x4200 0000 + 11412*32 +18*4

② Yes BSSR The mapped address of the register writes data directly   BSSR The address for   0x40011410, Then we are bit2 Write 1  , Operation for  *((volatile uint32_t *)0x40011410) = 0x04; to PD2 Output high level

perhaps bit18 Write 1,   *((volatile uint32_t *)0x40011410) = 0x040000;  to PD2 Output low level

③ Of course we can do it directly GPIOD Of BSSR Register operation           GPIOD->BSRR = 4;// Output high level

 GPIOD->BSRR = (uint32_t)4<<16;  // Output low level   It's totally ok Of

④ We are right.   ODR Registers perform bit segment operations    ODR The mapping address of is   0x4001140c    , Bit segment operation The address is   0x4200 0000 + 0x1140c*32 + 2*4

⑤ Direct pair ODR Register to write data

⑥ call HAL library

The code is as follows , Only for reference

#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))

#define MEM_ADDR(addr)  *((volatile unsigned long  *)(addr))
#define BIT_ADDR(addr, bitnum)   MEM_ADDR(BITBAND(addr, bitnum))   

int main()
{
//....
//.. some GPIO initialization 
while(1)
{
    HAL_Delay(1000);

 switch(way)
    {
      case 1:
	    //TODO:1    Yes BSSR Registers perform bit segment operations 
	    BIT_ADDR(0x40011410,2) = 1;     // The macro definition expands to   *((volatile unsigned long*)0x42228208) = 1;
	    HAL_Delay(1000);
	    BIT_ADDR(0x40011410,18) = 1;
	    break;
      case 2:
	    //TODO:2  Yes BSSR The mapped address of the register is written directly 
	    *((volatile uint32_t *)0x40011410) = 0x04;
	    HAL_Delay(1000);
	    *((volatile uint32_t *)0x40011410) = 0x040000;
	    break;
      case 3:
	    //TODO:3  Yes BSSR Register operation 
	    GPIOD->BSRR = 4;
	    HAL_Delay(1000);
	    GPIOD->BSRR = (uint32_t)4<<16;
	    break;
      case 4:
	    //TODO:4   Yes ODR Registers perform bit segment operations 
	    BIT_ADDR((GPIOD_BASE+12),2) = 1;
	    HAL_Delay(1000);
	    BIT_ADDR((GPIOD_BASE+12),2) = 0;
	    break;
      case 5:
	    //TODO:5  Yes ODR The mapped address of the register is written directly 
	    *((volatile uint32_t *)0x4001140c) = 0x04;
	    HAL_Delay(1000);
	    *((volatile uint32_t *)0x4001140c) = 0x00;
	    break;
      case 6:
	    //TODO:6   call HAL Library function 
	    HAL_GPIO_WritePin(GPIOD, LED1_Pin, 0);
	    HAL_Delay(1000);
	    HAL_GPIO_WritePin(GPIOD, LED1_Pin, 1);
	    break;
      default:break;

    }
}
}

Function is led flashing . With IDE yes STM32CUBE ide It's really easy to use !!!()

原网站

版权声明
本文为[ah_ yl]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221443018644.html