仿真圖proteus 8.9
程序編譯器:keil 5
編程語言:C語言
設(shè)計編號:C0085
1.主要功能
功能說明:
1、以STM32單片機(jī)和MQ-2控制核心設(shè)計煙霧濃度檢測報警設(shè)計;
2、通過液晶屏LCD1602和串口上位機(jī)顯示煙霧濃度,MV表示檢測值,ALM表示報警值;
3、可以通過按鍵設(shè)置煙霧濃度ALM報警值大小。
4、監(jiān)測煙霧濃度大于報警值時蜂鳴器報警電路導(dǎo)通,蜂鳴器報警。撥動開關(guān)打開情況下,風(fēng)扇轉(zhuǎn)動通風(fēng)。
5、默認(rèn)監(jiān)測到煙霧濃度高于200ppm蜂鳴器報警。
以下為本設(shè)計資料展示:
2.仿真
整體設(shè)計方案
本實(shí)驗(yàn)利用STM32單片機(jī)的ADC、GPIO、定時器等資源,將軟、硬件有機(jī)地結(jié)合起來,使得系統(tǒng)能夠正確地進(jìn)識別輸入模擬煙霧濃度傳感器的AD值,LCD1602能夠正確地顯示,蜂鳴器根據(jù)煙霧濃度報警值工作。需注意的是,proteus是沒有MQ-2等煙霧濃度傳感器的,本設(shè)計使用滑動變阻器模擬煙霧濃度變化,不能直接用于實(shí)物設(shè)計,有需要的需跟據(jù)實(shí)物調(diào)試。
仿真運(yùn)行情況:
開始仿真后LCD1602實(shí)時顯示檢測到的煙霧濃度,可以通過滑動變阻器改變測量值。可通過按鍵設(shè)置報警值濃度,按下設(shè)置鍵進(jìn)入設(shè)置模式,通過設(shè)置+調(diào)高報警值,通過設(shè)置-調(diào)低報警值。蜂鳴器報警電路在煙霧濃度高于報警值時啟動,有嘟嘟報警聲,低于不啟動。
本設(shè)計采用電磁式蜂鳴器進(jìn)行。電磁式蜂鳴器由振蕩器、電磁線圈、磁鐵、振動膜片及外殼等組成。接通電源后,振蕩器產(chǎn)生的音頻信號電流通過電磁線圈,使電磁線圈產(chǎn)生磁場。振動膜片在電磁線圈和磁鐵的相互作用下,周期性地振動發(fā)聲。因此需要一定的電流才能驅(qū)動它,單片機(jī)I/O引腳輸出的電流較小,單片機(jī)輸出的TTL電平基本上驅(qū)動不了蜂鳴器,因此需要增加一個電流放大的電路。蜂鳴器的正極接到VCC(+5V)電源上面,蜂鳴器的負(fù)極接到三極管的集電極C,三極管的基極B經(jīng)過限流電阻后由單片機(jī)的BEEP引腳控制,當(dāng)BEEP輸出低電平時,三級管QS截止,沒有電流流過線圈,蜂鳴器不發(fā)聲;當(dāng)BEEP輸出高電平時,三級管導(dǎo)通,這樣蜂鳴器的電流形成回路,發(fā)出聲音。
下圖檢測到煙霧濃度是192ppm,低于報警值200,蜂鳴器電路不工作。
下圖檢測到煙霧濃度是204ppm,大于等于報警值,三極管導(dǎo)通,蜂鳴器報警
3. 程序
程序是用keil5 mdk版本打開的,如果打開有問題,核實(shí)下keil的版本。程序是HAL庫版本編寫的,有注釋可以結(jié)合講解視頻理解。
/* 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結(jié)構(gòu)體
char str[20]; //字符串的存放數(shù)組
uint32_t adcv; //存放ADC轉(zhuǎn)換結(jié)果
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轉(zhuǎn)換結(jié)束,超時設(shè)定為10ms
adcv = HAL_ADC_GetValue(&hadc1); //讀取ADC1的轉(zhuǎn)換結(jié)果
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發(fā)送字符串,數(shù)組長度為12,超時10ms
HAL_UART_Transmit(&huart1, (uint8_t *)str, 6, 10); //串口1發(fā)送字符串,數(shù)組長度為5,超時10ms
HAL_UART_Transmit(&huart1, (uint8_t *)&"nr", 2, 10); //串口1發(fā)送字符串,數(shù)組長度為2,超時10ms
if(set_flag){//設(shè)置模式
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發(fā)送字符串,數(shù)組長度為12,超時10ms
HAL_UART_Transmit(&huart1, (uint8_t *)str, 6, 10); //串口1發(fā)送字符串,數(shù)組長度為5,超時10ms
HAL_UART_Transmit(&huart1, (uint8_t *)&"nr", 2, 10); //串口1發(fā)送字符串,數(shù)組長度為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進(jìn)一次
{
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線產(chǎn)生外部中斷事件
{
if(set_flag){
set_flag = 0;
}else{
set_flag = 1;
}
}
else if(GPIO_Pin == KEY2_Pin) //測到EXTI1線產(chǎn)生外部中斷事件
{
if(set_flag){
if(warming_val<390){//一次+10
warming_val+=10;
}
}
} else if(GPIO_Pin == KEY3_Pin) //測到EXTI2線產(chǎn)生外部中斷事件
{
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 %drn", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
4. 資料清單&下載鏈接
0、常見使用問題及解決方法–必讀?。。?!
1、程序代碼
2、Proteus仿真
3、功能要求
4、講解視頻
filename.bat
KEIL軟件資料
MQ135-2.jpg
MQ135.jpg
MQ系列傳感器工作原理.txt
Proteus軟件資料
單片機(jī)學(xué)習(xí)資料
答辯技巧
設(shè)計報告常用描述
鼠標(biāo)雙擊打開查找更多51 STM32單片機(jī)課程畢業(yè)設(shè)計.url
資料下載鏈接(可點(diǎn)擊)