标题:
STM32单片机烟雾检测,报警系统程序已完成调试,仅供参考
[打印本页]
作者:
刘宸硕
时间:
2022-6-14 16:27
标题:
STM32单片机烟雾检测,报警系统程序已完成调试,仅供参考
STM32,烟雾检测,报警系统,程序已完成调试,可以直接使用
单片机源程序如下:
#include "main.h"
#include "stm32f1xx_hal.h"
/* USER CODE BEGIN Includes */
#include "STM32_LCD1602.h"
#include "DS18B20.h"
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
TIM_HandleTypeDef htim3;
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
//下面定义按键部分
#define Key1 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_13)
#define Key2 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14)
#define Key3 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_15)
unsigned char Key1_flag=0;
unsigned char Key2_flag=0;
unsigned char Key3_flag=0;
unsigned char sec1=0;
unsigned char sec2=0;
//下面定义水泵,指示灯,蜂鸣器
#define SB_0 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,GPIO_PIN_RESET)
#define SB_1 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,GPIO_PIN_SET)
#define LED0_0 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11,GPIO_PIN_RESET)
#define LED0_1 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11,GPIO_PIN_SET)
#define LED1_0 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_RESET)
#define LED1_1 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_SET)
#define Beep_0 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET)
#define Beep_1 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET)
unsigned char beep1=0;
//下面定义一些变量,用作烟雾
unsigned char YW_H=0;
unsigned char YW=0;
//下面定义关于显示
unsigned char State=0;
unsigned char s0=0;
//下面定义关于存储的
unsigned char memory_flag=1;
//下面定义关于AD检测的
unsigned int adcBuf[5];
unsigned char Read_ADC;
//下面定义关于定时器的
unsigned int Time3_ms=0;
//下面定义关于温度部分
unsigned char Read_DS18B20=1;
int DS18B20_Temp=0;
int DS18B20_Temp_flsh=0; //出现一种情况,水泵工作的时候,18B20会重启一次,杨说,不让突变,突变数据不刷新
unsigned char DS18B20_Temp_H=0;
unsigned char DS18B20_Temp_flag=0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
static void MX_TIM3_Init(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
void Write_flsh_byte(uint32_t EEPROM_ADD,uint32_t EEPROM_Data)
{
//1、解锁FLASH
HAL_FLASH_Unlock();
//2、擦除FLASH
//初始化FLASH_EraseInitTypeDef
FLASH_EraseInitTypeDef f;
f.TypeErase = FLASH_TYPEERASE_PAGES;
f.PageAddress = EEPROM_ADD;
f.NbPages = 1;
//设置PageError
uint32_t PageError = 0;
//调用擦除函数
HAL_FLASHEx_Erase(&f, &PageError);
//3、对FLASH烧写
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, EEPROM_ADD, EEPROM_Data);
//4、锁住FLASH
HAL_FLASH_Lock();
}
//FLASH读取数据测试
uint32_t readFlash(uint32_t EEPROM_ADD)
{
uint32_t temp = *(__IO uint32_t*)(EEPROM_ADD);
return temp;
}
void memory()
{
if(memory_flag==1)
{
memory_flag=0;
Write_flsh_byte(0x0800F000,YW_H);
Write_flsh_byte(0x0800E000,DS18B20_Temp_H);
}
}
void read_memory()
{
YW_H=readFlash(0x0800F000);
DS18B20_Temp_H=readFlash(0x0800E000);
if(YW_H>100) YW_H=20;
if(DS18B20_Temp_H>100) YW_H=30;
}
void Key_Dispose() //按键处理函数
{
if(!Key1)
{
if(Key1_flag)
{
Key1_flag=0;
State=(State+1)%3;
}
}
else Key1_flag=1;
if(!Key2)
{
if(Key2_flag||sec1==0)
{
Key2_flag=0;
switch(State)
{
case 1: if(DS18B20_Temp_H<100) DS18B20_Temp_H++; break;
case 2: if(YW_H<100) YW_H++; break;
}
}
}
else
{
if(Key2_flag==0)
{
Key2_flag=1;
memory_flag=1;
}
sec1=2;
}
if(!Key3)
{
if(Key3_flag||sec2==0)
{
Key3_flag=0;
switch(State)
{
case 1: if(DS18B20_Temp_H>0) DS18B20_Temp_H-- ; break;
case 2: if(YW_H>0) YW_H--; break;
}
}
}
else
{
if(Key3_flag==0)
{
Key3_flag=1;
memory_flag=1;
}
sec2=2;
}
}
void Display()
{
if(State==0)
{
LCD1602_write(0,0x80);
LCD1602_writebyte((unsigned char *)" Temp:");
LCD1602_write(1,0x30+DS18B20_Temp/100%10);
LCD1602_write(1,0x30+DS18B20_Temp/10%10);
LCD1602_writebyte((unsigned char *)".");
LCD1602_write(1,0x30+DS18B20_Temp%10);
LCD1602_write(1,0xdf);
LCD1602_writebyte((unsigned char *)"C ");
LCD1602_write(0,0xC0);
LCD1602_writebyte((unsigned char *)"Smoke:");
LCD1602_write(1,0x30+YW/100%10);
LCD1602_write(1,0x30+YW/10%10);
LCD1602_write(1,0x30+YW%10);
LCD1602_writebyte((unsigned char *)"% ");
}
else
{
LCD1602_write(0,0x80);
LCD1602_writebyte((unsigned char *)" Temp_H:");
if(State==1&&s0==0) LCD1602_writebyte((unsigned char *)" ");
else
{
if(DS18B20_Temp_H>99) LCD1602_write(1,0x30+DS18B20_Temp_H/100%10);
else LCD1602_writebyte((unsigned char *)" ");
LCD1602_write(1,0x30+DS18B20_Temp_H/10%10);
LCD1602_write(1,0x30+DS18B20_Temp_H%10);
}
LCD1602_write(1,0xdf);
LCD1602_writebyte((unsigned char *)"C ");
LCD1602_write(0,0xC0);
LCD1602_writebyte((unsigned char *)"Smoke_H:");
if(State==2&&s0==0) LCD1602_writebyte((unsigned char *)" ");
else
{
if(YW_H>99) LCD1602_write(1,0x30+YW_H/100%10);
else LCD1602_writebyte((unsigned char *)" ");
LCD1602_write(1,0x30+YW_H/10%10);
LCD1602_write(1,0x30+YW_H%10);
}
LCD1602_writebyte((unsigned char *)"% ");
}
}
void Get_ADC()
{
if(Read_ADC==1)
{
Read_ADC=0;
HAL_ADC_Start(&hadc1); //启动Ad检测, MQ-2,能将检测到的烟雾量化成 0-5V电压数据输出,这里最终转化成百分比输出
HAL_ADC_PollForConversion(&hadc1,0xffff); //等待检测结束
// YW=(HAL_ADC_GetValue(&hadc1))/124; //--->这一句是测试,看从AD值到电压数据对不对,结果是正确的,结果是精确到小数点后一位的电压数据
// 就目前电路来说,节点电压理论最高 2.5V,所以最大AD值应该是 3102.272727272728,所以将 AD值 0 到 3102.272727272728 量化为 0-100 计算得到系数为------> 31.02272727272728
YW=(HAL_ADC_GetValue(&hadc1))/31.02272727272728; //AD值除以系数,量化成 0 - 100数据
if(YW>=100) YW=100; //不可能会出现大于100,除非节点电压大于2.5 ,但是还是过滤一下
HAL_ADC_Stop(&hadc1); //结束AD检测
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim==&htim3)
{
Time3_ms++;
if(Time3_ms % 50 == 0)
{
Key_Dispose();
}
if(Time3_ms %200 == 0)
{
s0=s0^0x01; //取反
if(beep1==1)
{
GPIOB->ODR ^= GPIO_PIN_0;
}
else Beep_0;
}
if(Time3_ms%1000==0)
{
if(sec1!=0) sec1--;
if(sec2!=0) sec2--;
Read_ADC=1;
}
if(Time3_ms>2000)
{
Time3_ms=0;
Read_DS18B20=1;
}
}
}
void Police()
{
if((DS18B20_Temp>=DS18B20_Temp_H*10)||(YW>=YW_H)) beep1=1;
else beep1=0;
if(DS18B20_Temp>DS18B20_Temp_H*10)LED0_0;
else LED0_1;
if(YW>=YW_H)LED1_0;
else LED1_1;
if((DS18B20_Temp>=DS18B20_Temp_H*10)&&(YW>=YW_H)) SB_1;
else SB_0;
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
*
* @retval None
*/
int main(void)
{
/* USER CODE BEGIN 1 */
int Temp_Error=0;
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* 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_TIM3_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim3);
LCD1602_cls();
read_memory();
Get_ADC();
do
{
DS18B20_Temp=DS18B20_Get_Temp();
}while(DS18B20_Temp==850);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
Display();
memory();
Get_ADC();
Police();
if(Read_DS18B20==1)
{
Read_DS18B20=0;
Temp_Error= DS18B20_Get_Temp();
if(Temp_Error!=850)
{
DS18B20_Temp=Temp_Error;
}
}
// if(Read_DS18B20==1)
// {
// Read_DS18B20=0;
// if(DS18B20_Temp_flag==1)
// {
// DS18B20_Temp_flag=0;
// DS18B20_Temp_flsh =DS18B20_Get_Temp();
// }
// else
// {
// DS18B20_Temp_flag=1;
// if(DS18B20_Temp_flsh>=DS18B20_Get_Temp())
// {
// if(DS18B20_Temp_flsh-DS18B20_Get_Temp()<100) DS18B20_Temp=DS18B20_Get_Temp();
//
// }
// else
// {
// if(DS18B20_Get_Temp()-DS18B20_Temp_flsh<100) DS18B20_Temp=DS18B20_Get_Temp();
// }
// }
//
// }
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Configure the Systick interrupt time
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
/**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
/* ADC1 init function */
static void MX_ADC1_Init(void)
{
ADC_ChannelConfTypeDef sConfig;
/**Common config
*/
hadc1.Instance = ADC1;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_9;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
/* TIM3 init function */
static void MX_TIM3_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
htim3.Instance = TIM3;
htim3.Init.Prescaler = 63;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 1000;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
/** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_RESET);
/*Configure GPIO pins : PB0 PB10 PB11 PB12 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pins : PB13 PB14 PB15 */
GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @param file: The file name as string.
* @param line: The line in file as a number.
* @retval None
*/
void _Error_Handler(char *file, int line)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
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,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/**
* @}
*/
/**
* @}
*/
复制代码
Keil代码下载:
代码.7z
(2.91 MB, 下载次数: 22)
2022-6-16 01:16 上传
点击文件名下载附件
STM32的烟雾报警系统程序
下载积分: 黑币 -5
作者:
尘土的飞扬
时间:
2022-6-15 08:39
如果展示一些文字和图片应该更好
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1