I2C troubles


Author Message
georgestheking
Newbie

Joined: 21/12/2021
Location: Belgium
Posts: 32
Posted: 10:31am 21 Jan 2022      

Hi,

I make this to read a GY-26 compass :

REM-------------------------
REM PICOMITE 5.0703  
REM -----------------------

option default integer
option explicit

DIM INTEGER RDATA(8)

SETPIN GP2,GP3, I2C2

sub compass

I2C2 OPEN 100, 1000
I2C2 WRITE &HE0, 0, 1, &H31 ' send read command &h31
I2C2 READ &HE1, 0, 8, RData() ' read 8 datas registers
I2C2 CLOSE

end sub

DO
compass

print rdata(0)
print rdata(1)

PAUSE 1000
LOOP

END

but i get an error : "pin not set for I2C2"

I use version 5.0703

Best regards

Georges

thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4378
Posted: 10:36am 21 Jan 2022      

IIRC

I2C2 CLOSE


releases the pins, so the second time you enter compass() they are not set. To resolve move the SETPIN into compass() or the OPEN/CLOSE out of compass().

Best wishes,

Tom

TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6546
Posted: 10:51am 21 Jan 2022      

It looks like you have included the read/write bits in the address: E0 and E1

Just use the 7bit address and MMBasic adds the required direction bit

georgestheking
Newbie

Joined: 21/12/2021
Location: Belgium
Posts: 32
Posted: 10:53am 21 Jan 2022      

Tom,

Thanks, no more error.

But I get 0,0.
Has anybody experience with the GY-26 compass ?

Best regards

Georges

georgestheking
Newbie

Joined: 21/12/2021
Location: Belgium
Posts: 32
Posted: 02:11pm 21 Jan 2022      

Thanks Jim,

The code is now :

Rem -------------------------
Rem PICOMITE 5.0701
Rem -----------------------

Option default integer
Option explicit
Dim INTEGER RDATA(8)

Rem  COMPASS GY-26
Sub compass

SetPin GP2,GP3,I2C2

I2C2 OPEN 100,1000
I2C2 WRITE &HE0,0,2,0,&H31 ' send read command &h31
I2C2 READ &HE0,0,8,RData() ' read 8 registers
I2C2 CLOSE

End Sub

DO
compass

print rdata(1)
print rdata(2)

PAUSE 1000
LOOP

But still return 0,0 !!!

Best regards

Georges

PS : pull up resistors are in the right place

thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4378
Posted: 02:26pm 21 Jan 2022      

You do know that the first element of rdata is rdata(0) don't you, unless you specify OPTION BASE 1 ?

Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5994
Posted: 02:29pm 21 Jan 2022      

Hi george,

You should use the 7 bit I2C address.
The &hE0 is an 8 bit address, use &h70 in stead.

Volhout

JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4352
Posted: 02:29pm 21 Jan 2022      

Seen TassyJim's post?

John

georgestheking
Newbie

Joined: 21/12/2021
Location: Belgium
Posts: 32
Posted: 02:32pm 21 Jan 2022      

Woaww

It work now !

Many thanks Volhout,
I spent a complete day with this problem.

Best regards

Georges

georgestheking
Newbie

Joined: 21/12/2021
Location: Belgium
Posts: 32
Posted: 02:44pm 21 Jan 2022      

Thanks to the complete team for the help,

The right code:

Rem -------------------------
Rem PICOMITE 5.0703
Rem -----------------------

Option default integer
Option explicit

Dim As INTEGER RDATA(8)
Dim As integer P,R

Rem  COMPASS GY-26
Sub compass

SetPin GP2,GP3,I2C2

I2C2 OPEN 100,1000
I2C2 WRITE &H70,0,2,0,&H31 ' send read &h31
I2C2 READ &H70,0,8,RData() ' read 8 registers
I2C2 CLOSE

End Sub

Do

compass

R=rdata(1)*256+rdata(2)
Print R

Print

Pause 1000
Loop

End
Edited 2022-01-22 00:45 by georgestheking

lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3809
Posted: 02:59pm 21 Jan 2022      

Glad you got it working. Note that the customary way to show code in a post is to include it within the forum tags "[ CODE]" and "[ /CODE]" (without the quote marks and without the space following "[".

Like this:

Rem -------------------------
Rem PICOMITE 5.0703
Rem -----------------------

Option default integer
Option explicit

Dim As INTEGER RDATA(8)
Dim As integer P,R

Rem  COMPASS GY-26

Sub compass
 SetPin GP2,GP3,I2C2

 I2C2 OPEN 100,1000
 I2C2 WRITE &H70,0,2,0,&H31 ' send read &h31
 I2C2 READ &H70,0,8,RData() ' read 8 registers
 I2C2 CLOSE
End Sub

Do
 compass
 R=rdata(1)*256+rdata(2)
 Print R
 Print
 Pause 1000
Loop

End

Note that this preserves indentation if your code has any.