这是
仿真和程序,查看了半天不知道哪里出了问题请大神们指正
#include<reg52.h>
#include <absacc.h>
#define uint unsigned int
#define uchar unsigned char
sbit rs=P2^0;
sbit rw=P2^1;
sbit en=P2^2;
sbit DQ=P3^4; // 18b20总线
/*****/
sbit ST=P0^1;
sbit SH=P0^2;
sbit Sjia=P3^2;
sbit Sjian=P3^3;
sbit BJ=P0^5;
uint tvalue;//温度值
uchar tflag;//温度正负标志
uint tem0,tem1;
uint temp0,temp1;
uint f=0;
int hhhh=0;
int tsheding=20;//设置温度值
int hsheding=40;//设置湿度值
/******/
unsigned char dat[9]={0}; //温度
unsigned char shuju1[9]={0}; //湿度
uchar code table1[17]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x54,0x45,0x4d,0x50,0x2d,0x2e,0x20}; //温度调用
uchar code table2[16]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x53,0x48,0x49,0x25,0x2e,0x20}; //湿度调用
/**************1602d调用函数*******************/
void write_com(uchar com);
void write_shuju(uchar shuju);
void init();
void delay(uchar z);
/**************18b20调用函数**************/
unsigned char reset(void); //18b20初始化//
void write(uchar dat); //18b20写时序
unsigned char read(void); //18b20读时序
void readtemperature(void); //18b20读二进制16位温度
void yanshi(unsigned int us);
/****************hs1101调用函数*************************/
void readfrequency(void); // 读频率函数//
bit minrange=0; //湿度下限标志位//
bit maxrange=0; //湿度上限标志位//
bit T0Over=0; //定时器结束标记
unsigned char w,x,y;
/*********************************************/
void zhongd0() interrupt 0//外部中断0:加设置
{
if(ST==1)
{
tsheding++;//温度值加1
}
if(SH==1)
{
hsheding++;//湿度值加1
}
}
void zhongd1() interrupt 2//外部中断1:减设置
{
if(ST==1)
{
tsheding--;//温度值减1
}
if(SH==1)
{
hsheding--;//湿度值减1
}
}
/****************************************/
/******************主函数******************/
void main()
{
init();
while(1)
{
readtemperature();
write_com(0x80);
write_shuju(table1[dat[0]]);
write_com(0x81);
write_shuju(table1[dat[1]]);
write_com(0x82);
write_shuju(table1[dat[2]]);
write_com(0x83);
write_shuju(table1[dat[3]]);
write_com(0x84);
write_shuju(table1[dat[4]]);
write_com(0x85);
write_shuju(table1[dat[5]]);
write_com(0x86);
write_shuju(table1[dat[6]]);
write_com(0x87);
write_shuju(table1[dat[7]]);
write_com(0x88);
write_shuju(table1[dat[8]]);
readfrequency();
}
}
/*****************1602延时程序*****************/
void delay(uchar z)
{
uint a,b;
for(a=0;a<z;a++)
for(b=0;b<110;b++);
}
/*******************1602初始化**************/
void init()
{
en=0;
write_com(0x38); //设置为8位并行,显示2行,5*7点阵显示
write_com(0x0c); //设置显示开 无光标 光标不闪烁
write_com(0x01); //清屏指令
write_com(0x80);
}
/***************1602写指令*********************/
void write_com(uchar com)
{
rs=0;
rw=0;
P1=com;
delay(5);
en=1;
en=0;
}
/**************1602写数据**********************/
void write_shuju(uchar shuju)
{
rs=1;
rw=0;
P1=shuju;
delay(5);
en=1;
en=0;
}
/*************18b20微秒延时********************/
void yanshi(unsigned int us)
{
int s;
for(s=0;s<us;s++);
}
/************************18b20初始化********/
unsigned char reset(void)
{
unsigned char presence;
DQ=0;
yanshi(60); //大概480微秒
DQ=1;
yanshi(8); //延时大概50微秒
presence=DQ;
yanshi(14);
return(presence);
}
/*************18b20写时序*************************/
void write(unsigned char dat)
{
unsigned char i;
for(i=8;i>0;i--)
{
DQ=0;
DQ=dat&0x01;
yanshi(16);
DQ=1;
dat>>=1;
}
}
/***************18b20读时序*********************/
unsigned char read(void)
{
unsigned char i,dat=0;
for(i=8;i>0;i--)
{
DQ=0;
dat>>=1;
DQ=1;
if(DQ)
dat|=0x80;
yanshi(7);
}
return (dat);
}
/**************18b20读温度**********************/
void readtemperature(void)
{
unsigned char temp;
int temperature;
reset();
write(0xcc);
write(0x44);
reset();
write(0xcc);
write(0xbe);
temperature=read();
temp=read();
temperature=temperature|(temp<<8);
dat[0]=10;
dat[1]=11;
dat[2]=12;
dat[3]=13;
dat[4]=16;
if(temperature<0)
{
dat[4]=14;
temperature=~temperature+1;
}
dat[5]=(temperature*10/16)/100;
dat[6]=((temperature*10/16)%100)/10;
dat[7]=15;
dat[8]=((temperature*10/16)%100)%10;
}
/****************中断********
void
timer0() interrupt 1
{
TR1=0;
TR0=0;
TF0=0;
ET0=0;
T0Over=1;
}
*********************/
/**********************/
void timer0() interrupt 1 //T0定时中断
{
TR0=0;
TR1=0;
TL0=0xB0;
TH0=0x3C;
tem0=TL1;
tem1=TH1;
TL1=0x00;
TH1=0x00;
TR0=1;
TR1=1;
}
void timer1() interrupt 3 //T1定时中断
{
TR0=0;
TR1=0;
TL0=0xB0;
TH0=0x3C;
TL1=0x00;
TH1=0x00;
TR0=1;
TR1=1;
}
void init_timer()
{
TMOD=0x51;
TL0=0xB0;
TH0=0x3C;
TL1=0x00;
TH1=0x00;
TR0=1;
TR1=1;
}
/*******************/
/*************湿度传感器频率采集及转换模块***************/
void readfrequency()
{
unsigned int ftequency,RH;
TMOD=0x51;
TH0=0x4C;
TL0=0X00;
TH1=0;
TL1=0;
TR1=1;
TR0=1;
ET0=1;
ET1=0;
EA=1;
T0Over=0;
while(!T0Over)
write_com(0x80+0x40);
write_shuju(table2[shuju1[0]]);
write_com(0x80+0x41);
write_shuju(table2[shuju1[1]]);
write_com(0x80+0x42);
write_shuju(table2[shuju1[2]]);
write_com(0x80+0x43);
write_shuju(table2[shuju1[3]]);
write_com(0x80+0x44);
write_shuju(table2[shuju1[4]]);
write_com(0x80+0x45);
write_shuju(table2[shuju1[5]]);
write_com(0x80+0x46);
write_shuju(table2[shuju1[6]]);
write_shuju(table2[shuju1[7]]);
write_shuju(table2[shuju1[8]]);
ftequency=(TH1*256+TL1)*20;
minrange=0;
maxrange=0;
if(ftequency)
{
if(ftequency<6033)
maxrange=1;
if(ftequency>7351)
minrange=1;
if(6330<ftequency<=7351)
RH=5880-0.8*ftequency;
if(6033<=ftequency<=6330)
RH=5860-0.8*ftequency;
shuju1[0]=10;
shuju1[1]=11;
shuju1[2]=12;
shuju1[3]=15;
shuju1[4]=RH/100;
shuju1[5]=(RH%100)/10;
shuju1[6]=14;
shuju1[7]=(RH%10);
shuju1[8]=13;
}
}
仿真文件是hex还是别的啊
温湿度检测.zip 下载积分: 积分 -1 分
88.39 KB, 下载次数: 7, 下载积分: 积分 -1 分
一周热门 更多>