STM32F103 DS18B20 V3.5.0固件库驱动程序

来源:互联网 发布:淘宝卖家 延长收货 编辑:程序博客网 时间:2024/05/01 05:32
        程序已经调通,硬件平台为:STM32F103RBT6 + DS18B20,使用STM32的V3.5.0的固件库,程序运行后,通过串口打印出温度。

  1. /********************       (C) COPYRIGHT 2015  ***************************
  2. * 文件名  :ds18b20.h
  3. * 描述    :ds18b20 应用函数库         
  4. * 实验平台:
  5. * 硬件连接:-----------------------
  6. *          |   PB6 - DS18B20 DQ    |   
  7. *           ----------------------- 
  8. * 库版本  :ST3.5.0
  9. * 编写日期:2015-03-16
  10. * 修改日期:
  11. * 作者    :
  12. ****************************************************************************/
  13. #ifndef __DS18B20_H
  14. #define        __DS18B20_H

  15. #include "stm32f10x.h"


  16. #define RCC_DS18B20                                                        RCC_APB2Periph_GPIOB
  17. #define GPIO_DS18B20_PORT                                GPIOB
  18. #define GPIO_DS18B20_Pin                                GPIO_Pin_6


  19. #define DS18B20_H                  GPIO_SetBits(GPIOB,GPIO_Pin_6)
  20. #define DS18B20_L                  GPIO_ResetBits(GPIOB,GPIO_Pin_6)

  21. void DS18B20_GPIO_Config(void);
  22. void DS18B20_Reset(void);
  23. void DS18B20_Init(void);
  24. void DS18B20_WriteBit1(void);
  25. void DS18B20_WriteBit0(void);
  26. uint8_t DS18B20_ReadBit(void);
  27. void DS18B20_WriteByte(uint8_t udata);
  28. uint8_t DS18B20_ReadByte(void);
  29. void DS18B20_GetTemp(void);
  30. #endif /* __LED_H */
复制代码





  1. /********************   (C) COPYRIGHT 2015      ***************************
  2. * 文件名  :DS18B20.c
  3. * 描述    :DS18B20温度传感器测试程序
  4. * 实验平台:STM32F103RBT6
  5. * 硬件连接:------------------------
  6. *          |   PB6 - DS18B20 DQ                |
  7. *           ------------------------ 
  8. * 库版本  :ST3.5.0
  9. * 编写时间:2015-03-16
  10. * 修改时间:
  11. * 作者    :
  12. ****************************************************************************/
  13. #include "ds18b20.h"
  14. #include "systick.h"

  15. uint8_t temp1,temp2;   /* 温度传感器数值:整数部分与小数部分 */

  16. /*
  17. * 函数名:DS18B20_GPIO_Config
  18. * 描述  :配置Relay用到的I/O口
  19. * 输入  :无
  20. * 输出  :无
  21. */
  22. void DS18B20_GPIO_Config(void)
  23. {                
  24.         GPIO_InitTypeDef GPIO_InitStructure;        

  25.         /*开启DS18B20对应的GPIO 的外设时钟*/
  26.         RCC_APB2PeriphClockCmd(RCC_DS18B20, ENABLE);         
  27.         /*选择要控制的DS18B20引脚*/                                                                                                                           
  28.   GPIO_InitStructure.GPIO_Pin = GPIO_DS18B20_Pin;        
  29.         /*设置引脚模式为 OD输出*/
  30.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;   
  31.         /*设置引脚速率为50MHz */   
  32.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  33.         /*调用库函数,初始化相应GPIO*/
  34.   GPIO_Init(GPIO_DS18B20_PORT, &GPIO_InitStructure);        
  35. }


  36. void DS18B20_Reset(void)
  37. {
  38.         DS18B20_H;
  39.         Delay_us(20);                 /*  10us延时  */
  40.         DS18B20_L;
  41.         Delay_us(550);    /*  550us延时  */
  42.         DS18B20_H;
  43.         while(GPIO_ReadInputDataBit(GPIO_DS18B20_PORT,GPIO_DS18B20_Pin));
  44.         Delay_us(500);                /*  500us延时  */
  45.         DS18B20_H;
  46. }


  47. void DS18B20_Init(void)
  48. {
  49.         DS18B20_Reset();
  50.         DS18B20_WriteByte(0xCC);
  51.         DS18B20_WriteByte(0x4E);
  52.         DS18B20_WriteByte(0x64);
  53.         DS18B20_WriteByte(0x8A);
  54.         DS18B20_WriteByte(0x1F);
  55. }



  56. void DS18B20_WriteBit0(void)
  57. {
  58.         DS18B20_H;
  59.         Delay_us(1);                /*  1us延时  */
  60.         DS18B20_L;
  61.         Delay_us(55);   /*  55us延时  */
  62.         DS18B20_H;
  63.         Delay_us(1);    /*  1us延时  */
  64. }

  65. void DS18B20_WriteBit1(void)
  66. {
  67.         DS18B20_H;
  68.         Delay_us(1);    /*  1us延时  */
  69.         DS18B20_L;
  70.         Delay_us(5);           /*  5us延时  */
  71.         DS18B20_H;
  72.         Delay_us(5);           /*  5us延时  */
  73.         Delay_us(50);   /*  50us延时  */
  74. }

  75. uint8_t DS18B20_ReadBit(void)
  76. {
  77.         uint8_t bdata;
  78.         DS18B20_H;
  79.         Delay_us(1);           /*  1us延时  */
  80.         DS18B20_L;
  81.         Delay_us(4);           /*  4us延时  */
  82.         DS18B20_H;
  83.         Delay_us(8);           /*  8us延时  */        
  84.         bdata=GPIO_ReadInputDataBit(GPIO_DS18B20_PORT,GPIO_DS18B20_Pin);
  85.         Delay_us(60);   /*  60us延时  */
  86.         Delay_us(2);    /*  2us延时  */
  87.         return bdata;
  88. }

  89. void DS18B20_WriteByte(uint8_t udata)
  90. {
  91.         uint8_t i;
  92.         for(i=0;i<8;i++)
  93.         {
  94.                 if(udata&0x01)
  95.                         DS18B20_WriteBit1();
  96.                 else
  97.                         DS18B20_WriteBit0();
  98.                 udata=udata>>1;
  99.         }
  100.         Delay_us(10);  /*  10us延时  */
  101. }

  102. uint8_t DS18B20_ReadByte(void)
  103. {
  104.         uint8_t i,udata,j;
  105.         udata=0;
  106.         for(i=0;i<8;i++)
  107.         {
  108.                 udata=udata>>1;
  109.                 j=DS18B20_ReadBit();
  110.                 if(j==0x01)
  111.                         udata|=0x80;
  112.                 else
  113.                         udata|=0x00;
  114.                 Delay_us(2);        /*  2us延时  */
  115.         }
  116.         return udata;
  117. }



  118. void DS18B20_GetTemp(void)
  119. {

  120.         DS18B20_WriteByte(0xCC);
  121.         DS18B20_WriteByte(0x44);
  122.         Delay_ms(100);
  123.         DS18B20_Reset();
  124.         DS18B20_WriteByte(0xCC);
  125.         DS18B20_WriteByte(0xBE);
  126.         
  127.         temp2=DS18B20_ReadByte();
  128.         temp1=DS18B20_ReadByte();
  129.         DS18B20_ReadByte();
  130.         DS18B20_ReadByte();
  131.         DS18B20_ReadByte();
  132.         DS18B20_ReadByte();
  133.         DS18B20_ReadByte();
  134.         DS18B20_ReadByte();
  135.         DS18B20_ReadByte();
  136.         
  137.         temp1=temp1<<4;
  138.         temp1+=temp2>>4;
  139.         temp2=(temp2&0x0F)?5:0;
  140.         DS18B20_Reset();
  141.         
  142. }
复制代码




      使用STM32的Systick定时器作为精确的延时。

  1. /*systick.h  */

  2. #ifndef __SYSTICK_H
  3. #define __SYSTICK_H

  4. #include "stm32f10x.h"

  5. void SysTick_Init(void);
  6. void Delay_ms(uint16_t nTime);
  7. void Delay_us(uint16_t nTime);
  8. #endif /*__SYSTICK_H */
复制代码



  1. /********************     (C) COPYRIGHT 2014    **************************
  2. * 文件名  :SysTick.c
  3. * 描述    :SysTick 系统滴答定时器设置。
  4. * 实验平台:
  5. * 库版本  :ST3.5.0
  6. *
  7. * 编写日期:2014-11-04
  8. * 修改日期:
  9. * 作者    :
  10. ****************************************************************************/
  11. #include "SysTick.h"

  12. __IO uint32_t TimingDelay;

  13. /*
  14. * 函数名:SysTick_Init
  15. * 描述         :启动系统滴答定时器 SysTick
  16. * 输入  : 无
  17. * 输出  :无
  18. * 调用  : 外部调用
  19. */
  20. void SysTick_Init(void)
  21. {
  22.         if(SysTick_Config(SystemCoreClock/1000000))         //1us定时器
  23.         {
  24.                 while(1);
  25.         }
  26.         //SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;                         //若无法启动则关闭
  27. }


  28. /*
  29. * 函数名:Delay_us
  30. * 描述         :us延时程序,1us为一个单位
  31. * 输入  : - nTime
  32. * 输出  :无
  33. * 示例  : Delay_us(1) 实现的延时为:1*1us=1us
  34. * 调用  :外部调用
  35. */
  36. void Delay_us(uint16_t nTime)
  37. {
  38.         TimingDelay = nTime;
  39.         //使能系统滴答定时器
  40.         while(TimingDelay !=0);
  41. }


  42. /*
  43. * 函数名:Delay_ms
  44. * 描述         :ms延时程序,1ms为一个单位
  45. * 输入  : - nTime
  46. * 输出  :无
  47. * 示例  : Delay_ms(1) 实现的延时为:1*1ms=1ms
  48. * 调用  :外部调用
  49. */
  50. void Delay_ms(uint16_t nTime)
  51. {
  52.         uint16_t i;
  53.         for(i=0;i<nTime;i++)
  54.         Delay_us(1000);
  55. }
复制代码



      以上为驱动程序。


整个工程打包下载:

点击打开链接

0 0
原创粉丝点击