仅作为个人学习笔记使用
一、引脚配置及功能
1号脚 SDA是IIC的数据线,需要一个上拉的电阻到电源
2号脚GND
6号脚SCL是IIC的时钟线,需要一个上拉电阻接到电源
5号脚VDD接电源
二、与主机通信方式&IIC介绍
与主机之间采用IIC方式进行通信
IIC具有两根双向信号线数据线SDA、时钟线SCL,每个接到IIC线上的设备都有唯一的地址
数据传送时,时钟线SCL为高电平期间数据线SDA必须要保持稳定
起始信号和终止信号都是由主机发出
起始:SCL为高电平期间,SDA下降沿
终止:SCL为高电平期间,SDA上升沿
Read and Write Operation
To access a particular register on the HDC1080, write the desired register address value to the Pointer Register.The pointer value is the first byte transferred after the slave address byte with the R/W bit low. Every writeoperation to the HDC1080 requires a value for the pointer register
翻译:在HDC1080上访问特殊的寄存器,将所需的寄存器地址值写入指针寄存器。指针值是在R/W位为低的从机地址之后传输的第一个字节。每一次对HDC1080写操作都需要一个指针寄存器的值。
总结一下: 如果要对寄存器写数据
1.写对应寄存器地址
2.写入相应数据值 分为高8位和低8位
When reading from the HDC1080, the last value stored in the pointer by a write operation is used to determine which register is accessed by a read operation. To change the pointer register for a read operation, a new valuemust be written to the pointer register. This transaction is accomplished by issuing the slave address byte withthe R/W bit low, followed by the pointer byte. No additional data is required
翻译:当从HDC1080中读数据,通过写操作存储在指针中最后一个值确定哪个是被读操作访问的寄存器。改变读取操作的的指针寄存器,必须向指针寄存器中写入新的值。这个事务的完成是通过发出从地址字节 R/W为低位,后跟指针字节,没有额外的数据要求。
The master can then generate a START condition and send the slave address byte with the R/W bit high to initiate the read command. Note that register bytes are sent MSB first, followed by the LSB. A write operation in a read-only register such as (DEVICE ID, MANUFACTURER ID, SERIAL ID) returns a NACK after each data byte; read/write operation to unused address returns a NACK after the pointer; a read/write operation with incorrect I2C address returns a NACK after the I2C address
翻译:主机产生START条件并且发出从地址字节 R/W位为高初始化读命令。注意,寄存器字节是先发送高位再发送低位。对只读寄存器的写操作例如(DEVICE ID, MANUFACTURER ID, SERIAL ID)要每个数据后返回一个NACK信号
总结一下:读指定寄存器应按照以下步骤进行
1.在读之前先要写入要指针寄存器的地址
2.发出读命令后 R/W位要为高
3.要在数据返回后做出一个应答信号,并在读取完最后一个字节数据后做出非应答信号
三、寄存器配置即使用
By default the HDC1080 will first perform a temperature measurement followed by a humidity measurement. On power-up, the HDC1080 enters a low power sleep mode and is not actively measuring. Use the following steps to perform a measurement of both temperature and humidity and then retrieve the results
翻译:默认情况下,HDC1080将先执行温度测量然后进行湿度的测量。上电后,HDC1080进入低功耗睡眠模式并且没有测量。使用以下步骤开始执行温度和湿度的测量并返回值
1. Configure the acquisition parameters in register address 0x02:
在寄存器地址0x02中配置采集参数
(a) Set the acquisition mode to measure both temperature and humidity by setting Bit[12] to 1.
将位12置1 采集模式是测量温度和湿度
(b) Set the desired temperature measurement resolution:
设置所需测量温度的分辨率
– Set Bit[10] to 0 for 14 bit resolution.
– Set Bit[10] to 1 for 11 bit resolution.
(c) Set the desired humidity measurement resolution:
设置所需测量湿度的分辨率
– Set Bit[9:8] to 00 for 14 bit resolution.
– Set Bit[9:8] to 01 for 11 bit resolution.
– Set Bit[9:8] to 10 for 8 bit resolution.
2. Trigger the measurements by executing a pointer write transaction with the address pointer set to 0x00
翻译:通过执行指针写入事务并将地址指针设置0x00来触发测量
3. Wait for the measurements to complete, based on the conversion time
翻译:需要等待测量结果 延时
4. Read the output data:
读数据
Read the temperature data from register address 0x00, followed by the humidity data from register address 0x01 in a single transaction. A read operation will return a NACK if the contents of the registers have not been updated
翻译:从寄存器地址为0x00读温度数据,随后从地址0x01读湿度数据。如果寄存器的内容没有背更新读操作将返回一个NACK
四、寄存器地址
五、温湿度转换公式
温度转换公式
湿度转换公式
六、源码
hdc1080.c
void HDC1080_Driver_Init(void)
{
uint8_t menufacture_buf[2] = {0}, device_buf[2] = {0};
uint16_t menufacture_id = 0, device_id = 0;
IIC_GPIO_Init();
HDC1080_Read_Register(HDC1080_MENUFACTURE_ID, menufacture_buf, 2);
menufacture_id = (uint16_t)(menufacture_buf[0]<<8);
menufacture_id |= menufacture_buf[1];
HDC1080_Read_Register(HDC1080_DEVICE_ID, device_buf, 2);
device_id = (uint16_t)(device_buf[0]<<8);
device_id |= device_buf[1];
if(menufacture_id==HDC1080_MENUFACTURE_ID_VALUE&&device_id==HDC1080_DEVICE_ID_VALUE)
{
DEBUG("<-----HDC1080 连接成功!----->\r\n");
}
else
DEBUG("<-----HDC1080 连接失败!----->\r\n");
//设置同时采集温度和湿度 14位分辨率 只要发送一个触发命令温湿度就会连续准换
HDC1080_Write_Register(HDC1080_CONFIG_ID, HDC1080_CONFIG_VALUE);
}
void HDC1080_Write_Register(unsigned char WriteAddr, unsigned int data)
{
uint8_t buf[2] = {0};
buf[0] = (uint8_t)(data>>8);
buf[1] = (uint8_t)(data&0x00ff);
IIC_Start(); //起始信号
IIC_SendoneByte(HDC1080_WRITE_ADDR); //HDC1080设备地址(7位设备地址+W/R)
IIC_Wait_ACK(); //产生应答
IIC_SendoneByte(WriteAddr); //寄存器地址
IIC_Wait_ACK(); //产生应答
IIC_SendoneByte(buf[0]); //写高位数据
IIC_Wait_ACK(); //产生应答
IIC_SendoneByte(buf[1]); //写低位数据
IIC_Stop(); //停止信号
}
//读生产商ID 设备ID
unsigned char HDC1080_Read_Register(unsigned char ReadAddr, \
unsigned char *buf, \
unsigned int length)
{
uint8_t i = 0;
IIC_Start(); //起始信号
IIC_SendoneByte(HDC1080_WRITE_ADDR); //HDC1080设备地址(7位设备地址+W/R)
IIC_Wait_ACK(); //产生应答
IIC_SendoneByte(ReadAddr); //寄存器地址
IIC_Wait_ACK(); //产生应答
IIC_Start(); //起始信号
IIC_SendoneByte(HDC1080_READ_ADDR); //HDC1080设备地址(7位设备地址+W/R) 读
IIC_Wait_ACK(); //产生应答
for(i = 0; i < length-1; i++)
{
buf[i] = IIC_ReadoneByte(); //读数据
IIC_Ack(); //主机产生应答
}
buf[i] = IIC_ReadoneByte(); //读数据
IIC_NAck(); //发送nACK
IIC_Stop(); //停止信号
return 0;
}
void Read_TH_Register(unsigned char *pbuff)
{
uint8_t length = 4, i = 0; //转换数据字节数
IIC_Start(); //起始信号
IIC_SendoneByte(HDC1080_WRITE_ADDR); //HDC1080设备地址(7位设备地址+W/R)
IIC_Wait_ACK(); //产生应答
IIC_SendoneByte(HDC1080_TEMPERATURE_ID); //触发事件地址
IIC_Wait_ACK(); //产生应答
delay_ms(10); //等待转化
IIC_Start(); //起始信号
IIC_SendoneByte(HDC1080_READ_ADDR); //HDC1080设备地址(7位设备地址+W/R) 读
IIC_Wait_ACK(); //产生应答
for(i = 0; i < length-1; i++)
{
pbuff[i] = IIC_ReadoneByte(); //读数据
IIC_Ack(); //主机产生应答
}
pbuff[i] = IIC_ReadoneByte();
IIC_NAck(); //发送nACK
IIC_Stop(); //停止信号
}
void Get_HDC1080_THValue(float *temp, float *humid)
{
uint8_t buf[4] = {0};
//读温湿度寄存器值保存在数组中
Read_TH_Register(buf);
//温度转换
*temp = (float)(buf[0]<<8|buf[1]);
*temp = (*temp/pow(2,16))*165-40;
//湿度转换
*humid = (float)(buf[2]<<8|buf[3]);
*humid = (*humid/pow(2, 16))*100;
}
hdc1080.h
#ifndef _HDC1080_DRIVE_H
#define _HDC1080_DRIVE_H
//寄存器地址
typedef enum{
HDC1080_WRITE_ADDR = 0x80,
HDC1080_READ_ADDR = 0x81,
HDC1080_MENUFACTURE_ID = 0xfe,
HDC1080_DEVICE_ID = 0xff,
HDC1080_CONFIG_ID = 0x02,
HDC1080_TEMPERATURE_ID = 0x00,
HDC1080_HUMIDITY_ID = 0x01,
}REGISTER_ADDR;
//寄存器值
typedef enum{
HDC1080_MENUFACTURE_ID_VALUE = 0x5449,
HDC1080_DEVICE_ID_VALUE = 0x1050,
HDC1080_CONFIG_VALUE = 0x1000, //温度 湿度同时转换 14位分辨率
}REGISTER_VALUE;
void HDC1080_Driver_Init(void);
void HDC1080_Write_Register(unsigned char WriteAddr, unsigned int data);
unsigned char HDC1080_Read_Register(unsigned char ReadAddr, unsigned char *buf, unsigned int length);
void Read_TH_Register(unsigned char *pbuff);
void Get_HDC1080_THValue(float *temp, float *humid);
#endif