|
400黑幣
串口模塊:
/**********************************
包含頭文件
**********************************/
#include "uart.h"
uchar uart_num;
/**********************************
函數(shù)定義
**********************************/
/****
*******串口初始化函數(shù)
*****/
void Uart_Init(void)
{
EA=1; //打開(kāi)總中斷
ES=0;
TMOD|=0x20; //設(shè)置定時(shí)器1工作方式為方式2
TR1=1; //啟動(dòng)定時(shí)器1
TH1=0xfd; //波特率9600
TL1=0xfd;
SCON=0X50; //允許接收
}
/****
*******串口發(fā)送一個(gè)字節(jié)函數(shù)
*****/
void Uart_Sent_Char(uchar date)
{
SBUF=date;
while(!TI);
TI=0;
}
/****
*******串口發(fā)送字符串函數(shù)
*****/
void Uart_Sent_Str(uchar *date)
{
while(*date != '\0')
Uart_Sent_Char(*date++);
}
/****
******* 串口中斷服務(wù)程序函數(shù)
*****/
void ser() interrupt 4
{
static uchar string[1]; //接收一位數(shù)據(jù),如果接收兩位數(shù)據(jù),則string[2]
if(RI)
{
RI=0;
string[0]=SBUF;
switch(string[0])
{
case('1'): uart_num = 1 ;break;
case('2'): uart_num = 2 ;break;
default: break;
}
}
}
主程序:#include "main.h"
#include "lcd1602.h"
#include "key.h"
#include "motor_zhiliu.h"
#include "ds1302.h"
#include "adc.h"
#include "dht11.h"
#include "uart.h"
/**********************************
變量定義
**********************************/
uchar key_num = 0; //按鍵標(biāo)志位
uchar time_num = 0; //延時(shí)標(biāo)記位
uchar flag_light = 0;
bit flag_person_cont = 0; //人數(shù)總計(jì),只確認(rèn)有沒(méi)有人所有用“bit”,只能是0和1
bit flag_per_add = 0; //判斷人數(shù)增加
bit flag_per_cut = 0; //判斷人數(shù)減少
bit flag_display_obj = 0; //顯示標(biāo)志位
uint temp,humi,ch4_value,light_value; //溫度,濕度,可燃性氣體,光照強(qiáng)度的參數(shù)
extern uchar light,uart_num; //繼承亮度參數(shù)
uchar send_buf[16]; //發(fā)送數(shù)據(jù)包
/**********************************
函數(shù)聲明
**********************************/
void Delay_function(uint x); //延時(shí)函數(shù)
void Key_function(void); //按鍵函數(shù)
void Monitor_function(void); //監(jiān)測(cè)函數(shù)
void Display_function(void); //顯示函數(shù)
void Manage_function(void); //處理函數(shù)
/****
******* 主函數(shù)
*****/
void main()
{
while(DHT11_Init()); //DHT11初始化
Delay_function(10); //延時(shí)10ms
Lcd1602_Init(); //LCD1602初始化
Delay_function(10); //延時(shí)10ms
lcd1602_clean(); //清屏
Delay_function(10); //延時(shí)10ms
Ds1302_Init(); //時(shí)間芯片初始化
Delay_function(10); //延時(shí)10ms
Init_timer(); //串口初始化
Uart_Init(); //定時(shí)器初始化
IR_per = 1;
// ds1302_write_time(); //向DS302寫(xiě)入時(shí)鐘數(shù)據(jù),只在第一次燒錄時(shí)開(kāi)啟
while(1)
{
Key_function(); //按鍵函數(shù)
Monitor_function(); //監(jiān)測(cè)函數(shù)
Display_function(); //顯示函數(shù)
Manage_function(); //處理函數(shù)
Delay_function(1); //延時(shí)10ms
time_num++; //計(jì)時(shí)變量+1
if(time_num > 100)
{
sprintf(send_buf,"溫度:%.1f%℃",temp/10.0); //發(fā)送溫度數(shù)據(jù)采用浮點(diǎn)型
Uart_Sent_Str(send_buf);
sprintf(send_buf,"濕度:%.1f%%",humi/10.0); //發(fā)送濕度數(shù)據(jù)采用浮點(diǎn)型
Uart_Sent_Str(send_buf);
sprintf(send_buf,"藍(lán)牙標(biāo)記:%d",uart_num); //發(fā)送濕度數(shù)據(jù)采用浮點(diǎn)型
Uart_Sent_Str(send_buf);
time_num = 0;
}
}
}
/****
******* 延時(shí) x ms函數(shù)
*****/
void Delay_function(uint x)
{
uint m,n;
for(m=x;m>0;m--)
for(n=110;n>0;n--);
}
/****
*******按鍵函數(shù)
*****/
void Key_function(void)
{
key_num = Chiclet_Keyboard_Scan(); //按鍵掃描
if(key_num != 0) //有按鍵按下
{
switch(key_num)
{
case 1: //按鍵1
flag_person_cont = 1; //開(kāi)燈
flag_light = 1;
break;
case 2: //按鍵2
flag_person_cont = 0; //關(guān)燈
flag_light = 0;
break;
case 3: //按鍵3
flag_display_obj = !flag_display_obj; //一共4個(gè)模式//顯示標(biāo)志位+1
lcd1602_clean(); //清屏
}
}
}
/****
*******監(jiān)測(cè)函數(shù)
*****/
void Monitor_function(void)
{
if(time_num % 5 == 0) //50ms檢測(cè)一次
{
ds1302_read_time(); //從DS302讀出時(shí)鐘數(shù)據(jù)
Dht11_Get_Temp_Humi_Value(&temp,&humi); //溫度、濕度
ch4_value = 100*((Adc0832_Get_Value(0)/256.0)*5); //獲取甲烷值
light_value = (100 - 20*((Adc0832_Get_Value(1)/256.0)*5))*10; //獲取光照值
}
}
/****
*******顯示函數(shù)
*****/
void Display_function(void)
{
if(!flag_display_obj) //界面1
{
lcd1602_display_str(1,0,"Light:");
lcd1602_display_str(2,0,"LEL:");
lcd1602_display_num(1,14,flag_person_cont);
lcd1602_display_light(1, 6, light_value);
lcd1602_display_gas(2, 6, ch4_value);
}
else //界面2
{
lcd1602_display_str(1,0,"Temp:");
lcd1602_display_str(2,0,"Humi:");
lcd1602_display_num(1,14,flag_person_cont);
lcd1602_display_temp(1,6,temp);
lcd1602_display_humi(2,6,humi);
}
}
/****
*******處理函數(shù)
*****/
void Manage_function(void)
{
if(uart_num == 1)
{
flag_light = 1;
}
if(uart_num == 2)
{
flag_light = 0;
}
if(ch4_value > 100)
{
in3 = 0;
in4 = 1;
}
else
{
in3 = 0;
in4 = 0;
}
if(time_buf[4] > 0x19|| time_buf[4] < 0x09)
{
if(!flag_light) //判斷是否為關(guān)燈狀態(tài)
{
if(IR_per == 0) //人體紅外檢測(cè)
{
Delay_function(1); //1ms消抖
if(IR_per == 0) //人體紅外檢測(cè)為低電平,確認(rèn)人數(shù)
{
light = 2;
}
while(IR_per) //while循環(huán),直到抬手跳出->按鍵按下沒(méi)有松開(kāi)的狀態(tài)
{
Key_function(); //按鍵函數(shù)
Monitor_function(); //監(jiān)測(cè)函數(shù)
Display_function(); //顯示函數(shù)
};
}
else
{
light = 0;
}
}
else
{
light = 9;
}
}
else
{
//兩路紅外檢測(cè)人數(shù),檢測(cè)到有人進(jìn)入時(shí)人數(shù)為1
if(flag_per_add == flag_per_cut)
{
if((IR_door_forw == 1)&&(flag_per_add == 0))
{
Delay_function(1);
if(IR_door_forw == 1)
{
flag_person_cont = flag_person_cont;
}
while(IR_door_forw);
flag_per_cut = 1;
}
if((IR_door_back == 1)&&(flag_per_cut == 0))
{
Delay_function(1);
if(IR_door_back == 1)
{
flag_person_cont = flag_person_cont;
}
while(IR_door_back);
flag_per_add =1;
}
if((IR_door_forw == 1)&&(flag_per_add == 1))
{
Delay_function(1);
if(IR_door_forw == 1)
{
//此為出去時(shí)判斷,再此不做處理,可添加
}
while(IR_door_forw);
flag_per_add = 0;
}
if((IR_door_back == 1)&&(flag_per_cut == 1))
{
Delay_function(1);
if(IR_door_back == 1)
{
flag_person_cont = 1;
}
while(IR_door_back);
flag_per_cut = 0;
}
}
else
{
flag_per_cut = 0;
flag_per_add = 0;
}
//通過(guò)人數(shù)判斷開(kāi)燈,并且燈的亮度與光強(qiáng)相反
if(flag_person_cont == 1)
{
light = (1000 - light_value)/100;
}
else
{
light = 0;
}
}
}
|
|