This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMP112,can't get the date of temperature

Other Parts Discussed in Thread: TMP112

#include<pic.h>
#define uchar unsigned char
#define uint  unsigned int
__CONFIG(UNPROTECT & BOREN & MCLRDIS & PWRTEN & WDTDIS & INTOSCIO);   
#define SDA RC0
#define SCL RC1
#define SDA_DIR TRISC0
#define SCL_DIR TRISC1
#define SDA_HIGH() SDA_DIR=1
#define SDA_LOW()  SDA_DIR=0
#define SCL_HIGH() SCL_DIR=1
#define SCL_LOW()  SCL_DIR=0

#define I2C_TM_BUS_FREE  5     //   min. 600ns
#define I2C_TM_START_SU  5     //   min.  100ns
#define I2C_TM_START_HD  4     //   min.  100ns
#define I2C_TM_SCL_LOW  5     //   min. 1300ns
#define I2C_TM_SCL_HIGH  4    //   min.  600ns
#define I2C_TM_DATA_SU  1    //   min.  100ns
#define I2C_TM_DATA_HD      0     //   min.    0ns
#define I2C_TM_SCL_TO_DATA 4   //   max.  300ns    /* SCL low to data valid */
#define I2C_TM_STOP_SU  4     //   min.  100ns
#define I2C_TM_SCL_TMO  10   //   max. 1000ns    /* clock time out */

#define I2C_ERROR (-1)
#define FALSE  0
#define TRUE  !FALSE


#ifndef XTAL_FREQ
#define XTAL_FREQ 4MHZ  /* Crystal frequency in MHz */
#endif

#define MHZ *1000L       /* number of kHz in a MHz */
#define KHZ *1           /* number of kHz in a kHz */

#define LEDoff     PORTA=0b00000000;TRISA=0b00001111
#define LED0on     PORTA=0b00010000;TRISA=0b11001111
#define LED1on     PORTA=0b00100000;TRISA=0b11001111
#define BUTTON     RA3

void DelayUs(uchar x); 
void DelayMs(uchar cnt);

void  start();
void  stop();
uchar ReadAck();
void  SendAck(uchar status);
uchar i2c_WaitForSCL();
uchar SendByte(uchar byte);
uchar ReadByte();
uchar ReadFrom(uchar address);

void  FlashLED0();
void  FlashLED1();
uchar tem1,tem2;
float Temperature;
uint  temper;


void main()
{

  TRISC=0Xff;     //PORTC Input SCL=RC1,SDA=RC0
  PORTC=0X00;    
 
  
       
 while(1)
       {
        if(BUTTON==0)
        {
       DelayMs(10);
       if(BUTTON==0)
        {
         while(!BUTTON);
                 
                  start();
               SendByte(0b10010010);
                  ReadAck();
                 
                  SendByte(0x00);
                  ReadAck();
               stop();
                 
                  start();
               SendByte(0b10010011);
                  ReadAck();
                
                 
                  temper=ReadByte();
              //SendAck(TRUE);
                 
                  //tem2=ReadByte();
                  //SendAck(FALSE);
                 
                  stop();
       
                  DelayMs(250); 
         


                  if(temper==0b00000000)  {LED1on;}     //!!!!here is the problem!!!
                 
                  //uchar a;
                  //a=tem1*16+tem2/16;
                  //Temperature=(tem1*16+tem2/16)*0.0625;
                  //temper=(int)Temperature;
                 
                  //LED0on;
   
              } 
           }
       
       }
}

 


 

 

 

void start()
{
        SCL_LOW();                      /* ensure clock is low */
        SDA_HIGH();                     /* ensure data is high */

 DelayUs(I2C_TM_DATA_SU);

 SCL_DIR = 1;  /* clock pulse high */
 DelayUs(I2C_TM_SCL_HIGH);

        SDA_LOW();                      /* the high->low transition */
        DelayUs(I2C_TM_START_HD);
 return;
}


void stop()
{
 

        SDA_LOW();                      /* ensure data is low first */
        SCL_HIGH();

 DelayUs(I2C_TM_DATA_SU);
 SCL_DIR =1;  /* float clock high */
 DelayUs(I2C_TM_STOP_SU);
        SDA_HIGH();                     /* the low->high data transistion */
 DelayUs(I2C_TM_BUS_FREE); /* bus free time before next start */
        SDA_DIR = 1;            /* float data high */

 return;
}


uchar ReadAck()
{
 uchar ack;
   
    SCL_LOW();                              /* make clock is low */
 SDA_DIR = 1;   /* disable data line - listen for ack */
 DelayUs(I2C_TM_SCL_TO_DATA);  /* SCL low to data out valid */
 SCL_DIR = 1;   /* float clock high */
 DelayUs(I2C_TM_DATA_SU);
 ack = SDA;    /* read the acknowledge */
   
  

   
    return ack;
   
}

 

 

void SendAck(uchar status)
{
        SCL_LOW();
        if ( status & 0x01) {
            SDA_LOW();                          /* drive line low -> more to come */
        }
        else {
            SDA_HIGH();
 }
 DelayUs(I2C_TM_DATA_SU);
 SCL_DIR = 1;   /* float clock high */
 DelayUs(I2C_TM_SCL_HIGH);
 return;
}

 

 

uchar i2c_WaitForSCL()
{
 /* SCL_DIR should be input here */
  
 if(!SCL)
 {
  DelayUs(I2C_TM_SCL_TMO);
  /* if the clock is still low -> bus error */
  if(!SCL)
   return TRUE;
 }
 return FALSE;
}

 

 

uchar SendByte(uchar byte)
{
    uchar i;
    for (i=0;i<8;i++)
      {
         SCL_LOW();                    /* drive clock low */
         /* data hold time = 0, send data now */
         SDA_DIR = ((byte<<i)&0x80);   /* bit to send */
         if ((byte<<i)&0x80) {
            SDA_HIGH();
             }
            else {
              SDA_LOW();
             }
      
         DelayUs(I2C_TM_DATA_SU);
      SCL_DIR = 1;               /* float clock high */

                    
      
         DelayUs(I2C_TM_SCL_HIGH);   /* clock high time */
      }            
    return FALSE;
}

 

 


uchar ReadByte()
{
 unsigned char i;
 unsigned char byte = 0;

 for(i=0; i<8; i++)
 {
        SCL_LOW();                      /* drive clock low */
  DelayUs(I2C_TM_SCL_LOW); /* min clock low  period */
  SDA_DIR = 1;  /* release data line */

  SCL_DIR = 1;  /* float clock high */
 
  DelayUs(I2C_TM_SCL_HIGH);
  byte = byte << 1;  /* read the next bit */
  byte |= SDA;
 }
 return byte;
}

 

 

}
void DelayUs(uchar x) 
{  uchar _dcnt;
   _dcnt = (x)/((12MHZ)/(XTAL_FREQ))|1;
  while(--_dcnt != 0)
 continue;
}


void DelayMs(uchar cnt)
{
 uchar i;
 do {
  i = 4;
  do {
   DelayUs(250);
  } while(--i);
 } while(--cnt);
}


void FlashLED1()
{  
      LED1on;
      DelayMs(200);
      LEDoff;
      DelayMs(200);
}

void FlashLED0()
{  
      LED0on;
      DelayMs(200);
      LEDoff;
      DelayMs(200);
}

 

========================================================================================

Sorry for a so long Program first.

I'm a newer for microcontroll and just learn C for a short time. now i'm using a pic16f676 from Microchip and TMP112.

I have programmed the code and also get the data from an o-scope, but I don't know why I get nothing through the sentence

"temper=ReadByte(); ".

In fact ReadByte equals to 0. 

But why from the o-scope, ReadByte()= ob00011000?

 

 

 

 

  • Hi Biqing Li,

    Since the initial value of byte is 0, and ReadByte() is returning a non-zero value, it might be possible that the temper value is not getting updated.

    Try defining temper as volatile and see if gets updated.

    It might be due to optimization standards enabled by the compiler.

    Hope this helps,

    Regards,

    Sid

     

  • thx Sid,

    I just found that I read no data form the sensor though it send to me.

    so I guess the part of Readbyte() is the problem. but I don't know where and what is the fault.

    appreciate for any reply

    Li 

  • Hi Li,

    Have you checked that the I2C protocol is getting followed exactly on scope?

    What do you mean that scope shows ReadByte value? or as per your last post, that was an erroneous condition?

    Are you operating I2C at 4MHz? Try going for lower frequencies say 400 KHz.

    Regards,

    Sid

  • Sorry for the unclear description.

    now the situation is: the senor gives me the temperature data back, while I can watch them through the scope.

    but I can't read them through the software ( the code ReadByte() ).

    I'll try your suggestion, Sid.

    Thank you very much : )

    Have a nice dream, good night!