先说这次的试验:这次试验历经了差不多两个周,所获不少。最终完成了,仍是有点小激动的,这一起也是我的课程规划。这次的规划首要是经过读取DHT11和HCSR04的数值,(Proteus的传感器形似这两款好用一点)把读取的DHT11和HCSR04数值经过转化在lcd1602中显现出来,还有便是能够设置经过按键设置DHT11的温湿度阈值。
运转成果图:
按下K1,K2为加1设置,K3为减1设置
调整湿度最小值要为88%
湿度低于88%;警报响,红灯亮
在调DHT11使它正常,一起也跳一下超声波,看数据传输是否正常
当超越阈值时警报,红灯亮起,绿灯灭。下面是首要代码:
首要代码:
#include
#include "delay.h"
#include "DHT11.h"
#include "LCD1602.h"
#include "SOUN.h"
#include "KEY_ExitInit.h"
#include "LED_INIT.h"
#include "HCSR04.h"
unsigned char move = 0x82;
unsigned char RH,RL,TH,TL;
unsigned char h0 = 0x38,h1 = 0x36;
unsigned char t0 = 0x32,t1 = 0x38;
unsigned char DATA[4];//存温湿度数据
unsigned char Leg[4];//存超声波
unsigned char R[7] = "SD:";
unsigned char H[7] = "WD:";
unsigned char L[7] = "Legth:";
unsigned char r[2] = "%";
unsigned char h[2] = "C";
////////////////////////// 读DHT11数据及数据转化 ///////////////////////////////////////
int DHT11_ReadDATA(void)
{
int i;
unsigned char RHTH[5]={0};
DHT11_Start();//开端信号
for(i=0;i<5;i++)
{
RHTH[i] = DHT11_ReadByte();//循环读5次一字节 刚好40bit
}
if(RHTH[0]+RHTH[1]+RHTH[2]+RHTH[3] == RHTH[4])
{
RH = RHTH[0];
RL = RHTH[1];
TH = RHTH[2];
TL = RHTH[3];
DATA[0]='0'+(RH/10);//数据显现处理
DATA[1]='0'+(RH%10);
DATA[2]='0'+(TH/10);
DATA[3]='0'+(TH%10);
return 0;
}
else
{
return -1;
}
}
///////////////////////////// END //////////////////////////////////////////
//////////////////////////// 获取超声波数据及转化////////////////////////
void HCSR04_data(void)
{
int a = 0;
a = HCSR04_GetLength();
Leg[0]='0'+(a/10);
Leg[1]='0'+(a%10);
a = a*100;
Leg[2] = '0'+(a%100)/10;
Leg[3] = '0'; // '0'+(a%1000) 只显现到第三位,所以第四位写入0
}
/////////////////////////// END ///////////////////////////////////////////
//////////////////////////// 显现设定的阈值 ////////////////////////////////
void LCD_Config(void)
{
LCD_WRITE_StrDATA(1,1,"H:");
LCD_WRITE_char(1,3,h0);
LCD_WRITE_char(1,4,h1);
LCD_WRITE_StrDATA(2,1,"T:");
LCD_WRITE_char(2,3,t0);
LCD_WRITE_char(2,4,t1);
delay_ms(9);
}
/////////////////////////// END ///////////////////////////////////////
//////////////////////////// 显现温湿度和超声波数值,及超越阈值后警报 ///////////////////////////////////
void LCD_Show(void)
{
while(1)
{
HCSR04_data();
delay_ms(30);
if(DHT11_ReadDATA() == 0)//读取温湿度成功
{
delay_ms(80);
LCD_WRITE_CMD( 0x0C );
DHT11_ReadDATA(); //承受数据
LCD_WRITE_StrDATA(1,1,R);
LCD_WRITE_StrDATA(2,1,H);
LCD_WRITE_char(1,4,DATA[0]);
LCD_WRITE_char(1,5,DATA[1]);
LCD_WRITE_StrDATA(1,6,r);
LCD_WRITE_StrDATA(1,9,L);
LCD_WRITE_char(2,4,DATA[2]);
LCD_WRITE_char(2,5,DATA[3]);
LCD_WRITE_char(2,6,0xDF);
LCD_WRITE_StrDATA(2,7,h);
LCD_WRITE_char(2,9,Leg[0]);
LCD_WRITE_char(2,10,Leg[1]);
LCD_WRITE_char(2,11,0xA5);
LCD_WRITE_char(2,12,Leg[2]);
LCD_WRITE_char(2,13,Leg[3]);
LCD_WRITE_StrDATA(2,14,"cm");
}
if(RH > ((h0-0x30)*10+(h1-0x30))|| TH < ((t0-0x30)*10+(t1-0x30)) )//正常
{
GPIO_SetBits(GPIOC,GPIO_Pin_7);
GPIO_ResetBits(GPIOC,GPIO_Pin_6);
GPIO_SetBits(GPIOB,GPIO_Pin_7);
}
if(RH <= ((h0-0x30)*10+(h1-0x30))|| TH >= ((t0-0x30)*10+(t1-0x30)) ) //警报
{
GPIO_SetBits(GPIOC,GPIO_Pin_6);
GPIO_ResetBits(GPIOC,GPIO_Pin_7);
GPIO_ResetBits(GPIOB,GPIO_Pin_7);
}
}
}
/////////////////////////// END ///////////////////////////////////
/////////////////////// 中止服务函数 ,第一个按键切换显现+光标移动////////////////////////////////////////
void EXTI0_IRQHandler(void)
{
static int Flag = 1;
switch (Flag)
{
case 1:
LCD_WRITE_CMD( 0x01 ); //清楚显现
LCD_WRITE_CMD( 0x0f ); //敞开光标、显现、闪耀
LCD_Config(); //切换显现
LCD_WRITE_CMD( move );
delay_ms(5);break;
case 2:
LCD_WRITE_CMD( 0x0f );
move ++; //光标移动
LCD_WRITE_CMD( move );
delay_ms(5);break;
case 3:
LCD_WRITE_CMD( 0x0f );
move += 0x3f;
LCD_WRITE_CMD( move );
delay_ms(5);break;
case 4:
LCD_WRITE_CMD( 0x0f );
move ++;
LCD_WRITE_CMD( move );
delay_ms(5);break;
case 5:
move = 0x82; //光标复位
LCD_WRITE_CMD( 0x01 );
LCD_WRITE_CMD( 0x0C );
LCD_Show();
Flag = 0;
delay_ms(5);break;
}
Flag ++;
EXTI_ClearITPendingBit(EXTI_Line0);
}
///////////////////////// 中止0 END ///////////////////////////////////
///////////////////////// 中止 1 阈值加1 ///////////////////////////
void EXTI1_IRQHandler(void)
{
if(move == 0x82) //判别光标方位
{
if((h0-0x30) >= 9)
{
h0 = 0x30;
}
else
{
h0 ++;
}
}
if(move == 0x83) //判别光标方位
{
if((h1-0x30) >= 9)
{
h1 = 0x30;
}
else
{
h1 ++;
}
}
if(move == 0xc2) //判别光标方位
{
if((t0-0x30) >= 9)
{
t0 = 0x30;
}
else
{
t0 ++;
}
}
if(move == 0xc3) //判别光标方位
{
if((t1-0x30) >= 9)
{
t1 = 0x30;
}
else
{
t1 ++;
}
}
LCD_WRITE_CMD( 0x0c ); //为了看清楚,封闭光标的闪耀功用
LCD_Config(); //显现
delay_ms(5);
EXTI_ClearITPendingBit(EXTI_Line1);
}
////////////////////////////// 中止1 END ///////////////////////////////////////
///////////////////////////// 中止 2 阈值加1 ////////////////////////////////
void EXTI2_IRQHandler(void)
{
if(move == 0x82)
{
if((h0-0x30) == 0)
{
h0 = 0x39;
}
else
{
h0 –;
}
}
if(move == 0x83)
{
if((h1-0x30) == 0)
{
h1 = 0x39;
}
else
{
h1 –;
}
}
if(move == 0xc2)
{
if((t0-0x30) == 0)
{
t0 = 0x39;
}
else
{
t0 –;
}
}
if(move == 0xc3)
{
if((t1-0x30) == 0)
{
t1 = 0x39;
}
else
{
t1 –;
}
}
LCD_WRITE_CMD( 0x0c );
LCD_Config();
delay_ms(5);
EXTI_ClearITPendingBit(EXTI_Line2);
}
///////////////////////////// 中止 2 END //////////////////////////////////////
int main()
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
Systick_init();//滴答定时器的初始化—精准延时
LED_INIT(); //警报灯
SOUN_INIT(); //蜂鸣器
KEY_ExitInit(); //中止gpio口初始化
LCD_INIT(); //LCD1602初始化
HCSR04_GPIO(); //超声波gpio口初始化
HCSR04_TIM2_Config(); //定时器2设置
TIM2_IRQHandler(); //定时器中止函数
LCD_Show(); //lcd1602显现
return 0;
}