TI公司的传感器芯片 使用IIC通讯
单片机源程序如下:
- /*
- * HDC1080.c:
- */
- #include "hdc1080.h"
- #include <stdlib.h>
- #include <stdio.h>
- #define HDC1080_I2CADDR 0x40
- #define HDC1080_TEMPERATURE 0x00
- #define HDC1080_HUMIDITY 0x01
- #define HDC1080_CONFIG 0x02
- #define HDC1080_CONFIG_RST (1 << 15)
- #define HDC1080_CONFIG_HEAT (1 << 13)
- #define HDC1080_CONFIG_MODE (1 << 12)
- #define HDC1080_CONFIG_BATT (1 << 11)
- #define HDC1080_CONFIG_TRES_14 0
- #define HDC1080_CONFIG_TRES_11 (1 << 10)
- #define HDC1080_CONFIG_HRES_14 0
- #define HDC1080_CONFIG_HRES_11 (1 << 8)
- #define HDC1080_CONFIG_HRES_8 (1 << 9)
- #define HDC1080_SERIAL_ID_FIRST 0xFB
- #define HDC1080_SERIAL_ID_MID 0xFC
- #define HDC1080_SERIAL_ID_LAST 0xFD
- #define HDC1080_MANUFID 0xFE
- #define HDC1080_DEVICEID 0xFF
- bool HDC1080_Init(void)
- {
- uint16_t data;
- uint8_t buf[2];
- uint16_t config = HDC1080_CONFIG_RST | HDC1080_CONFIG_MODE | HDC1080_CONFIG_TRES_14 | HDC1080_CONFIG_HRES_14;
- //soft reset
- IIC_WriteReg(HDC1080_I2CADDR, HDC1080_CONFIG, (config>>8)&0x00FF);
- DELAY_MS(15);
-
- IIC_ReadData(HDC1080_I2CADDR, HDC1080_MANUFID, buf, 2);
- data = (buf[0]<<8)+buf[1];
- if(data!=0x5449)
- return false;
-
- IIC_ReadData(HDC1080_I2CADDR, HDC1080_DEVICEID, buf, 2);
- data = (buf[0]<<8)+buf[1];
- if(data!=0x1050)
- return false;
-
- Usart1_Send("HDC1080 initialize register finished.\r\n");
- return true;
- }
- float HDC1080_Temperature(void)
- {
- float data;
- uint8_t buf[2];
- IIC_ReadData(HDC1080_I2CADDR, HDC1080_TEMPERATURE, buf, 2);
- data=(buf[0]<<8)+buf[1];
-
- data /= 65536;
- data *= 165;
- data -= 40;
- return data;
- }
- float HDC1080_Humidity(void)
- {
- float data;
- uint8_t buf[2];
- IIC_ReadData(HDC1080_I2CADDR, HDC1080_HUMIDITY, buf, 2);
- data=(buf[0]<<8)+buf[1];
-
- data /= 65536;
- data *= 100;
- return data;
- }
复制代码
Keil代码下载:
HDC1080.7z
(183.2 KB, 下载次数: 44)
|