STM32 LED light experiment based on register and firmware library

Table of Contents

1. Light up LED lights based on register

1. Definition of register

2. Create a register-based keil project

3. Register-based LED running light

2. Light up the LED lights based on the firmware library

1. Definition of firmware library

2. Create a keil project based on the firmware library

3. LED running lights based on firmware library

3. Comparison of the two methods


1. Lighting the LED light based on the register

1. Definition of register

The function of the register is to store binary code, which is composed of a combination of flip-flops with storage function. A flip-flop can store 1-bit binary code, so a register that stores n-bit binary codes needs to be composed of n flip-flops.

2. Create a register-based keil project

1) Create a new folder, named stm32 to light up the LED based on the register, open keil, click Project, select New uVision Project, select the folder (stm32 to light up the LED based on the register), create a new project (register LED), select the development board STM32F103C8.

2), add library files online
When using registers to control STM32, there is no need to add library files online and can be turned off directly.
3), add files
①Add existing files
Add the startup file (startup_stm32f10x_hd.s) to the newly created project. This file can be copied to the firmware library here startup_stm32f10x_hd.s.
②Create new file
stm32f10x.h
Created manually, used to store register mapping code, temporarily empty.
main.c
Created manually to store the main function, which is temporarily empty.

4), configure the magic wand tab
①Target setting, the purpose is to be able to use printf

②Output settings to generate hex files during compilation

③Listing settings, let the output file be located in the Listing folder

④Debug settings

⑤Utilities settings

3. Register-based LED running light

1), write code

#define GPIOC_BASE 0x40011000
#define GPIOA_BASE 0x40010800

#define RCC_APB2ENR (*(unsigned int *)0x40021018)

#define GPIOB_CRH (*(unsigned int *)0x40010C04)
#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)
\t


void Delay_ms(volatile unsigned int);
void A_LED_LIGHT(void);
void B_LED_LIGHT(void);
void C_LED_LIGHT(void);
void Delay_ms( volatile unsigned int t)
{
     unsigned int i;
     while(t--)
         for (i=0;i<800;i + + );
}

void A_LED_LIGHT(){
GPIOA_ODR=0x0<<4; //PA4 low level
GPIOB_ODR=0x1<<9; //PB9 high level
GPIOC_ODR=0x1<<13; //PC13 high level
}
void B_LED_LIGHT(){
GPIOA_ODR=0x1<<4; //PA4 high level
GPIOB_ODR=0x0<<9; //PB9 low level
GPIOC_ODR=0x1<<13; //PC13 high level
}
void C_LED_LIGHT(){
GPIOA_ODR=0x1<<4; //PA4 high level
GPIOB_ODR=0x1<<9; //PB9 high level
GPIOC_ODR=0x0<<13; //PC13 low level
}

int main(){
int j=100;
// Start 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
\t
\t

//Set GPIO as push-pull output
GPIOB_CRH & amp;= 0xffffff0f; //Set bit clear
GPIOB_CRH|=0x00000020; //PB9 push-pull output
\t 
GPIOC_CRH & amp;= ~(1111<<(4*5)); //Set bit and clear
GPIOC_CRH|=(1<<(4*5)); //PC13 push-pull output

 

GPIOA_CRL & amp;= 0xfff0ffff; //Set bit clear
GPIOA_CRL|=0x00010000; //PA4 push-pull output
\t 
// 3 LEDs are initialized to not light up (i.e. high point)
GPIOB_ODR |= (1<<9);
GPIOC_ODR |= (1<<13);
GPIOA_ODR |= (1<<4);
\t
while(j){
\t\t
B_LED_LIGHT();
Delay_ms(10000);
\t 
C_LED_LIGHT();
Delay_ms(10000);
\t 
A_LED_LIGHT();
Delay_ms(10000);
}

}

2), experimental results

2. Lighting the LED light based on the firmware library

1. Definition of firmware library

The firmware library, also known as the firmware function library, is a firmware function package, which consists of programs, data structures and macros, and includes the performance characteristics of all peripherals of the microcontroller. The function library also includes driver descriptions and application examples for each peripheral, providing an intermediate API for developers to access the underlying hardware. By using the firmware function library, developers can gain in-depth knowledge of the underlying hardware details. Every peripheral can be easily adapted. Therefore, using solid-state function libraries can reduce users’ programming time and further reduce development costs. Each peripheral driver consists of a set of functions, which cover all functions of the peripheral. The development of each device is driven by a common API (application programming interface), which standardizes the structure, function and parameter names of the driver.

2. Create a keil project based on the firmware library

The method is similar to the creation of registers.
difference:

Libraries files need to be added

3. LED running light based on firmware library

1) Write code

``
#include "stm32f10x.h" // Device header
#include "Delay.h"

int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
\t

GPIO_InitTypeDef GPIO_InitStructure;//Create structure
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//Set to push-pull output
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;//Initialize all pins of GPIOA
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, & amp;GPIO_InitStructure);
//Set the serial port to high level. Because the wiring method causes the LED light to light up at low level by default, so turn off the LED light first.
GPIO_SetBits(GPIOA,GPIO_Pin_0);
GPIO_SetBits(GPIOA,GPIO_Pin_1);
GPIO_SetBits(GPIOA,GPIO_Pin_2);
GPIO_SetBits(GPIOA,GPIO_Pin_3);
GPIO_SetBits(GPIOA,GPIO_Pin_4);
GPIO_SetBits(GPIOA,GPIO_Pin_5);
GPIO_SetBits(GPIOA,GPIO_Pin_6);
GPIO_SetBits(GPIOA,GPIO_Pin_7);
\t
while (1)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_0);//Low level lights up
Delay_ms(1000); //Delay 1s
GPIO_SetBits(GPIOA,GPIO_Pin_0); //High level off
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
Delay_ms(1000);
GPIO_SetBits(GPIOA,GPIO_Pin_1);
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
Delay_ms(1000);
GPIO_SetBits(GPIOA,GPIO_Pin_2);
\t\t
GPIO_ResetBits(GPIOA,GPIO_Pin_3);
Delay_ms(1000);
GPIO_SetBits(GPIOA,GPIO_Pin_3);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
Delay_ms(1000);
GPIO_SetBits(GPIOA,GPIO_Pin_4);
GPIO_ResetBits(GPIOA,GPIO_Pin_5);
Delay_ms(1000);
GPIO_SetBits(GPIOA,GPIO_Pin_5);
GPIO_ResetBits(GPIOA,GPIO_Pin_6);
Delay_ms(1000);
GPIO_SetBits(GPIOA,GPIO_Pin_6);
GPIO_ResetBits(GPIOA,GPIO_Pin_7);
Delay_ms(1000);
GPIO_SetBits(GPIOA,GPIO_Pin_7);;
}

}

2) Experimental results

3. Comparison of two methods

Register method
shortcoming:
①Slow development speed
②The program has poor readability
③Complex maintenance
advantage:
①The specific parameters are more intuitive
②The program takes up less resources when running
The open library development method just makes up for the shortcomings of register development.
Through the implementation of the two methods, you will find that using the register development method, you need to constantly check the corresponding manual to understand the address of the corresponding register.