Part Number: CC2530
Tool/software: TI C/C++ Compiler
Hello,
I want to interface CC2530 with htu21D which is based on I2C,i have implement it using software I2C code becoz cc2530 don't have I2c pins.
my problem is I always received 0xFF(for 2 days......too much time waste and so many different way tried) when I want to read "user register" of htu21d,same thing i have check with ardino i received 0x02 which is correct byte.
my configuration pin for i2c is P0.6 for CLK and P0.7 for SDA. below my code is given,if anything is wrong then please help me.I have checked my hardware connection its look fine.
if anyone have I2c code of cc2530 with htu21d then please send me.(email:hitesh.mathukiya@gmail.com)
code is here.......
void temperature_data(){ ///the main function for read user register
// HTU21D_I2C_ADDRESS 0x40
//#define TRIGGER_TEMP_MEASURE 0xE3
//#define TRIGGER_HUMD_MEASURE 0xE5
char rx[5]; char temp_buff[5]; char i;
I2C_Start();
I2C_Write(0x80); //?I2C???? ////write
I2C_WaitAck();
I2C_Write(0xFC); //?I2C???? //for software reset
I2C_WaitAck();
I2C_Stop();
milliSecondDelay(100);
I2C_Start();
I2C_Write(0x80); //?I2C???? //0x80 to write
I2C_WaitAck();
I2C_Write(0xE7); //?I2C???? //0xE7 for user read adress
I2C_WaitAck();
I2C_Write(0x81); //?I2C???? //to read 0x81
I2C_WaitAck();
rx[0] = I2C_Read(1); ///here it should be read 0x02 but always read 0xFF
I2C_Stop();
milliSecondDelay(1000);
buff[0]=rx[0];
HalUARTWrite(HAL_UART_PORT_0, buff,1); //send data to uart ////always getting 0xFF on UART
}
void SPIKEHalKeyInit( void )
{
P0SEL &= 0x00; //Set all pins as input inorder to power saving
P0DIR &= 0x00;
P1SEL &= 0x00;
P1DIR &= 0x00;
P2SEL &= 0xF8;
P2DIR &= 0xF0;
/*All the ports are pulled down*/
//P2INP |= (0xE0);
/*LED pin tri-state mode*/
//P2INP |= BV(0);
P0SEL &= ~BV(1);
P0DIR |= BV(1);
LED = 0;
P1INP |= BV(0);
P1SEL &= ~BV(0);
P1DIR |= BV(0);
P1_0 = 1;
milliSecondDelay(100);
SPIKE_UART_Init();
// I2C_Init();
I2C_Init(); ///hitesh
}
void I2C_Init(void)
{
//---------- hitesh--------------------------------------
P0SEL &= ~BV(6); ///SCL_PIN
P0DIR |= BV(6);
P0SEL &= ~BV(7); //SDA_PIN
P0DIR |= BV(7);
//------------------------------------------------------------
SDA_PIN=1;
SCL_PIN=1;
}
void I2C_Stop(void)
{
SCL_PIN = 0; // Pull SCL low
// microSecondDelay(4);
SDA_PIN = 0; // Pull SDA low
microSecondDelay(5);
SCL_PIN = 1; // Pull SCL High
microSecondDelay(5);
SDA_PIN = 1; // Now Pull SDA High, to generate the Stop Condition
microSecondDelay(5);
}
void I2C_Write(uint8 v_i2cData_u8)
{
uint8 i;
/* for(i=0;i<8;i++) // loop 8 times to send 1-byte of data
{
SDA_PIN = v_i2cData_u8 & 0x80; // Send Bit by Bit on SDA line
i2c_Clock(); // Generate Clock at SCL
v_i2cData_u8 = v_i2cData_u8<<1;// Bring the next bit to be transmitted to MSB position
}
i2c_Clock();*/
//SCL_PIN=0;
for(i=0; i<8; i++){
SCL_PIN=0;
microSecondDelay(5);
if(v_i2cData_u8&0x80)
{
SDA_PIN=1;
}
else
{
SDA_PIN=0;
}
v_i2cData_u8 = v_i2cData_u8 << 1;
microSecondDelay(5);
SCL_PIN=1;
microSecondDelay(5);
}
SCL_PIN=0;
microSecondDelay(10);
}
void I2C_Start(void)
{
SCL_PIN = 1;
microSecondDelay(5);
SDA_PIN = 1; // Pull SCL low
microSecondDelay(5);
SDA_PIN = 0; //Pull SCL high
microSecondDelay(5);
SCL_PIN = 0; //Now Pull SDA LOW, to generate the Start Condition
microSecondDelay(5);
}
uint8 I2C_Read(uint8 v_ackOption_u8)
{
uint8 i, inByte;
/* SDA_PIN =1; //Make SDA as I/P
for(i=0;i<8;i++) // loop 8times read 1-byte of data
{
microSecondDelay(10);
SCL_PIN = 1; // Pull SCL High
microSecondDelay(10);
v_i2cData_u8 = v_i2cData_u8<<1; //v_i2cData_u8 is Shifted each time and
v_i2cData_u8 = v_i2cData_u8 | SDA_PIN; //ORed with the received bit to pack into byte
SCL_PIN = 0; // Clear SCL to complete the Clock
}
if(v_ackOption_u8==1) /*Send the Ack/NoAck depending on the user option*/
// {
/* i2c_Ack();
}
else
{
i2c_NoAck();
}
return v_i2cData_u8; // Finally return the received Byte* */
//unsigned char inByte, n;
//SDA_PIN =1; \
SCL_PIN =0;
microSecondDelay(5);
SDA_PIN =1;
P0SEL &= ~BV(7); //SDA_PIN as input
P0DIR &= ~ BV(7);
for (i=0; i<8; i++){
SCL_PIN=1;
microSecondDelay(5);
// inByte <<= 1;
if(SDA_PIN)
inByte = (inByte << 1) | 0x01; // msbit first
else
inByte = inByte << 1;
SCL_PIN=0;
microSecondDelay(5);
}
P0SEL &= ~BV(7); //SDA_PIN output
P0DIR |= BV(7);
return inByte;
}
static void i2c_Ack(void)
{
/*SDA_PIN = 0; //Pull SDA low to indicate Positive ACK
i2c_Clock(); //Generate the Clock
SDA_PIN = 1; // Pull SDA back to High(IDLE state) */
SDA_PIN=0;
SCL_PIN=1;
SCL_PIN=0;
SDA_PIN=1;
SCL_PIN=1;
}
static void i2c_NoAck(void)
{
/* SDA_PIN = 1; //Pull SDA high to indicate Negative/NO ACK
i2c_Clock(); // Generate the Clock
SCL_PIN = 1; // Set SCL */
SDA_PIN=1;
SCL_PIN=1;
SCL_PIN=0;
}
uint8 I2C_WaitAck(void)
{
uint8 ack;
SDA_PIN=1;
microSecondDelay(5);
SCL_PIN=1; //?????????
microSecondDelay(5);
if(SDA_PIN)
ack = 1;
else
ack = 0;
SCL_PIN=0;
microSecondDelay(5);
return ack;
}