找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3648|回复: 2
收起左侧

STM32 实现DS18B20的驱动程序

[复制链接]
ID:507898 发表于 2019-4-9 15:04 | 显示全部楼层 |阅读模式
  1. ------------------第一部分是--------ds18b20.h----------------------
  2. #ifndef __DS18B20_H
  3. #define __DS18B20_H

  4. #include "stm32f10x.h"
  5. #include "bsp_SysTick.h"  //精确延时函数头文件----参考[url]http://blog.csdn.net/xuxuechen/article/details/40783209[/url]这个看一下

  6. #define HIGH 1
  7. #define LOW  0

  8. #define DS18B20_CLK   RCC_APB2Periph_GPIOB
  9. #define DS18B20_PIN   GPIO_Pin_10         
  10. #define DS18B20_PORT GPIOB         //总体代表DS18B20的GPIO口为PB10


  11. //带参宏,可以像内联函数一样使用,输出高电平或低电平
  12. #define DS18B20_DATA_OUT(a)if (a)
  13. GPIO_SetBits(GPIOB,GPIO_Pin_10);
  14. else
  15. GPIO_ResetBits(GPIOB,GPIO_Pin_10)
  16. //读取引脚的电平
  17. #define DS18B20_DATA_IN() GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10)

  18. uint8_t DS18B20_Init(void);
  19. float DS18B20_Get_Temp(uint8_t *a,uint8_t b);
  20. void read_serial(uint8_t *serial);
  21. #endif /* __DS18B20_H */

  22. ----------------第二部分是----------DS18B20.C源文件---------------------

  23. #include "bsp_ds18b20.h"

  24. uint8_t serial_1[8]={0x28,0x2d,0x9a,0xdd,0x02,0x00,0x00,0x3b};
  25. uint8_t serial_2[8]={0x28,0x3b,0x2b,0xbc,0x02,0x00,0x00,0x4f};
  26. uint8_t serial_3[8]={0x28,0x00,0x49,0x1b,0x03,0x00,0x00,0x4c};//序列号,需要根据自己的DS18B20修改,具体读取方法,会有另一篇介绍。

  27. static void DS18B20_GPIO_Config(void)
  28. {
  29. GPIO_InitTypeDef GPIO_InitStructure;

  30. RCC_APB2PeriphClockCmd(DS18B20_CLK, ENABLE);
  31. GPIO_InitStructure.GPIO_Pin = DS18B20_PIN;
  32. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  33. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  34. GPIO_Init(DS18B20_PORT, &GPIO_InitStructure);

  35. GPIO_SetBits(DS18B20_PORT, DS18B20_PIN);
  36. }

  37. static void DS18B20_Mode_IPU(void) //使DS18B20-DATA引脚变为输入模式
  38. {
  39. GPIO_InitTypeDef GPIO_InitStructure;

  40. GPIO_InitStructure.GPIO_Pin = DS18B20_PIN;
  41. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  42. GPIO_Init(DS18B20_PORT, &GPIO_InitStructure);
  43. }

  44. static void DS18B20_Mode_Out_PP(void) //使DS18B20-DATA引脚变为输出模式
  45. {
  46. GPIO_InitTypeDef GPIO_InitStructure;

  47. GPIO_InitStructure.GPIO_Pin = DS18B20_PIN;
  48. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  49. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  50. GPIO_Init(DS18B20_PORT, &GPIO_InitStructure);
  51. }

  52. static void DS18B20_Rst(void) //主机给从机发送复位脉冲
  53. {
  54. DS18B20_Mode_Out_PP();
  55. DS18B20_DATA_OUT(LOW);
  56. Delay_Us(750); //主机至少产生480us的低电平复位信号
  57. DS18B20_DATA_OUT(HIGH); //主机在产生复位信号后,需将总线拉高
  58. Delay_Us(15); //从机接收到主机的复位信号后,会在15~60us后给主机发一个存在脉冲
  59. }

  60. static uint8_t DS18B20_Presence(void) //检测从机给主机返回的存在脉冲
  61. {
  62. uint8_t pulse_time = 0;

  63. DS18B20_Mode_IPU(); //主机设置为上拉输入

  64. //等待存在脉冲的到来,存在脉冲为一个60~240us的低电平信号
  65. //如果存在脉冲没有来则做超时处理,从机接收到主机的复位信号后,会在15~60us后给主机发一个存在脉冲

  66. while( DS18B20_DATA_IN() && pulse_time<100 )
  67. {
  68.   pulse_time++;
  69.   Delay_Us(1);
  70. }
  71. if( pulse_time >=100 ) //经过100us后,存在脉冲都还没有到来
  72.     return 1;
  73. else
  74.     pulse_time = 0;
  75. while( !DS18B20_DATA_IN() && pulse_time<240 ) //存在脉冲到来,且存在的时间不能超过240us
  76. {
  77.   pulse_time++;
  78.   Delay_Us(1);
  79. }
  80. if( pulse_time >=240 )
  81.   return 1;
  82. else
  83.   return 0;
  84. }

  85. static uint8_t DS18B20_Read_Bit(void) //从DS18B20读取一个bit
  86. {
  87. uint8_t dat;

  88. DS18B20_Mode_Out_PP(); //读0和读1的时间至少要大于60us
  89. DS18B20_DATA_OUT(LOW); //读时间的起始:必须由主机产生 >1us <15us 的低电平信号
  90. Delay_Us(10);
  91. DS18B20_Mode_IPU(); //设置成输入,释放总线,由外部上拉电阻将总线拉高
  92. if( DS18B20_DATA_IN() == SET )
  93.    dat = 1;
  94. else
  95.   dat = 0;
  96. Delay_Us(45);  //这个延时参数请参考时序图

  97. return dat;
  98. }

  99. uint8_t DS18B20_Read_Byte(void) //从DS18B20读一个字节,低位先行
  100. {
  101. uint8_t i, j, dat = 0;

  102. for(i=0; i<8; i++)
  103. {
  104.    j = DS18B20_Read_Bit();
  105. dat = (dat) | (j<<i);
  106. }

  107. return dat;
  108. }

  109. void DS18B20_Write_Byte(uint8_t dat) //写一个字节到DS18B20,低位先行
  110. {
  111. uint8_t i, testb;

  112. DS18B20_Mode_Out_PP();
  113. for( i=0; i<8; i++ )
  114. {
  115.   testb = dat&0x01;
  116.   dat = dat>>1;
  117.   if (testb)//写0和写1的时间至少要大于60us
  118.   {
  119.     DS18B20_DATA_OUT(LOW);
  120.     Delay_Us(8);
  121.     DS18B20_DATA_OUT(HIGH);
  122.     Delay_Us(58);
  123.   }
  124. else
  125.   {
  126.     DS18B20_DATA_OUT(LOW);
  127.     Delay_Us(70);
  128.     DS18B20_DATA_OUT(HIGH);
  129.     Delay_Us(2);
  130.    }
  131. }
  132. }

  133. void DS18B20_Start(void)
  134. {
  135. DS18B20_Rst();
  136. DS18B20_Presence();
  137. DS18B20_Write_Byte(0XCC); //跳过 ROM
  138. DS18B20_Write_Byte(0X44); //开始转换
  139. }

  140. uint8_t DS18B20_Init(void)
  141. {
  142. DS18B20_GPIO_Config();
  143. DS18B20_Rst();

  144. return DS18B20_Presence();
  145. }

  146. void DS18B20_Match_Serial(uint8_t a)  //匹配序列号
  147. {
  148. uint8_t i;
  149. DS18B20_Rst();
  150. DS18B20_Presence();
  151. DS18B20_Write_Byte(0X55); //匹配序列号指令
  152. if(a==1)
  153. {
  154.   for(i=0;i<8;i++)
  155.    DS18B20_Write_Byte(serial_1[i]);
  156. }
  157. else if(a==2)
  158. {
  159.   for(i=0;i<8;i++)
  160.    DS18B20_Write_Byte(serial_2[i]);
  161. }
  162. else if(a==3)
  163. {
  164.   for(i=0;i<8;i++)
  165.    DS18B20_Write_Byte(serial_3[i]);
  166. }   
  167. }</i);


  168. //存储的温度是16 位的带符号扩展的二进制补码形式
  169. //当工作在12位分辨率时,其中5个符号位,7个整数位,4个小数位

  170. //     |---------整数----------|-----小数 分辨率 1/(2^4)=0.0625----|
  171. //低字节 | 2^3 | 2^2 | 2^1 | 2^0 | 2^(-1) | 2^(-2) | 2^(-3) | 2^(-4) |

  172. //    |-----符号位:0->正 1->负-------|-----------整数-----------|
  173. //高字节 | s | s | s | s |  s  |  2^6 |  2^5 |  2^4 |
  174. //温度 = 符号位 + 整数 + 小数*0.0625


  175. float DS18B20_Get_Temp(uint8_t*a,uint16_tgo_temp,uint8_tb)
  176. {
  177. uint8_t tpmsb, tplsb;
  178. short s_tem;
  179. float f_tem;
  180. int temp_num;

  181. DS18B20_Rst();
  182. DS18B20_Presence();
  183. DS18B20_Write_Byte(0XCC); // 跳过 ROM
  184. DS18B20_Match_Serial(b); //匹配序列号
  185. DS18B20_Write_Byte(0X44); // 开始转换

  186. DS18B20_Rst();
  187. DS18B20_Presence();
  188. DS18B20_Write_Byte(0XCC); //跳过 ROM
  189. DS18B20_Match_Serial(b); //匹配序列号
  190. DS18B20_Write_Byte(0XBE); //读温度值

  191. tplsb = DS18B20_Read_Byte();
  192. tpmsb = DS18B20_Read_Byte();

  193. s_tem = tpmsb<<8;
  194. s_tem = s_tem | tplsb;

  195. if( s_tem < 0 ) //负温度
  196. {
  197.   f_tem = (~s_tem+1) * 0.0625;
  198.   temp_num = (~s_tem+1) * 0.0625*10;
  199.   go_temp= temp_num;
  200.   if(temp_num>=1000)
  201.   {
  202.    a[0]='-';
  203.    a[1]= temp_num/1000+'0';
  204.    a[2]= temp_num%1000/100+'0';
  205.    a[3]= temp_num%100/10+'0';
  206.    a[4]='.';
  207.    a[5]= temp_num%10+'0';
  208.    a[6]= '';
  209.   }
  210.   else
  211.   {
  212.    a[0]='-';
  213.    a[1]= temp_num/100+'0';
  214.    a[2]= temp_num%100/10+'0';
  215.    a[3]='.';
  216.   a[4]=temp_num%10+'0';
  217.    a[5]= '';
  218.   }
  219. }
  220. else
  221. {
  222.   f_tem = s_tem * 0.0625;
  223.   temp_num = s_tem * 0.0625*10;
  224. go_temp= temp_num;
  225. if(temp_num>=1000)
  226. {
  227.   a[0]='+';
  228.   a[1]= temp_num/1000+'0';
  229.   a[2]= temp_num%1000/100+'0';
  230.   a[3]= temp_num%100/10+'0';
  231.   a[4]='.';
  232.   a[5]= temp_num%10+'0';
  233.   a[6]= '';
  234.   }
  235. else
  236. {
  237.   a[0]='+';
  238.   a[1]= temp_num/100+'0';
  239.   a[2]= temp_num%100/10+'0';
  240.   a[3]='.';
  241.   a[4]=temp_num%10+'0';
  242.   a[5]= '';
  243.   }
  244. }
  245. return f_tem;
  246. }

  247. void read_serial(uint8_t *serial) //读取序列号
  248. {
  249. uint8_t i;
  250. DS18B20_Rst();
  251. DS18B20_Presence();
  252. DS18B20_Write_Byte(0X33); //读取序列号指令
  253. for(i=0;i<8;i++)
  254.   serial[i] = DS18B20_Read_Byte();
  255. }

  256. ----------------第三部分是-------------main.c--------------
复制代码
回复

使用道具 举报

ID:445069 发表于 2019-4-12 08:17 | 显示全部楼层
网上有这段程序,有一些问题,改了后才能跑过。
回复

使用道具 举报

ID:656818 发表于 2019-12-5 20:09 来自手机 | 显示全部楼层
ontheroad 发表于 2019-4-12 08:17
网上有这段程序,有一些问题,改了后才能跑过。

请问哪里有问题呢?能不能修改一下
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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