| 
 
| 18B20的读写程序,原先是用在,AT2051上的,现在把它放到STC89C52上,32Mhz晶振,双倍速,(相当于64MHz)不管我怎样修收它的延时时间还是不可以正常读写. 调了两天还是不行.大家有什么办法啊?是不是18B20不支持那高的频率啊?
 
 用在2051上不管是12MHz和24MHz我都用这个程序的,只要改延时单间变可以用了,但现在用在STC上,而且是32MHz 双倍速,(相当于64MHz) 不管我怎样调时间还是不行啊!!!!!!!!
 
 加长了延时间的程序如下:
 (延时函数longdelay2(unsigned int i) 后的那个数字就是用在AT2051 12MHz 上的延时时间 现在用在STC上,不行。)
 ////////////////////////////////////////////////////////
 //****************温度采集部分************************//
 ////////////////////////////////////////////////////////
 
 void longdelay2(unsigned int i)   // [32MHz晶振]
 {
 while(i--);
 }
 
 //初始化函数
 void Init_DS18B20(void)
 {
 unsigned char x=0;
 DQ = 1;    //DQ复位
 longdelay2(60);  // 8
 DQ = 0;    //单片机将DQ拉低
 longdelay2(470); //80
 DQ = 1;    //拉高总线
 longdelay2(80);  //14
 x=DQ;      //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
 if(x == 0) month1 = 3;
 longdelay2(220);  //(延时了213us); 20
 }
 
 //读一个字节
 uchar ReadOneChar(void)
 {
 unsigned char i=0;
 unsigned char dat = 0;
 for (i=8;i>0;i--)
 {
 DQ = 0; // 给脉冲信号
 dat>>=1;
 DQ = 1; // 给脉冲信号
 if(DQ)
 dat|=0x80;
 longdelay2(40);  //4
 }
 return(dat);
 }
 
 //写一个字节
 void WriteOneChar(unsigned char dat)
 {
 unsigned char i=0;
 for (i=8; i>0; i--)
 {
 DQ = 0;
 DQ = dat&0x01;
 longdelay2(50);   //5
 DQ = 1;
 dat>>=1;
 }
 //delay(4);
 }
 
 //读取温度
 uchar ReadTemperature(void)
 {
 unsigned char a=0;
 unsigned char b=0;
 unsigned char t=0;
 //float tt=0;
 Init_DS18B20();
 WriteOneChar(0xCC); // 跳过读序号列号的操作
 WriteOneChar(0x44); // 启动温度转换
 Init_DS18B20();
 WriteOneChar(0xCC); //跳过读序号列号的操作
 WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
 a=ReadOneChar();
 b=ReadOneChar();
 //t=b;
 b<<=4;
 t=(a&0xF0)>>4;
 b=b|t;
 //t= tt*10+0.5; //放大10倍输出并四舍五入---此行没用
 return(b);
 }
 
 | 
 |