#define HDC1080_I2C_ADDR 0x80
#define Temperature 0x00
#define Humidity 0x01
#define Configura
tion 0x02
#define Manufacturer_ID 0xFE
#define Device_ID 0xFF
#define Manufacturer_ID_value 0x5449
#define Device_ID_value 0x1050
#define Configuration_1 0x1000
#define Configuration_2 0x3000
#define I2C_SCL_PORT BIT3
#define I2C_SDA_PORT BIT1
#define I2C_SDA_AS_IN( ) P3DIR &=~(BIT1)
#define I2C_SDA_AS_OUT( ) P3DIR|= BIT1
typedef unsigned char uint8_t;
extern int temp_value;
extern int humi_value;
#include <stdio.h>
#define PLLCON_SETTING CLK_PLLCON_50MHz_HXT
#define PLL_CLOCK 50000000
int main1(void);
#include "hhh.h"
#include "msp430x14x.h"
/*---------------------------------------------------------------------------------------------------------*/
/* Global variables */
/*---------------------------------------------------------------------------------------------------------*/
void DelayTime10us(unsigned char n)
{
unsigned char i;
while(n--)
for(i=0;i<10;i++);
}
void MyApp_InitI2c( void )
{
/* Configure P1.2 and P4.1 to default Quasi-bidirectional mode */
P3DIR|= BIT1;
P3DIR|= BIT3;
P3OUT|= BIT3;
P3OUT|= BIT1;
}
void MyApp_I2cStart( void )
{
I2C_SDA_AS_OUT( );
P3OUT|= BIT1 ;
DelayTime10us(2);
P3OUT|= BIT3 ;
DelayTime10us(2);
P3OUT &=~(BIT1);
}
void MyApp_I2cReStart( void )
{
P3OUT &=~(BIT3);
DelayTime10us(2);
I2C_SDA_AS_OUT( );
P3OUT|= BIT1;
DelayTime10us(2);
P3OUT|= BIT3;
DelayTime10us(2);
P3OUT &=~(BIT1);
}
void MyApp_I2cStop( void )
{
P3OUT &=~(BIT3);
DelayTime10us(2);
I2C_SDA_AS_OUT( );
P3OUT &=~(BIT1);
DelayTime10us(2);
P3OUT|= BIT3 ;
DelayTime10us(2);
P3OUT|= BIT1;
}
void MyApp_I2cSendAck( void )
{
P3OUT &=~(BIT3);
DelayTime10us(2);
I2C_SDA_AS_OUT( );
P3OUT &=~(BIT1);
DelayTime10us(2);
P3OUT|= BIT3 ;
DelayTime10us(2);
}
void MyApp_I2cSendNAck( void )
{
P3OUT &=~(BIT3);
DelayTime10us(2);
I2C_SDA_AS_OUT( );
P3OUT|= BIT1;
DelayTime10us(2);
P3OUT|= BIT3;
DelayTime10us(2);
DelayTime10us(2);
}
void MyApp_I2cSendByte( unsigned char bData )
{
uint8_t i;
uint8_t BitMask = 0x80;
// 20Us/Bit
for (i=0; i<8; i++)
{
DelayTime10us(2);
P3OUT &=~(BIT3);
DelayTime10us(2);
I2C_SDA_AS_OUT( );
if (bData & BitMask)
{
P3OUT|= BIT1 ;
}
else
{
P3OUT &=~(BIT1) ;
}
BitMask >>= 1;
DelayTime10us(2);
P3OUT|= BIT3 ;
DelayTime10us(2);
}
}
unsigned char MyApp_I2cReceiveByte( void )
{
uint8_t i;
uint8_t ByteData = 0;
uint8_t BitMask = 0x80;
P3OUT &=~(BIT3);
I2C_SDA_AS_IN( );
// 20Us/Bit
for (i=0; i<8; i++)
{
P3OUT &=~(BIT3);
DelayTime10us(2);
DelayTime10us(2);
P3OUT|= BIT3;
DelayTime10us(250);
if (I2C_SDA_PORT)
{
ByteData |= BitMask;
}
DelayTime10us(2);
BitMask >>= 1;
}
return ByteData;
}
unsigned char MyApp_I2cCheckAck( void )
{
P3OUT &=~(BIT3);
I2C_SDA_AS_IN( );
DelayTime10us(2);
DelayTime10us(2);
P3OUT|= BIT3 ;
DelayTime10us(2);
if(I2C_SDA_PORT!=0)
return 1;
else
return 0;
}
void I2C_Write_HDC1080(unsigned char dev_addr, unsigned char register_addr, unsigned int value)
{
unsigned char i;
unsigned char datax[2];
datax[0] = (uint8_t)((value& 0xFF00) >> 8);
datax[1] = (uint8_t)(value & 0x00FF);
for(i=20;i>0;i--)
{
MyApp_I2cStart( );
MyApp_I2cSendByte( dev_addr); // HDC1080 Addr + W
if(MyApp_I2cCheckAck( ))
{
MyApp_I2cStop();
continue;
}
MyApp_I2cSendByte( register_addr);
if(MyApp_I2cCheckAck( ))
{
MyApp_I2cStop();
continue;
}
MyApp_I2cSendByte( datax[0] );
if(MyApp_I2cCheckAck( ))
{
MyApp_I2cStop();
continue;
}
MyApp_I2cSendByte( datax[1] );
if(MyApp_I2cCheckAck( ))
{
MyApp_I2cStop();
continue;
}
MyApp_I2cStop( );
return;
}
}
void I2C_Read_HDC1080(unsigned char dev_addr, unsigned char register_addr, unsigned char *datax)
{
unsigned char i;
datax[0]=0;
datax[1]=0;
for(i=20;i>0;i--)
{
MyApp_I2cStart( );
MyApp_I2cSendByte( dev_addr); // HDC1080 Addr + W
if(MyApp_I2cCheckAck( ))
{
MyApp_I2cStop();
continue;
}
MyApp_I2cSendByte( register_addr);
if(MyApp_I2cCheckAck( ))
{
MyApp_I2cStop();
continue;
}
MyApp_I2cReStart( );
MyApp_I2cSendByte( dev_addr|0x01); // HDC1080 Addr + R
if(MyApp_I2cCheckAck( ))
{
MyApp_I2cStop();
continue;
}
datax[0] = MyApp_I2cReceiveByte( );
MyApp_I2cSendAck( );
datax[1] = MyApp_I2cReceiveByte( );
MyApp_I2cSendNAck( );
MyApp_I2cStop( );
return;
}
}
void I2C_Read_HDC1080_TempHumidity(unsigned char dev_addr, unsigned char register_addr, unsigned char *datax)
{
datax[0]=0;
datax[1]=0;
datax[2]=0;
datax[3]=0;
MyApp_I2cStart( );
MyApp_I2cSendByte( dev_addr); // HDC1080 Addr + W
if(MyApp_I2cCheckAck( ))
{
MyApp_I2cStop();
return;
}
MyApp_I2cSendByte( register_addr);
if(MyApp_I2cCheckAck( ))
{
MyApp_I2cStop();
return;
}
DelayTime10us(2);
DelayTime10us(2);
DelayTime10us(2);
DelayTime10us(2);// It's better to be set over 30ms.
MyApp_I2cReStart( );
MyApp_I2cSendByte( dev_addr|0x01); // HDC1080 Addr + R
if(MyApp_I2cCheckAck( ))
{
MyApp_I2cStop();
return;
}
datax[0] = MyApp_I2cReceiveByte( );
MyApp_I2cSendAck( );
datax[1] = MyApp_I2cReceiveByte( );
MyApp_I2cSendAck( );
datax[2] = MyApp_I2cReceiveByte( );
MyApp_I2cSendAck( );
datax[3] = MyApp_I2cReceiveByte( );
MyApp_I2cSendNAck( );
MyApp_I2cStop( );
}
void Convert_HDC1080_TempHumidity(unsigned char *datax)
{
//100 times of the actural value
unsigned long temp,humidity;
temp=(unsigned long)(datax[0]<<8);
temp+=datax[1];
temp=(temp*16500)>>16;
temp-=4000;
humidity=(unsigned long)(datax[2]<<8);
humidity+=datax[3];
humidity=(humidity*100)>>16;
}
//HDC1080_Init subroutine
//return : 0 - HDC1080 initiate unstable or you should check with your i2c subroutine. 1-HDC1080 initiate ok.
unsigned char HDC1080_Init(void)
{
unsigned int data1,data2;
unsigned char datax[2];
I2C_Read_HDC1080(HDC1080_I2C_ADDR,Manufacturer_ID,datax);
data1=(unsigned int)(datax[0]<<8);
data1+=datax[1];
printf("HDC1080 Manufacturer_ID = 0x%x
", data1);
I2C_Read_HDC1080(HDC1080_I2C_ADDR,Device_ID,datax);
data2=(unsigned int)(datax[0]<<8);
data2+=datax[1];
printf("HDC1080 Device_ID = 0x%x
", data2);
if(data1!=Manufacturer_ID_value||data2!=Device_ID_value)
{
printf("I2C_Read_HDC1080 Manufacturer_ID or Device_ID_value error
");
return 0;
}
data1=Configuration_1;
I2C_Write_HDC1080(HDC1080_I2C_ADDR, Configuration, data1);
//wait at least 20ms to readout temp&humidity value
printf("I2C_Write_HDC1080 register 0x02 = 0x%x
", data1);
I2C_Read_HDC1080(HDC1080_I2C_ADDR,Configuration,datax);
data1=(unsigned int)(datax[0]<<8);
data1+=datax[1];
printf("I2C_Read_HDC1080 register 0x02 = 0x%x
", data1);
//SysTickDelay_ms(2000);
return 1;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Main Function */
/*---------------------------------------------------------------------------------------------------------*/
int main1(void)
{
unsigned char datax[4];
/* Init System, peripheral clock and multi-function I/O */
printf("HDC1080 Test Ok
");
printf("I2C0_SDA(P3.1), I2C0_SCL(P3.3) - I2C Master
");
/* Init I2C0 */
MyApp_InitI2c();
HDC1080_Init();
printf("main while(1)
");
while(1)
{
//I2C_Write_HDC1080(HDC1080_I2C_ADDR, Configuration, 0x1000);
I2C_Read_HDC1080_TempHumidity(HDC1080_I2C_ADDR,Temperature,datax);
Convert_HDC1080_TempHumidity(datax);
DelayTime10us(2);
DelayTime10us(2);
DelayTime10us(2);
DelayTime10us(2);// this delay can be cancelled.
}
}
看资料,温度计算应该是
temp=(temp*165)>>16;
temp-=40;
另外在你程序初始化部分有读传感器的信息,也可以跟资料对比这些信息,看与传感器通讯是否正确
一周热门 更多>