ds18b20.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "iocc2530.h"
  2. #include "ds18b20.h"
  3. #include "public.h"
  4. unsigned char Ds18b20Initial(void);
  5. void Ds18b20Write(unsigned char infor);
  6. unsigned char Ds18b20Read(void);
  7. //ds18b20初始化 初始化成功返回0x00,失败返回0x01
  8. unsigned char Ds18b20Initial(void)
  9. {
  10. unsigned char Status = 0x00;
  11. unsigned int CONT_1 = 0;
  12. unsigned char Flag_1 = 1;
  13. DS18B20_PIN_OUT();
  14. DS18B20_PIN = 1; //DQ复位
  15. system_delay_us(260); //稍做延时
  16. DS18B20_PIN = 0; //单片机将DQ拉低
  17. system_delay_us(750); //精确延时 大于 480us 小于960us
  18. DS18B20_PIN = 1; //拉高总线
  19. DS18B20_PIN_IN();//设置IO输入
  20. while((DS18B20_PIN != 0)&&(Flag_1 == 1))//等待ds18b20响应,具有防止超时功能
  21. { //等待约60ms左右
  22. CONT_1++;
  23. system_delay_us(10);
  24. if(CONT_1 > 8000)Flag_1 = 0;
  25. Status = DS18B20_PIN;
  26. }
  27. DS18B20_PIN_OUT();
  28. DS18B20_PIN = 1;
  29. system_delay_us(100);
  30. return Status; //返回初始化状态
  31. }
  32. void Ds18b20Write(unsigned char infor)
  33. {
  34. unsigned int i;
  35. DS18B20_PIN_OUT();
  36. for(i=0;i<8;i++)
  37. {
  38. if((infor & 0x01))
  39. {
  40. DS18B20_PIN = 0;
  41. system_delay_us(6);
  42. DS18B20_PIN = 1;
  43. system_delay_us(50);
  44. }
  45. else
  46. {
  47. DS18B20_PIN = 0;
  48. system_delay_us(50);
  49. DS18B20_PIN = 1;
  50. system_delay_us(6);
  51. }
  52. infor >>= 1;
  53. }
  54. }
  55. unsigned char Ds18b20Read(void)
  56. {
  57. unsigned char Value = 0x00;
  58. unsigned int i;
  59. DS18B20_PIN_OUT();
  60. DS18B20_PIN = 1;
  61. system_delay_us(10);
  62. for(i=0;i<8;i++)
  63. {
  64. Value >>= 1;
  65. DS18B20_PIN_OUT();
  66. DS18B20_PIN = 0;// 给脉冲信号
  67. system_delay_us(3);
  68. DS18B20_PIN = 1;// 给脉冲信号
  69. system_delay_us(3);
  70. DS18B20_PIN_IN();
  71. if(DS18B20_PIN == 1) Value |= 0x80;
  72. system_delay_us(15);
  73. }
  74. return Value;
  75. }
  76. //温度读取函数
  77. unsigned char ReadDs18B20(void)
  78. {
  79. unsigned char V1,V2; //定义高低8位 缓冲
  80. unsigned char temp; //定义温度缓冲寄存器
  81. Ds18b20Initial();
  82. Ds18b20Write(0xcc); // 跳过读序号列号的操作
  83. Ds18b20Write(0x44); // 启动温度转换
  84. Ds18b20Initial();
  85. Ds18b20Write(0xcc); //跳过读序号列号的操作
  86. Ds18b20Write(0xbe); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
  87. V1 = Ds18b20Read(); //低位
  88. V2 = Ds18b20Read(); //高位
  89. temp = ((V1 >> 4)+((V2 & 0x07)*16)); //转换数据
  90. return temp;
  91. }
  92. //温度读取函数 带1位小数位
  93. float floatReadDs18B20(void)
  94. {
  95. unsigned char V1,V2; //定义高低8位 缓冲
  96. unsigned int temp; //定义温度缓冲寄存器
  97. float fValue;
  98. Ds18b20Initial();
  99. Ds18b20Write(0xcc); // 跳过读序号列号的操作
  100. Ds18b20Write(0x44); // 启动温度转换
  101. Ds18b20Initial();
  102. Ds18b20Write(0xcc); //跳过读序号列号的操作
  103. Ds18b20Write(0xbe); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
  104. V1 = Ds18b20Read(); //低位
  105. V2 = Ds18b20Read(); //高位
  106. //temp = ((V1 >> 4)+((V2 & 0x07)*16)); //转换数据
  107. temp=V2*0xFF+V1;
  108. fValue = temp*0.0625;
  109. return fValue;
  110. }