CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

TMP102 temperature Sensor not working with PIC16F877A

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kufre



Joined: 19 Oct 2013
Posts: 7
Location: Nigeria

View user's profile Send private message

TMP102 temperature Sensor not working with PIC16F877A
PostPosted: Sat Oct 19, 2013 1:40 pm     Reply with quote

Hey Guys!

Am newbie in PIC programming!
I have a new TMP102 breakout board from Sparkfun to read temperature as part of my school project. The sensor is a two wire serial (I2C) device.
I've connected the SDA and SCL on the breakout board to the SDA and SCL pins of the PIC16F877A respectively. The sensor is powered with +3.3vcc and the PIC is +5vcc both of them has common Gnd. The ADD0 pin of the TMP102 in connected to ground.

The issue is when all of them are powered on, I'll get 0xFF (255) for MSB and 0xFF (255) for LSB which shouldn't be so. The combination of the MSB and LSB should give me the temperature reading.

From the datasheet I should get something similar to this:
Code:

Temperature ( oC)    Digital Reading (Binary)   Hex
50                   0 0011 0010 0000           0320
25                   0 0001 1001 0000           0190
0.25                 0 0000 0000 0100           0004
0                    0 0000 0000 0000           0000
0.25                 1 1111 1111 1100           1FFC

Here is code, am using CCS C v4.068

Code:
#include <16F877A.h>
#FUSES NOWDT                     
#FUSES HS                       
#FUSES NOBROWNOUT               
#FUSES NOLVP, PUT


#use delay(clock=20000000)
#use i2c(Master, sda=PIN_C4,scl=PIN_C3) // Configure Device as Master
#use rs232(baud=4800,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=UART, ERRORS)

#define TMP_RD       0x93
#define TMP_WR       0x92 //Assume ADR0 is tied to VCC
#define TEMP_REG    0x00

void main()
{
delay_ms(2000);         
 fprintf(UART,"TMP102 Example \r\n");
 fprintf(UART,"Reading Started...\r\n");
 delay_ms(500);

 while (1)
   {
      unsigned int8 msb, lsb 
   i2c_start(); 
   delay_ms(50);
   i2c_write(TMP_WR); //We want to write a value to the TMP
   delay_ms(50);
   i2c_write(TEMP_REG);       //Set pointer register to temperature register (it's already there by default, but you never know) 
   i2c_start();
   delay_ms(50);
   i2c_write(TMP_RD); // Read from this I2C address, R/*W Set
   delay_ms(50);
   
   msb= i2c_read(1);            //Read the MSB data
   lsb = i2c_read(0);            //Read the LSB data
   i2c_Stop();
   
   printf(" %u ", msb); //
   printf(" %u ", lsb);    //         
     delay_ms(3000);
   }
}


Here is the sample result I'm getting
TMP102 Example
Reading Started...
255 255
255 255
255 255
255 255
255 255
255 255
255 255
255 255
255 255

Is anything wrong with the code or is there anything am missing out? Pls I need your help.
Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Oct 19, 2013 2:01 pm     Reply with quote

Quote:
The sensor is powered with +3.3vcc and the PIC is +5vcc both of them has common Gnd.

Change your #use i2c() statement to this:
Quote:

#use i2c(Master, sda=PIN_C4, scl=PIN_C3, FORCE_HW, SMBUS)


Quote:
The ADD0 pin of the TMP102 in connected to ground.

#define TMP_RD 0x93
#define TMP_WR 0x92 //Assume ADR0 is tied to VCC

These statements conflict with each other. The slave address is set by
the A0 connection. What is the true connection on your board ? If it's
really connected to GND, then the TMP102 slave address is 0x90 for
writing and 0x91 for reading.
kufre



Joined: 19 Oct 2013
Posts: 7
Location: Nigeria

View user's profile Send private message

TMP102 temperature Sensor not working with PIC16F877A
PostPosted: Sat Oct 19, 2013 4:38 pm     Reply with quote

Am sorry for the mix up. Actually, I connected the ADD0 to +vcc ( from sparkfun sample code).

Thanks for your reply.

I connected the ADD0 to GND and modify the code slightly to check if there is acknowledgement.
Code:

#include <16F877A.h>
#FUSES NOWDT                     
#FUSES HS                       
#FUSES NOBROWNOUT               
#FUSES NOLVP, PUT

#use delay(clock=20000000)

#use i2c(Master, sda=PIN_C4,scl=PIN_C3, FORCE_HW, SMBUS ) // Configure Device as Master

#use rs232(baud=4800,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=UART, ERRORS)


#define TMP_RD       0x91
#define TMP_WR       0x90 //Assume ADR0 is tied to GND
#define TEMP_REG     0x00

void main()
{

delay_ms(3000);         
 fprintf(UART,"TMP102 Example \r\n");
 fprintf(UART,"Reading Started...\r\n");
 delay_ms(500);
 while (1)
   {
    unsigned int8 msb, lsb;
   int8 temp;
     int re, re2, re3;
   i2c_start(); 
   delay_ms(50);
   re=i2c_write(TMP_WR); //We want to write a value to the TMP
   delay_ms(50);
   re2=i2c_write(TEMP_REG);       //Set pointer register to temperature register (it's already there by default, but you never know) 
   i2c_start();
   delay_ms(50);
   re3=i2c_write(TMP_RD); // Read from this I2C address, R/*W Set
   delay_ms(50);
   
   msb= i2c_read();            //Read the MSB data
   lsb = i2c_read();            //Read the LSB data
   i2c_Stop();
   
   printf(" %u ", msb); //
   printf(" %u ", lsb);    //
   
   printf(" re:%d re2:%d re3:%d\n", re, re2, re3);
     delay_ms(3000);
   }
}

I obsvered that the acknowledgement bits stored in re, re2, re3 are all "1" instead of "0" which means there's no acknowledgement at all.
The MSB and LSB still return 0xFF (255).

Pls could it be that the tmp102 is bad or there's something am missing out?

Your Suggestions is much appreciated.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Oct 19, 2013 5:19 pm     Reply with quote

Run this program. See if it detects the TMP102:
http://www.ccsinfo.com/forum/viewtopic.php?t=49713
You will have to change the top lines of the program to fit your PIC,
#fuses, #use delay, etc. Make sure you change the #use i2c to use
your pins, and add FORCE_HW and SMBUS.


Quote:

void main()
{
delay_ms(3000);
fprintf(UART,"TMP102 Example \r\n");
fprintf(UART,"Reading Started...\r\n");
delay_ms(500);
while (1)
{
unsigned int8 msb, lsb;
int8 temp;
int re, re2, re3;

Do not declare local variables in mid-code. Always put them at the start of
the function. In this case, move them up to the start of main().


Quote:

msb= i2c_read(); //Read the MSB data
lsb = i2c_read(); //Read the LSB data
i2c_Stop();

Here, you have changed this from your original code. I have no idea why.
It was correct in your first code. The last call to i2c_read() must give it
a 0x00 parameter. You removed it. Put it back in.


Quote:
#FUSES NOBROWNOUT

I don't see any reason for Nobrownout. It's highly helpful. Only in a very
low power situation (battery operated device) should you consider having
no brownout. Change it to BROWNOUT.
kufre



Joined: 19 Oct 2013
Posts: 7
Location: Nigeria

View user's profile Send private message

TMP102 temperature Sensor not working with PIC16F877A
PostPosted: Sun Oct 20, 2013 12:37 pm     Reply with quote

Thanks for the Link.
I modified and ran the code at http://www.ccsinfo.com/forum/viewtopic.php?t=49713, its detect the TMP102 and return the result as:
Quote:

Start:
ACK addr: 90

Number of i2c chips found: 1


I then copied part of it to my main code and is working (reading the temperature).

I really appreciate your support and explanations.
Once again thanks.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group