基于STM32的温度、电流、电压检测proteus仿真系统(OLED、DHT11、继电器、电机)
目录
一、主要功能
二、硬件资源
三、程序编程
四、实现现象
一、主要功能
基于STM32F103C8T6 采用DHT11读取温度、滑动变阻器模拟读取电流、电压。
通过OLED屏幕显示,设置电流阈值为80,电流小阈值为50,电压阈值为60,温度阈值为30
随便哪个超过预祝,则继电器切断,LED灯灭掉,若电流小于50,则屏幕清屏,表示待机。
二、硬件资源
基于KEIL5编写C++代码,PROTEUS8.15进行仿真,全部资源在页尾,提供安装包。
三、程序编程
#include "main.h"
#include "adc.h"
#include "gpio.h"
#include "./HAL/key/key.h"
#include "./HAL/OLED/OLED_NEW.H"
#include "./HAL/dht11/dht11.h"
void Monitor_function(void); //监测函数
void Display_function(void); //显示函数
void Manage_function(void); //处理函数
#define LED(a) (a?HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET):HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET))
#define BEEP(a) (a?HAL_GPIO_WritePin(BEEP_GPIO_Port, BEEP_Pin, GPIO_PIN_RESET):HAL_GPIO_WritePin(BEEP_GPIO_Port, BEEP_Pin, GPIO_PIN_SET))
uint8_t adc_ch; //adc的个数
uint32_t adc_buf[4]; //adc数值的存储数组
uint16_t temp,humi; //温湿度
uint16_t dl,dy,wdnum; //电流 电压 温度
uint16_t dlmin=50,dlmax=80,dymax=60,wdmax=300; //电流最小50 最大80 电压最大60 温度最大30
uint8_t flag_led,flag_beep; //灯、报警标志位
uint16_t time_num;
static int mode=0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
uint16_t dong_get_adc(){
//开启ADC1
HAL_ADC_Start(&hadc1);
//等待ADC转换完成,超时为100ms
HAL_ADC_PollForConversion(&hadc1,100);
//判断ADC是否转换成功
if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1),HAL_ADC_STATE_REG_EOC)){
//读取值
return HAL_ADC_GetValue(&hadc1);
}
return 0;
}
/****
*******监测函数
*****/
void Monitor_function(void)
{
DHT11_Read_TempAndHumidity(&DHT11_Data);//调用获取温湿度、电流、电压
temp = DHT11_Data.temperature; //获取温度
humi = DHT11_Data.humidity; //获取湿度
//将获取的值存储到adc_buf中
for(adc_ch=0;adc_ch<4;adc_ch++){
//分别存放通道1、2、3、4的ADC值
adc_buf[adc_ch]=dong_get_adc();
}
dl=adc_buf[0]/4096.00*100; //电流
dy=adc_buf[3]/4096.00*100; //电压
}
/****
*******显示函数
*****/
void Display_function(void)
{
//第一行
Oled_ShowCHinese(0,0,"电流");
Oled_ShowString(32,0,":");
OLED_ShowNum(40,0,dl,2,16);
Oled_ShowCHinese(64,0,"电压");
Oled_ShowString(96,0,":");
OLED_ShowNum(104,0,dy,2,16);
//第二行
Oled_ShowCHinese(0,2,"温度");
Oled_ShowString(32,2,":");
OLED_Show_Temp(40,2,temp);
//第三行
// Oled_ShowCHinese(0,4,"湿度");
// Oled_ShowString(32,4,":");
// OLED_Show_Humi(40,4,humi/10);
}
/****
*******处理函数
*****/
void Manage_function(void)
{
if(dl > dlmax) //电流超过电流MAX
{
flag_led=0;
flag_beep=1;
}
if(dy> dymax ) //电压大于电压MAX
{
flag_led=0;
flag_beep=1;
}
if(temp>wdmax) //温度大于温度MAX
{
flag_led=0;
flag_beep=1;
}
if(dl>dlmin && dl < dlmax && dy < dymax && temp < wdmax)
{
flag_led=1;
flag_beep=0;
}
if(dl<dlmin)
{
mode = 1;
}
if(flag_beep==1)
BEEP(1);
else
BEEP(0);
if(flag_led==1)
LED(1);
else
LED(0);
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init(); //GPIO口设置
MX_ADC1_Init(); //ADC转换
OLED_Init(); //OLED初始化
OLED_Clear(); //OLED清屏
flag_led = 0;
while (1)
{
if(mode == 0)
{
Monitor_function(); //监测函数
Display_function(); //显示函数
Manage_function(); //处理函数
}
else
{
OLED_Clear(); //OLED清屏
}
HAL_Delay(10);
time_num++;
if(time_num >= 5000)
time_num = 0;
}
}
/**
* @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_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
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_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_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 */
/* 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 */
四、实现现象
具体动态效果看B站演示视频:
基于STM32的温度、电流、电压检测系统(OLED、DHT11、继电器、电机)_哔哩哔哩_bilibili
全部资料(源程序、仿真文件、安装包、演示视频):
通过百度网盘分享的文件:基于STM32的温度、电流、电压检测系统(1).zip
链接:https://pan.baidu.com/s/1h93-TTKkkdf2hBryU9v55Q?pwd=p4sq
提取码:p4sq
--来自百度网盘超级会员V4的分享