标题:
汽车座椅有无人状态监测设计Proteus仿真电路+单片机程序 如何理解?
[打印本页]
作者:
952795279527
时间:
2019-12-27 09:16
标题:
汽车座椅有无人状态监测设计Proteus仿真电路+单片机程序 如何理解?
硬件:
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
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1