Use the STM32 minimum system board register to complete the water flow lamp

1, STM32F103 series chip address mapping and register mapping principle and GPIO port initialization setting
2, STM lights up the water flow lamp
3, Summary

1, STM32F103 series chip address mapping and register mapping principle and GPIO port initialization setting

1. Introduction to STM32F103 series chips
STM32 is a microcontroller with various common communication interfaces, such as USART,12C,SPI, etc. it can connect many sensors and control many devices. F stands for basic type.

  • Pin classification pin description
    Power supply: (VBAT), (VDD VSS), (VDDA VSSA), (VREF+ VREF -), etc
    Crystal oscillator IO: main crystal oscillator IO, RTC crystal oscillator IO
    Download IO: io for JTAG Download: JTMS, JTCK, JTDI, JTDO, NJTRST
    BOOT IO: BOOT0, BOOT1, used to set the system startup mode
    Reset IO NRST for external reset

    2. STM32 peripheral address mapping
    On chip peripherals are divided into three buses. APB1 mounts vulgar peripherals, APB2 and AHB mount high-speed peripherals, in which APB2 is the base address of peripherals.
  • Bus base address
  • Peripheral base address
    Take GPIO high-speed peripherals as an example and mount them on APB2 bus

    3. Register mapping
    The memory itself has no address. We call the process of assigning an address to the memory memory memory mapping. In the memory, four bytes are a unit, a total of 32bit. According to the different functions of each unit, we alias the memory unit in the name of function. This alias is what we often call the register. The process of aliasing the memory unit with specific function that has been assigned an address is called register mapping.
  • Access memory cells by absolute address
1 //All GPIOB ports output high level
2 *(unsighed int*)(0x4001 0C0C)=0xFFFF;
  • Access memory cells by register alias
1 // All GPIOB ports output high level
2 #define GPIOB_ODR (unsighed int*)(GPIOB_BASE+0x0C);  
  • Access memory cells by register alias
1 //All GPIOB ports output high level
2 #define GPIOB_ODR *(unsighed int*)(GPIOB_BASE+0x0C)

4. GPIO port initialization settings
GPIO is the abbreviation of general input and output port. In short, it is the controllable pin of STM32. The most basic output function is that the STM32 control pin outputs high and low levels to realize switch control. For example, if the GPIO pin is connected to the LED lamp, the LED lamp can be controlled to turn on and off. The most basic input function is to detect the external input level. For example, connect the GPIO pin to the key, and distinguish whether the key is pressed by the level.

On page 28 of STM32 Chinese reference manual _V10, there are address ranges of different registers

  • Configure clock enable
    Why configure the clock? In order to save power, the default clock is off. Before configuring any resources of STM32, the clock must be enabled first.
    The name of clock control is RCC, which belongs to AHB bus. GPIOB belongs to APB2.

    As we know, the address of GPIO port B starts from 0x4001 0C00. Next, just look for the address of the clock enable register:
    The address of reset and clock control RCC starts from 0x4002 1000
    See manual RCC_APB2ENR, bit 3 is IOPBEN, and the name is IO port B clock enable, which is what we want. RCC_ Bit 3 of apb2enr is assigned to 1 to turn on the GPIOB clock
  • Configure general output
    The control LED needs to output high level or low level, so it needs to be configured as output. In STM32, use the port configuration low register (GPIOx_CRL) to configure pins Px0-Px7, and use the port configuration high register (GPIOx_CRH) to configure pins Px8-Px15
    What we need is the output high and low level, so we need to set it to output. There are several output modes:

    Push pull output: it can output high and low levels and connect digital devices; Push-pull structure generally means that two triodes are controlled by two complementary signals respectively, and one triode is always on when the other is off
    Open drain requires an external pull-up resistor to output high level, which is not suitable here. Therefore, it needs to be set as push-pull output

2, Turn on the running water lamp

1. Implemented with C language code

  • New project
    Open keil and click Project to create a new Project

    Select STM32F103C8X chip

    Check CORE and Startup under CMSIS and Device respectively
  • Click the output part of the magic wand and check create hex file;
  • Click the Debug option, check use stimulator and run to main, and set the Dialog DLL and Parameter as follows
  • Edit the LED.c program and compile the program
//--------------APB2 enable clock register------------------------
#define RCC_AP2ENR	*((unsigned volatile int*)0x40021018)
	//----------------GPIOA configuration register------------------------
#define GPIOA_CRL	*((unsigned volatile int*)0x40010800)
#define	GPIOA_ORD	*((unsigned volatile int*)0x4001080C)
//----------------GPIOB configuration register------------------------
#define GPIOB_CRH	*((unsigned volatile int*)0x40010C04)
#define	GPIOB_ORD	*((unsigned volatile int*)0x40010C0C)
//----------------GPIOC configuration register------------------------
#define GPIOC_CRH	*((unsigned volatile int*)0x40011004)
#define	GPIOC_ORD	*((unsigned volatile int*)0x4001100C)
//-------------------Simple delay function-----------------------
void  Delay_ms( volatile  unsigned  int  t)
     unsigned  int  i;
         for (i=0;i<800;i++);
void A_LED_LIGHT(){
	GPIOA_ORD=0x0<<7;		//PA7 low level
	GPIOB_ORD=0x1<<9;		//PB9 high level
	GPIOC_ORD=0x1<<15;		//PC15 high level
void B_LED_LIGHT(){
	GPIOA_ORD=0x1<<7;		//PA7 high level
	GPIOB_ORD=0x0<<9;		//PB9 low level
	GPIOC_ORD=0x1<<15;		//PC15 high level
void C_LED_LIGHT(){
	GPIOA_ORD=0x1<<7;		//PA7 high level
	GPIOB_ORD=0x1<<9;		//PB9 high level
	GPIOC_ORD=0x0<<15;		//PC15 low level	
//------------------------Main function--------------------------
int main()
	int j=100;
	RCC_AP2ENR|=1<<2;			//APB2-GPIOA peripheral clock enable
	RCC_AP2ENR|=1<<3;			//APB2-GPIOB peripheral clock enable	
	RCC_AP2ENR|=1<<4;			//APB2-GPIOC peripheral clock enable
	//These two lines of code can be combined into RCC_ APB2ENR|=1<<3|1<<4;
	GPIOA_CRL&=0x0FFFFFFF;		//Set bit reset	
	GPIOA_CRL|=0x20000000;		//PA7 push pull output
	GPIOA_ORD|=1<<7;			//Set PA7 initial light to off
	GPIOB_CRH&=0xFFFFFF0F;		//Set bit reset	
	GPIOB_CRH|=0x00000020;		//PB9 push pull output
	GPIOB_ORD|=1<<9;			//Set the initial light to off
	GPIOC_CRH&=0x0FFFFFFF;		//Set bit reset
	GPIOC_CRH|=0x30000000;   	//PC15 push pull output
	GPIOC_ORD|=0x1<<15;			//Set the initial light to off	

  • Compiler
  • Simulation debugging
    Click start of Debug to enter the debugging interface, and click logical analyze of systerm analyze

    Click setup in the upper left corner, add pin GPIOX, set Display Type to Bit, set color, and select close

    Click Run in the upper left corner to view the simulation waveform

    As shown in the figure, the simulation waveform shows that the simulation has been successful
    2. Hardware wiring and installation of relevant download programs and drivers
  • Connection and selection of ports and pins
    The USB selected interfaces are GND, RXD, TXD and 3V3
    STM32 core board select port
    Corresponding connection


The selected PA5, PB7 and PC15 shall be connected with the corresponding led lamp.
Jumper shall be used for the minimum core board and BOOT mode switch. Set boot0 to 1 and boot1 to 0, as shown in the figure:

  • Install serial driver and download program
    Install ch340 driver serial driver
    Right click my computer and click device manager

    Click port to check whether the port is displayed (this step requires USB to be inserted into the computer)
  • Open mcuisp (this software is installed wirelessly and can be downloaded directly)
    mcuisp software download link extraction code h2xc
    Click search serial port to automatically search serial port, set bps to 256000, add hex file, set low level reset of DTR at the bottom, and level RTS high point into BootLoader

Click start programming and press the reset key on the core board at the same time

  • Water lamp effect

    STMLED flashing

3, Summary

Tags: C stm32 gpio

Posted on Sat, 23 Oct 2021 03:03:23 -0400 by helloworld