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

Problems with TMP102 and PIC18F46K22

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



Joined: 29 Nov 2020
Posts: 1

View user's profile Send private message

Problems with TMP102 and PIC18F46K22
PostPosted: Sun Nov 29, 2020 8:02 pm     Reply with quote

Here is my code:
Code:

#include <18f46K22.h>
#device PASS_STRINGS = IN_RAM
#define BME280_I2C_ADDRESS  0xEC

//#fuses XT,NOWDT,NOPROTECT,PUT,NOLVP,BROWNOUT
#fuses XT,NOWDT,NOPROTECT,PUT,NOLVP,NOBROWNOUT
#use i2c(Master, sda=PIN_D1, scl=PIN_D0)
#use delay(clock = 20MHz,crystal = 20MHz)
#use rs232(baud=9600,bits=8,parity=N,xmit=PIN_C6,rcv=PIn_C7)                                  //SERIAL de GPS
//#use RS232(UART1, BAUD = 9600, ERRORS)  //UART1
//#use i2c(MASTER, sda=pin_D1,scl=pin_D0, FAST = 400000, STREAM = TMP117_STREAM, FORCE_HW, SMBUS )   //BME280
//#use I2C(MASTER, sda=pin_B0,scl=pin_B1, FAST = 400000, STREAM = INA_STREAM, )
//#include <BME280_Lib.c>  // include BME280 sensor driver source file
//#include <GPS_Lib.c>    //Libreria GPS
#include <stdio.h>                               
#include <string.h>
#include<math.h>
//#define TMP_RD       0x91
//#define TMP_WR       0x90//Assume ADR0 is tied to VCC
//#define TEMP_REG    0x00

signed int32 temperature;
unsigned int32 pressure, humidity;


//Varaibles del sensor de corriente
int data1,data2;
 int16 VSina = 0;
 unsigned int16 VSinau;
float valuecurrent;

int8 get_ack_status(int8 address)
{
int8 status;

i2c_start();
status = i2c_write(address);  // Status = 0 if got an ACK
i2c_stop();

if(status == 0)
   return(TRUE);
else
   return(FALSE);
}

 //void dac(unsigned int16 sample){
   
   //  i2c_start(TMP117_STREAM);
   //i2c_write(TMP117_STREAM,0B11000000);  // I2C slave address 0x40
   // i2c_write(TMP117_STREAM,0B1000000);  // INA219 config reg
   // i2c_write(TMP117_STREAM,((sample & 0xFF0) >> 4)); // bus 16V, shunt 320mV, 12-bit continuous Vshunt Vbus
   // i2c_write(TMP117_STREAM,((sample & 0xF) << 4));
   // i2c_write(INA_STREAM,0x9F); //
   // i2c_stop(TMP117_STREAM);
   
   // }
   
   

void main(){

 unsigned int8 msb, lsb ;
 int re, re2, re3;

 
   
   delay_ms(2000);         
 printf("TMP102 Example \r\n");
 printf("Reading Started...\r\n");
 delay_ms(500);
   
   
  while(TRUE)
  {
 
  unsigned int8 i;
unsigned int8 status;
unsigned int8 count = 0;

printf("\n\rStart:\n\r");

delay_ms(1000);

// Try all slave addresses from 0x10 to 0xEF.
// See if we get a response from any slaves
// that may be on the i2c bus.
for(i=0x10; i < 0xF0; i+=2)
   {
    status = get_ack_status(i);
    if(status == TRUE)
      {
       printf("ACK addr: %X\n\r", i);
       count++;
       delay_ms(2000);
      }
   }

if(count == 0)
   printf("\n\rNothing Found");
else
   printf("\n\rNumber of i2c chips found: %u", count);
   
   
   i2c_start();
  delay_ms(50);
   re=i2c_write(i); //We want to write a value to the TMP
  delay_ms(50);
   re2=i2c_write(0X00);       //Set pointer register to temperature register (it's already there by default, but you never know)
   i2c_start();
   delay_ms(50);
   re3=i2c_write(i+1); // Read from this I2C address, R/*W Set
   delay_ms(50);
   
   msb= i2c_read();            //Read the MSB data
   delay_ms(50);
  lsb = i2c_read(0);            //Read the LSB data
   i2c_Stop();
   
   printf(" %u ", msb); //
   printf(" %u ", lsb);    //   
  printf(" re:%d re2:%d \n", re, re2);
     delay_ms(3000);
 
  }

  }
Ttelmah



Joined: 11 Mar 2010
Posts: 19505

View user's profile Send private message

PostPosted: Mon Nov 30, 2020 4:16 am     Reply with quote

Comments inline

Code:

//#fuses XT,NOWDT,NOPROTECT,PUT,NOLVP,BROWNOUT
#fuses XT,NOWDT,NOPROTECT,PUT,NOLVP,NOBROWNOUT
//This is wrong. XT is for a crystal <=4MHz. You should be selecting HS

#use delay(clock = 20MHz,crystal = 20MHz)
//This though overrides the previous error.

#use i2c(Master, sda=PIN_D1, scl=PIN_D0)
//Put this _after_ the clock setup. Otherwise timings may not be right

#use rs232(baud=9600,bits=8,parity=N,xmit=PIN_C6,rcv=PIn_C7)


You then need to get rid of your delay_ms(50) lines. The chip has a
built in timeout, if a transaction does not complete in 30mSec . These
delays will prevent the I2C from working properly. Once a 'start' has been
sent, the I2C should be continuous, without delays.
On a standard I2C device, there is no 'timeout', but SMBUS devices
have a timeout. This is an SMBUS device.
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Nov 30, 2020 5:35 am     Reply with quote

re: the hardware...just downloaded the TMP102...The datasheet says it's a '3 volt ' device, so be SURE to run the PIC at 3 volts !!

Also, add 'ERRORS' to the #Use RS232(...options...). It'll prevent overrun errors (UART freezing).
Jay
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