找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2086|回复: 5
收起左侧

TI HDC1080温度传感器单片机程序

[复制链接]
ID:958854 发表于 2021-12-13 14:59 | 显示全部楼层 |阅读模式
适用于C51单片机
  1. /*****************************************************************************
  2. 编写人:Yayi                                              
  3. *****************************************************************************/
  4. // 包含头文件
  5. #include <reg52.h>
  6. #include <INTRINS.H>
  7. #include <stdio.h>
  8. #include <Delay.h>
  9. #include <IIC.h>
  10. #include <hdc1080.h>
  11. #include <UART.h>

  12. /*****************************************************************************
  13. //主程序
  14. *****************************************************************************/
  15. void main()
  16. {
  17.     float temp,hum;
  18.     unsigned char buf[5];
  19.     int i;
  20.     Delay_Ms(500);
  21.         Uart_Init();       
  22.     //初始化HDC1080
  23.     if(HDC1080_Init()==-1)
  24.         Send_String("HDC1080 initialize error.\r\n");
  25.     else
  26.         Send_String("HDC1080 initialize register finished.\r\n");

  27.         while(1)
  28.         {
  29.         Send_String("Temperature:");   
  30.         
  31.         temp=HDC1080_Temperature();
  32.         
  33.         buf[0]=(int)temp/10+0x30;
  34.         buf[1]=(int)temp%10+0x30;
  35.         buf[2]='.';
  36.         buf[3]=(int)(temp*10)%(int)temp+0x30;
  37.         buf[4]=(int)(temp*100)%(int)(temp*10)+0x30;
  38.         
  39.         for(i=0;i<5;i++)
  40.             Send_Char(buf[i]);
  41.                
  42.                 Send_String("C\r\n");

  43.         Send_String("Humidity:");   

  44.         hum=HDC1080_Humidity();
  45.         
  46.         buf[0]=((int)hum)/10+0x30;
  47.         buf[1]=((int)hum)%10+0x30;
  48.         buf[2]='.';
  49.         buf[3]=(int)(hum*10)%(int)hum+0x30;
  50.         buf[4]=(int)(hum*100)%(int)(hum*10)+0x30;
  51.         
  52.         for(i=0;i<5;i++)
  53.             Send_Char(buf[i]);
  54.             
  55.         Send_String("%%\r\n");
  56.         Delay_Ms(1000);
  57.         }
  58. }
复制代码



  1. /*****************************************************************************
  2. 定义类型及变量
  3. *****************************************************************************/
  4. #define HDC1080_I2CADDR         0x40
  5. #define HDC1080_TEMPERATURE     0x00
  6. #define HDC1080_HUMIDITY        0x01
  7. #define HDC1080_CONFIG          0x02
  8. #define HDC1080_CONFIG_RST      (1 << 15)
  9. #define HDC1080_CONFIG_HEAT     (1 << 13)
  10. #define HDC1080_CONFIG_MODE     (1 << 12)
  11. #define HDC1080_CONFIG_BATT     (1 << 11)
  12. #define HDC1080_CONFIG_TRES_14  0
  13. #define HDC1080_CONFIG_TRES_11  (1 << 10)
  14. #define HDC1080_CONFIG_HRES_14  0
  15. #define HDC1080_CONFIG_HRES_11  (1 << 8)
  16. #define HDC1080_CONFIG_HRES_8   (1 << 9)

  17. #define HDC1080_SERIAL_ID_FIRST 0xFB
  18. #define HDC1080_SERIAL_ID_MID   0xFC
  19. #define HDC1080_SERIAL_ID_LAST  0xFD
  20. #define HDC1080_MANUFID         0xFE
  21. #define HDC1080_DEVICEID        0xFF


  22. /*****************************************************************************
  23. HDC1080初始化
  24. /****************************************************************************/
  25. unsigned char HDC1080_Init()
  26. {
  27.     unsigned short int config = HDC1080_CONFIG_RST | HDC1080_CONFIG_MODE | HDC1080_CONFIG_TRES_14 | HDC1080_CONFIG_HRES_14;
  28.     unsigned char buf[2];
  29.     unsigned short int tmp;
  30.     SlaveAddress=HDC1080_I2CADDR;
  31.     Single_WriteI2C(HDC1080_CONFIG, (config>>8)&0x00FF);
  32.    
  33.         Delay_Ms(15);
  34.    
  35.     Single_ReadI2C(HDC1080_MANUFID,buf,2);
  36.     tmp = (buf[0]<<8)+buf[1];
  37.     if(tmp!=0x5449)
  38.         return -1;
  39.         
  40.     Single_ReadI2C(HDC1080_DEVICEID,buf,2);   
  41.     tmp = (buf[0]<<8)+buf[1];
  42.     if(tmp!=0x1050)
  43.         return -1;
  44.         
  45.     return 0;
  46. }

  47. /*****************************************************************************
  48. HDC1080读取温度
  49. /****************************************************************************/
  50. float HDC1080_Temperature(void)
  51. {
  52.     float tmp;
  53.     unsigned char buf[2];
  54.     Single_ReadI2C(HDC1080_TEMPERATURE,buf,2);  
  55.     tmp=(buf[0]<<8)+buf[1];
  56.    
  57.     tmp /= 65536;
  58.     tmp *= 165;
  59.     tmp -= 40;
  60.     return tmp;
  61. }

  62. /*****************************************************************************
  63. HDC1080读取湿度
  64. /****************************************************************************/
  65. float HDC1080_Humidity(void)
  66. {
  67.     float tmp;
  68.     unsigned char buf[4];
  69.    
  70.     Single_ReadI2C(HDC1080_HUMIDITY,buf,2);
  71.     tmp=((buf[0]<<8)+buf[1])& 0xFFFF;
  72.    
  73.     tmp /= 65536;
  74.     tmp *= 100;
  75.     return tmp;
  76. }
复制代码

51hei.png

Keil代码下载: c样本51hei.7z (26.77 KB, 下载次数: 34)

评分

参与人数 1黑币 +40 收起 理由
admin + 40 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:958854 发表于 2022-2-4 17:05 | 显示全部楼层
有HDC1080的AD封装吗?能不能发一下?
回复

使用道具 举报

ID:198210 发表于 2022-5-24 11:57 | 显示全部楼层
怎么一直显示湿度99%,温度<4%
回复

使用道具 举报

ID:476556 发表于 2023-3-7 11:21 | 显示全部楼层
如果温度是零下  是怎么显示的呢
回复

使用道具 举报

ID:963375 发表于 2023-3-28 11:53 | 显示全部楼层
64639F 发表于 2022-2-4 17:05
有HDC1080的AD封装吗?能不能发一下?

请问什么问题?你解决了吗?
回复

使用道具 举报

ID:963375 发表于 2023-3-30 13:38 | 显示全部楼层
lxbsc 发表于 2022-5-24 11:57
怎么一直显示湿度99%,温度

请问什么问题?你解决了吗?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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