Coding and general discussion relating to the compiler
Moderators: David Barker, Jerry Messina
-
skartalov
- Posts: 37
- Joined: Fri Apr 09, 2010 10:50 am
- Location: BULGARIA
Post
by skartalov » Fri Feb 01, 2019 8:55 am
Hi,
i've been trying this for a few days already- no success.
I am trying to connect to TCS34725 color sensor via i2c, but seems like no any connection.
When I try to read from address it always return 255.
Here it is my code:
Device = 18F26k20
Clock = 64
Config FOSC = INTIO67
#option I2C_SCL = PORTB.4
#option I2C_SDA = PORTB.5
Dim I2C_EEPROM As Byte
Include "si2c.bas"
Public Sub i2cwrite (paddress As byte, pdata As Byte)
SI2C.Start
SI2C.WriteByte(I2C_EEPROM)
SI2C.WriteByte(paddress)
SI2C.WriteByte(pdata)
SI2C.Stop
DelayMS(10)
End Sub
Public Function i2cread (pAddress As byte) As Byte
SI2C.Start
SI2C.WriteByte(I2C_EEPROM)
SI2C.WriteByte(paddress)
SI2C.Restart
SI2C.WriteByte(I2C_EEPROM + 1)
Result = SI2C.ReadByte
SI2C.Acknowledge(I2C_NOT_ACKNOWLEDGE)
SI2C.Stop
End Function
Sub test()
I2C_EEPROM = $29
I2CWrite(0,1)
delayms(20)
I2CWrite(0, %00011011)
While true
temp=i2cread($12)
GLCD.WriteAt(0,0,DecToStr(temp)+" ")
DelayMS (100)
End While
End Sub
main:
test()
End
So what I am doing wrong? I have also 24LC256 EEPROM on the same i2c bus, and it works OK.
Attached is the datasheet of the color sensor.
https://cdn-shop.adafruit.com/datasheets/TCS34725.pdf
Please help!
-
Jerry Messina
- Swordfish Developer
- Posts: 1473
- Joined: Fri Jan 30, 2009 6:27 pm
- Location: US
Post
by Jerry Messina » Fri Feb 01, 2019 10:53 am
I think the $29 address is in 7-bit format so it would need to be shifted left one bit.
Since you say the EEPROM works I'll assume you have all the pins & hardware setup ok.
Try something like this and see what happens:
Code: Select all
sub check_addr(addr as byte)
dim DEVICE_PRESENT_LED as PORTA.0 ' LED or something
' send address and see if device acks
SI2C.Start
SI2C.WriteByte(addr)
SI2C.Stop
if (SI2C.NotAcknowledged = true) then
low(DEVICE_PRESENT_LED) ' addr did not ack... led off
else
high(DEVICE_PRESENT_LED) ' addr acked... device detected, led on
endif
end sub
Sub test()
I2C_EEPROM = $29 << 1 ' TCS34725 7-bit address
check_addr(I2C_EEPROM)
end sub
-
skartalov
- Posts: 37
- Joined: Fri Apr 09, 2010 10:50 am
- Location: BULGARIA
Post
by skartalov » Fri Feb 01, 2019 1:00 pm
I shifted the address, but still no success.
I wonder if I initialize the sensor correctly!
Would you mind take a quick look at the documentation, as on power on the sensor is in sleep state, and needs to be wake up first.
Thanks!
-
Jerry Messina
- Swordfish Developer
- Posts: 1473
- Joined: Fri Jan 30, 2009 6:27 pm
- Location: US
Post
by Jerry Messina » Fri Feb 01, 2019 2:53 pm
If you get an ack in the above check_addr() example then it's using the 7-bit address format (which matches what the arduino code uses).
If that works then the first thing the arduino code does is a read from reg $92 (COMMAND_BIT ($80) + ID ($12) = $92) and check to see if the result is $44
-
skartalov
- Posts: 37
- Joined: Fri Apr 09, 2010 10:50 am
- Location: BULGARIA
Post
by skartalov » Fri Feb 01, 2019 4:18 pm
I dont have subroutine: check_addr()
OK, what means COMMAND_BIT($80)?
should I understand that I have to send two command bytes ($80 and $12) in order to read from register (address $12)?
-
Jerry Messina
- Swordfish Developer
- Posts: 1473
- Joined: Fri Jan 30, 2009 6:27 pm
- Location: US
Post
by Jerry Messina » Fri Feb 01, 2019 7:43 pm
I dont have subroutine: check_addr()
You will if you add one similar to what I showed.
Have you verified that you can actually talk to the chip? That's the purpose of check_addr().
If the chip doesn't ACK its address then there's no reason to continue... something is wrong.
OK, what means COMMAND_BIT($80)?
should I understand that I have to send two command bytes ($80 and $12) in order to read from register (address $12)?
Not two bytes, just one. It seems that to generate the Command Code you need to OR the register addr $12 with $80 (setting the COMMAND_BIT, bit 7). That's how the arduino code does it.
-
skartalov
- Posts: 37
- Joined: Fri Apr 09, 2010 10:50 am
- Location: BULGARIA
Post
by skartalov » Sat Feb 02, 2019 2:21 pm
OK, I got it running! Thanks for your help!