基于STM32的蔬菜大棚环境监控系统设计的主程序设计
时间: 2023-06-13 12:06:01 浏览: 134
基于STM32F407xx的PID温湿度监控系统设计,蔬菜大棚、仓库,包含原理图,代码,连线图,器件选型
5星 · 资源好评率100%
由于蔬菜大棚环境监控系统需要涉及到多个传感器的数据采集和控制,因此主程序设计需要考虑多任务调度和数据处理的问题。以下是一个基于STM32的蔬菜大棚环境监控系统的主程序设计示例:
```C
#include "stm32f10x.h"
#include "stdio.h"
#include "dht11.h"
#include "lcd1602.h"
#include "ds18b20.h"
#define DHT11_PIN GPIO_Pin_8
#define DHT11_PORT GPIOB
#define DS18B20_PIN GPIO_Pin_0
#define DS18B20_PORT GPIOA
uint8_t humi, temp;
float temp_ds18b20;
void delay_ms(uint16_t nms)
{
uint32_t temp;
SysTick->LOAD = 9*nms;
SysTick->VAL = 0x00;
SysTick->CTRL = 0x01;
do
{
temp = SysTick->CTRL;
}while(temp&0x01 && !(temp&(1<<16)));
SysTick->CTRL = 0x00;
SysTick->VAL = 0x00;
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
DHT11_Start();
delay_ms(20);
DHT11_Read_Data(&humi, &temp);
}
}
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
DS18B20_Start();
DS18B20_Read_Temp(&temp_ds18b20);
}
}
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3|RCC_APB1Periph_TIM4, ENABLE);
SysTick_Config(SystemCoreClock/1000);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 7199;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DHT11_Init(DHT11_PORT, DHT11_PIN);
DS18B20_Init(DS18B20_PORT, DS18B20_PIN);
LCD1602_Init();
LCD1602_Clear();
TIM_Cmd(TIM3, ENABLE);
TIM_Cmd(TIM4, ENABLE);
while(1)
{
LCD1602_Set_Cursor(0, 0);
LCD1602_Print("Humidity:");
LCD1602_Set_Cursor(0, 1);
LCD1602_Print("Temperature:");
LCD1602_Set_Cursor(10, 0);
LCD1602_Print_Num(humi);
LCD1602_Print("%");
LCD1602_Set_Cursor(13, 1);
LCD1602_Print_Num((int)temp_ds18b20);
LCD1602_Print(".");
LCD1602_Print_Num((int)(temp_ds18b20*10)%10);
LCD1602_Print("C");
delay_ms(500);
}
}
```
上述代码中,使用了定时器3和定时器4分别定时20ms和1s,分别用于DHT11温湿度传感器的数据采集和DS18B20温度传感器的数据采集。在主循环中通过LCD1602显示监测到的温湿度数据和DS18B20传感器的温度数据。需要注意的是,本示例代码中使用了DHT11、DS18B20和LCD1602等外设库,需要自行添加相应的库文件。同时,也需要根据实际连接情况修改宏定义和初始化代码。
阅读全文