DS18B20_ATY.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /**
  2. * @file DS18B20_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2026 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief functions of DS18B20 for C platform
  20. *
  21. * @version
  22. * - 1_01_220927 > ATY
  23. * -# Preliminary version, first Release
  24. * - Undone
  25. ********************************************************************************
  26. */
  27. #ifndef __DS18B20_ATY_C
  28. #define __DS18B20_ATY_C
  29. #include "DS18B20_ATY.h"
  30. #if 0 /* Legacy implementation disabled after Dev-style refactor */
  31. /******************************* For user *************************************/
  32. /******************************************************************************/
  33. #include "DS18B20.h"
  34. #include "delay.h"
  35. #include "usart.h"
  36. u8 ROM_ID[8];
  37. u8 Refresh_OLED_Data = 0;
  38. /*******************************************
  39. DS18B20 单总线对延时精度要求非常高!!!
  40. 本程序使用了系统嘀嗒定时器来当做精准延时。
  41. DQ :GPIOB_7
  42. *******************************************/
  43. /*功能:DS18B20初始化*/
  44. void DS18B20_Init(void)
  45. {
  46. /*1.GPIOC口初始化*/
  47. RCC->APB2ENR |= 1 << 3;
  48. GPIOB->CRL &= 0x0FFFFFFF;
  49. GPIOB->CRL |= 0x30000000;
  50. GPIOB->ODR |= 1 << 7;
  51. /*2.检测DS18B20设备是否正常*/
  52. switch(DS18B20_CheckDevice())
  53. {
  54. case 0: printf("DS18B20_Init OK!\n"); break;
  55. case 1: printf("DS18B20设备响应复位信号失败!\n"); break;
  56. case 2: printf("DS18B20设备释放总线失败!\n"); break;
  57. }
  58. }
  59. /*功能:向DS18B20发送一个复位信号*/
  60. void DS18B20_SendRestSingle(void)
  61. {
  62. /*主机发送复位信号*/
  63. DS18B20_OutPut_Mode();
  64. DS18B20_OUT = 0; //拉低总线480~960 us ,对 DS18B20 芯片进行复位
  65. DelayUs(750);
  66. DS18B20_OUT = 1;
  67. DelayUs(15); //释放总线15~60 us
  68. }
  69. /*
  70. 功能:检测DS18B20存在脉冲
  71. 返回值:
  72. 0 DS18B20设备正常
  73. 1 DS18B20设备响应复位信号失败
  74. 2 DS18B20设备释放总线失败
  75. */
  76. u8 DS18B20_CheckReadySingle(void)
  77. {
  78. u8 cnt = 0;
  79. /*1.检测存在脉冲*/
  80. DS18B20_InPut_Mode();
  81. while(DS18B20_IN && cnt < 240) //等待DS18B20 拉低总线 (60~240 us 响应复位信号)
  82. {
  83. DelayUs(1);
  84. cnt++;
  85. }
  86. if(cnt > 240) return 1;
  87. /*2.检测DS18B20是否释放总线*/
  88. cnt = 0;
  89. DS18B20_InPut_Mode();
  90. while((!DS18B20_IN) && cnt < 240) //判断DS18B20是否释放总线(60~240 us 响应复位信号之后会释放总线)
  91. {
  92. DelayUs(1);
  93. cnt++;
  94. }
  95. if(cnt > 240) return 2;
  96. else return 0;
  97. }
  98. /*
  99. 功能:检测DS18B20设备是否正常
  100. 返回值:
  101. 0 DS18B20设备正常
  102. 1 DS18B20设备响应复位信号失败
  103. 2 DS18B20设备释放总线失败
  104. */
  105. u8 DS18B20_CheckDevice(void)
  106. {
  107. DS18B20_SendRestSingle();/*1.主机发送复位信号*/
  108. return DS18B20_CheckReadySingle();/*2.检测存在脉冲*/
  109. }
  110. /*功能:向DS18B20写一个字节数据(命令)*/
  111. u8 BS18B20_WriteByte(u8 cmd)
  112. {
  113. u8 i = 0;
  114. DS18B20_OutPut_Mode();
  115. for(i = 0; i < 8; i++)
  116. {
  117. DS18B20_OUT = 0;
  118. DelayUs(2); //主机拉低总线写数据时间隙2us
  119. DS18B20_OUT = cmd & 0x01; //如果该写入位为1,必须在15us之内把总线拉高,为0 保持60us即可。
  120. DelayUs(60); //确保DS18B20已经成功读取到该位数据
  121. DS18B20_OUT = 1; //一位发送完成
  122. cmd >>= 1;
  123. DelayUs(2); //位间隙2us
  124. }
  125. return 0;
  126. }
  127. /*功能:从DS18B20读取一个字节数据*/
  128. u8 DS18B20_ReadByte(void)
  129. {
  130. u8 i, data = 0;
  131. for(i = 0; i < 8; i++)
  132. {
  133. DS18B20_OutPut_Mode();//初始化为输出模式
  134. DS18B20_OUT = 0;
  135. DelayUs(2); //主机拉低总线读数据时间隙2us
  136. DS18B20_OUT = 1; //释放总线,准备读取位数据
  137. DS18B20_InPut_Mode(); //初始化为输入模式
  138. DelayUs(10); //等待DS18B20的数据输出
  139. data >>= 1; //高位补0,默认以0为准
  140. if(DS18B20_IN) data |= 0x80;
  141. DelayUs(60); //延时确保DS18B20采样周期已经过去(非常重要)
  142. DS18B20_OUT = 1; //释放总线准备读取下一位位数据
  143. }
  144. return data;
  145. }
  146. /*
  147. 函数功能: 读取一次DS18B20的温度数据
  148. 返 回 值: 读取的温度数据
  149. 考虑的情况: 总线上只是接了一个DS18B20的情况
  150. */
  151. u16 DS18B20_GetTemperature(void)
  152. {
  153. u16 temp = 0;
  154. u8 temp_H, temp_L;
  155. int intT, decT;
  156. DS18B20_CheckDevice(); //发送复位脉冲、检测存在脉冲
  157. BS18B20_WriteByte(0xCC); //跳过ROM序列检测
  158. BS18B20_WriteByte(0x44); //启动一次温度转换
  159. //等待温度转换完成
  160. while(DS18B20_ReadByte() != 0xFF){}
  161. DS18B20_CheckDevice(); //发送复位脉冲、检测存在脉冲
  162. BS18B20_WriteByte(0xCC); //跳过ROM序列检测
  163. BS18B20_WriteByte(0xBE); //读取温度
  164. temp_L = DS18B20_ReadByte(); //读取的温度低位数据
  165. temp_H = DS18B20_ReadByte(); //读取的温度高位数据
  166. temp = temp_L | (temp_H << 8); //合成温度
  167. intT = temp >> 4; /*合成实际温度整数部分****精度相对上面的更高*/
  168. decT = temp & 0xF; /*合成实际温度小数部分*/
  169. printf("温度:%d.%d ℃ \n", intT, decT);
  170. return temp;
  171. }
  172. ///*功能:从DS18B20读取温度值,多点测温*/
  173. //u16 DS18B20_GetTemperature(void)
  174. //{
  175. // u16 temp;
  176. // u8 TL,TH;
  177. // DS18B20_SendRestSingle(); /*1.主机发送复位信号*/
  178. // if(DS18B20_CheckReadySingle()) return 0xFF; /*2.检测存在脉冲*/
  179. // DS18B20_MatchROM(); /*3.匹配 ROM 指令 (仅适用于总线上有多个设备)*/
  180. BS18B20_WriteByte(0xCC); /*3.跳跃 ROM 指令 (仅适用于总线上只有一个设备)*/
  181. // BS18B20_WriteByte(0x44); /*4.启动温度转换*/
  182. // while(DS18B20_ReadByte()!=0xFF); //等待温度转换成功
  183. // DS18B20_SendRestSingle(); /*5.主机发送复位信号*/
  184. // if(DS18B20_CheckReadySingle()) return 0xFF; /*6.检测存在脉冲*/
  185. // DS18B20_MatchROM(); /*6.匹配 ROM 指令 (仅适用于总线上有多个设备)*/
  186. BS18B20_WriteByte(0xCC); /*7.跳跃 ROM 指令 (仅适用于总线上只有一个设备)*/
  187. // BS18B20_WriteByte(0xBE); /*8.发送读取温度命令*/
  188. //
  189. // TL = DS18B20_ReadByte(); /*9.读取温度高低位、校验位*/
  190. // TH = DS18B20_ReadByte();
  191. // DS18B20_SendRestSingle(); /*10.主机发送复位信号,示意停止读取剩下的数据*/
  192. // temp = TH<<8 | TL; /*11.合成温度值*/
  193. // return temp;
  194. //
  195. temp = temp * 0.0625; /*直接合成实际温度*/
  196. value = temp * 100 + (value > 0 ? 0.5 : -0.5); //将它放大100倍, 使显示时可显示小数点后两位, 并对小数点后第三进行4舍5入
  197. //
  198. intT = temp >> 4; /*合成实际温度整数部分****精度相对上面的更高*/
  199. decT = temp & 0xF; /*合成实际温度小数部分*/
  200. ///* crc = x^8 + x^5 + x^4 + 1*/
  201. //}
  202. /*功能:从DS18B20读取ROM信息 (ROM_ID= 28-92-AF-AC-17-13-1-F1)*/
  203. u8 DS18B20_ReadRomInfo(void)
  204. {
  205. u8 i = 0;
  206. BS18B20_WriteByte(0x33); /*4.启动读取ROM*/
  207. for(i = 0; i < 8; i++)
  208. {
  209. ROM_ID[i] = DS18B20_ReadByte();
  210. }
  211. printf("ROM_ID= ");
  212. for(i = 0; i < 8; i++)
  213. {
  214. printf("%X", ROM_ID[i]);
  215. if(i == 7) printf("\n");
  216. else printf("-");
  217. }
  218. return 0;
  219. }
  220. /*
  221. ROM_ID[0]=0x28
  222. ROM_ID[1]=0x92
  223. ROM_ID[2]=0xAF
  224. ROM_ID[3]=0xAC
  225. ROM_ID[4]=0x17
  226. ROM_ID[5]=0x13
  227. ROM_ID[6]=0x1
  228. ROM_ID[7]=0xF1
  229. */
  230. /*功能:匹配 DS18B20 ROM信息*/
  231. u8 DS18B20_MatchROM(void)
  232. {
  233. u8 i = 0;
  234. BS18B20_WriteByte(0x55); /*4.匹配64位 ROM 信息*/
  235. for(i = 0; i < 8; i++)
  236. {
  237. BS18B20_WriteByte(ROM_ID[i]);
  238. }
  239. return 0;
  240. }
  241. // todo: not tested
  242. #endif /* Legacy implementation end */
  243. uint8_t DS18B20_InitDev(struct DS18B20_ATY_Dev* dev)
  244. {
  245. if (!dev || !dev->drive_low || !dev->release_bus || !dev->read_bus || !dev->delay_us) return 1;
  246. if (dev->debugEnable == 1 && dev->LOG) dev->LOG("\r\nDS18B20 init done.");
  247. return 0;
  248. }
  249. void DS18B20_ResetDev(struct DS18B20_ATY_Dev* dev)
  250. {
  251. __ATY_LOCK(dev);
  252. dev->drive_low();
  253. dev->delay_us(750);
  254. dev->release_bus();
  255. dev->delay_us(15);
  256. __ATY_UNLOCK(dev);
  257. }
  258. uint8_t DS18B20_CheckReadyDev(struct DS18B20_ATY_Dev* dev)
  259. {
  260. __ATY_LOCK(dev);
  261. uint16_t cnt = 0;
  262. while (dev->read_bus() && cnt < 240) { dev->delay_us(1); cnt++; }
  263. if (cnt >= 240) { __ATY_UNLOCK(dev); return 1; }
  264. cnt = 0;
  265. while (!dev->read_bus() && cnt < 240) { dev->delay_us(1); cnt++; }
  266. if (cnt >= 240) { __ATY_UNLOCK(dev); return 2; }
  267. __ATY_UNLOCK(dev);
  268. return 0;
  269. }
  270. void DS18B20_WriteByteDev(struct DS18B20_ATY_Dev* dev, uint8_t cmd)
  271. {
  272. __ATY_LOCK(dev);
  273. for (uint8_t i = 0; i < 8; ++i) {
  274. dev->drive_low();
  275. dev->delay_us(2);
  276. if (cmd & 0x01) {
  277. dev->release_bus();
  278. dev->delay_us(60);
  279. } else {
  280. dev->delay_us(60);
  281. dev->release_bus();
  282. }
  283. cmd >>= 1;
  284. dev->delay_us(2);
  285. }
  286. __ATY_UNLOCK(dev);
  287. }
  288. uint8_t DS18B20_ReadByteDev(struct DS18B20_ATY_Dev* dev)
  289. {
  290. __ATY_LOCK(dev);
  291. uint8_t data = 0;
  292. for (uint8_t i = 0; i < 8; ++i) {
  293. dev->drive_low();
  294. dev->delay_us(2);
  295. dev->release_bus();
  296. dev->delay_us(10);
  297. data >>= 1;
  298. if (dev->read_bus()) data |= 0x80;
  299. dev->delay_us(60);
  300. }
  301. __ATY_UNLOCK(dev);
  302. return data;
  303. }
  304. uint16_t DS18B20_GetTemperatureDev(struct DS18B20_ATY_Dev* dev)
  305. {
  306. __ATY_LOCK(dev);
  307. uint16_t temp = 0;
  308. uint8_t temp_L = 0, temp_H = 0;
  309. DS18B20_ResetDev(dev);
  310. (void)DS18B20_CheckReadyDev(dev);
  311. DS18B20_WriteByteDev(dev, 0xCC);
  312. DS18B20_WriteByteDev(dev, 0x44);
  313. while (DS18B20_ReadByteDev(dev) != 0xFF) {}
  314. DS18B20_ResetDev(dev);
  315. (void)DS18B20_CheckReadyDev(dev);
  316. DS18B20_WriteByteDev(dev, 0xCC);
  317. DS18B20_WriteByteDev(dev, 0xBE);
  318. temp_L = DS18B20_ReadByteDev(dev);
  319. temp_H = DS18B20_ReadByteDev(dev);
  320. temp = temp_L | (temp_H << 8);
  321. if (dev->debugEnable == 1 && dev->LOG) dev->LOG("\r\nDS18B20 read temperature.");
  322. __ATY_UNLOCK(dev);
  323. return temp;
  324. }
  325. uint8_t DS18B20_ReadRomInfoDev(struct DS18B20_ATY_Dev* dev, uint8_t rom8[8])
  326. {
  327. __ATY_LOCK(dev);
  328. if (!rom8) { __ATY_UNLOCK(dev); return 1; }
  329. DS18B20_WriteByteDev(dev, 0x33);
  330. for (uint8_t i = 0; i < 8; ++i) rom8[i] = DS18B20_ReadByteDev(dev);
  331. if (dev->debugEnable == 1 && dev->LOG) dev->LOG("\r\nDS18B20 read ROM.");
  332. __ATY_UNLOCK(dev);
  333. return 0;
  334. }
  335. #endif /* __DS18B20_ATY_C */
  336. /******************************** End Of File *********************************/