New to I2C. Need some help!

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Francesco.C
Posts: 41
Joined: Thu Feb 26, 2009 6:54 pm
Location: UK

New to I2C. Need some help!

Post by Francesco.C » Wed Jun 09, 2010 9:58 pm

Hi guys,
Please can you take a look at my code below?
I am writing my first I2C code.
I am trying to read the ID register of a SE95 (a temperature sensor device)
I should get 'A1h'. All I get are 'FF'.

Can you put me on the right track?

Thanks
Francesco



{
*****************************************************************************
* Name : SE95.BAS *
* Author : Francesco cembrola *
* *
* : *
*****************************************************************************
}
// set clock and osc...
Device = 18F242
Clock = 4


// import usart module...
Include "usart.bas"
Include "convert.bas"
Include "I2C.bas"
Include "utils.bas"
Dim ID_Val As Byte

Const SE95_addr = $00 ' Address of SE95 temperature sensor
Const ID=$05 'Device ID register

Sub Read_ID()
I2C.Initialize
I2C.Start
I2C.WriteByte(SE95_addr) ' Access device by its address
I2C.WriteByte(ID) ' Access ID register
I2C.Restart
I2C.WriteByte(SE95_addr) ' Access device by its address
ID_Val = I2C.ReadByte ' Read ID
I2C.Acknowledge(I2C_NOT_ACKNOWLEDGE)
i2c.stop
End Sub

USART.SetBaudrate(br9600)

While true
Read_ID
USART.Write(HexToStr(ID_Val)) 'Print ID
Wend

RKP
Registered User
Registered User
Posts: 82
Joined: Mon Oct 22, 2007 3:14 pm
Location: Maryland

Post by RKP » Thu Jun 10, 2010 2:21 am

Francesco,

Try looking at my post here: http://www.mikroe.com/forum/viewtopic.p ... 03#p112903
I know it is with a diiferent compiler but functionality is the same. The SE95 is a more accurate version and a drop in replacement of a LM75.

Anyway there are a few things I noticed that need to be changed.
1 The address is $90 if the address pins are all 0.
2 When you read the SE95 you need to write the address value as $91,
3 Also you need to read two bytes of data for a correct value.

Please look at the link above this worked with ME basic compiler.
I wrote that example for ME forum for just that reason.

Make those changes and let us know how it works.

RKP

Francesco.C
Posts: 41
Joined: Thu Feb 26, 2009 6:54 pm
Location: UK

Post by Francesco.C » Thu Jun 10, 2010 8:19 pm

Thank a lot RKP,

I followed your advice and I managed to read the ID register.

So I got ambitious now and tryed to read the temperature register.

I can read one of the byte but the other gives me FF.
According to the timing diagramm of the data-sheet I have to read the two bytes as I have done in the code below!
Can you see anything wrong in code below?

Thank again.
Regards

Francesco

Sub Read_Temp()
I2C.Initialize
I2C.Start
I2C.WriteByte(SE95_addr) ' Access device by its address
I2C.WriteByte(TEMP) ' Access TEMP register
I2C.Restart
I2C.WriteByte(SE95_addr+1)
TEMP_Val_MSB = I2C.ReadByte ' Read TEMP MSB
TEMP_Val_LSB = I2C.ReadByte ' Read TEMP LSB
I2C.Acknowledge(I2C_NOT_ACKNOWLEDGE)
I2C.Stop
End Sub



USART.SetBaudrate(br2400)

While true
Read_Temp
USART.Write(HexToStr(TEMP_Val_MSB), HexToStr(TEMP_Val_LSB),13,10)
Wend

RKP
Registered User
Registered User
Posts: 82
Joined: Mon Oct 22, 2007 3:14 pm
Location: Maryland

Post by RKP » Thu Jun 10, 2010 8:51 pm

Francesco,

Could post all your code and put it in the "Code" tags.

RKP

Francesco.C
Posts: 41
Joined: Thu Feb 26, 2009 6:54 pm
Location: UK

Post by Francesco.C » Thu Jun 10, 2010 9:46 pm

Sorry about that!
Here is the code.
Thanks

Code: Select all

// set clock and osc...
Device = 18F242
Clock = 4


// import  modules...
Include "usart.bas"
Include "convert.bas"
Include "I2C.bas"
Include "utils.bas" 

Dim  TEMP_Val_LSB, TEMP_Val_MSB As Byte

Const SE95_addr = %10010000 ' Address of SE95 temperature sensor 
Const TEMP=$00

Sub Read_Temp()
 I2C.Initialize
 I2C.Start
 I2C.WriteByte(SE95_addr)          ' Access device by its address
 I2C.WriteByte(TEMP)                 ' Access TEMP register
 I2C.Restart
 I2C.WriteByte(SE95_addr+1)           
 TEMP_Val_MSB = I2C.ReadByte             ' Read TEMP MSB
 TEMP_Val_LSB = I2C.ReadByte              ' Read TEMP LSB
 I2C.Acknowledge(I2C_NOT_ACKNOWLEDGE)
 I2C.Stop 
End Sub


 USART.SetBaudrate(br9600)

 While true
 
  Read_Temp
  USART.Write(HexToStr(TEMP_Val_MSB), HexToStr(TEMP_Val_LSB),13,10)
  
 Wend

RKP
Registered User
Registered User
Posts: 82
Joined: Mon Oct 22, 2007 3:14 pm
Location: Maryland

Post by RKP » Thu Jun 10, 2010 10:33 pm

Try adding this line I2C.Acknowledge(I2C_ACKNOWLEDGE)

Code: Select all

Sub Read_Temp() 
 I2C.Initialize 
 I2C.Start 
 I2C.WriteByte(SE95_addr)          ' Access device by its address 
 I2C.WriteByte(TEMP)                 ' Access TEMP register 
 I2C.Restart 
 I2C.WriteByte(SE95_addr+1)            
 TEMP_Val_MSB = I2C.ReadByte             ' Read TEMP MSB
 I2C.Acknowledge(I2C_ACKNOWLEDGE) ' *****Added this****
 TEMP_Val_LSB = I2C.ReadByte              ' Read TEMP LSB 
 I2C.Acknowledge(I2C_NOT_ACKNOWLEDGE) 
 I2C.Stop 
End Sub 
Hope that helps,
RKP

Francesco.C
Posts: 41
Joined: Thu Feb 26, 2009 6:54 pm
Location: UK

Post by Francesco.C » Fri Jun 11, 2010 5:23 pm

THANK YOU RKP, It Works :D :D

Regards

Francesco

Post Reply