0
\$\begingroup\$

Here I've issue related to DS1621 digital thermometer interfacing with PIC18F26J50 controller.

Below you can see my code and this code I build in MPLAB X v.1.4 IDE with C18 compiler:

 unsigned char i2c_read(void)
 {
    char buf[20];
    unsigned char i,j;
    j=0;
    i=DATA;
    for(i=0;i<8;i++)
    {
        j<<=1;
        CLOCK = HIGH;
        j|=DATA;
        delay_ms(100);
        CLOCK = LOW;
    }
    //////////////////////
    **sprintf(buf,"%d",j);    // 255
    transmit_str1(buf); //UART transmit string**
    /////////////////////
    return buf;//return j;
  }


main()
{      
while(1)
{
    i2c_start();
    i2c_write(0x90);
    i2c_write(0xAC);
    i2c_write(0x02);
    i2c_stop();

    i2c_start();
    i2c_write(0x90);
    i2c_write(0xEE);
    i2c_stop();

    i2c_start();
    i2c_write(0x90);
    i2c_write(0xAA);
    i2c_start();          
    i2c_write(0x91);     
    ch = i2c_read();
    i2c_stop();
 ///////////////////
     temperature = 0;
     convert(ch); //convert binary to Decimal

     if(flag==FALSE_)
     {
         flag = TRUE_;
         tmp=temperature;
         //itoa(tmp,buffer);
         sprintf(tmp,"%s",temperature);
     }
     else
     {
         if(tmp!=temperature)
         {
             tmp=temperature;
             sprintf(str,"%d%s",temperature,"Centigrade");
         }
     }
   } 
 }

Here I'm interfacing DS1621 Thermometer with PIC18F26J50.

and I'm doing I2C communication between them. and I see this result on UART, but I get "255" data.

This "255" data string I got from "i2c_read(void)" function.

Here I'm attaching a screenshot of Oscilloscope from Proteus software. (YELLOW: SDA | BLUE: SCL).

Can you please tell me how to get reading from DS1621?

enter image description here

\$\endgroup\$
  • 1
    \$\begingroup\$ Your SCL pulses are too short. You need two delays. \$\endgroup\$ – Janka Apr 21 '17 at 13:12
  • \$\begingroup\$ If I'm put delay then those delay affected in SDA not in SCL. \$\endgroup\$ – Pratik Apr 21 '17 at 13:29
  • \$\begingroup\$ Unroll the loop, then you see what you have: SCL->high, Data sample, delay, SCL->low, SCL->high, Data sample, delay, SCL->low, … You need a delay between SCL->low and SCL->high, too, or the chip may not be able to put its data on the bus. \$\endgroup\$ – Janka Apr 21 '17 at 13:42
  • \$\begingroup\$ I put a delay in between SCL->high and SCL->LOW but still I get same waveform of SCL. \$\endgroup\$ – Pratik Apr 21 '17 at 14:00
  • \$\begingroup\$ Put a delay after the CLOCK = LOW. \$\endgroup\$ – Tim Mottram Apr 21 '17 at 14:47
1
\$\begingroup\$

From page 15 of the datasheet the LOW period of the clock signal must be 1.3uS minimum in fast mode and 4.7 uS in standard mode. Without a delay after your

CLOCK = LOW; 

The clock line goes high again after a few cycles of your sys clock. You can see this in your blue trace. Put a delay longer than 10 us in after this instruction, also you might want to shorten the delay after the

CLOCK = HIGH; 

command or you're going to be waiting a second for 10 bits of data...

\$\endgroup\$
0
\$\begingroup\$

I got the solution from this link:

https://electronicsproject.org/temperature-indicator-using-at89c52/

This is the AT89C52 controller but I do some little changes in code according to the PIC18 controller pins.

and don't see I2C data on UART always you get "255" only.

If you want to see I2C data use LCD or Oscilloscope or I2C debugger in proteus.

That's it!

Thank you stack exchange community for your great response.

\$\endgroup\$
0
\$\begingroup\$

Judging by your waveform data, your clock line isn't staying low for long enough. Your data line doesn't seem very stable either. Your clock line has to go low for a certain period of time as others have suggested so you must create delays to take in to account the following criteria from the datasheet:

Clock high time (min) Clock low time (min) SDA/SCL rise time (max) SDA/SCL fall time (max)

This is likely the reason you are getting the wrong value at the UART output which is showing a constant 1 because the data is not being clocked and just being read as a 1 throughout.

Are you getting an acknowledgement back from the IC? If you have not already done this with the start condition you will have problems.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.