01:STM32 lighting master and buzzer

Table of contents

1: Light up 1 LED

1: Connection diagram

2: Function introduction

3: Lighting code

2: LED flashing

1: Function introduction

2: flashing code

Three: LED running lights

1: Connection diagram

2: Function introduction

3: Running water lamp code

Four: Button control LED

1:Circuit diagram

2: Connection diagram

3: Function introduction

 4: Button control LED code

Five: Buzzer

1: Connection diagram

 2:Buzzer code

6: Photoresistor control buzzer

1: Connection diagram

2: Function introduction 

3: Photoresistor control buzzer code


1: Light up 1 LED

1: Connection diagram

 Because the IO port is connected to the negative pole of the LED, the IO port outputs low power frequency to light up the LED (low power frequency is used to light up)

STM32's GPIO (general-purpose input and output port) requires a total of 3 steps

A: The first step is to use RCC to turn on the GPIO clock.

B: In the second step, use the GPIO_Init function to initialize the GPIO

C: The third step is to use the output or input function to control the GPIO port

2: Function introduction

There are only 3 commonly used functions in the stm32f10x_rcc.h file (RCC turns on the GPIO clock):

void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState);
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState);

RCC_AHBPeriphClockCmd: The first parameter is which peripheral to select, the second parameter is ENABLE (start) or DISABLE (invalid))

The RCC_APB2PeriphClockCmd and RCC_APB1PeriphClockCmd parameters have the same meaning: the first parameter selects the peripheral, and the second parameter enables ENABLE or disables DISABLE

There are many frequently used functions in the stm32f10x_gpio.h file (PIO_Init function initializes GPIO):

void GPIO_DeInit(GPIO_TypeDef* GPIOx);
void GPIO_AFIODeInit(void);
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct);
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);

 GPIO_Init: The function of this function is to initialize the GPIO port with the parameters of the structure. We first need to define a structure variable, then assign a value to the structure, and finally call this function. The structure will be automatically read inside this function. Internal values, and then automatically configure various parameters of the peripheral. 

        The first parameter is which peripheral to choose, and the second parameter is the structure. Let’s copy the structure type first.

Use output or input functions to control the GPIO port

GPIO_ResetBits: The first parameter is to select which peripheral (GPIOx x=A~G), the second parameter is GPIO_Pin_x (x=0~15) to set the specified port to low power frequency.

3: Lighting code

#include "stm32f10x.h"                  // Device header
int main(void){
	//第一步,使用RCC开启GPIO的时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	//第二步,使用GPIO Init函数初始化GPIO
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP;//推挽输出
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	//第三步,使用输出或者输入的函数控制GPIO口
	GPIO_ResetBits(GPIOA,GPIO_Pin_0);
	while(1){
	}
}

Push-pull output GPIO_Mode_Out_PP has driving capabilities at both high and low frequency

2: LED flashing

The connection diagram is the same (1: light up 1 LED)

1: Function introduction

There are many frequently used functions in the stm32f10x_gpio.h file (PIO_Init function initializes GPIO):

void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);

The parameters of the GPIO_SetBits and GPIO_ResetBits functions are the same: the first parameter is which peripheral to select (GPIOx x=A~G), the second parameter is GPIO_Pin_x (x=0~15),

The second parameter of GPIO_SetBits can set the specified parameter to a high frequency

The second parameter of GPIO_ResetBits can set the specified parameter to a low power frequency.

GPIO_WriteBit: The first parameter is to select which peripheral (GPIOx x=A~G), the second parameter is GPIO_Pin_x (x=0~15), the third parameter is Bit_RESET (set the second parameter to low power frequency ) or Bit_SET (Set the second parameter to high frequency   and write 1 bit at a time ; Bit_RESET=(BitAction)0 Force 0 to be converted to BitAction enumeration type in the same way as Bit_SET

2: flashing code

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
int main(void){
	//第一步,使用RCC开启GPIO的时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	//第二步,使用GPIO Init函数初始化GPIO
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP;//推挽输出
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	//第三步,使用输出或者输入的函数控制GPIO口
	//GPIO_ResetBits(GPIOA,GPIO_Pin_0);低
	//GPIO_SetBits(GPIOA,GPIO_Pin_0); 
	//GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);   //Bit_SET高   Bit_RESET低
	while(1){
		//Bit_RESET=(BitAction)0  把0强制转化为BitAction枚举类型
		GPIO_WriteBit(GPIOA,GPIO_Pin_0,(BitAction)0);
		Delay_ms(500);
		GPIO_WriteBit(GPIOA,GPIO_Pin_0,(BitAction)1);
		Delay_ms(500);
	}
}

Three: LED running lights

1: Connection diagram

2: Function introduction

There are many frequently used functions in the stm32f10x_gpio.h file (PIO_Init function initializes GPIO): 

void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);

 GPIO_Write: The first parameter is to select which peripheral (GPIOx x=A~G), and the second parameter is to be written directly to the ODR register of GPIO; what is written here is. Specifies the value to write to the output data register; writes 16 bits at a time

3: Running water lamp code

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
int main(void){
	//第一步,使用RCC开启GPIO的时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	//第二步,使用GPIO Init函数初始化GPIO
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP;//推挽输出
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_All; //打开GPIOA的16个IO口
	GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	//第三步,使用输出或者输入的函数控制GPIO口
	//GPIO_ResetBits(GPIOA,GPIO_Pin_0);低
	//GPIO_SetBits(GPIOA,GPIO_Pin_0); 
	//GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);   //Bit_SET高   Bit_RESET低
	while(1){
		int data=0x0001;    //0x0001=0000 0000 0000 0001
		for(int i=0;i<8;i++){  
			GPIO_Write(GPIOA,~data);   
			Delay_ms(500);
			data=data<<1;
		}
	}
}

Four: Button control LED

1:Circuit diagram

There are two modes that are commonly used

        The smaller the resistance value, the stronger the pull-down capability; stm32 must be in pull-up input (GPIO_Mode_IPU) mode; 

        When k1 is pressed, the resistance of k1 is ignored, the pull-down ability is strong, and because it is connected low, low frequency is used; on the contrary, not pressing K1 is equivalent to a very large circuit-breaking resistance, the pull-down ability is weak, and the pull-up ability is relatively strong;

These two connection methods: press down for low power frequency (0); release for high power frequency (1);

2: Connection diagram

3: Function introduction

GPIO reading (input, output) function in stm32f10x gpio.h file

uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);

        The parameters are similar to the above; those with bits at the back are input or output bit by bit, and those without bits are input or output of 16 bits at a time.

        To read the IO port, use the xInputx function; in output mode, if you want to see what is being output, use the xOutputx function.

        The GPIO_ReadInputData function is used to read the value of the input data register of the GPIO port, and the GPIO_ReadOutputData function is used to read the value of the output data register of the GPIO port.

        The specific differences are as follows:
1. The GPIO_ReadInputData function reads the value of the input data register of the GPIO port, that is, it reads the status of the input signal actually connected to the GPIO pin. It can be used to detect the input status of external devices, such as whether a button is pressed, whether a sensor has signal input, etc.

2. The GPIO_ReadOutputData function reads the value of the output data register of the GPIO port, that is, reads the level status of the GPIO pin output. It can be used to read the level status of GPIO pins that have been configured in output mode, such as whether the LED is on, whether the driver is working, etc.

In summary, GPIO_ReadInputData is used to read the input signal status, and GPIO_ReadOutputData is used to read the output signal status. If the return function is used, the value bit is 1 or 0.

 4: Button control LED code

#include "stm32f10x.h"                  // Device header

void Delay_ms(uint32_t xms)
{
	while(xms--)
	{
		Delay_us(1000);
	}
}
 
uint8_t KEY_init(){
	//第一步,使用RCC开启GPIO的时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	//第二步,使用GPIO_Init函数初始化GPIO
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;//上拉模式
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1 | GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
}

uint8_t LED_init(){
	//第一步,使用RCC开启GPIO的时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	//第二步,使用GPIO_Init函数初始化GPIO
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;推挽输出
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1 | GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	//默认为低电频,低电频点亮LED
	GPIO_SetBits(GPIOA,GPIO_Pin_1 | GPIO_Pin_2);
}

//按键函数
uint8_t KEY_pad(){
	uint8_t KeyName;
	if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0){
		Delay_ms(20);
		while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0);
		Delay_ms(20);
		KeyName=1;
	}
	if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0){
		Delay_ms(20);
		while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0);
		Delay_ms(20);
		KeyName=2;
	}
	return KeyName;
}



//取反函数
uint8_t LED1_Turn()
{
	if (GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_1) == 0)//输出寄存器为低电频
	{
		GPIO_SetBits(GPIOA, GPIO_Pin_1);
	}
	else
	{
		GPIO_ResetBits(GPIOA, GPIO_Pin_1);
	}
}
uint8_t LED2_Turn()
{
	if (GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_2) == 0)
	{
		GPIO_SetBits(GPIOA, GPIO_Pin_2);
	}
	else
	{
		GPIO_ResetBits(GPIOA, GPIO_Pin_2);
	}
}

uint8_t KeyNum;
int main(void){
	LED_init();
	KEY_init();
	while(1){
		KeyNum=KEY_pad();
		if(KeyNum==1){
			LED1_Turn();
		}
		if(KeyNum==2){
			LED2_Turn();
		}

		}
	}

Five: Buzzer

We use an active buzzer: low frequency trigger

Active buzzer: It has an internal oscillation source. Connect the positive and negative poles to DC voltage to continuously sound, with a fixed frequency.

1: Connection diagram

 2:Buzzer code

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
int main(void){
	//第一步,使用RCC开启GPIO的时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	//第二步,使用GPIO Init函数初始化GPIO
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP;//推挽输出
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12; //打开GPIOA的16个IO口
	GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	//第三步,使用输出或者输入的函数控制GPIO口
	//GPIO_ResetBits(GPIOA,GPIO_Pin_0);低
	//GPIO_SetBits(GPIOA,GPIO_Pin_0); 高
	//GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);   //Bit_SET高   Bit_RESET低
	while(1){

		GPIO_WriteBit(GPIOB,GPIO_Pin_12,(BitAction)0);
		Delay_ms(100);
		GPIO_SetBits(GPIOB,GPIO_Pin_0);
		Delay_ms(100);
		GPIO_ResetBits(GPIOB,GPIO_Pin_0);
		Delay_ms(100);
		GPIO_WriteBit(GPIOB,GPIO_Pin_12,(BitAction)1);
		Delay_ms(700);

		}
	}

6: Photoresistor control buzzer

1: Connection diagram

Photoresistor:, DO digital output terminal

2: Function introduction 

uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

Reading one bit of the IO pin (input data register),  

3: Photoresistor control buzzer code

#include "stm32f10x.h"                  // Device header

uint8_t Buzzer_init(){
	//第一步,使用RCC开启GPIO的时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	//第二步,使用GPIO_Init函数初始化GPIO
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
}
uint8_t LightSensor_init(){
	//第一步,使用RCC开启GPIO的时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	//第二步,使用GPIO_Init函数初始化GPIO     //GPIO_Mode_Out_PP;推挽输出
	GPIO_InitTypeDef GPIO_InitStructure;  //GPIO_Mode_IPU   上拉输出
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;推挽输出
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);

}

uint8_t Buzzer_ON(){
	GPIO_ResetBits(GPIOB,GPIO_Pin_12);
}

uint8_t Buzzer_OFF(){
	GPIO_SetBits(GPIOB,GPIO_Pin_12); //高电频
}


uint8_t LightSensor(){
//读取Io引脚PB13状态的读取,返回值为1或者o
	return GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13); 
}

int num;
int main(void){
	Buzzer_init();
	LightSensor_init();
	while (1){
		num=LightSensor();
		if (num==1){
			Buzzer_ON();
		}
		else {
			Buzzer_OFF();		
		}

	}
}

Guess you like

Origin blog.csdn.net/m0_74739916/article/details/132229264