#include "hhh.h"
#include "msp430x14x.h"
#define CPU_F ((double)8000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
/*---------------------------------------------------------------------------------------------------------*/
/* Global variables */
/*---------------------------------------------------------------------------------------------------------*/
unsigned long temp,humi;
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 ;
delay_us(1);
P3OUT|= BIT3 ;
delay_us(1);
P3OUT &=~(BIT1);
}
void MyApp_I2cReStart( void )
{
P3OUT &=~(BIT3);
delay_us(1);
I2C_SDA_AS_OUT( );
P3OUT|= BIT1;
delay_us(1);
P3OUT|= BIT3;
delay_us(1);
P3OUT &=~(BIT1);
}
void MyApp_I2cStop( void )
{
P3OUT &=~(BIT3);
delay_us(1);
I2C_SDA_AS_OUT( );
P3OUT &=~(BIT1);
delay_us(1);
P3OUT|= BIT3 ;
delay_us(5);
P3OUT|= BIT1;
}
void MyApp_I2cSendAck( void )
{
P3OUT &=~(BIT3);
delay_us(1);
I2C_SDA_AS_OUT( );
P3OUT &=~(BIT1);
delay_us(1);
P3OUT|= BIT3 ;
delay_us(5);
}
void MyApp_I2cSendNAck( void )
{
P3OUT &=~(BIT3);
delay_us(1);
I2C_SDA_AS_OUT( );
P3OUT|= BIT1;
delay_us(1);
P3OUT|= BIT3;
delay_us(5);
}
void MyApp_I2cSendByte( unsigned char bData )
{
uint8_t i;
uint8_t BitMask = 0x80;
// 20Us/Bit
for (i=0; i<8; i++)
{
delay_us(1);
P3OUT &=~(BIT3);
delay_us(1);
I2C_SDA_AS_OUT( );
if (bData & BitMask)
{
P3OUT|= BIT1 ;
}
else
{
P3OUT &=~(BIT1) ;
}
BitMask >>= 1;
delay_us(1);
P3OUT|= BIT3 ;
delay_us(1);
}
}
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);
delay_us(5);
P3OUT|= BIT3;
delay_us(1);
if (I2C_SDA_PORT)
{
ByteData |= BitMask;
}
delay_us(1);
BitMask >>= 1;
}
return ByteData;
}
unsigned char MyApp_I2cCheckAck( void )
{
P3OUT &=~(BIT3);
I2C_SDA_AS_IN( );
delay_us(5);
P3OUT|= BIT3 ;
delay_us(1);
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);
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 Convert_HDC1080_TempHumidity(unsigned char *datax)
{
//100 times of the actural value
temp=(unsigned long)(datax[0]<<8);
temp+=datax[1];
temp=(temp*165000)>>16;
temp-=40000;
humi=(unsigned long)(datax[2]<<8);
humi+=datax[3];
humi=(humi*1000)>>16;
}
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];
I2C_Read_HDC1080(HDC1080_I2C_ADDR,Device_ID,datax);
data2=(unsigned int)(datax[0]<<8);
data2+=datax[1];
data1=Configuration_1;
I2C_Write_HDC1080(HDC1080_I2C_ADDR, Configuration, data1);
delay_ms(40);
I2C_Read_HDC1080(HDC1080_I2C_ADDR,Configuration,datax);
data1=(unsigned int)(datax[0]<<8);
data1+=datax[1];
return 1;
}
/* Main Function*/
int main1(void)
{
unsigned char datax[4];
MyApp_InitI2c();
HDC1080_Init();
I2C_Read_HDC1080(HDC1080_I2C_ADDR,Temperature,datax);
I2C_Read_HDC1080(HDC1080_I2C_ADDR,Humidity,datax);
Convert_HDC1080_TempHumidity(datax);
return 0;
}