![]() |
Forum Index : Microcontroller and PC projects : I2C troubles
Author | Message | ||||
georgestheking Newbie ![]() Joined: 21/12/2021 Location: BelgiumPosts: 32 |
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 KingdomPosts: 4311 |
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: AustraliaPosts: 6283 |
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: BelgiumPosts: 32 |
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: BelgiumPosts: 32 |
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 KingdomPosts: 4311 |
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: NetherlandsPosts: 5089 |
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 KingdomPosts: 4044 |
Seen TassyJim's post? John |
||||
georgestheking Newbie ![]() Joined: 21/12/2021 Location: BelgiumPosts: 32 |
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: BelgiumPosts: 32 |
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 Pause 1000 Loop End Edited 2022-01-22 00:45 by georgestheking |
||||
lizby Guru ![]() Joined: 17/05/2016 Location: United StatesPosts: 3378 |
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 Pause 1000 Loop End Note that this preserves indentation if your code has any. PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |