找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1720|回复: 0
收起左侧

基于STM32的HDC1080传感器程序

[复制链接]
ID:958854 发表于 2021-12-14 12:29 | 显示全部楼层 |阅读模式
TI公司的传感器芯片 使用IIC通讯

单片机源程序如下:
  1. /*
  2. * HDC1080.c:

  3. */
  4. #include "hdc1080.h"
  5. #include <stdlib.h>
  6. #include <stdio.h>

  7. #define HDC1080_I2CADDR         0x40
  8. #define HDC1080_TEMPERATURE     0x00
  9. #define HDC1080_HUMIDITY        0x01
  10. #define HDC1080_CONFIG          0x02
  11. #define HDC1080_CONFIG_RST      (1 << 15)
  12. #define HDC1080_CONFIG_HEAT     (1 << 13)
  13. #define HDC1080_CONFIG_MODE     (1 << 12)
  14. #define HDC1080_CONFIG_BATT     (1 << 11)
  15. #define HDC1080_CONFIG_TRES_14  0
  16. #define HDC1080_CONFIG_TRES_11  (1 << 10)
  17. #define HDC1080_CONFIG_HRES_14  0
  18. #define HDC1080_CONFIG_HRES_11  (1 << 8)
  19. #define HDC1080_CONFIG_HRES_8   (1 << 9)

  20. #define HDC1080_SERIAL_ID_FIRST 0xFB
  21. #define HDC1080_SERIAL_ID_MID   0xFC
  22. #define HDC1080_SERIAL_ID_LAST  0xFD
  23. #define HDC1080_MANUFID         0xFE
  24. #define HDC1080_DEVICEID        0xFF

  25. bool HDC1080_Init(void)
  26. {
  27.     uint16_t data;
  28.     uint8_t buf[2];
  29.     uint16_t config = HDC1080_CONFIG_RST | HDC1080_CONFIG_MODE | HDC1080_CONFIG_TRES_14 | HDC1080_CONFIG_HRES_14;

  30.     //soft reset
  31.     IIC_WriteReg(HDC1080_I2CADDR, HDC1080_CONFIG, (config>>8)&0x00FF);
  32.     DELAY_MS(15);
  33.    
  34.     IIC_ReadData(HDC1080_I2CADDR, HDC1080_MANUFID, buf, 2);
  35.     data = (buf[0]<<8)+buf[1];
  36.     if(data!=0x5449)
  37.         return false;
  38.         
  39.     IIC_ReadData(HDC1080_I2CADDR, HDC1080_DEVICEID, buf, 2);
  40.     data = (buf[0]<<8)+buf[1];
  41.     if(data!=0x1050)
  42.         return false;
  43.         
  44.     Usart1_Send("HDC1080 initialize register finished.\r\n");
  45.     return true;
  46. }

  47. float HDC1080_Temperature(void)
  48. {
  49.     float data;
  50.     uint8_t buf[2];
  51.     IIC_ReadData(HDC1080_I2CADDR, HDC1080_TEMPERATURE, buf, 2);
  52.     data=(buf[0]<<8)+buf[1];
  53.    
  54.     data /= 65536;
  55.     data *= 165;
  56.     data -= 40;
  57.     return data;
  58. }

  59. float HDC1080_Humidity(void)
  60. {
  61.     float data;
  62.     uint8_t buf[2];
  63.     IIC_ReadData(HDC1080_I2CADDR, HDC1080_HUMIDITY, buf, 2);
  64.     data=(buf[0]<<8)+buf[1];
  65.    
  66.     data /= 65536;
  67.     data *= 100;
  68.     return data;
  69. }
复制代码

Keil代码下载:
HDC1080.7z (183.2 KB, 下载次数: 44)

评分

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

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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