• AVR Freaks

Hot!PIC2FJ64GB106- I2C interfacing with ADC101C027

Author
shekhashoukath
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2019/06/20 21:49:43
  • Location: 0
  • Status: offline
2019/09/20 02:13:28 (permalink)
0

PIC2FJ64GB106- I2C interfacing with ADC101C027

hai,
i would like to read the data from adc via i2c. PIC24FJ64GB106 is the master . in adc the converted data is stored in conversion register which is 16 bit of that D11 to D2(10 bit) is the converted data bits .  when i tried to data the program hangs at   after writing conversion reg address. if i comment the while() and tries to read data from slave it always returns 0x00ff, irrespective of any register address in adc. can somebody help me to figure out this problem?  i have given the code below;
 
void main(void)
{
unsigned char incoming_data = 0;
while(1)
{
CloseI2C2();

OpenI2C2(0xB000,0x25); //configure I2C2(configure control register and baud rate generator)

conReg = I2C2CON;

while(I2C2CONbits.SEN || I2C2CONbits.PEN || I2C2STATbits.TRSTAT)
{

}
StartI2C2();//Assert a Start condition on SDA2 and SCL2

while(I2C2CONbits.SEN ) //Wait till Start sequence is completed
{

}

//Send the I2C device address byte to the slave
MasterWriteI2C2(0x50); //Write Slave address and set master for transmission

while(I2C2STATbits.TBF)//Wait till transmitted
{

}

wh ile(I2C2STATbits.ACKSTAT)//Wait for an Acknowledge from the slave.
{

}
// delay
for(i=0;i<25000;i++)
{
}
//send conv reg add of adc(slave)
MasterWriteI2C2(0x00);
while(I2C2STATbits.TBF)//Wait till transmitted
{

}

// delay
// for(i=0;i<25000;i++)
// {
// }


while(I2C2STATbits.ACKSTAT)//Wait for an Acknowledge from the slave.
{

}


RestartI2C2();
while(I2C2CONbits.SEN ) //Wait till Start sequence is completed
{

}

// // delay
// for(i=0;i<25000;i++)
// {
// }
//

//Send the I2C device address byte to the slave
MasterWriteI2C2(0x51); //Write Slave address and set master for transmission


while(I2C2STATbits.TBF)//Wait till transmitted
{

}

while(I2C2STATbits.ACKSTAT)//Wait for an Acknowledge from the slave.
{

}
for(i=0;i<25000;i++)
{
}

incoming_data= MasterReadI2C2();


I2C2CONbits.ACKDT = 1; // Tell slave the Master does not want any more data
I2C2CONbits.ACKEN = 1; // Initiate Acknowledge sequence on SDA and SCL pins, and transmit ACKDT data bit.
while( I2C2CONbits.ACKEN); // Wait for ACKEN to go low indicating transmission is complete

StopI2C2();
}
}
 
void CloseI2C2(void)
{
/* clear the I2CEN bit */
I2C2CONbits.I2CEN = 0;
/* clear the SI2C3 & MI2C3 Interrupt enable bits */
IEC3bits.SI2C2IE = 0;
IEC3bits.MI2C2IE = 0;
/* clear the SI2C2 & MI2C2 Interrupt flag bits */
IFS3bits.SI2C2IF = 0;
IFS3bits.MI2C2IF = 0;
}



void OpenI2C2(unsigned int config1,unsigned int config2)
{
I2C2BRG = config2;
I2C2CON = config1;
}



void StartI2C2(void)
{
I2C2CONbits.SEN = 1; /* initiate Start on SDA and SCL pins */
}


char MasterWriteI2C2(unsigned char data_out)
{
I2C2TRN = data_out;
if(I2C2STATbits.IWCOL) /* If write collision occurs,return -1 */

return -1;
else
{
return 0;
}
}
 
 


void RestartI2C2(void)
{
I2C2CONbits.RSEN = 1; /* initiate restart on SDA and SCL pins */
}
 
 

unsigned char MasterReadI2C2(void)
{
I2C2CONbits.RCEN = 1; // enable receive mode
while(I2C2CONbits.RCEN);
I2C2STATbits.I2COV = 0; //receive overflow flag bit,0- no overflow
return(I2C2RCV); // return the value of receive register
}


void StopI2C2(void)
{
I2C2CONbits.PEN = 1; /* initiate Stop on SDA and SCL pins */
}
 
#1

5 Replies Related Threads

    ric
    Super Member
    • Total Posts : 23833
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC2FJ64GB106- I2C interfacing with ADC101C027 2019/09/22 17:10:17 (permalink)
    5 (1)
    Like most I2C first-timers, you have misunderstood the operation of the ACK bit.
    You never wait for that bit. You wait until the data transfer is complete, then you read that bit ONCE.
    If it's low, the data was accepted. If it's high, the data was not accepted.
    Most often that happens when sending the slave address, because either:
    [1] The slave is not there, or
    [2] You used the wrong address.
     
    In your case, it's #2 because you are using the wrong slave address.
    The ADC datasheet tells you the 7 bit address, but you have to shift that left one bit and put the R/W flag in bit 0.
    So, to write to the chip, the address you must send is 0xA0 (not 0x50), and to read from it use 0xA1 (not 0x51).
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    shekhashoukath
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/06/20 21:49:43
    • Location: 0
    • Status: offline
    Re: PIC2FJ64GB106- I2C interfacing with ADC101C027 2019/09/25 06:30:30 (permalink)
    0
    Thank you so much, i didn't notice that, by doing so i got the ACK from slave. 
    #3
    shekhashoukath
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/06/20 21:49:43
    • Location: 0
    • Status: offline
    Re: PIC2FJ64GB106- I2C interfacing with ADC101C027 2019/09/26 04:54:42 (permalink)
    0
    hai,
    as i mentioned earlier i want read the value from adc via i2c using pic24fj64gb106. for that i did as following, first set i2c start bit, then send slave addr(A0 with r/w/=0), then send the conv reg add(00 with write bit set),then restart bit set, again send slave address with read bit set and then tried to read from slave.the conversion reg of adc-adc101c027 is 16 bit[d15:d0], the converted result is in d11 to d2 bits, d15 is alert flag, d14:d12 and d1:d0 is reserved. my i2c rcv reg is 8 bit long. if i call i2c read function , the function will return the value of i2c rcv reg. for reading 2 bytes from adc as per its datasheet pic has to send an ack(ackdt of control reg =0), then i again called the read function, then i set ackdt=1, then stopped the communication. but both the times i called the read function its returning the same value . the value in i2c rcv reg is not changing. i have given below the code for read , please help to figure out the issue.
     
     
    msb = MasterReadI2C2();
    AckI2C2();
    lsb = MasterReadI2C2();
    NotAckI2C2();
    StopI2C2();
     
     

    unsigned char MasterReadI2C2(void)
    {
    I2C2CONbits.RCEN = 1; // enable receive mode
    while(I2C2CONbits.RCEN);
    I2C2STATbits.I2COV = 0; //receive overflow flag bit,0- no overflow
    return(I2C2RCV); // return the value of receive register
    }
     
     
     
    void NotAckI2C2(void)
    {
    I2C2CONbits.ACKDT = 1;
    I2C2CONbits.ACKEN = 1;
    }
     
     
     
    void AckI2C2(void)
    {
    I2C2CONbits.ACKDT = 0;
    I2C2CONbits.ACKEN = 1;
    }
     
    void StopI2C2(void)
    {
    I2C2CONbits.PEN = 1; /* initiate Stop on SDA and SCL pins */
    }
    #4
    ric
    Super Member
    • Total Posts : 23833
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC2FJ64GB106- I2C interfacing with ADC101C027 2019/09/26 05:32:39 (permalink)
    0
    You never mention WHAT value you are getting for the two reads.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    shekhashoukath
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/06/20 21:49:43
    • Location: 0
    • Status: offline
    Re: PIC2FJ64GB106- I2C interfacing with ADC101C027 2019/09/26 20:35:14 (permalink)
    0
    Hai,
    when input to adc is 2.56V value in i2crcv reg is 0B.
    when input to adc is 2.17V value in i2crcv reg is 0A.
    when input to adc is 2.05V value in i2crcv reg is 09.
    when input to adc is 1.95V value in i2crcv reg is 08.
    ADC gets its input from a battery source , when battery is not connected , i2c read returns 0.
    #6
    Jump to:
    © 2019 APG vNext Commercial Version 4.5