# PIC18F26J11, temp sensor,I2C

Status
Not open for further replies.

#### Malkovic

##### New Member
Hi!

I'm having some troubles with the I2C communication between my MCU and my temperature sensor.

Sensor
https://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/DATASHEET/CD00153511.pdf

MCU

Link to mikroe and the function definitions.
https://www.mikroe.com/esupport/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=135

Code:
void main()
{

double temp;
TRISA.B0=0;
TRISA.B1=0;
TRISC=0xFF; //outputs for i2c
ANCON0=0xFF;
I2C1_Init(100000);

while(1)
{

I2C1_Start();
I2C1_Wr(0b10010010); //controll code + adress + r/w = 0
I2C1_Wr(0b00000011); //point to temperature reg

I2C1_Repeated_Start();
I2C1_Wr(0b10010011); //controll code + adress + r/w = 1

take1 = I2C1_Rd(1); //ACK
take2 = I2C1_Rd(0); //NACK

I2C1_Stop();

LATA.B0 = ~LATA.B0; //just to see if it hangs or not
delay_ms(500);

}
}

I've tested so that I can write successfully to the sensor. But the problem is when I used I2C1_Rd, the MCU just hangs when I do that.

Any suggestions to what might cause these problems?

#### Malkovic

##### New Member
I2C1_Wr(0b00000011); //point to temperature reg

Should be

I2C1_Wr(0b00000000); //point to temperature reg

0b00000011 is another register. Still did not work to read from it though

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
How do you know the writing works?..... I read the data sheet as slave address 0x90 and 0x91.... Have you set A0, A1 and A3 to 0v?

If you are reading from the wrong address you'll never get to reading byte two. as the temp sensor is waiting for a stop.

#### Malkovic

##### New Member

I'm quite convinced that the writing is working since the writing returns a 0 (no errors) for every adress I've tried but [A2:A0] = 001. So A2 and A1 is grounded while A0 is kept high.

So in my case my 8-bit adress, incl the r/w-bit, is 0x92 (writing) and 0x93(reading).

I was thinking it might be problem with the init, but I'm not exactly sure how it works. I'm also going to try if I can successfully write to the configuration register today. If that does not work, the sensor must be fried or something...

I can also add the the SCL and SDA lines are working since on the same PCB I've an EEPROM which works.

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
Check your speed.... EEPROM's work a 400khz (normally) but sensors may work at 100khz only... secondly what value resistors have you used for SDA and SCK lines....I use two devices and set them to 4k7

Status
Not open for further replies.

Replies
0
Views
2K
Replies
0
Views
2K
Replies
3
Views
2K
Replies
0
Views
2K
Replies
12
Views
7K