STM32 lights up the LED lights (registered version)

Preface

Use the register to light up the LED. This article describes how to find the register. Refer to the manual to find the address of the corresponding register and light up the LED.

View schematic

Insert picture description here
Turn on the R (red) red light and pull the PB5 pin low.

Initialize the clock

First, find out which clock PB5 is hung under. STM32 system architecture diagram:
Insert picture description here
PB5 is GPIOB5. From the system architecture diagram, you can see that GPIOB is hung on the APB2 bus, so turn on the APB2 clock first. What is the address of APB2?

Insert picture description hereInsert picture description here

It can be seen that the offset address of the APB2 register is 0x18, the Bit3 bit is set to PB, 0 is disable, and 1 is enable; then what is the base address of the register?
Insert picture description here
Continue to find the manual APB2 belongs to RCC, so you need to find the register map in Chapter 3.3 as follows:
Insert picture description here
From the register table, you can see that the base address of the register RCC is 0x4002 1000.
So the address of APB2 of the register is: 0x40021000 + 0x18 = 0x40021018

GPIOx mode setting

Insert picture description here
The offset address of the GPIOB register address is 0x00, and the base address is found from Chapter 3.3 of the manual:
0x4001 0C00-0x4001 0FFF GPIO Port B
0x4001 0C00 + 0x00 (offset) = 0x4001 0C00
GPIOB_CRL register address is: 0x4001 0C00

It can be seen from the table that the registers of GPIOx_CRL (x=A...G), CNFy, MODEy (y=1...7) control a group of GPIOy pins every 4 bits. Because each group of GPIOx has 16 pins. 16*4=64, so the control register needs 64 bits. But one register is 32 bits, so two registers are needed. Therefore, it is divided into CRL to control 1-7 pins, and CRH to control 8-16 pins. Because PB5 is the fifth, the CRL register is selected.

MODEy sets the input and output mode,
the configuration of the GPIO rate CNFy in the input mode and the configuration in the output mode.
Here we configure PB0 as a general push-pull output, and the output speed is 10M

GPIOx pin level control

Insert picture description here
Register offset address 0x0C. , The base address is found from Chapter 3.3 of the manual to
0x4001 0C00-0x4001 0FFF GPIO Port B
0x4001 0C00 + 0x0C (offset) = 0x4001 0C0C
GPIOB_ODR register address is: 0x4001 0C0C

The lower 16 bits are valid, set the corresponding pin to 0 (pull low) or 1 (pull high).
PB5 pin needs to set bit5 to 0 to light up the LED

Program source code

#include "stm32f10x.h"
int main (void)
{
    
    
	// APB2时钟设置
	*( unsigned int * )0x40021018 |=  ( (1) << 3 );	
	//设置PB5输出模式,推挽输出
	*( unsigned int * )0x40010C00 |=  ( (1) << (4*5) );//blue
	// 控制引脚电平
	*( unsigned int * )0x40010C0C &= ~(1<<1);
}

Note: Address cannot be assigned directly, it needs to be converted into pointer form

Guess you like

Origin blog.csdn.net/WANGYONGZIXUE/article/details/115042428