|
V_59IQ)76VA)GCL6NG$MN.png (37.56 KB, 下載次數(shù): 71)
下載附件
2017-5-5 12:19 上傳
- #include<reg52.h>
- #include <absacc.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit rs=P1^2;
- sbit rw=P1^3;
- sbit en=P1^4;
- sbit DQ=P3^4;
- sbit start=P1^0; // 18b20總線
- sbit eoc=P3^3;
- sbit oe=P1^1;
- sbit CLK=P3^6;
- unsigned char tem;
- unsigned char dat[8]={0}; //溫度
- unsigned char shuju1[7]={0}; //濕度
- unsigned char shuju2[9]={0};
- uchar codetable1[16]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x54,0x3a,0x2d,0x2e,0x20,0x2f}; //溫度調(diào)用
- uchar codetable2[15]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x53,0x3a,0x25,0x2e,0x20}; //濕度調(diào)用
- uchar codetable3[15]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x43,0x6f,0x3a,0x20,0x2e};
- /**************1602d調(diào)用函數(shù)*******************/
- void write_com(uchar com);
- void write_shuju(uchar shuju);
- void init();
- void delay(uchar z);
- /**************18b20調(diào)用函數(shù)**************/
- unsigned char reset(void); //18b20初始化//
- void write(uchar dat); //18b20寫(xiě)時(shí)序
- unsigned char read(void); //18b20讀時(shí)序
- void readtemperature(void); //18b20讀二進(jìn)制16位溫度
- void yanshi(unsigned int us);
- /****************hs1101調(diào)用函數(shù)*************************/
- void readfrequency(void); // 讀頻率函數(shù)//
- bit minrange=0; //濕度下限標(biāo)志位//
- bit maxrange=0; //濕度上限標(biāo)志位//
- bit T0Over=0; //定時(shí)器結(jié)束標(biāo)記
- unsigned char w,x,y;
- /******************主函數(shù)******************/
- void main()
- {
- init();
- while(1)
- {
- readtemperature();
- write_com(0x80+0x40);
- write_shuju(table1[dat[0]]);
- write_com(0x80+0x41);
- write_shuju(table1[dat[1]]);
- write_com(0x80+0x42);
- write_shuju(table1[dat[2]]);
- write_com(0x80+0x43);
- write_shuju(table1[dat[3]]);
- write_com(0x80+0x44);
- write_shuju(table1[dat[4]]);
- write_com(0x80+0x45);
- write_shuju(table1[dat[5]]);
- write_com(0x80+0x46);
- write_shuju(table1[dat[6]]);
- write_com(0x80+0x47);
- write_shuju(table1[dat[7]]);
- readfrequency();
- }
- }
- /*****************1602延時(shí)程序*****************/
- void delay(uchar z)
- {
- uint a,b;
- for(a=0;a<z;a++)
- for(b=0;b<110;b++);
- }
- /*******************1602初始化**************/
- void init()
- {
- oe=0;
- en=0;
- write_com(0x38); //設(shè)置為8位并行,顯示2行,5*7點(diǎn)陣顯示
- write_com(0x0c); //設(shè)置顯示開(kāi)無(wú)光標(biāo) 光標(biāo)不閃爍
- write_com(0x01); //清屏指令
- write_com(0x80);
- }
- /***************1602寫(xiě)指令*********************/
- void write_com(uchar com)
- {
- rs=0;
- rw=0;
- P0=com;
- delay(5);
- en=1;
- en=0;
- }
- /**************1602寫(xiě)數(shù)據(jù)**********************/
- void write_shuju(uchar shuju)
- {
- rs=1;
- rw=0;
- P0=shuju;
- delay(5);
- en=1;
- en=0;
- }
- /*************18b20微秒延時(shí)********************/
- 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); //延時(shí)大概50微秒
- presence=DQ;
- yanshi(14);
- return(presence); }
- /*************18b20寫(xiě)時(shí)序*************************/
- 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讀時(shí)序*********************/
- 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); //跳過(guò)讀序號(hào)操作
- write(0x44); //啟動(dòng)溫度轉(zhuǎn)換
- reset();
- write(0xcc); //跳過(guò)讀序號(hào)操作
- write(0xbe); //讀取溫度寄存器
- temperature=read();
- temp=read();
- temperature=temperature|(temp<<8);
- dat[0]=10;
- dat[1]=11;
- dat[2]=14;
- if(temperature<0)
- {
- dat[2]=12;
- temperature=~temperature+1;
- }
- dat[3]=(temperature*10/16)/100; //十位
- dat[4]=((temperature*10/16)%100)/10; //個(gè)位
- dat[5]=13;
- dat[6]=((temperature*10/16)%100)%10; //小數(shù)點(diǎn)后一位
- dat[7]=15;
- }
- /****************中斷*****************************/
- void timer0() interrupt 1
- {
- TR1=0;
- TR0=0;
- TF0=0;
- ET0=0;
- T0Over=1;
- }
- /*******AD控制程序*******************************/
- void zhuanhuan()
- {
- start=0;
- start=1;
- start=0;
- while(!eoc);
- oe=1;
- tem=P0;
- oe=0;
- tem=(tem*2*1*98.039216/100); //AD采樣換算
- }
- /*************濕度傳感器頻率采集及轉(zhuǎn)換模塊***************/
- 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+0x48);
- write_shuju(table2[shuju1[0]]);
- write_com(0x80+0x49);
- write_shuju(table2[shuju1[1]]);
- write_com(0x80+0x4a);
- write_shuju(table2[shuju1[2]]);
- write_com(0x80+0x4b);
- write_shuju(table2[shuju1[3]]);
- write_com(0x80+0x4c);
- write_shuju(table2[shuju1[4]]);
- write_com(0x80+0x4d);
- write_shuju(table2[shuju1[5]]);
- write_com(0x80+0x4e);
- write_shuju(table2[shuju1[6]]);
- 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]=RH/100;
- shuju1[3]=(RH%100)/10;
- shuju1[4]=13;
- shuju1[5]=(RH%10);
- shuju1[6]=12;
- write_com(0x80);
- write_shuju(table3[shuju2[0]]);
- write_com(0x81);
- write_shuju(table3[shuju2[1]]);
- write_com(0x82);
- write_shuju(table3[shuju2[2]]);
- write_com(0x83);
- write_shuju(table3[shuju2[3]]);
- write_com(0x84);
- write_shuju(table3[shuju2[4]]);
- write_com(0x85);
- write_shuju(table3[shuju2[5]]);
- write_com(0x86);
- write_shuju(table3[shuju2[6]]);
- write_com(0x87);
- write_shuju(table3[shuju2[7]]);
- write_com(0x88);
- write_shuju(table3[shuju2[8]]);
- shuju2[0]=10;
- shuju2[1]=11;
- shuju2[2]=12;
- shuju2[3]=13;
- shuju2[4]= tem/1000;
- shuju2[5]= tem%1000/100;
- shuju2[6]= 14;
- shuju2[7]= tem%100/10;
- shuju2[8]= tem%10;
- zhuanhuan();
- }
- }
復(fù)制代碼
|
|