TI 狂欢+CC2640R2通过HDC1080测量温度
<div class='showpostmsg'>CC2640R2LP通过HDC1080测量温度void *mainThread(void *arg0)
{
unsigned int i;
uint16_t temperature;
uint8_t txBuffer;
uint8_t rxBuffer;
I2C_Handle i2c;
I2C_Params i2cParams;
I2C_Transaction i2cTransaction;
/* Call driver init functions */
Display_init();
GPIO_init();
I2C_init();
/* Open the HOST display for output */
display = Display_open(Display_Type_UART, NULL);
if (display == NULL) {
while (1);
}
/* Turn on user LED */
GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);
Display_printf(display, 0, 0, "Starting the i2ctmp007 example\n");
/* Create I2C for usage */
I2C_Params_init(&i2cParams);
i2cParams.bitRate = I2C_100kHz;
i2c = I2C_open(Board_I2C0, &i2cParams);
if (i2c == NULL) {
Display_printf(display, 0, 0, "Error Initializing I2C\n");
while (1);
}
else {
Display_printf(display, 0, 0, "I2C Initialized!\n");
}
/* Point to the T ambient register and read its 2 bytes */
i2cTransaction.slaveAddress = HDC1080_SLAVE_ADDR;
i2cTransaction.writeBuf = txBuffer;
i2cTransaction.readBuf = rxBuffer;
//初始化HDC1080
txBuffer = CONFIGURATION;
txBuffer = 0x90; //BIT 15 ~ 8,RST = 1(Software Reset_,MODE = 1(Temperature and Humidity are acquired in sequence, Temperature first.)
txBuffer = 0x00; //BIT 7~ 0;
i2cTransaction.writeCount = 3;
i2cTransaction.readCount = 0;
I2C_transfer(i2c, &i2cTransaction);
//读取DEVICE_ID
txBuffer = DEVICE_ID;
i2cTransaction.writeCount = 1;
i2cTransaction.readCount = 2;
I2C_transfer(i2c, &i2cTransaction);
Display_printf(display, 0, 0, "DeviceId is:0x%x%x\r\n",rxBuffer,rxBuffer);
while (1) {
//发送地址
txBuffer =TEMPERATURE;
i2cTransaction.writeCount = 1;
i2cTransaction.readCount = 0;
I2C_transfer(i2c, &i2cTransaction);
sleep(3);
//读温湿度
i2cTransaction.writeCount = 0;
i2cTransaction.readCount = 4;
if (I2C_transfer(i2c, &i2cTransaction)) {
//Combine the two bytes to make one 16 bit int
uint16_t temp = (((unsigned int)rxBuffer <<8 | rxBuffer));
//Temp(C) = reading/(2^16)*165(C) - 40(C)
float temperature = (double)(temp)/(65536.00)*165.00-40;
//Combine the two bytes to make one 16 bit int
float humidity = (((unsigned int)rxBuffer <<8 | rxBuffer));
//Humidity(%) = reading/(2^16)*100%
humidity =(double)(humidity)/(65536.00)*100;
Display_printf(display, 0, 0, "temperature:%.2f\thumidity:%.2f\r\n", temperature,humidity);
}
else {
Display_printf(display, 0, 0, "I2C Bus fault\n");
}
sleep(1);
}
/* Deinitialized I2C */
I2C_close(i2c);
Display_printf(display, 0, 0, "I2C closed!\n");
return (NULL);
}
此内容由EEWORLD论坛网友littleshrimp原创,如转载或用于商业用途无需征得作者同意和注明出处
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> 我司专注蓝牙,WIFI,zigbee无线射频技术,TI,博通等欧美品牌射频芯片价格优势,模组性能稳定,也可以提供完善技术支持服务,详情可联系 13828711210.QQ 2638434590 版主,看你测得的数据挺好的,为什么我测试HDC1080温度和湿度寄存器值是一样的?主函数:
void main(void)
{
CLK_Config();
GPIO_Init(GPIOD, GPIO_Pin_0, GPIO_Mode_Out_PP_High_Slow);
HDC1080_Init();
enableInterrupts();
while (1)
{
Delay_ms(1000);
GPIO_ToggleBits(GPIOD,GPIO_Pin_0);
testdata = HDC1080_Read_DeviceID();
testdata = HDC1080_Read_ManufacturerID();
testdata = HDC1080_Read_Temper();
testdata = HDC1080_Read_Humidi();
//testdata = HDC1080_Read_Conf();
}
}
下面是我的HDC1080驱动程序:
/*********************************************************************************
@file HDC1080.c
@brief Humidity and Temperature sensor 驱动模块
@details I2C接口
@details Copyright 2016, Testo, Ltd., Shenzhen
@author pli
@date 2016.02.16
@version 00.00
<br><b>编译条件:</b>
<br><b><i>芯片型号..-</i></b> MSP430i2041
<br><b><i>编译器版本-</i></b> IAR for ARM - Ver6.4
<br><b><i>优化级别..-</i></b> High balanced
<br><b>修改记录:</b>
<br><b><i> </i></b>
<br>初版00.00
*******************************************************************************/
//#include <stdint.h>
//#include "io430.h"
//#include "main.h"
#include "HDC1080.h"
#include "I2C.h"
#include "timing_delay.h"
//#include <stm8l15x.h>
/*******************************************************************************
* @briefHDC1080_Write_Buffer
* @paramuint8_t addr is point register
* @paramuint8_t *buffer is the need to write data point
* @paramuint8_t len is the write data length
* @retval void
*******************************************************************************/
void HDC1080_Write_Buffer(uint8_t addr, uint8_t *buffer, uint8_t len)
{
I2C_Start();
I2C_Send_Byte(HDC1080_Device_Adderss);
I2C_Wait_Ack();
I2C_Send_Byte(addr);
I2C_Wait_Ack();
while ( len-- )
{
I2C_Send_Byte(*buffer);
I2C_Wait_Ack();
buffer++;
}
I2C_Stop();
}
/*******************************************************************************
* @briefHDC1080_Read_Buffer between triger and read there is no wait.
* @paramuint8_t addr is point register
* @paramuint8_t *buffer is the need to read data point
* @paramuint8_t len is the read data length
* @retval void
*******************************************************************************/
void HDC1080_Read_Buffer(uint8_t addr, uint8_t *buffer, uint8_t len)
{
uint8_t temp = 0;
I2C_Start();
I2C_Send_Byte(HDC1080_Device_Adderss);
I2C_Wait_Ack();
I2C_Send_Byte(addr);
I2C_Wait_Ack();
I2C_Stop();
while( pin_SCL_READ())
{
Delay_us(1);
temp++;
if ( temp >= 100 )
{
break;
}
}
I2C_Start();
I2C_Send_Byte(HDC1080_Device_Adderss|0x01); //read
I2C_Wait_Ack();
while ( len-- )
{
*buffer = I2C_Recv_Byte();
buffer++;
if ( len == 0 )
I2C_Send_NoAck();
else
I2C_Send_Ack();
}
I2C_Stop();
}
/*******************************************************************************
* @briefHDC1080_Read_MBuffer becareful between triger and read there is a wait.
* @paramuint8_t addr is point register
* @paramuint8_t *buffer is the need to read data point
* @paramuint8_t len is the read data length
* @retval void
*******************************************************************************/
void HDC1080_Read_MBuffer(uint8_t addr, uint8_t *buffer, uint8_t len)
{
uint8_t temp = 0;
I2C_Start();
I2C_Send_Byte(HDC1080_Device_Adderss);
I2C_Wait_Ack();
I2C_Send_Byte(0X00);
I2C_Wait_Ack();
I2C_Stop();
while( pin_SCL_READ())
{
Delay_us(1);
temp++;
if ( temp >= 100 )
{
break;
}
}
//Delay_ms(10); // after triger should wait at least 6.5ms
Delay_ms(10); // after triger should wait at least 6.5ms
I2C_Start();
I2C_Send_Byte(HDC1080_Device_Adderss|0x01); //read
I2C_Wait_Ack();
while ( len-- )
{
*buffer = I2C_Recv_Byte();
buffer++;
if ( len == 0 )
I2C_Send_NoAck();
else
I2C_Send_Ack();
}
I2C_Stop();
}
/*******************************************************************************
* @briefHDC1080_Soft_Reset
* @paramvoid
* @retval void
*******************************************************************************/
void HDC1080_Soft_Reset(void)
{
uint8_t temp;
temp = 0x80;
temp = 0x00;
HDC1080_Write_Buffer(HDC1080_Read_Config, temp, 2);
Delay_ms(20);//there should be waiting for more than 15 ms.
}
/*******************************************************************************
* @briefHDC1080_Setting
* @paramvoid
* @retval void
*******************************************************************************/
void HDC1080_Setting(void)
{
uint16_t tempcom = 0;
uint8_t temp;
tempcom |= 1<<HDC1080_Mode ;//连续读取数据使能
// 温度与温度都为14bit
temp = (uint8_t)(tempcom >> 8);
temp = (uint8_t)tempcom;
HDC1080_Write_Buffer(0x02, temp, 2);
}
/*******************************************************************************
* @briefHDC1080_Init
* @paramvoid
* @retval void
*******************************************************************************/
void HDC1080_Init(void)
{
//uint8_t temp = 0;
I2C_Init();
HDC1080_Soft_Reset();
HDC1080_Setting();
/*
while( pin_SCL_READ() )
{
Delay_us(1);;
temp++;
if ( temp >= 100 )
{
break;
}
}*/
}
/*******************************************************************************
* @briefHDC1080_Read_Temper
* @paramvoid
* @retval int16_t tempr value
*******************************************************************************/
int16_t HDC1080_Read_Temper(void)
{
uint8_t buffer;
HDC1080_Read_MBuffer(0x00, buffer, 2);
return ((buffer<<8)|buffer);
//return (int16_t)(((((buffer<<8)|buffer)/65536.0)*165-40)*100);
}
/*******************************************************************************
* @briefHDC1080_Read_Humidity
* @paramvoid
* @retval int16_t Humidity value
*******************************************************************************/
uint16_t HDC1080_Read_Humidi(void)
{
uint8_t buffer;
HDC1080_Read_MBuffer(HDC1080_Read_Humidity, buffer, 2);
return (buffer<<8)|buffer;
//return (int16_t)((((buffer<<8)|buffer)/65536.0)*10000);
}
/*******************************************************************************
* @briefHDC1080_Read_Configuration
* @paramvoid
* @retval Config value
*******************************************************************************/
uint16_t HDC1080_Read_Conf(void)
{
uint8_t buffer;
HDC1080_Read_Buffer(HDC1080_Read_Config, buffer, 2);
return ((buffer<<8)|buffer);
}
/*******************************************************************************
* @briefHDC1080_Read_ManufacturerID
* @paramvoid
* @retval ManufacturerID
*******************************************************************************/
uint16_t HDC1080_Read_ManufacturerID(void)
{
uint8_t buffer;
HDC1080_Read_Buffer(0xfe, buffer, 2);
return ((buffer<<8)|buffer);
}
/*******************************************************************************
* @briefHDC1080_Read_DeviceID
* @paramvoid
* @retval DeviceID
*******************************************************************************/
uint16_t HDC1080_Read_DeviceID(void)
{
uint8_t buffer;
HDC1080_Read_Buffer(0xff, buffer, 2);
return ((buffer<<8)|buffer);
}
uint16_t testcntA,testcntB;
//--------------------------------------26us
void Delay_us(uint16_t xcnum)
{
for(testcntA=0;testcntA<xcnum;testcntA++)
{
for(testcntB=0;testcntB<40;testcntB++)
{
nop();
}
}
}
//--------------------------------------1ms
void Delay_ms(uint16_t xcnum)
{
for(testcntA=0;testcntA<xcnum;testcntA++)
{
for(testcntB=0;testcntB<1580;testcntB++)
{
nop();
}
}
}
499549794 发表于 2019-1-30 16:48
版主,看你测得的数据挺好的,为什么我测试HDC1080温度和湿度寄存器值是一样的?主函数:
void main(void) ...
用我的代码试没? :pleased:楼主提供的代码真心不错,谢谢分享啊。 txBuffer =TEMPERATURE; i2cTransaction.writeCount = 1; i2cTransaction.readCount = 0; I2C_transfer(i2c, &i2cTransaction);这一段是读温度吗?注释太少了,楼主有没有HAl库版本的? 楼主,直接配置0x02寄存器,然后就开始读数据,可以读出来吗?读了一天都是没有数据出来 希求233 发表于 2019-5-14 16:34
楼主,直接配置0x02寄存器,然后就开始读数据,可以读出来吗?读了一天都是没有数据出来
是用我上传的原码吗?
不是,我用的是HAL库STM32单片机。 现在可以读出0xFE,0xFF了,但是读不出温湿度数据,一直是0 希求233 发表于 2019-5-15 10:06
现在可以读出0xFE,0xFF了,但是读不出温湿度数据,一直是0
HDC1080好像分为同步读取和异步读取
同步读取在发送完指令后要延时一段时间等待转换完成再读取
void hdc1018_read(float *temp,float *hum)
{
uint8_t data;
uint16_t _temp,_hum;
i2c_read_n_byte_waitMs((0x40 << 1),0x00,data,4,1000);
_temp = ((((uint16_t)data) << 8U) & 0xff00) + (((uint16_t)data) & 0x00ff);
_hum = ((((uint16_t)data) << 8U) & 0xff00) + (((uint16_t)data) & 0x00ff);
*temp = (((float)(((uint32_t)_temp) * 165))/65536) - 40;
*hum = ((float)_hum)*100/65536;
} <p>为什么我串口输出怎么很乱,还很的箭头。7[10r[1;1H[2KAdv Set 0 Disabled87[10r[1;1H[2KAdv Set 0 disabled after conn 0</p>
页:
[1]