无论按那个开关 都这样显示 就是显示不出来正确结果 求大佬帮忙看看
#include<reg51.h> unsigned intshu[]={0x30,0x31,0x32,0x33,0x34,0X35,0x36,0x37,0x38,0x39}; unsignedlong t; unsignedchar k,f=0; sbitRS=P2^0; sbitRW=P2^1; sbitE=P2^2; sbitDS=P1^1;
void del(int count ) //延时程序 {while(count--);}
voiddelay(unsigned int count)//延时程序 {int p; while(count--) for(p=0;p<125;p++); }
voidwrite(unsigned char date)//向DS18B20中写入数据 { int i; for(i=0;i<8;i++) {DS=0; DS=date&0x01; // 写入数据。 del(15) DS=1; date>>=1; } }
void init(unsigned int n)// RW=0,RS=0;向指令寄存器中写入命令,即对1602初始化。 {delay(10); E=0; RS=0; RW=0; E=1; P0=n; E=0; }
voidwrite_data(unsigned char n)//RS=1,RW=0;向数据寄存器中写入数据,即显示的数符。 {delay(10); E=0; RS=1; RW=0; E=1; P0=n; E=0; }
voidwrite2(unsigned char i) { i+=0x30; if(i==4){i=0;} write(0x55); write(0x28); write(i); write(0xc5); write(0xb8); write(0x00); write(0x00); write(0x00); }
voidmatchrom() //匹配ROM { unsigned char i; k=P3; switch(k) { case 0: { f=1; init(0x80);write_data(0x50);//press init(0x81);write_data(0x72); init(0x82);write_data(0x65); init(0x83);write_data(0x73); init(0x84);write_data(0x73); init(0x85);write_data(0x80); init(0x86);write_data(0x61);//a init(0x87);write_data(0x80); init(0x88);write_data(0x6b);//key init(0x89);write_data(0x65); init(0x8a);write_data(0x79); init(0x8b);write_data(0x80); init(0x8c);write_data(0x21);//! for(i=0x8d;i<=0xcf;i++) {init(i);write_data(0x80);} }break; case 1: { init(0x83);write_data(0x31); //NO 1 write2(0); write(0x8e); }break; case 2: { init(0x83);write_data(0x32);//NO 2 write2(1); write(0xb9); }break; case 4: { init(0x83);write_data(0x33); //NO 3 write2(2); write(0xe0); }break; case 8:{ init(0x83);write_data(0x34); //NO 4 write2(3); write(0xd7); } break; default:{ f=1; init(0x80);write_data(0x50); //please init(0x81);write_data(0x6c); init(0x82);write_data(0x65); init(0x83);write_data(0x61); init(0x84);write_data(0x73); init(0x85);write_data(0x65); init(0x86);write_data(0x80); init(0x87);write_data(0x70); //press init(0x88);write_data(0x72); init(0x89);write_data(0x65); init(0x8a);write_data(0x73); init(0x8b);write_data(0x73); init(0x8c);write_data(0x80); init(0x8d);write_data(0x6f); //one init(0x8e);write_data(0x6e); init(0x8f);write_data(0x65); init(0xc0);write_data(0x80); init(0xc1);write_data(0x80); init(0xc2);write_data(0x80); init(0xc3);write_data(0x6b);//key init(0xc4);write_data(0x65); init(0xc5);write_data(0x79); init(0xc6);write_data(0x80); init(0xc7);write_data(0x6f);//only init(0xc8);write_data(0x6e); init(0xc9);write_data(0x6c); init(0xca);write_data(0x79); init(0xcb);write_data(0x80); init(0xcc);write_data(0x21);//! init(0xcd);write_data(0x80); init(0xce);write_data(0x80); init(0xcf);write_data(0x80); }break; } }
void lcd(unsigned char m) //当为负温度时,液晶显示可以高位为0屏蔽. { init(0x80);write_data(0x4e);//NO init(0x81);write_data(0x4f); init(0x82);write_data(0x80); init(0x84);write_data(0x80); init(0x85);write_data(0x74);//temperature init(0x86);write_data(0x65); init(0x87);write_data(0x6d); init(0x88);write_data(0x70); init(0x89);write_data(0x65); init(0x8a);write_data(0x72); init(0x8b);write_data(0x61); init(0x8c);write_data(0x74); init(0x8d);write_data(0x75); init(0x8e);write_data(0x72); init(0x8f);write_data(0x65); init(0xc0);write_data(0x80); init(0xc1);write_data(0x80); init(0xc2);write_data(0x69); //is: init(0xc3);write_data(0x73); init(0xc4);write_data(0x3a); if(m==1) {if((t/1000%10)!=0) {init(0xc5);write_data(0x2d); init(0xc6);write_data(shu[t/1000%10]); } else {init(0xc5);write_data(0xfe); init(0xc6);write_data(0x2d); } } else {if(t/10000!=0) {init(0xc5);write_data(shu[t/10000]); init(0xc6);write_data(shu[t/1000%10]); } else {if((t/1000%10)!=0) {init(0xc5);write_data(0xfe); init(0xc6);write_data(shu[t/1000%10]); } else {init(0xc5);write_data(0xfe); init(0xc6);write_data(0xfe); } } init(0xc7);write_data(shu[t/100%10]); init(0xc8);write_data(0x2e); init(0xc9);write_data(shu[(t%100)/10]); init(0xcb);write_data(0x27); init(0xcc);write_data(0x43); init(0xcd);write_data(0x80); init(0xce);write_data(0x80); init(0xcf);write_data(0x80); } } void jiance() {unsignedchar n=0; do{DS=1; DS=0; del(100); DS=1; del(10); 。 n=DS; del(20); //低电平至少要持续60~240us } while(n==1); 。 }
read() //从DS18B20中读出数据 {unsigned char i; unsigned long date=0; for(i=0;i<16;i++) { DS=0; //主机在某一时刻将总线从高电平拉到低电平. date>>=1; DS=1; del(3); //保持15us将总线拉到高电平,产生读时间隙 if(DS) date|=0x8000; del(8); //读数据需要持续35us~60us. } return(date); }
void main() {unsigned long flag=0; EA=0; SP=0X60; init(0x01); //对液晶屏初始化 init(0x38); init(0x0c); init(0x06); while(1) { jiance(); //对DS18B20初始? matchrom(); write(0x44); //启动温度变换 del(100); jiance(); matchrom(); if(f==1) {f=0;} else { write(0xbe); // 读暂存存储器 t=read(); flag=t&0x8000; //对读取的数据进行处理 if(flag==0x8000) {t=~t; t=t+1; t=t*25; t>>=2; lcd(1);//负值 } else { t=t*25; t>>=2; lcd(0);//正值 } } } }
|