一个关于DS18B20的问题,小数点位只显示0

[复制链接]
2314|3
 楼主| 我是胖子 发表于 2013-8-6 22:30 | 显示全部楼层 |阅读模式
Init_DS18B20.c
  1. #include<HL_1.h>
  2. #include<intrins.h>

  3. void delay_11us(uint n)
  4. {
  5.         while (n--);
  6. }

  7. /*
  8. * 18B20复位函数
  9. */
  10. void DS18b20_reset(void)
  11. {
  12.         bit flag=1;

  13.         while (flag)
  14.         {
  15.                 while (flag)
  16.                  {
  17.                         DQ = 1;
  18.                         delay_11us(1);
  19.                         DQ = 0;
  20.                         delay_11us(50); // 550us
  21.                         DQ = 1;
  22.                         delay_11us(6);  // 66us
  23.                         flag = DQ; //如果DQ返回信号为低,证明复位成功
  24.                    }
  25.                 delay_11us(45);    //延时500us
  26.                 flag = ~DQ;
  27.         }
  28.         DQ=1;
  29. }


  30. /*
  31. * 18B20写1个字节函数
  32. * 向1-WIRE总线上写一个字节
  33. */
  34. void write_byte(uchar val)
  35. {
  36.         uchar i;

  37.         for (i=0; i<8; i++)
  38.         {
  39.                 DQ = 1;
  40.                 _nop_();
  41.                 DQ = 0;
  42.                 _nop_();
  43.                 _nop_();
  44.                 _nop_();
  45.                 _nop_();//4us
  46.                 DQ = val & 0x01;      //最低位移出
  47.                 delay_11us(6);           //66us
  48.                 val >>= 1;          //右移一位
  49.         }
  50.         DQ = 1;
  51.         delay_11us(1);  
  52. }

  53. /*
  54. * 18B20读1个字节函数
  55. * 从1-WIRE总线上读取一个字节
  56. */
  57. uchar read_byte(void)
  58. {
  59.         uchar i, value=0;

  60.         for (i=0; i<8; i++)
  61.         {
  62.                 DQ=1;
  63.                 _nop_();
  64.                 value >>= 1;
  65.                 DQ = 0;
  66.                 _nop_();
  67.                 _nop_();
  68.                 _nop_();
  69.                 _nop_();   //4us
  70.                 DQ = 1;
  71.                 _nop_();
  72.                 _nop_();
  73.                 _nop_();
  74.                 _nop_();   //4us
  75.                 if (DQ)
  76.                         value|=0x80;
  77.                 delay_11us(6);           //66us
  78.         }
  79.         DQ=1;

  80.         return(value);
  81. }

  82. /*
  83. * 启动温度转换
  84. */
  85. void start_temp_sensor(void)
  86. {
  87.         DS18b20_reset();
  88.         write_byte(0xCC); // 发Skip ROM命令
  89.         write_byte(0x44); // 发转换命令
  90. }

  91. /*
  92. * 读出温度
  93. */
  94. float read_temp(void)
  95. {
  96.         uchar temp_data[2]; // 读出温度暂放
  97.         volatile int tt;
  98.         float temp;

  99.         DS18b20_reset();  // 复位
  100.         write_byte(0xCC); // 发Skip ROM命令
  101.         write_byte(0xBE); // 发读命令
  102.         temp_data[0]=read_byte();  //温度低8位
  103.         temp_data[1]=read_byte();  //温度高8位

  104.         tt = temp_data[1];//把高八位放到int的高八位上,因为最高位是一,为负
  105.         tt <<= 8;
  106.         tt |= temp_data[0];//再把低八位放到int的低八位上
  107.         temp = tt*0.0625;

  108.         return temp;
  109. }
主函数
  1. #include<reg52.h>
  2. #include<HL_1.h>
  3. #include"Init_DS18B20.h"

  4. int tem;//温度值
  5. uchar shi, ge, xiaoshu;
  6. uint NUM[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
  7. uint PLACE[7] = {0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf};

  8. void main()
  9. {
  10.         while(1)
  11.         {
  12.                 start_temp_sensor();
  13.                 delay(1);
  14.                 tem = read_temp();

  15.                 shi = (int)tem/10;
  16.                 ge = (int)tem%10;
  17.                 xiaoshu = (int)(tem*10)%10;
  18.                
  19.                 WE = 1;
  20.                 P0 = PLACE[4];
  21.                 WE = 0;

  22.                 DU = 1;
  23.                 P0 = NUM[shi];
  24.                 DU = 0;
  25.                 delay(2);

  26.                 WE = 1;
  27.                 P0 = PLACE[5];
  28.                 WE = 0;

  29.                 DU = 1;
  30.                 P0 = NUM[ge];
  31.                 DU = 0;
  32.                 delay(2);

  33.                 WE = 1;
  34.                 P0 = PLACE[5];
  35.                 WE = 0;

  36.                 DU = 1;
  37.                 P0 = 0x80;
  38.                 DU = 0;
  39.                 delay(2);

  40.                 WE = 1;
  41.                 P0 = PLACE[6];
  42.                 WE = 0;

  43.                 DU = 1;
  44.                 P0 = NUM[xiaoshu];
  45.                 DU = 0;
  46.                 delay(2);                        
  47.         }       
  48. }
dirtwillfly 发表于 2013-8-6 22:40 | 显示全部楼层
好长的程序,难道是改错题?
 楼主| 我是胖子 发表于 2013-8-7 09:00 | 显示全部楼层
dirtwillfly 发表于 2013-8-6 22:40
好长的程序,难道是改错题?

自己写的  版主  你就 救救我吧
叶伤 发表于 2013-8-15 21:14 来自手机 | 显示全部楼层
我自己做的时候怎么改,都只有0或5,好像第二字节低四位不是0000就是0111,即使读出计数剩余值和每度计数值进行计算(直接计算后小数*10取个位或者fprintf取小数ASCII码-48,code会多出3100字节),也只能显示7,无论多少摄氏度,郁闷,这DS18B20到底怎么回事
您需要登录后才可以回帖 登录 | 注册

本版积分规则

6

主题

12

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部