程序: 求帮助,完成不了毕不了业,求帮忙
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define Addr 0x90 //??pcf8591???
#define Addw 0x91 //??pcf8591???
uchar ch0,ch1; //??pcf8591??0?????1??
uchar turblow=0; //浊度下限,低于此值led3亮起,蜂鸣器报警
uchar turbhigh=5;
uchar phlow=0;
uchar phhigh=14;
sbit led1=P2^5;//wendu
sbit led2=P2^6;//ph
sbit led3=P2^7;//浊度turb(b)
sbit beep=P1^4;//?????
sbit key1=P1^2;
sbit key2=P1^3;
sbit key3=P3^2;
sbit RS=P2^2;
sbit RW=P2^3;
sbit EN=P2^4; //??1602
sbit sda=P2^0; //???,??ph???????
sbit scl=P2^1; //???
sbit DS=P1^0; //???????
uchar code table[]="temp is"; //code?????????ROM(?????)??
uchar code table1[]="turb is";
uchar code table2[]="ph is";
uchar code table3[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f, //?????0-9??
0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef}; //??????0-9??
uchar code table4[]="error";
uint temp; //?????
float ftemp; //??????
uchar tflag; //??????
uchar data tempdata[5]; //????????,??,??,??,???
uint templow=20; //?????,????????????
uint temphigh=50; //?????,????????????
void delayms(uint xms) //??????
{
uint i,j;
for(i=xms;i>0;i++)
for(j=110;j>0;j--);
}
void delay() //????????
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
void write_com(uchar com) //1602???
{
RS=0;
P0=com;
delayms(5);
EN=1;
delayms(5);
EN=0;
}
void write_data(uchar dat) //1602???
{
RS=1;
P0=dat;
delayms(5);
EN=1;
delayms(5);
EN=0;
}
void init1602() //1602???
{
EN=0;
write_com(0x38); //??16*2??,5*7??,8?????
write_com(0x0c); //?????,?????
write_com(0x06); //????????????
write_com(0x01); //????,??????
}
void display(uchar *p) //1602????????
{
while(*p!=' ') //??????
{
write_data(*p);
p++; //???1
delayms(5);
}
}
init_playtemp() //1602????????
{
init1602();
write_com(0x80);
display(table); //???????????????table:temp is
}
init_playturb() //1602????????
{
init1602();
write_com(0x80);
display(table1); //???????????????table1:turb is
}
init_playph() //1602?ph?????
{
init1602();
write_com(0x80);
display(table2); //???????????????table2:ph is
}
void ds18b20rst() //ds18b20??
{
uchar x=0;
DS=1; //DS??
delayms(5);
DS=0; //DS??
delayms(200);
DS=1;
delayms(40);
}
bit ds18b20readbit() //ds18b20?????
{
uint i;
bit datt;
DS=0;i++; //i++?????
DS=1;i++;i++;
datt=DS;
i=8;
while(i>0)
i--;
return(datt);
}
uchar ds18b20read() //ds18b20??????8???
{
uchar n,j;
uchar dat=0;
for(n=8;n>0;n--)
{
j=ds18b20readbit();
dat=(j<<7)|(dat>>1);
}
return(dat);
}
void ds18b20write(uchar dat) //?ds18b20???????
{
uint a;
uchar b;
bit testb;
for(b=1;b<=8;b--)
{
testb=dat&0x01;
dat=dat>>1;
if(testb)
{
DS=0;
a++;a++;
DS=1;
a=8;while(a>0)a--;
}
else
{
DS=0;
a=8;while(a>0)a--;
DS=1;
a++;a++;
}
}
}
uint ds18b20change() //ds18b20???????
{
uchar c,d;
ds18b20rst();
delayms(1);
ds18b20write(0xcc); //???ROM??
ds18b20write(0x44); //??????,??????9???RAM?
ds18b20rst();
ds18b20write(0xcc);
ds18b20write(0xbe); //????????,?ds18b20???????RAM?9???????
c=ds18b20read();
d=ds18b20read();
temp=d;
temp<<=8;
temp=temp|c;
if(temp<0x0fff)
{
tflag=0; //?5??0,????????,????0
ftemp=temp*0.0625; //????????12?,????0.0625
temp=ftemp*10+0.5; // *10??????????,+0.5?????
ftemp=ftemp+0.05;
}
else
{
temp=~temp+1;
tflag=1; //???????
}
return(temp);
}
void ds18b20display() //?????
{
uchar tflagdata;
tempdata[0]=temp/1000+0x30; //???
tempdata[1]=temp%1000/100+0x30;//???
tempdata[2]=temp%100/10+0x30;//???
tempdata[3]=temp%10+0x30;//???
if(tflag==0)
tflagdata=0x20; //????????
else
tflagdata=0x2d;//???????
write_com(0x88); //?????????8????
write_data(tflagdata); // ??? ??);
write_com(0x89);
write_data(tempdata[0]); //????
write_com(0x8a);
write_data(tempdata[1]);
write_com(0x8b);
write_data(tempdata[2]);
write_com(0x8c);
write_data(tempdata[3]);
}
void deal(uint i) //??????
{
uint y;
y=i;
if(y<templow)
{
while(1)
{
beep=1;
led1=1;
delayms(5);
beep=~beep;
led1=0;
delayms(5);
}
}
if(y>temphigh)
{
while(1)
{
beep=1;
led1=1;
delayms(5);
beep=~beep;
led1=0;
delayms(5);
}
}
}
void init() //i2c?????
{
scl=1;
delay();
sda=1;
delay();
}
void start() //????
{
sda=1;
delay();
scl=1; //scl???
delay();
sda=0; //scl????sda?????,??
delay();
scl=0;
}
void ask() //????
{
uchar r=0;
scl=1;
delay();
while((sda==1)&&(r<255)) //???????????????????????????????????????????
r++;
scl=0;
delay();
}
void noask() //?????
{
sda=1; //???????,????,?????
delay();
scl=1;
delay();
scl=0;
delay();
}
void stop() //????
{
sda=0;
delay();
scl=1;
delay();
sda=1; //scl????sda?????,??
delay();
scl=0;
sda=0;
}
void sendbyte(uchar dat) //???????pcf8591
{
uchar t,shuju;
shuju=dat;
for(t=0;t<8;t++)
{
shuju=shuju<<1;
scl=0;
delay();
sda=CY;
delay();
sda=1;
delay();
}
scl=0;
delay();
sda=1; //????
delay();
}
uchar readbyte() //?????
{
uchar w,k;
scl=0;
delay();
sda=1; //????
for(w=0;w<8;w++)
{
scl=1;
delay();
k=(k<<1)|sda; //??8?,??7?
scl=0;
delay();
}
delay();
return k;
}
uchar read_adc(uchar channel) //?ad??
{
uchar bl; //bianliang????????
start(); //????
sendbyte(Addw); //???????,???????,?????????
ask(); //??
sendbyte(0x40|channel); //???????????,channel?0?1,?0x40????????
ask(); //??
start(); //??????da??,?????????
sendbyte(Addr); //???????
ask();
bl=readbyte();
noask();
stop();
return bl;
}
void phdisplay() //ph???
{
uchar b0,b1,b2;
if(ch0>=2.5)
{
b0=(ch0-25/10)/(-5419)*100000/10+0x30; //??
b0=(ch0-25/10)/(-5419)*100000%10+0x30; //??????
b0=(ch0-25/10)/(-5419)*100000+7%100+0x30; //????2?
write_com(0x86);
write_data(b0+7); //????
write_com(0x87);
write_data(b1);
write_com(0x88);
write_com(b2);
}
else
write_com(0x86);
display(table4);
}
void bdisplay() //?????
{
uchar a0,a1,a2;
if(ch1<4.5)
{
a0=(35/10-7/10*ch1)/10+0x30;
a0=(35/10-7/10*ch1)%10+0x30;
a0=(35/10-7/10*ch1)%100+0x30;
write_com(0x88);
write_data(a0); //????
write_com(0x89);
write_data(a1);
write_com(0x8a);
write_com(a2);
}
else
write_com(0x88);
display(table4);
}
void phdeal() //ph????
{
uchar e;
e=ch0;
if(e<phlow)
{
while(1)
{
beep=1;
led2=1;
delayms(5);
beep=~beep;
led2=0;
delayms(5);
}
}
if(e>phhigh)
{
while(1)
{
beep=1;
led2=1;
delayms(5);
beep=~beep;
led2=0;
delayms(5);
}
}
}
void bdeal() //??????
{
if(ch1<turblow)
{
while(1)
{
beep=1;
led3=1;
delayms(5);
beep=~beep;
led3=0;
delayms(5);
}
}
if(ch1>turbhigh)
{
while(1)
{
beep=1;
led3=1;
delayms(5);
beep=~beep;
led3=0;
delayms(5);
}
}
}
void main()
{
if(key1==0) //??
{
delayms(10);
if(key1==0)
{
init_playtemp(); //1602???????
while(1)
{
ds18b20change(); //???????
ds18b20display(); //?????
deal(temp); //??????
}
}
}
if(key2==0) //ph
{
delayms(10);
if(key2==0)
{
init_playph();
init();
while(1)
{
ch0=read_adc(0);
phdisplay();
phdeal();
}
}
}
if(key3==0) //??
{
delayms(10);
if(key3==0)
{
init_playturb();
init();
while(1)
{
ch1=read_adc(0x01);
bdisplay();
bdeal();
}
}
}
}
-
-
毕设仿真3.DSN
下载积分: 积分 -1 分
125.79 KB, 下载次数: 8, 下载积分: 积分 -1 分
一周热门 更多>