The register of 32 turns on the water flow lamp

Content of this article: This article mainly introduces the address configuration mode of the register and the code calling the register to light a string of water lamps.

catalogue

1, What is a register

2, GPIO

(1) Introduction

(2) Working mode

(3) Addressing of GPIO

3, Light up

(1) Assembly lighting

(2) C language lighting

  (3) Library function to realize water lamp

4, Summary

5, Reference articles  

1, What is a register

In official terms, registers are some small storage areas used to store data in the CPU, which are used to temporarily store the data and operation results involved in the operation.

In short, if our computer is compared to a building, and the register is every house in the building, the register address can be regarded as the house number, but the house number is a little special, which is composed of 01 bit stream.

2, GPIO

(1) Introduction

GPIO It is the abbreviation of general input and output port. In short, it is STM32 Controllable pin, STM32 chip
of GPIO The pin is connected with external equipment to realize the functions of external communication, control and data acquisition.
be-all GPIO All pins have basic input and output functions.

(2) Working mode

 typedef enum
 {
    GPIO_Mode_AIN = 0x0, // Analog input
    GPIO_Mode_IN_FLOATING = 0x04, // Floating input
    GPIO_Mode_IPD = 0x28, // Drop down input
    GPIO_Mode_IPU = 0x48, // Pull up input
    GPIO_Mode_Out_OD = 0x14, // Open drain output
    GPIO_Mode_Out_PP = 0x10, // Push pull output
    GPIO_Mode_AF_OD = 0x1C, // Multiplexed open drain output
    GPIO_Mode_AF_PP = 0x18 // Multiplexed push-pull output
 } GPIOMode_TypeDef;
When we turn on the LED, we need to set it to push-pull output mode.

(3) Addressing of GPIO

  For details, please move to the big man and speak clearly:

STM32 register introduction, address search, and direct operation register_ Geekyatao CSDN blog

3, Light up

(1) Assembly lighting

The following is the assembler for lighting the LED of PB5 connection:

Note: when selecting the operating environment, you do not need to check "Startup" and "CORE".

LED0 EQU 0x42218194
RCC_APB2ENR EQU 0x40021018
;GPIOA_CRH EQU 0x40010804
GPIOB_CRL EQU 0x40010C00

Stack_Size      EQU     0x00000400

                AREA    STACK, NOINIT, READWRITE, ALIGN=3
Stack_Mem       SPACE   Stack_Size
__initial_sp

                AREA    RESET, DATA, READONLY

__Vectors       DCD     __initial_sp               
                DCD     Reset_Handler              
                    
                    
                AREA    |.text|, CODE, READONLY
                    
                THUMB
                REQUIRE8
                PRESERVE8
                    
                ENTRY
Reset_Handler 
                BL LED_Init
MainLoop        BL LED_ON
                BL Delay
                BL LED_OFF
                BL Delay
                
                B MainLoop
             
LED_Init
                PUSH {R0,R1, LR}
                
                LDR R0,=RCC_APB2ENR
                ORR R0,R0,#0x08		
                LDR R1,=RCC_APB2ENR
                STR R0,[R1]
                
                LDR R0,=GPIOB_CRL
                BIC R0,R0,#0XFF0FFFFF	
                LDR R1,=GPIOB_CRL
                STR R0,[R1]
                
                LDR R0,=GPIOB_CRL
                ORR R0,R0,#0X00300000
                LDR R1,=GPIOB_CRL
                STR R0,[R1]
                
                MOV R0,#1 
                LDR R1,=LED0
                STR R0,[R1]
             
                POP {R0,R1,PC}

             
LED_ON
                PUSH {R0,R1, LR}    
                
                MOV R0,#0 
                LDR R1,=LED0
                STR R0,[R1]
             
                POP {R0,R1,PC}
             
LED_OFF
                PUSH {R0,R1, LR}    
                
                MOV R0,#1 
                LDR R1,=LED0
                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}    

                END

Next, use mcuisp to burn the program

Burning succeeded

Here are the connections:  

Connect one end of LED to PB5 and one end to 3.3V

Note: it's best not to connect 5V. There is no resistance protection. The author has burned several

Here I use the STM32F103RC small blackboard I bought when preparing for the video game

The effect is shown in the figure:

 

(2) C language lighting

I changed it with the lighting program of punctual atom. Because there are too many documents, only some main documents are listed below

The code is as follows:

test.c

#include "sys.h"
#include "usart.h"		
#include "delay.h"	
#include "led.h" 


int main(void)
{		 
	Stm32_Clock_Init(9); 	//System clock setting
	delay_init(72);	     	//Delay initialization
	LED_Init();		  	 	//LED initialization  
	while(1)
	{
		LED0=0;
		delay_ms(1000);    //Delay 1s
		LED0=1;
		delay_ms(1000);
	}	 
}

 led.h

#ifndef __LED_H
#define __LED_H	 
#include "sys.h"

#define LED0 PAout(8)	// PA8	

void LED_Init(void);		 				    
#endif

 led.c

#include "sys.h"   
#include "led.h"    
//LED IO initialization
void LED_Init(void)
{
	RCC->APB2ENR|=1<<2;    //Enable PORTA clock	   	    	 
	GPIOA->CRH&=0XFFFFFFF0; 
	GPIOA->CRH|=0X00000003;//PA8 push pull output	 
    GPIOA->ODR|=1<<8;      //PA8 output high
}

Both ends of the LED are connected to PA8 and 3.3V respectively

The effect is shown in the figure:

  (3) Library function to realize water lamp

Compared with the register code, the library function is more readable and more convenient to use. This is also the way the author has been using before. Let's write a flow lamp with the library function

The code is as follows:

main.c

#include "led.h"
#include "delay.h"
#include "sys.h"
 int main(void)
 {	
	delay_init();	    	 //Delay function initialization  
	LED_Init();		  	//LED initialization
	while(1)
	{
		LED0=0;
		delay_ms(1000);	 //Delay 1s
		LED0=1;
		delay_ms(1000);	
		LED1=0;
		delay_ms(1000);	 
		LED1=1;
		delay_ms(1000);	
		LED2=0;
		delay_ms(1000);	 
		LED2=1;
		delay_ms(1000);	
	}
 }

led.h

#ifndef __LED_H
#define __LED_H	 
#include "sys.h"

#define LED0 PAout(6)	// PA6	
#define LED1 PBout(7)	// PA7
#define LED2 PBout(8)	// PA8

void LED_Init(void);		 				    
#endif

led.c

#include "led.h"     
//LED IO³õʼ»¯
void LED_Init(void)
{
 
 GPIO_InitTypeDef  GPIO_InitStructure;
 	
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);	 //Enable PB port clock
	
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;				 //PB6,PB7,PB8 port configuration
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //Push pull output
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 //The IO speed is 50MHz
 GPIO_Init(GPIOB, &GPIO_InitStructure);					 //Initialize GPIOB according to the setting function
 GPIO_SetBits(GPIOB,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8);		 //PB6,PB7,PB8 output high

}

Three LEDs are used here. The long pin of each LED is connected to 3.3V and the short pin is connected to pb6, 7 and 8 respectively

The effect of hardware is as follows:

 

4, Summary

This experiment mainly carried out the LED register lighting experiment. Before, it had been written with library functions and had not used registers. This time, I really felt its cumbersome use, especially the GPIO address. I also needed to consult the manual, which was very poor in readability and encountered many difficulties in the programming process. However, I also learned the addressing principle of registers by consulting materials, Although cumbersome, it feels good to communicate with the computer more intuitively.

5, Reference articles  

  STM32 assembler and lighting experiment_ Lxy1360832244 blog - CSDN blog

STM32 register introduction, address search, and direct operation register_ Geekyatao CSDN blog 

Tags: stm32

Posted on Thu, 14 Oct 2021 23:14:35 -0400 by supergrame