After learning how to build a project, you must be eager to use your own development board to show your skills. Don't worry, take your time. When learning C language, the first thing you compile must be the eternal classic code, yes, that is Hello World - simple, clear, and you can see the phenomenon intuitively. There is also a simple, clear, and intuitive program on STM32 - light up. This is our current hello world, let's start learning from it!!!
study
What we need to use to light up the lamp is to control the I/O port we need, so let's take a look at the GPIO port of STM32F first. Each GPIO port in the STM32F0 series microcontroller has: two 32-bit configuration registers (GPIOx_OTYPER and GPIOx_MODER), two 32-bit data registers (GPIOx_IDR and GPIOx_ODR), a 32-bit position/reset register (GPIOx_BSRR), a 16-bit reset register (GPIOx_BRR) and a 32-bit lock register (GPIOx_LCKR). According to the specific hardware characteristics of each I/O port listed in the data sheet, each bit of the GPIO port can be configured by software into: input floating, input pull-up, input pull-down, analog input, open-drain output, push-pull output, push-pull multiplexing and open-drain multiplexing functions. See Table for details.
Each I/O port can be freely programmed, however the I/O port registers must be accessed as 32-bit bytes. The GPIOx_BSRR and GPIOx_BRR registers allow independent access to read/write any GPIO register;
Therefore, we can program the register to generate square waves at the I/O port to control the light on and off. But first we need to find out which I/O port the LED on the development board is on: Find the schematic diagram of the development board and you will find
The LED is connected to the PA5 port and lights up at a high level. (Since the boards are different, you can find out which port your LED is connected to by yourself. If you can't find it, you can connect it externally.)
Now let's start programming our development board to light up the lights.
Create a project according to yesterday's project, and continue to add gpio.c and rcc.c in the Lib group.
Come to main.c to write the program.
program
1. Include header files
#include "stm32f0xx.h"
2. Delay function
void Delay(volatile unsigned int ncount)
{
while(ncount--);
}
3. Define the GPIO initialization structure
GPIO_InitTypeDef GPIO_InitStructure;
4. Enter the main function
int main(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //enable the clock
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_P;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
while (1)
{
GPIO_SetBits(GPIOA,GPIO_Pin_5);
Delay(0xfffff);
GPIO_ResetBits(GPIOA,GPIO_Pin_5);
Delay(0xfffff);
}
}
Next, download the program as written yesterday to the development board. When you see the LED light start to flash, it's done. Is everything done? It's still early. We are just beginning. Let's start analyzing why it is written this way. Knowing the result is not only about knowing the result, but also knowing the reason.
analyze
The delay function is fine (but pay attention to the use of volatile. It doesn't matter in KEIL, but if you use GCC for programming, the non-volatile parts will be optimized away, making the delay function meaningless)
So what does the following GPIO_InitTypeDef GPIO_InitStructure; mean?
There is also the following code in GPIO.h
typedef struct
{
uint32_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured.
This parameter can be any value of @ref GPIO_pins_define */
GPIOMode_TypeDef GPIO_Mode; /*!< Specifies the operating mode for the selected pins.
This parameter can be a value of @ref GPIOMode_TypeDef */
GPIOSpeed_TypeDef GPIO_Speed; /*!< Specifies the speed for the selected pins.
This parameter can be a value of @ref GPIOSpeed_TypeDef */
GPIOOType_TypeDef GPIO_OType; /*!< Specifies the operating output type for the selected pins.
This parameter can be a value of @ref GPIOOType_TypeDef */
GPIOPuPd_TypeDef GPIO_PuPd; /*!< Specifies the operating Pull-up/Pull down for the selected pins.
This parameter can be a value of @ref GPIOPuPd_TypeDef */
}GPIO_InitTypeDef;
The two pieces of code together are what we are looking for. First, GPIO_InitTypeDef is defined, and then GPIO_InitStructure is defined based on this structure. This structure will be used in the subsequent GPIO settings.
What is the first sentence in the main function RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);? I am configuring GPIO, so what is this?
In STM32, all clocks are off by default after power-on and need to be turned on manually, so this step is to turn on the clock first and control the clock of GPIOPA on AHB in the M0 core.
So to turn on the clock on PA5, you must enable AHB, in the function
void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_RCC_AHB_PERIPH(RCC_AHBPeriph));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
RCC->AHBENR |= RCC_AHBPeriph;
}
else
{
RCC->AHBENR &= ~RCC_AHBPeriph;
}
}
The variable uint32_t RCC_AHBPeriph is defined in the library as follows #define RCC_AHBPeriph_GPIOA RCC_AHBENR_GPIOAEN #define RCC_AHBENR_GPIOAEN ((uint32_t)0x00020000) The bitwise OR of 0x00020000 and the register is assigned the value that corresponds to GPIOA to enable.
GPIO Settings
First assign values to the variables in the structure, set the PA5 port, set the port to output, push-pull output (I have only a limited understanding of analog electronics), and set the output speed to 50MHz.
Then enter the program to turn the LED on and off, for GPIO_SetBits(GPIOA,GPIO_Pin_5); let's look at its original function:
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_PIN(GPIO_Pin));
GPIOx->BSRR = GPIO_Pin;
}
It passes two parameters: *GPIOx, GPIO_Pin. In stm32f0xx.h there are
typedef struct
{
__IO uint32_t MODER;
__IO uint16_t OTYPER;
uint16_t RESERVED0;
__IO uint32_t OSPEEDR;
__IO uint32_t PUPDR;
__IO uint16_t IDR;
uint16_t RESERVED1;
__IO uint16_t ODR;
uint16_t RESERVED2;
__IO uint32_t BSRR;
__IO uint32_t LCKR;
__IO uint32_t AFR[2];
__IO uint16_t BRR;
uint16_t RESERVED3;
}GPIO_TypeDef;
For the definition of __IO as volatile, you can read this article. Here is the link. Thanks to the author. Through this structure, you should also understand why the BSRR register can be operated.
#define GPIO_Pin_5 ((uint16_t)0x0020)
So this function sets the fifth bit ODR. After the delay, GPIO_ResetBits(GPIOA,GPIO_Pin_5); is similar to the above, so I won't explain it one by one.
At this point, the lighting procedure is almost done. You may not be able to write what you have done. The writing process is a process of sublimation and improvement. It can solidify your own abilities, think about yourself, and go through what you took for granted before. You can gain a lot...
PS: After adding the *.c file in the library, if it prompts that the header file cannot be found during compilation, you need to enable the corresponding .h file in stm32f0xx_cof.h (that is, remove the previous comment).
Previous article:STM32 KEY control LED
Next article:STM32 buzzer - library function
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- If the clamp meter (multimeter) does not measure but there are still numbers, is it a malfunction?
- EEWORLD University Hall - Digital Oscilloscope Operation Digital Oscilloscope Operation_National Taiwan Normal University_Teacher Zhang Guowei
- [ESP32-Audio-Kit Audio Development Board Review] Part 1: Building esp-idf and esp-adf development environments based on vs code
- [Raspberry Pi Pico Review] Pico power supply part
- Live broadcast has ended | Microchip's latest SAM and PIC32 microcontroller software development platform - MPLAB Harmony V3
- [GD32E503 Review]——07.LCD Part 2: Drawing Custom Patterns
- Xiaozhi Science Popularization丨Why do instruments generally have set values and readback values?
- 【TI Recommended Course】#What can universal fast charging bring?#
- Prize-giving Event | Tektronix High Precision Power Supply Test Knowledge Planet
- Post-COVID-19 Era: What important experiences and gains should we have? Check out these ten!