找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1378|回复: 0
打印 上一主题 下一主题
收起左侧

新人贴 基于51单片机的ds18b20的应用

[复制链接]
跳转到指定楼层
楼主
ID:373775 发表于 2018-7-18 09:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式


  1. #include <reg51.h>
  2. #include <intrins.h>
  3. #define uint unsigned int
  4. #define uchar unsigned char
  5. sbit DQ = P3^6;
  6. uchar code DSY_CODE[] =
  7. { 0X3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F,0X00};
  8. uchar code df_Table[] = {0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9};
  9. uchar CurrentT = 0;
  10. uchar Temp_Value[]={0x11,0x22};
  11. uchar Display_Digit[]={0,0,0,0};
  12. bit DS18B20_IS_OK = 1;
  13. void Delay(uint x)
  14. {
  15.          while(--x);
  16. }
  17. uchar Init_DS18B20()
  18. {
  19.          uchar status;
  20. DQ = 1;
  21. Delay(8);
  22. DQ = 0;
  23. Delay(90);
  24. DQ = 1;
  25. Delay(8);
  26. DQ = 1;
  27. return status;
  28. }
  29. uchar ReadOneByte()
  30. {
  31.          uchar i,dat=0;
  32. DQ = 1;
  33. _nop_();
  34. for(i=0;i<8;i++)
  35. {
  36.          DQ = 0;
  37. dat >>= 1;
  38. DQ = 1;
  39. _nop_();
  40. _nop_();
  41. if(DQ)
  42. dat |= 0X80;
  43. Delay(30);
  44. DQ = 1;
  45. }
  46. return dat;
  47. }
  48. void WriteOneByte(uchar dat)
  49. {
  50.          uchar i;
  51. for(i=0;i<8;i++)
  52. {
  53.          DQ = 0;
  54. DQ = dat& 0x01;
  55. Delay(5);
  56. DQ = 1;
  57. dat >>= 1;
  58. }
  59. }
  60. void Read_Temperature()
  61. {
  62.          if(Init_DS18B20() ==1 )
  63. DS18B20_IS_OK = 0;
  64. else
  65. {
  66. WriteOneByte(0xcc);
  67. WriteOneByte(0x44);
  68. Init_DS18B20();
  69. WriteOneByte(0xcc);
  70. WriteOneByte(0xbe);
  71. Temp_Value[0] = ReadOneByte();
  72. Temp_Value[1] = ReadOneByte();
  73. DS18B20_IS_OK=1;
  74. }
  75. }
  76. void Display_Temperature()
  77. {
  78. uchar i;
  79. uchar t=150;
  80. uchar ng=0, np=0;

  81. if ( (Temp_Value[1] & 0xf8) == 0xf8)
  82. {
  83. Temp_Value[1] = ~Temp_Value[1];
  84. Temp_Value[0] = ~Temp_Value[0]+1;
  85. if (Temp_Value[0] == 0x00) Temp_Value[1]++;
  86. ng=1;np=0xfd;
  87. }
  88. Display_Digit[0] = df_Table[ Temp_Value[0] & 0x0f ];
  89. CurrentT = ((Temp_Value[0] & 0xf0)>>4) | ((Temp_Value[1] & 0x07)<<4);
  90. Display_Digit[3] = CurrentT / 100;
  91. Display_Digit[2] = CurrentT % 100 / 10;
  92. Display_Digit[1] = CurrentT % 10;
  93. if (Display_Digit[3] == 0)
  94. {
  95. Display_Digit[3] = 10;
  96. np = 0xfb;
  97. if (Display_Digit[2] == 0)
  98. {
  99. Display_Digit[2] = 10;
  100. np = 0xf7;
  101. }
  102. }
  103. for (i=0;i<30;i++)
  104. {
  105. P0=0x39;P2=0x7f;Delay(t);P2=0xFF;
  106. P0=0x63;P2=0xbf;Delay(t);P2=0xff;
  107. P0=DSY_CODE[Display_Digit[0]];
  108. P2=0xDF;Delay(t);P2=0xff;
  109. P0=(DSY_CODE[Display_Digit[1]]) | 0x80;
  110. P2=0xef;Delay(t);P2=0xff;
  111. P0=DSY_CODE[Display_Digit[2]];
  112. P2=0xf7;Delay(t);P2=0xff;
  113. P0=DSY_CODE[Display_Digit[3]];
  114. P2=0xfb; Delay(t); P2=0xff;
  115. if (ng)
  116. {
  117. P0 = 0x40; P2 = np; Delay(t); P2=0xff;
  118. }
  119. }
  120. }
  121. void main()
  122. {
  123. Read_Temperature();
  124. Delay(50000);
  125. Delay(50000);
  126. while(1)
  127. {
  128.          Read_Temperature();         
  129. if(DS18B20_IS_OK)
  130. Display_Temperature();

  131. }
  132. }
复制代码



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表