STM32F103ZET6 study notes - marquee experiment (library function)

1. First, we need to add a PROGREAM (named according to our preference) folder in the project directory, and create a subfolder 1_LED under this folder, and then create led.c and led.h files under 1_LED, and convert led. c and led.h are added to the project. Remember to add 1_LED to the path of the header file. It is described in detail in the previous note. If you forget it, you can go back and look at it.

 

 

 Two, write led.c and led.h files

1,led.h:

#ifndef __LED_H
#define __LED_H

void LED_Init(void);//函数声明


#endif

2,led.c:

The steps to operate any IO port are:

(1) Enable the IO port clock and call the function RCC_APB2PeriphColckCmd();

(2) Initialize the IO port mode and call the function GPIO_Iint();

(3) Operate the IO port to output high and low levels.

Specific operation:

        1) According to the schematic diagram of the development board, LED0 and LED1 are respectively connected to PB5 and PE5 of the development board, so we need to enable the clocks of PB5 and PE5 first:

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//GPIOB
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);//GPIOE

        2) After enabling the IO group, you need to define the corresponding structure first, then set the corresponding pin number, mode and speed, and finally set the output level of the IO port:

    GPIO_InitTypeDef GPIO_InitStr;//定义GPIO_Init结构体    

    GPIO_InitStr.GPIO_Mode=GPIO_Mode_Out_PP;//设置为推挽输出
	GPIO_InitStr.GPIO_Pin=GPIO_Pin_5;//设置PB5
	GPIO_InitStr.GPIO_Speed=GPIO_Speed_50MHz;//设置速度为50Mhz
	GPIO_Init(GPIOB,&GPIO_InitStr);//初始化PB
	GPIO_SetBits(GPIOB,GPIO_Pin_5);//PB5输出高电平
	
	//同PB5
	GPIO_InitStr.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStr.GPIO_Pin=GPIO_Pin_5;
	GPIO_InitStr.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOE,&GPIO_InitStr);
	GPIO_SetBits(GPIOE,GPIO_Pin_5);

        3) The entire code of led.c:

#include"led.h"
#include"stm32f10x.h"

void LED_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStr;//定义GPIO_Init结构体
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//GPIOB
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);//GPIOE
	
	GPIO_InitStr.GPIO_Mode=GPIO_Mode_Out_PP;//设置为推挽输出
	GPIO_InitStr.GPIO_Pin=GPIO_Pin_5;//设置PB5
	GPIO_InitStr.GPIO_Speed=GPIO_Speed_50MHz;//设置速度为50Mhz
	GPIO_Init(GPIOB,&GPIO_InitStr);//初始化PB
	GPIO_SetBits(GPIOB,GPIO_Pin_5);//PB5输出高电平
	
	//同PB5
	GPIO_InitStr.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStr.GPIO_Pin=GPIO_Pin_5;
	GPIO_InitStr.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOE,&GPIO_InitStr);
	GPIO_SetBits(GPIOE,GPIO_Pin_5);
}

Three, the main function:

#include"stm32f10x.h"
#include"led.h"
#include"delay.h"

int main(void)
{
	delay_init();
	LED_Init();
	
	while(1)
	{
		GPIO_SetBits(GPIOB,GPIO_Pin_5);
		GPIO_SetBits(GPIOE,GPIO_Pin_5);
		delay_ms(500);
		
		GPIO_ResetBits(GPIOB,GPIO_Pin_5);
		delay_ms(500);
		GPIO_ResetBits(GPIOE,GPIO_Pin_5);
		delay_ms(500);
	}
	
}

Guess you like

Origin blog.csdn.net/qq_63306482/article/details/126223428