Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 10:31 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : I2C troubles

Author Message
georgestheking
Newbie

Joined: 21/12/2021
Location: Belgium
Posts: 32
Posted: 10:31am 21 Jan 2022
Copy link to clipboard 
Print this post

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: 4311
Posted: 10:36am 21 Jan 2022
Copy link to clipboard 
Print this post

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
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 10:51am 21 Jan 2022
Copy link to clipboard 
Print this post

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
VK7JH
MMedit
 
georgestheking
Newbie

Joined: 21/12/2021
Location: Belgium
Posts: 32
Posted: 10:53am 21 Jan 2022
Copy link to clipboard 
Print this post

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
Copy link to clipboard 
Print this post

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: 4311
Posted: 02:26pm 21 Jan 2022
Copy link to clipboard 
Print this post

You do know that the first element of rdata is rdata(0) don't you, unless you specify OPTION BASE 1 ?
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5089
Posted: 02:29pm 21 Jan 2022
Copy link to clipboard 
Print this post

Hi george,

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

Volhout
PicomiteVGA PETSCII ROBOTS
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 02:29pm 21 Jan 2022
Copy link to clipboard 
Print this post

Seen TassyJim's post?

John
 
georgestheking
Newbie

Joined: 21/12/2021
Location: Belgium
Posts: 32
Posted: 02:32pm 21 Jan 2022
Copy link to clipboard 
Print this post

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
Copy link to clipboard 
Print this post

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: 3378
Posted: 02:59pm 21 Jan 2022
Copy link to clipboard 
Print this post

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.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025