Fourth, use the register to light up the first LED

1. LED module circuit

The LED module used is a common anode

LED module circuit diagram

Two, STM32 startup file

For this part of the startup file, we mainly summarize its functions without explaining the code inside in detail. Its functions are as follows:

1. Initialize the stack pointer SP;

2. Initialize the program counter pointer PC;

3. Set the size of the heap and stack;

4. Set the entry address of the interrupt vector table;

5. Configure external SRAM as data storage (this is configured by the user, general development boards may not have external SRAM);

6. Call the SystemInit () function to configure the system clock of STM32. Set the branch entry "_main" of the C library (which is ultimately used to call the main function); the
startup file startup_stm32f10x_hd.s is in the installation directory, you can manually search for it and add it to the project directory. The startup files are all written in assembly language.

Three, write a program to light up an LED

The function we want to achieve is: light up the first LED, that is, the D1 indicator light is on. To operate the STM32 register, we need to use the C language to package it. We put this part of the program in stm32f10x.h.

It can be seen from the circuit diagram that LED1 is connected to the PC0 port. We need to set the PC0 port to zero to turn it on and the LED light will light up.
First find out which bus the GPIO is mounted on, namely the Block2 bus. All the on-chip peripherals are on Block2.

#define PERIPH_BASE ((unsigned int)0x40000000)
#define APB2PERIPH_BASE (PERIPH_BASE +0x00010000)
#define GPIOC_BASE (APB2PERIPH_BASE + 0x1000)
#define GPIOC_CRL *(unsigned int*)(GPIOC_BASE+0x00)
#define GPIOC_CRH *(unsigned int*)(GPIOC_BASE+0x04)
#define GPIOC_IDR *(unsigned int*)(GPIOC_BASE+0x08)
#define GPIOC_ODR *(unsigned int*)(GPIOC_BASE+0x0C)
#define GPIOC_BSRR *(unsigned int*)(GPIOC_BASE+0x10)
#define GPIOC_BRR *(unsigned int*)(GPTOC_BASE+0x14)
#define GPIOC_LCKR *(uunsigned int*)(GPIOC_BASE+0x18)
#define AHBPERIPH_BASE (PERIPH_BASE + 0x20000)
#define RCC_BASE (AHBPERIPH_BASE + 0x1000)
#define RCC_APB2ENR *(unsigned int*)(RCC_BASE+0x18)

Before operating any peripheral, be sure to turn on the clock of the corresponding peripheral. Only when the clock is turned on, the peripheral can be set.

If Keil5 writes annotation Chinese characters as ?? when encoding, you can change the settings on Baidu https://jingyan.baidu.com/article/7082dc1c281faee40a89bda1.html

#include "stm32f10x.h"

void SystemInit()
{
	
}

int main()
{
	//1.开启GPIOC的端口时钟
	RCC_APB2ENR |=1<<4;//或运算是为了防止对其它位的干扰
	
	//2.设置工作模式
	//推挽输出
	GPIOC_CRL&=~(unsigned)(0x0f<<(4*0));//4*0表示第0个管脚,第一个管脚就是4*1,每四位控制一个引脚
	GPIOC_CRL|=(0x03<<(4*0));//设置输出模式为50MHz
	
	//3.设置引脚输出为0,点亮LED
	GPIOC_BSRR=(0x1<<(16+0));
	while(1)
	{
		
	}	
}

Flashing function can also be realized:

#include "stm32f10x.h"
typedef unsigned int u32;
void delay(u32);
void SystemInit(void);

void SystemInit()
{
	
}
void delay(u32 i)
{
	while(i--);
}
int main()
{
	//1.开启GPIOC的端口时钟
	RCC_APB2ENR |=1<<4;//或运算是为了防止对其它位的干扰
	
	//2.设置工作模式
	//推挽输出
	GPIOC_CRL&=~(unsigned)(0x0f<<(4*0));//4*0表示第0个管脚,第一个管脚就是4*1,每四位控制一个引脚
	GPIOC_CRL|=(0x03<<(4*0));//设置输出模式为50MHz
	
	//3.设置引脚输出为0,点亮LED
	GPIOC_BSRR=(0x1<<(16+0));
	while(1)
	{
		GPIOC_BSRR=(1<<(16+0));//点亮LED
		delay(0xfffff);//延时
		GPIOC_BSRR=(1<<(0));//关闭LED
		delay(0xfffff);
	}
	
}

Note that you can view the size of the compiled file in the output:

Guess you like

Origin blog.csdn.net/qq_40836442/article/details/109538791