Two, memory and registers

One, what is memory mapping

The memory itself does not have address information. Its address is assigned by the chip manufacturer or user. The process of assigning an address to the memory is called memory mapping. If another address is assigned, it is called remapping (refer to the chip data manual and Chinese reference manual for specific address assignment) ) 

2. What is register and register map

By giving an alias to a memory unit with a specific function, this alias is what we call a register. The process of aliasing memory cells with specific functions that have been allocated addresses is called register mapping.

Three, how to access the STM32 register content

We know that registers are memory units with specific functions, so to access STM32 registers is to operate STM32 memory units. According to the characteristics of C language pointers, pointers can be used to operate STM32 memory units.
If we want to make the 0th pin of STM32 GPIOC output low level, how do we use C language to deal with it?
First, we need to know which bus the GPIOC is connected to, and we need to know its address. The STM32 bus address is as follows:
 

Bus name Bus base address Offset relative to the peripheral base address
APB1 0x4000 0000 0x0
APB2 0x4001 0000 0x0001 0000
AHB 0x4001 8000 0x0001 8000 
Peripheral name Peripheral base address Relative address offset of APB2 bus
GPIOA 0x4001 0800 0x0000 0800
GPIOB 0x4001 0C00 0x0000 0C00
GPIOC 0x4001 1000 0x0000 1000
GPIOD 0x4001 1400 0x0000 1400
GPIOE 0x4001 1800 0x0000 1800
GPIOF 0x4001 1C00 0x0000 1C00
GPIOG 0x4001 2000 0x0000 2000

So use the C language macro to define the peripheral base address: #define PERIPH_BASE ((unsigned int)0x40000000)

Then define the APB2 bus base address: #define APB2PERIPH_BASE (PERIPH_BASE+0x00010000)

Then you also need to know the port peripheral address, and also use the C language macro to define its port address: #define GPIOC_BASE (APB2PEARIPH_BASE+0x1000)

After having the peripheral port address, you also need to know the register address of the GPIOC port, as shown in the following table:

Register name Register address Offset relative to GPIOC base address
GPIOC_CRL 0x4001 1000 0x00
GPIOC_CRH 0x4001 1004 0x04
GPIOC_IDR 0x4001 1008 0x08
GPIOC_ODR 0x4001 100C 0x0C
GPIOC_BSRR 0x4001 1010 0x10
GPIOC_BRR 0x4001 1014 0x14
GPIOC_LCKR 0x4001 1018 0x18

Also use C language macro to define its port register address:

#define GPIOC_CRL *(unsigned int*)(GPIOC_BASE+0x00)
#define GPIOC_CRH *(unsigned int*)(GPIOC_BASE+0x04)
#define GPIOC_IDR *(unsigned int*)(GPIOC_BASE+0x08)
#define GPIOC_ODR *(unsigned int*)(GPIOC_BASE+0x0C)
#define GPIOC_BSRR *(unsigned int*)(GPIOC_BASE+0x10)
#define GPIOC_BRR *(unsigned int*)(GPIOC_BASE+0x14)
#define GPIOC_LCKR *(unsigned int*)(GPIOC_BASE+0x18)

In the compiler, (GPIOC_BASE+0x00) is considered to be an immediate number, so it must be forced to be converted to an address, and the number inside must be manipulated, so *

(4) Register template creation

1. Create two folders Obj and User

0bj folder: used to store the compiled c/assembly/link list, debugging information, hex file, preview information, package library and other files.
User folder: used to store user-written main.c, STM32F1 startup files, and stm32f10x.h header files. /2, /2

2. New construction

Open μVision Keil5 software, click Project->New uVision Project... to create a new project, select the chip STM32103ZE

Click OK, the interface that pops up behind is the online software package upgrade interface, don’t worry, just close it, the project directory will appear

3. Add files

Double-click the project directory

 Select the file type as *.*

The Object files and Listings files in the project directory are used by Keil5 to generate intermediate files. The header files and startup files are added to the project

4. Configure the magic wand tab

Click on the magic wand, check Use MicroLIB, this library needs to be used for printf output

 Generate a hex file, the hex file is downloaded into the MCU for use

You can choose the generation path of the Object file by yourself

You can also choose the generation path of the Listings file yourself

How to set up the ARM emulator, also need to set up the emulator

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_40836442/article/details/109508681