Smoke concentration detection alarm proteus simulation design based on stm32 (simulation + program + explanation)

Smoke concentration detection and alarm simulation design based on STM32 (simulation + program + explanation)


Smoke concentration detection and alarm simulation design based on STM32 (simulation + program + explanation)

Simulation diagram proteus 8.9

Program compiler: keil 5

Programming language: C language

Design number: C0085

1. Main functions

Function Description:

1. Use STM32 microcontroller and MQ-2 control core to design smoke concentration detection and alarm design;

2. Display the smoke concentration through the LCD screen LCD1602 and the serial port host computer, MV represents the detection value, and ALM represents the alarm value;

3. The smoke concentration ALM alarm value can be set by pressing the button.

4. When the monitored smoke concentration is greater than the alarm value, the buzzer alarm circuit is turned on and the buzzer alarms. When the toggle switch is turned on, the fan rotates for ventilation.

5. The default buzzer alarm is detected when the smoke concentration is higher than 200ppm.

Main hardware equipment: STM32F103 microcontroller

The following is a display of this design information:

2. Simulation

Overall design plan

This experiment uses the ADC, GPIO, timer and other resources of the STM32 microcontroller to organically combine the software and hardware, so that the system can correctly identify the AD value input to the simulated smoke concentration sensor, and the LCD1602 can display it correctly, and the buzzer can be displayed according to the Smoke concentration alarm value works. It should be noted that proteus does not have smoke concentration sensors such as MQ-2. This design uses a sliding rheostat to simulate changes in smoke concentration and cannot be directly used in physical design. If necessary, it must be debugged according to the physical object.

Simulation running status:

After starting the simulation, LCD1602 displays the detected smoke concentration in real time, and the measured value can be changed through the sliding rheostat. The alarm value concentration can be set by pressing the button, press the setting button to enter the setting mode, increase the alarm value through setting +, and decrease the alarm value through setting -. The buzzer alarm circuit starts when the smoke concentration is higher than the alarm value, and there is a beeping alarm sound, and it does not start when the smoke concentration is lower than the alarm value.

This design uses an electromagnetic buzzer. The electromagnetic buzzer consists of an oscillator, electromagnetic coil, magnet, vibrating diaphragm and shell. After the power is turned on, the audio signal current generated by the oscillator passes through the electromagnetic coil, causing the electromagnetic coil to generate a magnetic field. The vibrating diaphragm periodically vibrates and produces sound under the interaction of the electromagnetic coil and the magnet. Therefore, a certain current is required to drive it. The current output by the I/O pin of the microcontroller is small. The TTL level output by the microcontroller basically cannot drive the buzzer, so a current amplification circuit needs to be added. The positive electrode of the buzzer is connected to the VCC (+5V) power supply, the negative electrode of the buzzer is connected to the collector C of the triode, and the base B of the triode is controlled by the BEEP pin of the microcontroller after passing through the current limiting resistor. When the BEEP output is low When BEEP outputs a high level, the triode QS is turned off, no current flows through the coil, and the buzzer does not sound; when BEEP outputs a high level, the triode is turned on, so that the current of the buzzer forms a loop and makes a sound.

The picture below shows that the smoke concentration is 192ppm, which is lower than the alarm value of 200, and the buzzer circuit does not work.
img

In the picture below, the detected smoke concentration is 204ppm, which is greater than or equal to the alarm value. The transistor is turned on and the buzzer alarms.img

3. Procedure

The program is opened with the keil5 mdk version. If there are problems opening it, check the keil version. The program is written in the HAL library version, and there are comments that can be understood in conjunction with the explanation video.
img

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "lcd1602.h"
#include "stdio.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
	uint16_t Tim_cnt = 0;  //定时器变量
	uint8_t set_flag = 0;
	float warming_val=200;	//报警浓度大小
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
    
    
  /* USER CODE BEGIN 1 */
	ADC_ChannelConfTypeDef sConfig = {
    
    0};  //建立sConfig结构体
	char str[20];  //字符串的存放数组
	uint32_t adcv; //存放ADC转换结果
	float temp;
	set_flag = 0;


  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */
	sConfig.Rank = ADC_REGULAR_RANK_1;
	sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;   //采样周期为1.5个周期
  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_ADC1_Init();
  MX_USART1_UART_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */
	LCD_Init();  //初始化LCD1602
	HAL_TIM_Base_Start_IT(&htim3);//开启定时器3
//	LCD_ShowString(0,0,dis_str);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    
    
		sConfig.Channel = ADC_CHANNEL_1;   //选择通道1
		HAL_ADC_ConfigChannel(&hadc1, &sConfig);  //选择ADC1的通道道1
		HAL_ADC_Start(&hadc1);										//启动ADC1
		HAL_ADC_PollForConversion(&hadc1, 10);		//等待ADC1转换结束,超时设定为10ms
		adcv = HAL_ADC_GetValue(&hadc1);					//读取ADC1的转换结果
		
		
		temp=(float)adcv*(4.0/4095)*100;		
	
		sprintf(str,"%4.0fppm",temp);
		LCD_ShowString(0,0,"MV:");	
		LCD_ShowString(0,4,str);	
	  HAL_UART_Transmit(&huart1, (uint8_t *)&"AL=", 3, 10);  //串口1发送字符串,数组长度为12,超时10ms
		HAL_UART_Transmit(&huart1, (uint8_t *)str, 6, 10);		 //串口1发送字符串,数组长度为5,超时10ms
		HAL_UART_Transmit(&huart1, (uint8_t *)&"\n\r", 2, 10); //串口1发送字符串,数组长度为2,超时10ms	
		
		if(set_flag){
    
    //设置模式
			sprintf(str,"%4.0fppm^ ",warming_val);
			LCD_ShowString(1,0,"ALM:");	
			LCD_ShowString(1,4,str);
		}else{
    
    
			sprintf(str,"%4.0fppm  ",warming_val);
			LCD_ShowString(1,0,"ALM:");	
			LCD_ShowString(1,4,str);			
		}
		
	  HAL_UART_Transmit(&huart1, (uint8_t *)&"ALM=", 4, 10);  //串口1发送字符串,数组长度为12,超时10ms
		HAL_UART_Transmit(&huart1, (uint8_t *)str, 6, 10);								//串口1发送字符串,数组长度为5,超时10ms
		HAL_UART_Transmit(&huart1, (uint8_t *)&"\n\r", 2, 10);						//串口1发送字符串,数组长度为2,超时10ms	
		
		if(temp>warming_val&&!set_flag){
    
    //如果超过报警值
			HAL_GPIO_WritePin(GPIOA,BEEP_Pin, GPIO_PIN_RESET);//BEEP引脚拉低
		}else{
    
    
			HAL_GPIO_WritePin(GPIOA,BEEP_Pin, GPIO_PIN_SET);
		}

		HAL_ADC_Stop(&hadc1);											//停止ADC1
		HAL_Delay(300);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
    
    
  RCC_OscInitTypeDef RCC_OscInitStruct = {
    
    0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {
    
    0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {
    
    0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    
    
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    
    
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    
    
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    
    
if (htim->Instance == htim3.Instance) 
	{
    
    
		Tim_cnt++;
		if(Tim_cnt==5)  //2.5ms进一次
		{
    
    
			Tim_cnt=0;   //请
			HAL_GPIO_TogglePin(LED0_GPIO_Port, LED0_Pin);
		}
	}

}	
//中断处理
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    
    
	 UNUSED(GPIO_Pin);
	if(GPIO_Pin == KEY1_Pin)  //测到EXTI0线产生外部中断事件
	{
    
    
		if(set_flag){
    
    
				set_flag = 0;
		}else{
    
    
				set_flag = 1;
		}
	}
	else if(GPIO_Pin == KEY2_Pin) //测到EXTI1线产生外部中断事件
	{
    
    
			if(set_flag){
    
    
				if(warming_val<390){
    
    //一次+10
					warming_val+=10;			
				}
			}
	}	else if(GPIO_Pin == KEY3_Pin) //测到EXTI2线产生外部中断事件
	{
    
    
			if(set_flag){
    
    
				if(warming_val>10){
    
    //一次-10
					warming_val-=10;
				}
			}
	}
	
}


/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
    
    
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
    
    
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    
    
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

4. Information list & download link

0. Common usage problems and solutions – a must-read! ! ! !

1. Program code

2. Proteus simulation

3. Functional requirements

4. Explanation video

Altium Designer Software Information

filename.bat

KEIL software information

MQ135-2.jpg

MQ135.jpg

MQ series sensor working principle.txt

Proteus software information

Microcontroller learning materials

Defense skills

Common descriptions for design reports

Double-click the mouse to open and find more 51 STM32 Microcontroller Course Graduation Project.url

img

Data download link (clickable):

Guess you like

Origin blog.csdn.net/weixin_52733843/article/details/132612975