Bit operation code in sys. H file, to achieve the bit operation of each IO port of STM32, including read in and output. Of course, before calling these functions, we must first enable the IO clock and define the IO function.

One. Principle of bit band operation

Principle analysis of bit band operation of STM32 single chip microcomputer

Expand a bit to a 32-bit address. If you want to write the bit as 1, you only need to write 1

Two. Which regions support bitband operation?

Principle analysis of bit band operation of STM32 single chip microcomputer

Principle analysis of bit band operation of STM32 single chip microcomputer

For example, an SRAM area

There are 32 bits in 0x20000000, and each bit can be mapped to an address. If you want to write 1 to which bit, you only need to write 1 to the mapped address of this bit, so as to achieve the purpose of operating bits.

Principle analysis of bit band operation of STM32 single chip microcomputer

Three. Advantages of bit band operation

Principle analysis of bit band operation of STM32 single chip microcomputer

To set bit2 to 1 without bitband operation, first read the value of 0x20000000, then set bit2 to 1, and then write the value of the register back to 0x20000000. If bitband operation is used, you already know that bit2 is mapped to the address of 0x22000008, so you can write 1 directly to this address.

Principle analysis of bit band operation of STM32 single chip microcomputer

Four. The relation of mapping

Principle analysis of bit band operation of STM32 single chip microcomputer

A register or a peripheral will have a base address. First of all, we need to find the address of the register, and then we can find the relevant bits. Then we can get the mapped address through formula calculation. There is no need to delve into it here.

Five. In sys. H, bit band operation is implemented in the input and output part of GPIO

Principle analysis of bit band operation of STM32 single chip microcomputer

The address of ODR in gpioa is the base address of gpioa + the offset address of ODR = gpioa_ BASE+0x0C

For example, paout output is to operate the ODR register, paout (1) is to calculate the address of this bit mapping through a series of calculations, and operate on this address

The input of painis to operate the IDR register.

Six. Instance operation

Running lantern experiment

Programming steps:

Principle analysis of bit band operation of STM32 single chip microcomputer

LED0 to pb5, LED1 to pe5

LED. C file

//Initialize pb5 and pe5 as output ports. And enable the clock of these two ports

//Led IO initialization

void LED_ Init(void)

{

GPIO_ InitTypeDef GPIO_ InitStructure;

RCC_ APB2PeriphClockCmd(RCC_ APB2Periph_ GPIOB|RCC_ APB2Periph_ GPIOE, ENABLE); // Enable Pb, PE port clock

GPIO_ InitStructure.GPIO_ Pin = GPIO_ Pin_ 5; // LED0 — “Pb. 5 port configuration

GPIO_ InitStructure.GPIO_ Mode = GPIO_ Mode_ Out_ PP; // Push pull output

GPIO_ InitStructure.GPIO_ Speed = GPIO_ Speed_ 50MHz; // The IO port speed is 50MHz

GPIO_ Init(GPIOB, &GPIO_ InitStructure); // Initialize gpiob. 5 according to the set parameters

GPIO_ SetBits(GPIOB,GPIO_ Pin_ 5); // Pb. 5 high output

GPIO_ InitStructure.GPIO_ Pin = GPIO_ Pin_ 5; // LED1 — “PE. 5 port configuration, push-pull output

GPIO_ Init(GPIOE, &GPIO_ InitStructure); // Push pull output, IO port speed is 50MHz

GPIO_ SetBits(GPIOE,GPIO_ Pin_ 5); // PE. 5 high output

}

Principal function

#include “stm32f10x.h”

#include “delay.h”

#include “led.h”

int main(void)

{

delay_ init();

LED_ Init();

while(1)

{

PBout(5)= 1; // Operating Pb. 5 pin with bitband

PEout(5)= 1;

delay_ ms(500);

PBout(5)= 0; // Operating Pb. 5 pin with bitband

PEout(5)= 0;

delay_ ms(500);

}

}

Source; 21ic

Leave a Reply

Your email address will not be published. Required fields are marked *