智能燃气泄漏报警器
作者:蓝色天使
五月下旬拿到板子,只把硬件和数据采集调通。蓝牙以及aap数据上传没有做。现将完成部分提交。
一、作品简介
(智能燃气泄漏报警器基于TGS813可燃气体传感器感知空气中燃气浓度转换对应的电阻值,RSL10蓝牙开发板通过IIC接口,用高精度AD MCP3421将传感器电阻值转换燃气浓度值,进行判断。超出安全值,通过无线蓝牙发送至手机告知用户通风检查。
二、系统框图
软件架构:RSL10 系统初始化之后 (包含系统时钟,定时器,蓝牙,IIC接口)。系统进入休眠状态每隔5分钟(这个时间可以通过手机APP自行设定)定时器中断唤醒主机采集一次数据,通过蓝牙发给手机。手机对数据进行判断,如果异常提醒用户。
三、各部分功能说明
电源部分:将AC220V转换为DC15V:
传感器&ADC部分:该部分就是为可燃性气体传感器TGS813提供电源系统(按照DATASHEET操作)为AD采集提供条件;然后通过IIC接口与RSL10I蓝牙版IIC接口通信
四、作品源码
目前因为蓝牙部分还没有调通,传感器部分是在STM32F103RCT6上调试的。现将传感器采集部分展示出来:
IIC接口初始化:
/**
******************************************************************************
* File Name : I2C.c
* Description : This file provides code for the configuration
* of the I2C instances.
******************************************************************************
*
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "i2c.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
I2C_HandleTypeDef hi2c1;
/* I2C1 init function */
void MX_I2C1_Init(void)
{
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 100000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
}
void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(i2cHandle->Instance==I2C1)
{
/* USER CODE BEGIN I2C1_MspInit 0 */
/* USER CODE END I2C1_MspInit 0 */
__HAL_RCC_GPIOB_CLK_ENABLE();
/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* I2C1 clock enable */
__HAL_RCC_I2C1_CLK_ENABLE();
/* USER CODE BEGIN I2C1_MspInit 1 */
/* USER CODE END I2C1_MspInit 1 */
}
}
void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
{
if(i2cHandle->Instance==I2C1)
{
/* USER CODE BEGIN I2C1_MspDeInit 0 */
/* USER CODE END I2C1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_I2C1_CLK_DISABLE();
/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6);
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7);
/* USER CODE BEGIN I2C1_MspDeInit 1 */
/* USER CODE END I2C1_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
MCP3421采集:
#include "mcp3421.h"
#include "usart.h"
#include "i2c.h"
/***************************************************************************/
void MCP3421_Init(void)
{
if(HAL_I2C_IsDeviceReady(&hi2c1,MCP3421ADDR,5,100)==HAL_OK)
// if(!HAL_I2C_IsDeviceReady(&hi2c1,MCP3421ADDR,5,100))
{
printf("MCP3421 is ok.\n\r");
}
else
{
printf("MCP3421 is error.\n\r");
}
uint8_t parameter = 0x9c; //系统参数配置为18位精度,自动转换模式,无PGA增益。配置字见数据手册。
HAL_I2C_Master_Transmit(&hi2c1,MCP3421ADDR,¶meter,1,100);
HAL_Delay(5);
while(HAL_I2C_IsDeviceReady(&hi2c1,MCP3421ADDR,5,100))
{
HAL_Delay(5);
}
printf ("MCP3421 RESET is OK.\n\r");
}
/************************************************************************/
uint32_t MCP3421_Get_Value(void)
{
uint32_t ADC_Value;
uint8_t Adc_Temp[3]={0};
if(HAL_I2C_IsDeviceReady(&hi2c1,MCP3421ADDR,5,100)==HAL_OK)
{
HAL_I2C_Master_Receive(&hi2c1,MCP3421ADDR,Adc_Temp,3,100);
ADC_Value = Adc_Temp[0];
ADC_Value&=0x03;
ADC_Value<<=8;
ADC_Value+= Adc_Temp[1];
ADC_Value<<=8;
ADC_Value+= Adc_Temp[2];
}
else
{
}
return ADC_Value;
}
/*****************************************************************/
void Voltage_Convert(void)
{
uint32_t Voltage;
float Measure;
Voltage = MCP3421_Get_Value();
printf("Voltage = %d\n\r",Voltage);
if((Voltage&0x00020000)==0x00020000) //判断最高位符号位是否为为1;为“1”则为负电压
{
Voltage&=0x0001ffff;
Voltage=(~Voltage)+1;
Voltage &=0x0001ffff;
Measure = 15.625*Voltage; //15.625为18位分辨率的精度。其他分辨率的精度见数据手册。
Measure = Measure *0.001;
printf("Measure Result Voltage = -%fmv\n\r",Measure);
}
else //为“0”则正电压直接转换
{
Voltage&=0x0001ffff;
Measure = 15.625*Voltage;
Measure = Measure *0.001;
printf("Measure Result Voltage = %fmv\n\r",Measure);
}
}
/******************************************************/
五、作品功能演示视频
(暂无)
六、项目总结
(暂无)
七、其他