This post was last edited by lvxinn2006 on 2019-1-11 08:53 [Purpose of the experiment]
- Be familiar with how to analyze circuit principles through schematic diagrams;
- Be familiar with how to use the user reference manual of the MCU;
- Master the use of GPIO output functions through this experiment;
- Begin to be familiar with the configuration method of the microcontroller registers;
- Light up the LED with the least code.
[Experimental environment]
- NUCLEO-G071RB development board
- Keil MDK-ARM (Keil uVision 5.25.2.0)
- Keil.STM32G0xx_DFP.1.0.0.pack
[Experimental materials]
- NUCLEO-G071RB development board schematic diagram
- STM32G071x8/xB Data Sheet
- STM32G071 chip user reference manual
[Experimental phenomenon]
[Schematic diagram analysis]
- Open the schematic diagram and find the location of LD4;
- Analysis principle, LD4 anode is connected to 3.3V positive voltage, cathode is connected to drain of N-channel field effect tube T2, source of field effect tube T2 is connected to power ground;
- Conditions for lighting LD4: When the field effect tube is turned on, the cathode of LD4 is directly connected to the ground, generating current, and LD4 can be lit;
- The gate of the field effect tube is pulled down to the ground through a 1M resistor. When a voltage difference is generated between Vds, the field effect tube can be turned on, that is, when the gate is connected to a high level (3.3V), Vds=-3.3V, the field effect tube can be turned on, and then LD4 can be lit.
- As can be seen from the schematic diagram, the gate of the field effect tube is connected to the PA5 pin, so you only need to make PA5 output a high level to light up LD4.
- Next, configure the PA5 pin according to the chip reference manual.
[MCU resource analysis and pin configuration]
- Check the "STM32G071 Chip User Reference Manual", and you can see the following paragraph in the "Memory and Bus Structure" chapter:
According to the prompts in the paragraph, you can know that if you want to use the GPIO function unit, you must enable the corresponding clock.
Here we can see that the RCC_IOPENR register can control the clock of the GPIO function, and we can know that the clock of GPIOA is at the [0] position of the register, and we only need to set the [0] position to 1
- By looking at the Data Sheet of the STM32G071 chip, we can know that in this series of processors, there are 5 groups of GPIOs, namely PA, PB, PC, PD, and PF
- For the 64-pin chips of the R series, there are up to 60 pins used as GPIOs
- As can be seen in the figure above, each GPIO group has many pins. The available GPIOs of the 64-pin chip are PA0...PA15, PB0...PB15, PC0...PC15, PD0...PD9, PF0...1, a total of 60 pins
- By consulting the data sheet, we can find out that each GPIO group in the chip corresponds to several registers with the same function. The specific functions of the registers are shown in the following table:
Register classification | Specific register | Function description |
Configuration register | GPIOx_MODER | Set input/output mode |
GPIOx_OTYPER | Set output type (push-pull, open-drain) |
GPIOx_OSPEEDR | Set output speed |
GPIOx_PUPDR | Set internal pull-up and pull-down resistors |
Data register | GPIOx_IDR | Input data |
GPIOx_ODR | Output data |
Set reset register | GPIOx_BSRR | Set, clear data |
Lock register | GPIOx_LCKR | Lock pin data status |
Multiplex function selection register | GPIOx_AFRH | Multiplex function high data |
GPIOx_AFRL | Multiplex function low data |
Each register has its own function. For the specific definition of registers, please refer to the chapter on GPIO in the "STM32G071 Chip User Reference Manual".
- The principle of this experiment is relatively simple. Only two registers, MODER and ODR, are used, so we only study the use of these two registers
- First look at GPIOx_MODER:
The MODER register is mainly used to set the function of each IO pin in a GPIO group. Every 2 data bits in the register determine the function of a pin, for example: [1:0] bits control the function of the PA0 pin, [3:2] bits control the function of the PA1 pin...
According to the needs of the experiment, we want to use PA5 to output a high level to control LD4, so we need to set the corresponding position of PA5, that is, [11:10] bits.
In the description, we can see that different combinations of two binary bits can determine the function of the pin. 00 indicates input mode, 01 indicates output mode, 10 indicates multiplexed function mode, and 11 is analog mode.
Because we want to use PA5 to output a level signal to control the circuit, we need to set PA5 to output mode, that is, set the two bits [11:10] to 01.
- Let's take a look at GPIOx_ODR:
When the GPIO pin is set to output mode, the value of the corresponding data bit of the ODR register will determine the output level of the pin, 1 for high level, 0 for low level.
According to the experimental requirements, PA5 needs to output a high level, so it is necessary to set the [5] bit in the GPIOA_ODR register to 1.
[Analysis and Summary] All the above key points are organized into a mind map as follows:
The next thing to do is to express these details in program code. 【Experimental code】
- Open Keil and create a new project
Create a main.c file
- #include "stm32g0xx.h" // Device header int main(void) { //Enable GPIOA clock RCC->IOPENR |= (1<<0); //Set PA5 to output mode GPIOA->MODER &= ~(0x3<<10); //[11:10] clear 0 GPIOA->MODER |= (1<<10); //[11:10]=01 Set PA5 to output mode //Light up LD4 GPIOA->ODR |= (1<<5); //[5]=1 PA5 outputs high level}
复制代码
- Press the reset button, run the program, and view the experimental results.
This content is created by EEWORLD forum user lvxinn2006. If you need to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source