仿真圖proteus 8.9
程序編譯器:keil 5
編程語言:C語言
設(shè)計(jì)編號(hào):C0085
1.主要功能
功能說明:
1、以STM32單片機(jī)和MQ-2控制核心設(shè)計(jì)煙霧濃度檢測報(bào)警設(shè)計(jì);
2、通過液晶屏LCD1602和串口上位機(jī)顯示煙霧濃度,MV表示檢測值,ALM表示報(bào)警值;
3、可以通過按鍵設(shè)置煙霧濃度ALM報(bào)警值大小。
4、監(jiān)測煙霧濃度大于報(bào)警值時(shí)蜂鳴器報(bào)警電路導(dǎo)通,蜂鳴器報(bào)警。撥動(dòng)開關(guān)打開情況下,風(fēng)扇轉(zhuǎn)動(dòng)通風(fēng)。
5、默認(rèn)監(jiān)測到煙霧濃度高于200ppm蜂鳴器報(bào)警。
以下為本設(shè)計(jì)資料展示:
2.仿真
整體設(shè)計(jì)方案
本實(shí)驗(yàn)利用STM32單片機(jī)的ADC、GPIO、定時(shí)器等資源,將軟、硬件有機(jī)地結(jié)合起來,使得系統(tǒng)能夠正確地進(jìn)識(shí)別輸入模擬煙霧濃度傳感器的AD值,LCD1602能夠正確地顯示,蜂鳴器根據(jù)煙霧濃度報(bào)警值工作。需注意的是,proteus是沒有MQ-2等煙霧濃度傳感器的,本設(shè)計(jì)使用滑動(dòng)變阻器模擬煙霧濃度變化,不能直接用于實(shí)物設(shè)計(jì),有需要的需跟據(jù)實(shí)物調(diào)試。
仿真運(yùn)行情況:
開始仿真后LCD1602實(shí)時(shí)顯示檢測到的煙霧濃度,可以通過滑動(dòng)變阻器改變測量值??赏ㄟ^按鍵設(shè)置報(bào)警值濃度,按下設(shè)置鍵進(jìn)入設(shè)置模式,通過設(shè)置+調(diào)高報(bào)警值,通過設(shè)置-調(diào)低報(bào)警值。蜂鳴器報(bào)警電路在煙霧濃度高于報(bào)警值時(shí)啟動(dòng),有嘟嘟報(bào)警聲,低于不啟動(dòng)。
本設(shè)計(jì)采用電磁式蜂鳴器進(jìn)行。電磁式蜂鳴器由振蕩器、電磁線圈、磁鐵、振動(dòng)膜片及外殼等組成。接通電源后,振蕩器產(chǎn)生的音頻信號(hào)電流通過電磁線圈,使電磁線圈產(chǎn)生磁場。振動(dòng)膜片在電磁線圈和磁鐵的相互作用下,周期性地振動(dòng)發(fā)聲。因此需要一定的電流才能驅(qū)動(dòng)它,單片機(jī)I/O引腳輸出的電流較小,單片機(jī)輸出的TTL電平基本上驅(qū)動(dòng)不了蜂鳴器,因此需要增加一個(gè)電流放大的電路。蜂鳴器的正極接到VCC(+5V)電源上面,蜂鳴器的負(fù)極接到三極管的集電極C,三極管的基極B經(jīng)過限流電阻后由單片機(jī)的BEEP引腳控制,當(dāng)BEEP輸出低電平時(shí),三級(jí)管QS截止,沒有電流流過線圈,蜂鳴器不發(fā)聲;當(dāng)BEEP輸出高電平時(shí),三級(jí)管導(dǎo)通,這樣蜂鳴器的電流形成回路,發(fā)出聲音。
下圖檢測到煙霧濃度是192ppm,低于報(bào)警值200,蜂鳴器電路不工作。
下圖檢測到煙霧濃度是204ppm,大于等于報(bào)警值,三極管導(dǎo)通,蜂鳴器報(bà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; //定時(shí)器變量
uint8_t set_flag = 0;
float warming_val=200; //報(bào)警濃度大小
/* 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個(gè)周期
/* 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);//開啟定時(shí)器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); //啟動(dòng)ADC1
HAL_ADC_PollForConversion(&hadc1, 10); //等待ADC1轉(zhuǎn)換結(jié)束,超時(shí)設(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,超時(shí)10ms
HAL_UART_Transmit(&huart1, (uint8_t *)str, 6, 10); //串口1發(fā)送字符串,數(shù)組長度為5,超時(shí)10ms
HAL_UART_Transmit(&huart1, (uint8_t *)&"nr", 2, 10); //串口1發(fā)送字符串,數(shù)組長度為2,超時(shí)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,超時(shí)10ms
HAL_UART_Transmit(&huart1, (uint8_t *)str, 6, 10); //串口1發(fā)送字符串,數(shù)組長度為5,超時(shí)10ms
HAL_UART_Transmit(&huart1, (uint8_t *)&"nr", 2, 10); //串口1發(fā)送字符串,數(shù)組長度為2,超時(shí)10ms
if(temp>warming_val&&!set_flag){//如果超過報(bào)警值
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; //請(qǐng)
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è)計(jì)報(bào)告常用描述
鼠標(biāo)雙擊打開查找更多51 STM32單片機(jī)課程畢業(yè)設(shè)計(jì).url
資料下載鏈接(可點(diǎn)擊)