STM32F103 Register-mode LED Streaming Light

1. Relevant Principles

(1) Principles of address mapping and register mapping for chips

1. What are registers?

  • Modern computers mainly include three levels of storage, register, memory and external memory, and the rate of data storage decreases in turn.
  • We might as well Abstract both registers and memory into a large array, where each element has a byte (8-bit) size and the CPU addressing is done in the smallest unit of that element. If the address of the previous element is 0x1FFFFFF0, the address of the next element is 0x1FFFFFF1. It can be understood that both registers and memory memory on the hardware component are linearly arranged with an 8-bit size component, and the address corresponds to the address of the element in the array mentioned above.
  • Registers are constructed inside the CPU and are used primarily for storing information.
  • Why do registers appear?
    We know that programs are loaded in memory and run by the CPU, whose primary responsibility is to process data. This process must involve reading and writing data from the memory, because it involves sending data requests through the control bus and entering the memory storage unit, obtaining data through the same channel. This process is cumbersome and involves a large amount of memory consumption, and there are some commonly used memory pages that are actually unnecessary. So there are registers that are stored inside the CPU.

2. Principles of address mapping and register mapping:

  • We know that memory itself has no address. The process of assigning addresses to memory is called memory mapping. What is register mapping? What exactly are registers?
  • When it comes to mapping, you might think of functional mapping, and you have a picture in your mind: an element in a set on the left "shoots" an arrow straight line to an element in a set on the right.
  • In fact, the memory mapping principle of peripheral devices is the same, except that the collective on the left becomes the CPU, and the collection on the right becomes the peripheral device. The arrow line is the address bus that connects the CPU to the pin on the peripheral address.
  • Memory itself does not have address information. Its address is assigned by the chip manufacturer or user. The process of assigning addresses to memory is called memory mapping.
  • You know, not all CPU address pins of a development board are connected to memory components. If there are peripherals on the board (such as a single graphics card), then the CPU needs to allocate some pins to connect with the address pins of the peripheral. This is equivalent to allocating a portion of memory addressing space to the peripheral. That is not equivalent to empty CPU allocating address addressing space to the peripheral?
  • This is not the case. In order to speed up processing, general peripherals have their own in-chip RAM (for example, video memory, which you also know is important to the performance of the graphics card), the allocated address space is physically linked to in-chip RAM, so that the CPU can access the in-chip RAM of the peripherals just like memory, which is also called memory mapping.
  • In the area units of memory, each unit corresponds to a different function, and when we control these units, we can drive the peripheral work. We can find the starting address of each cell, and then access these cells by means of C language pointer. If we access them by this way every time, not only bad memory but also error prone. Then we can alias this memory unit by its function according to the function of each cell. This alias is what we often refer to as a register. This process of aliasing memory units with specific functions that have been assigned addresses is called register mapping.

(2) Initialization setup steps for GPIO ports

1. Clock of single-chip computer

  • What is the clock?
    The clock of stm32 is the "frequency" generated by internal or external oscillators and is known as the "system clock". The maximum conversion period T is 72MHz: 1/72MHz_13.9ns.
  • Why use a clock?
    Because of the power consumption, stm32 is powerful and can do a lot of things, but at the same time, the more serious the consumption is. When stm32 does not introduce a clock, the peripherals are turned on as 51, so the corresponding power consumption is serious. To solve this problem, the manufacturer (st company) introduced the "clock concept", which is given to which peripheral clock (frequency) even if which peripheral is used. Turn off when not in use (without oscillation). This method greatly reduces power consumption and lasts a long time.
  • In 51 single-chip computer, a clock packs all the clocks, while the clock of stm32 has division of work, and the frequency of each type of clock is different, because it is not necessary that all clocks are the highest frequency, as long as enough, just like the size of water coming out of a door. If you only wash your face, but the water coming out like a flood, it is unnecessary and consumes more energy. So different clocks will have different frequencies, or clock crossover can be configured when configuring.

2. Introduction to GPIO

  • GPIO is short for Universal Input and Output Port. Simply speaking, it is the control pin of STM32. The GPIO pin of STM32 chip is connected with the external device to realize the functions of external communication, control and data collection. The GPIO of the STM32 chip is divided into groups, each with 16 pins.
  • For example, the STM32F103VET6 chip has five sets of GPIOA, GPIOB, GPIOC to GPIOE. The chip has a total of 100 pins, of which GPIO accounts for the majority. All GPIO pins have basic input and output functions.
  • The most basic output function is to control the pin output by STM32 to achieve switch control. If the GPIO pin is connected to the LED lamp, then the LED lamp can be controlled on and off, and the pin can be connected to the relay or triode, then the external high power circuit can be controlled through the relay or triode.

3. GPIO mode

  • There are eight main working modes of GPIO: four input modes and four output modes.
    They are input float, input pull-up, input drop-down, analog input.
    Output mode is open-drain output, open-drain multiplex output, push-pull output, push-pull multiplex output.

GPIO 8 working modes:
(1) GPIO_Mode_AIN analog input (use ADC analog input, or save power at low power consumption)
(2) GPIO_Mode_IN_FLOATING floating input (floating is floating in the air, can be pulled up or down by other objects, can be used for key input)
(3) GPIO_Mode_IPD drop-down input (IO internal drop-down resistance input)
(4) GPIO_Mode_IPU pull-up input (IO internal pull-up resistance input)
(5) GPIO_Mode_Out_OD open-leak output (open-leak output: output is equivalent to the collector of a triode. A pull-up resistance is required to obtain a high level state)
(6) GPIO_Mode_Out_PP push-pull output (push-pull is push-pull level is determined, do not need pull-up and pull-down, IO output 0-connect GND, IO output 1-connect VCC, read input value is unknown)
(7) GPIO_Mode_AF_OD multiplex open-leak output (on-chip and Out-of-chip capabilities (SCL,SDA for I2C)
(8) GPIO_Mode_AF_PP multiplex push-pull output (TX1,MOSI,MISO.SCK.SS)

4. Input and Output

  • Simply put: the output is controlled after the CPU is calculated, and the input is calculated after the CPU is read.
  • The input collects the data, the external circuit inputs the analog through the IO port, and then reads the data to the CPU through the TTL Schottky Trigger, which converts the relatively slow changing analog signal into a rectangular signal for later reading.
  • The output of GPIO is similar to that of IO port 51. Both of them output high and low levels to control the external circuit:
  • Processing: The CPU issues output high or low level instructions, directives to configure the Bit Settings/Cleanup Register (GPIOx_BSRR) (set to "1" high level, clear to "0" low level), then configures the output data register (GPIOx_ODR) by the bit register, passes through a selector (choose general output or multiplex function output), and then performs output control. Control what mode it is: push-pull, open-leak or close, then output high or low level to IO port.

5. GPIO Initialization Steps
Step 1: Enable the GPIOx port clock
Step 2: Indicate which GPIOx port is, the speed size and mode of this bit
Step 3: Initialize by calling the GPIOx initialization function
Step 4: Call the GPIO-SetBits function to place the corresponding bits

2. Connecting Circuits


3. Lighting LED Streaming Light

(1) C Language Implementation

1. New Project

Create a new project and click Project->New uVision Project.

Select the project path, name the file and save it.

Select an analog chip.

Choose nothing, OK directly.

Click Options for Target... and in the Output interface, check Create HEX File to generate the hex file.

2. Add Files

Right-click Source Group 1 and click Add New Item to Group'Source Group 1'/.

Select the file type, click C File (.c), enter the file name main, and click Add.

Copy the startup file to the project directory.

Copy to this.

Right-click Select.

Add startup file.

3. Write code

main.c code:

#define GPIOB_BASE 0x40010C00
#define GPIOC_BASE 0x40011000
#define GPIOA_BASE 0x40010800
#define RCC_APB2ENR (*(unsigned int *)0x40021018)
#define GPIOB_CRL (*(unsigned int *)0x40010C00)
#define GPIOC_CRH (*(unsigned int *)0x40011004)
#define GPIOA_CRL (*(unsigned int *)0x40010800)
#define GPIOB_ODR (*(unsigned int *)0x40010C0C)
#define GPIOC_ODR (*(unsigned int *)0x4001100C)
#define GPIOA_ODR (*(unsigned int *)0x4001080C)

void SystemInit(void);
void Delay_ms(volatile  unsigned  int);
void Delay_ms( volatile  unsigned  int  t)
{
     unsigned  int  i;
     while(t--)
         for (i=0;i<800;i++);
}
int main(){
	// Turn on the clock
	RCC_APB2ENR |= (1<<3); // Turn on GPIOB clock
	RCC_APB2ENR |= (1<<4); // Turn on GPIOC clock
	RCC_APB2ENR |= (1<<2); // Turn on GPIOA clock	
	// Set GPIO as push-pull output
	// Set the last four bits of GPIOB to 0001 (B0)
	GPIOB_CRL |= (1<<0);  // Last bit set to 1
	GPIOB_CRL &= ~(0xE);  // The last two, three, and four bits are set to zero
	// Set the first four bits of GPIOC to 0001 (C15)
	GPIOC_CRH |= (1<<28); // The fourth bit is set to 1
	GPIOC_CRH &= ~(0xE0000000);  // Top three set to 0
	// Set the last four bits of GPIOA to 0001 (A0)
	GPIOA_CRL |= (1<<0);  // Last bit set to 1
	GPIOA_CRL &= ~(0xE);  // The last two, three, and four bits are set to zero	
	// Initialize 3 LED s as not bright (i.e. high point)
	GPIOB_ODR |= (1<<0);  // Last bit set to 1
	GPIOC_ODR |= (1<<15); // Set the 15th last bit to 1
	GPIOA_ODR |= (1<<0);  // Last bit set to 1	
	while(1){
		GPIOB_ODR &= ~(1<<0); // Lighting 1
		Delay_ms(1000000);
		GPIOB_ODR |= (1<<0);  // Light out 1
		Delay_ms(1000000);		
		GPIOC_ODR &= ~(1<<15); // Lighting 2
		Delay_ms(1000000);
		GPIOC_ODR |= (1<<15);  // Light out 2
		Delay_ms(1000000);		
		GPIOA_ODR &= ~(1<<0); // Lighting 3
		Delay_ms(1000000);
		GPIOA_ODR |= (1<<0);  // Light out 3
		Delay_ms(1000000);		
	}	
}
void SystemInit(){	
}

Compile.

4. Light up

Use FlyMcu or Mcuisp as shown.


Success.
Click to view video effects

(2) Implementation of assembly language

Steps and C language implementation are no different.
Code:

RCC_APB2ENR EQU 0x40021018;To configure RCC register,Clock,0x40021018 For clock address

GPIOB_BASE EQU 0x40010C00
GPIOC_BASE EQU 0x40011000
GPIOA_BASE EQU 0x40010800

GPIOB_CRL EQU 0x40010C00
GPIOC_CRH EQU 0x40011004
GPIOA_CRL EQU 0x40010800

GPIOB_ODR EQU 0x40010C0C
GPIOC_ODR EQU 0x4001100C
GPIOA_ODR EQU 0x4001080C

Stack_Size EQU  0x00000400;Stack size

                AREA    STACK, NOINIT, READWRITE, ALIGN=3 ;NOINIT:  = NO Init,Not initialized. READWRITE : Readable and writable. ALIGN =3 : 2^3 Alignment, that is, 8-byte alignment.
Stack_Mem       SPACE   Stack_Size
__initial_sp




                AREA    RESET, DATA, READONLY

__Vectors       DCD     __initial_sp               ; Top of Stack
                DCD     Reset_Handler              ; Reset Handler
                    
                    
                AREA    |.text|, CODE, READONLY
                    
                THUMB
                REQUIRE8
                PRESERVE8
                    
                ENTRY
Reset_Handler 
bl LED_Init;bl: A jump instruction with a link. Current address when jumping with this command(PC)Will be automatically fed in LR register
MainLoop        BL LED_ON_C
                BL Delay
                BL LED_OFF_C
                BL Delay
BL LED_ON_A
                BL Delay
                BL LED_OFF_A
                BL Delay
BL LED_ON_B
                BL Delay
                BL LED_OFF_B
                BL Delay
                
                B MainLoop;B:Jump unconditionally.
LED_Init;LED Initialization
                PUSH {R0,R1, LR};R0,R1,LR Values in put on the stack
                ;Control Clock
                LDR R0,=RCC_APB2ENR;LDR Is to load an address into a register(such as R0). 
                ORR R0,R0,#0x1c
                LDR R1,=RCC_APB2ENR
                STR R0,[R1]


                ;Initialization GPIOA_CRL
                LDR R0,=GPIOA_CRL
                BIC R0,R0,#0x0fffffff;BIC first reverses the immediate number, then bitwise AND
                LDR R1,=GPIOA_CRL
                STR R0,[R1]

                LDR R0,=GPIOA_CRL
                ORR R0,#0x00000001
                LDR R1,=GPIOA_CRL
                STR R0,[R1]
                ;take PA0 Set 1
                MOV R0,#0x01
                LDR R1,=GPIOA_ORD
                STR R0,[R1]


                ;Initialization GPIOB_CRL
                LDR R0,=GPIOB_CRL
                BIC R0,R0,#0x0fffffff;BIC first reverses the immediate number, then bitwise AND
                LDR R1,=GPIOB_CRL
                STR R0,[R1]

                LDR R0,=GPIOB_CRL
                ORR R0,#0x00000001
                LDR R1,=GPIOB_CRL
                STR R0,[R1]
                ;take PB0 Set 1
                MOV R0,#0x01
                LDR R1,=GPIOA_ORD
                STR R0,[R1]


 ;Initialization GPIOC
                LDR R0,=GPIOC_CRH
                BIC R0,R0,#0x0fffffff
                LDR R1,=GPIOC_CRH
                STR R0,[R1]

                LDR R0,=GPIOC_CRH
                ORR R0,#0x01000000
                LDR R1,=GPIOC_CRH
                STR R0,[R1]
                ;take PC15 Set 1
                MOV R0,#0x8000
                LDR R1,=GPIOC_ORD
                STR R0,[R1]
             
                POP {R0,R1,PC};Pre-existing in stack R0,R1,LR Value returned to R0,R1,PC
LED_ON_A
                PUSH {R0,R1, LR}    
                
                MOV R0,#0x00
                LDR R1,=GPIOA_ORD 
                STR R0,[R1]
             
                POP {R0,R1,PC}
             
LED_OFF_A
                PUSH {R0,R1, LR}    
                
                MOV R0,#0x01
                LDR R1,=GPIOA_ORD 
                STR R0,[R1]
             
                POP {R0,R1,PC}  
LED_ON_B;Light on
                PUSH {R0,R1, LR}    
                
                MOV R0,#0x00
                LDR R1,=GPIOB_ORD
                STR R0,[R1]
             
                POP {R0,R1,PC}
             
LED_OFF_B;Turn off the light
                PUSH {R0,R1, LR}    
                
                MOV R0,#0x01
                LDR R1,=GPIOB_ORD
                STR R0,[R1]
             
                POP {R0,R1,PC}  
LED_ON_C;Light on
                PUSH {R0,R1, LR}    
                
                MOV R0,#0x00
                LDR R1,=GPIOC_ORD
                STR R0,[R1]
             
                POP {R0,R1,PC}
             
LED_OFF_C;Turn off the light
                PUSH {R0,R1, LR}    
                
                MOV R0,#0x0100
                LDR R1,=GPIOC_ORD
                STR R0,[R1]
             
                POP {R0,R1,PC}             
             
Delay
                PUSH {R0,R1, LR}
                
                MOVS R0,#0
                MOVS R1,#0
                MOVS R2,#0
                
DelayLoop0        
                ADDS R0,R0,#1

                CMP R0,#330
                BCC DelayLoop0
                
                MOVS R0,#0
                ADDS R1,R1,#1
                CMP R1,#330
                BCC DelayLoop0

                MOVS R0,#0
                MOVS R1,#0
                ADDS R2,R2,#1
                CMP R2,#15
                BCC DelayLoop0
                                
                POP {R0,R1,PC}    
                NOP
				END

4. Summary

The programming part of the experiment is not difficult, but the practice is very testing the hands-on ability, encountering too many small problems. Patience and care are needed to make this experiment because of faulty connections, wrong settings, etc.

Reference

Detailed principles of register mapping, initial setup steps for GPIO ports
STM32 F103 Lighting LED Streaming Light (STM32 Getting Started)

Tags: stm32

Posted on Tue, 19 Oct 2021 12:00:22 -0400 by cluce