• AVR Freaks

PIC2FJ64GB106- I2C interface with TSYS02

Author
shekhashoukath
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2019/06/20 21:49:43
  • Location: 0
  • Status: offline
2019/12/23 05:47:28 (permalink)
0

PIC2FJ64GB106- I2C interface with TSYS02

hai,
i have to read data from a temperature sensor via I2C , i am using pic24fj64gb106. scl and sda lines are pulled up to 3.3v through 10k resistors. first i send slave addr 0x80, then 0xFE, then restarted i2c, again slave addr with read bit set, 0x81. but when i debug at ackstat  after sending 0xFE , the code hangs at that point. when i program again without turnig off the power it the issue disappear. can someone help to solve this issue.
 
below is my i2c code:
while(1)
{
CloseI2C2();

for(i=0;i<4;i++)
{
}
for(i=0;i<25000;i++)
{
}
// OpenI2C2(0xB000,0x25); //configure I2C2(configure control register and baud rate generator)
OpenI2C2(0xB200,0x99); //configure I2C2(configure control register and baud rate generator)


for(i=0;i<4;i++)
{
}
//conReg = I2C2CON;
conReg = I2C3CON;

//while(I2C2CONbits.SEN || I2C2CONbits.PEN || I2C2STATbits.TRSTAT)
while(I2C3CONbits.SEN || I2C3CONbits.PEN || I2C3STATbits.TRSTAT)
{
}

//IdleI2C3();


// for(i=0;i<100;i++)
// {
// }
while(I2C3STATbits.S == 1)
{
}
while(I2C3STATbits.P == 1)
{
}
StartI2C2();//Assert a Start condition on SDA2 and SCL2
for(i=0;i<4;i++)
{
}

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

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

//Send the I2C device address byte to the slave
MasterWriteI2C2(0x80); //Write Slave address and set master for transmission
for(i=0;i<4;i++)
{
}

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

}
for(i=0;i<4;i++)
{
}
// while(I2C2STATbits.ACKSTAT)//Wait for an Acknowledge from the slave.
while(I2C3STATbits.ACKSTAT)//Wait for an Acknowledge from the slave.
{

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

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

}

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

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

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

/******************************************************************************/
RestartI2C3();

// StartI2C3();//Assert a Start condition on SDA2 and SCL2
for(i=0;i<25000;i++)
{
}
//while(I2C2CONbits.SEN ) //Wait till Start sequence is completed
while(I2C3CONbits.SEN ) //Wait till Start sequence is completed
{

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

//Send the I2C device address byte to the slave
MasterWriteI2C2(0x81); //Write Slave address and set master for transmission
for(i=0;i<20;i++)
{
}

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

}
for(i=0;i<4;i++)
{
}
//while(I2C2STATbits.ACKSTAT)//Wait for an Acknowledge from the slave.
while(I2C3STATbits.ACKSTAT)//Wait for an Acknowledge from the slave.
{

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

msb = MasterReadI2C3();
msbOgl = msb;
msb = msb << 8;
AckI2C3();
for(i=0;i<25000;i++)
{
}
lsb = MasterReadI2C3();
AckI2C3();
// x= MasterReadI2C2();
for(i=0;i<25000;i++)
{
}
chksum = MasterReadI2C3();
NotAckI2C3();
//msb= msb+lsb;
StopI2C3();
}
#1

4 Replies Related Threads

    ric
    Super Member
    • Total Posts : 25101
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC2FJ64GB106- I2C interface with TSYS02 2019/12/23 15:10:50 (permalink)
    0
    This:
    while(I2C3STATbits.ACKSTAT)//Wait for an Acknowledge from the slave.

    That is NOT how you should use ACKSTAT.
    Wait for TRSTAT to clear (not TBF), then read the ACKSTAT bit once.
    If it is low, you received an ACK.
    If it is high, you received a NAK, and no amount of waiting will change it to an ACK!
     
    Refer to "Figure 5-3: Master Transmission Timing Diagram" in the I2C FRM (DS70000195) for the timing of a transaction.
     

    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
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3510
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: PIC2FJ64GB106- I2C interface with TSYS02 2019/12/23 16:13:43 (permalink)
    0
    1. The compiler will skip code that would generate nothing.
    2. What made you think that delays needed to be scattered everywhere?
    post edited by Gort2015 - 2019/12/23 16:15:52

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #3
    ric
    Super Member
    • Total Posts : 25101
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC2FJ64GB106- I2C interface with TSYS02 2019/12/23 17:31:55 (permalink)
    0
    Gort2015
    1. The compiler will skip code that would generate nothing.

    In other words, if the "i" variable has not been qualified as "volatile", then all those for() loops scattered through your code will be thrown away by the compiler.
    Correctly written code doesn't needs those delays anyway, the peripheral's status flags tell you when you need to wait.
     

    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!
    #4
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3510
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: PIC2FJ64GB106- I2C interface with TSYS02 2019/12/23 18:05:25 (permalink)
    0
    Before writing I2CxTRN make sure that i2c is not busy by masking all the status flags: $1F
     
     
       mov     #0x1f,w0
       and     I2C1CON,wreg
       bra     nz,$-4


    Exit when I2C1CON is zero.

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #5
    Jump to:
    © 2020 APG vNext Commercial Version 4.5