硬件:
1、單片機開發板
2、紅外遙控器(模擬人體發出紅外光)
3、壓力傳感器
4、LCD1602液晶顯示
軟件:
1、keil4
2、stc-isp
使用方法:
上電初始狀態液晶顯示NO PERSON 當在壓力傳感器放上一部手機時(我用的6sp)如果紅外遙控器不發出紅外光則液晶仍然顯示NO PERSON 反之則顯示CHILD(小孩)如果增加一部手機則顯示ADULT(大人)如果撤銷壓力傳感器的所有物體則顯示NO PERSON 此時如果重新放置物品在紅外遙控器發出紅外光的情況下屏幕仍然顯示NO PERSON
求大佬幫忙解讀程序。。。拜托拜托了。。。
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
51hei.png (24.78 KB, 下載次數: 50)
下載附件
2019-12-29 15:42 上傳
單片機源程序如下:
- #include "reg52.h"
- #include "lcd1602.h"
- #include "ds18b20.h"
- #include "i2c.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define GapValue 429.5
- sbit led = P1^2;
- sbit LED = P1^0;
- unsigned int qupi=0;
- unsigned long weight = 0;
- bit weight_flag = 0;
- bit temp_flag = 0;
- bit no_person = 0;
- uchar IrValue[6];
- uchar Time;
- uint temp;
- //void delay(uint n)
- //{
- // uint j;
- // for(j=0;j<n;j++)
- // {
- // _nop_();
- // }
- //}
- void init()
- {
- Write_Cmd(0x38); //設置16*2顯示
- Write_Cmd(0x0c); //顯示地址
- Write_Cmd(0x06);//地址指針移位命令
- Write_Cmd(0x01);////清屏
- TMOD|=0x01;//設置定時器0工作模式1
- TH0=(65536-50000)/256;//定時器裝初值
- TL0=(65536-50000)%256;
- EA=1; //開總中斷
- ET0=1; //開定時器0中斷
- TR0=1; //啟動定時器0
- }
- void Get_temp()
- {
- uchar L,M; //存儲溫度的高八位和低八位
- ds_init();//初始化DS18B20
- dsWait();
- write_byte(0xcc);//發送跳躍ROM指令
- write_byte(0x44);//發送溫度轉換指令
- ds_init();//初始化DS18B20
- dsWait();
- write_byte(0xcc);//發送跳躍ROM指令
- write_byte(0xbe);//讀取DS18B20暫存器值
- L = read_byte();//讀取溫度第八位
- M = read_byte();//讀取溫度高八位
- temp = M;
- temp <<= 8;
- temp |= L;
- temp = temp * 0.0625 + 0.5;//temp是16位數據其二進制每增加一那么它的十進制就增加1/16=0.0625,用temp乘以0.0625就可以得到其十進制數是多少,加0.5是為了四舍五入
- //write_weight(13,temp);
- }
- void main()
- {
- init();
- while(1)
- {
- if(temp_flag)
- {
- temp_flag = 0;
- Get_temp();
- if(temp > 35&&temp < 39)
- {
- no_person = 1;
- }
- else
- {
- no_person = 0;
- Write_Cmd(0x01); //清屏
- write_string(1,0,"NO PERSON");
-
- }
- }
-
- if(weight_flag)
- {
- weight_flag = 0;
- weight = ADC_read(0x03);
- // write_weight(1,weight);
- if(no_person)
- {
- if(weight<85||weight>250)
- {
- Write_Cmd(0x01); //清屏
- write_string(1,0,"NO PERSON");
- // write_weight(1,weight);
- no_person = 0;
- break;
- }
- if(weight>85&&weight<170)
- {
- Write_Cmd(0x01); //清屏
- write_string(1,0,"CHILD");
- // write_weight(1,weight);
- }
- if(weight>170&&weight<250)
- {
- Write_Cmd(0x01); //清屏
- write_string(1,0,"ADULT");
- // write_weight(1,weight);
- }
- }
- else
- {
- Write_Cmd(0x01); //清屏
- write_string(1,0,"NO PERSON");
- }
- }
- }
- }
- void timer0() interrupt 1
- {
- static uchar weight_count = 0,temp_count = 0;
- weight_count++;
- temp_count++;
- if(weight_count == 10)
- {
- weight_count = 0;
- weight_flag = 1;
- }
- if(temp_count == 10)
- {
- temp_count = 0;
- temp_flag = 1;
- }
- }
復制代碼- #include "reg52.h"
- #include "Hx711.h"
- #include "lcd1602.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define GapValue 429.5
- sbit led = P2^4;
- sbit IRIN=P3^2;
- unsigned int qupi=0;
- unsigned long weight = 0;
- bit weight_flag = 0;
- bit temp_flag = 0;
- bit no_person = 0;
- uchar IrValue[6];
- uchar Time;
- uint temp;
- void delay(uint n)
- {
- uint j;
- for(j=0;j<n;j++)
- {
- _nop_();
- }
- }
- void init()
- {
- Write_Cmd(0x38); //設置16*2顯示
- Write_Cmd(0x0c); //顯示地址
- Write_Cmd(0x06);//地址指針移位命令
- Write_Cmd(0x01);////清屏
- TMOD|=0x01;//設置定時器0工作模式1
- TH0=(65536-50000)/256;//定時器裝初值
- TL0=(65536-50000)%256;
- EA=1; //開總中斷
- ET0=1; //開定時器0中斷
- TR0=1; //啟動定時器0
- IT0=1;//下降沿觸發
- EX0=1;//打開中斷0允許
- IRIN=1;//初始化端口
- }
- void Get_Weight()
- {
- weight = HX711_Read();
-
- weight = (unsigned int)((float)weight*10/GapValue)-qupi; //計算實物的實際重量
- }
- void main()
- {
- init();
- while(1)
- {
- if(temp_flag)
- {
- temp_flag = 0;
- if(IrValue[2] == 0)
- {
- no_person = 0;
- Write_Cmd(0x01); //清屏
- write_string(1,0,"NO PERSON");
- }
- else
- {
- no_person = 1;
- }
- }
-
- if(weight_flag)
- {
- weight_flag = 0;
- Get_Weight();
- // write_weight(1,weight);
- if(no_person)
- {
- if(weight<1000||weight>5000)
- {
- Write_Cmd(0x01); //清屏
- write_string(1,0,"NO PERSON");
- // write_weight(1,weight);
- no_person = 0;
- break;
- }
- if(weight>1000&&weight<2000)
- {
- Write_Cmd(0x01); //清屏
- write_string(1,0,"CHILD");
- // write_weight(1,weight);
- }
- if(weight>2000&&weight<4000)
- {
- Write_Cmd(0x01); //清屏
- write_string(1,0,"ADULT");
- // write_weight(1,weight);
- }
- }
- else
- {
- Write_Cmd(0x01); //清屏
- write_string(1,0,"NO PERSON");
- }
- }
- }
- }
- void timer0() interrupt 1
- {
- static uchar weight_count = 0,temp_count = 0;
- weight_count++;
- temp_count++;
- if(weight_count == 10)
- {
- weight_count = 0;
- weight_flag = 1;
- }
- if(temp_count == 10)
- {
- temp_count = 0;
- temp_flag = 1;
- }
- }
- void ReadIr() interrupt 0
- {
- uchar j,k;
- uint err;
- Time=0;
- delay(700); //7ms
- if(IRIN==0) //確認是否真的接收到正確的信號
- {
-
- err=1000; //1000*10us=10ms,超過說明接收到錯誤的信號
- /*當兩個條件都為真是循環,如果有一個條件為假的時候跳出循環,免得程序出錯的時
- 侯,程序死在這里*/
- while((IRIN==0)&&(err>0)) //等待前面9ms的低電平過去
- {
- delay(1);
- err--;
- }
- if(IRIN==1) //如果正確等到9ms低電平
- {
- err=500;
- while((IRIN==1)&&(err>0)) //等待4.5ms的起始高電平過去
- {
- delay(1);
- err--;
- }
- for(k=0;k<4;k++) //共有4組數據
- {
- for(j=0;j<8;j++) //接收一組數據
- {
- err=60;
- while((IRIN==0)&&(err>0))//等待信號前面的560us低電平過去
- {
- delay(1);
- err--;
- }
- err=500;
- while((IRIN==1)&&(err>0)) //計算高電平的時間長度。
- {
- delay(10); //0.1ms
- Time++;
- err--;
- if(Time>30)
- {
- return; //認定通信失敗強制退出
- }
- }
- IrValue[k]>>=1; //k表示第幾組數據
- if(Time>=8)
- //如果大于0.8ms
- //如果高電平出現大于565us,那么是1
- {
- IrValue[k]|=0x80;
- }
- Time=0; //用完時間要重新賦值
- }
- }
- }
- if(IrValue[2]!=~IrValue[3])
- {
- return;
- }
- }
- }
復制代碼
所有資料51hei提供下載:
汽車座椅有無人狀態監測.zip
(268.85 KB, 下載次數: 24)
2019-12-27 09:15 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|