Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 03:43 29 Nov 2024 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 : Help i2c

Author Message
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 11:28am 25 Apr 2023
Copy link to clipboard 
Print this post

I've just come bqack to Picomite after a break and I've been scratching my head all morning
I cannot get i2c to work

  Quote  
SETPIN GP4, GP5, I2C
>
> run
[16] I2C OPEN 100,100,PU  'speed kHz,timeout ms,pullup
Error : Pin not set for I2C
>


This is my options



Anyone got any ideas how to fix it?
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9133
Posted: 11:42am 25 Apr 2023
Copy link to clipboard 
Print this post

The setpin needs to be in the program
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 11:47am 25 Apr 2023
Copy link to clipboard 
Print this post

  matherp said  The setpin needs to be in the program

Thanks Peter, I did try that first

This is the program
Option base 0
Option default none
Option explicit on

Const qmc5883l.I2C_ADDR% = &h0D

SetPin GP4, GP5, I2C
I2C OPEN 100, 1000

Dim mag_x%, mag_y%, mag_z%, temp%

Print "Initialising..."
qmc5883l.init()

Print "Running..."
Do
'  qmc5883l.read_all()
qmc5883l.read(mag_x%, mag_y%, mag_z%, temp%)
Print mag_x%, mag_y%, mag_z%, temp%
Pause 1000
Loop

' Initialises the QMC5883L.
Sub qmc5883l.init()

' Reset the chip.
qmc5883l.write_register(&h0A, &h80)
qmc5883l.read_all()

qmc5883l.write_register(&h0B, &h01)
qmc5883l.read_all()

' Continuous (0x01), 200Hz (0x0C), 8G (0x10), OSR=512 (0x00)
' qmc5883l.write_register(&h09, &h01 Or &h0C Or &h10 Or &h00)
qmc5883l.write_register(&h09, &h1D)
qmc5883l.read_all()
End Sub

' Writes value to a QMC5883L register.
Sub qmc5883l.write_register(register%, value%)
' Address the slave and write two bytes, the register and the value.
I2C Write qmc5883l.I2C_ADDR%, 0, 2, register%, value%
If MM.I2C Then Error qmc5883l.get_i2c_error$()
End Sub

' Gets string corresponding to MM.I2C error code.
Function qmc5883l.get_i2c_error$()
Select Case MM.I2C
  Case 0    : qmc5883l.get_i2c_error$ = "None"
  Case 1    : qmc5883l.get_i2c_error$ = "NACK"
  Case 2    : qmc5883l.get_i2c_error$ = "Timeout"
  Case Else : qmc5883l.get_i2c_error$ = "Unexpected error: " + Str$(MM.I2C)
End Select
End Function

' Reads X, Y, Z from the magnetic sensor and also the temperature.
Sub qmc5883l.read(mag_x%, mag_y%, mag_z%, temp%)
' TODO: should probably check if data ready bit/register is set.

' Address the slave and send the first register address to read (0x00).
I2C Write qmc5883l.I2C_ADDR%, 1, 1, &h00
If MM.I2C Then Error qmc5883l.get_i2c_error$()

' Read 8 registers/bytes (0x00 .. 0x07).
Local buf$
I2C Read qmc5883l.I2C_ADDR%, 0, 8, buf$
If MM.I2C Then Error qmc5883l.get_i2c_error$()

mag_x% = Peek(Var buf$, 1) Or (Peek(Var buf$, 2) << 8)
mag_y% = Peek(Var buf$, 3) Or (Peek(Var buf$, 4) << 8)
mag_z% = Peek(Var buf$, 5) Or (Peek(Var buf$, 6) << 8)
temp%  = Peek(Var buf$, 8) Or (Peek(Var buf$, 9) << 8)
End Sub

' Reads and dumps the contents of all 14 QMC5883L registers.
Sub qmc5883l.read_all()
' Address the slave and send the first register address to read (0x00).
I2C Write qmc5883l.I2C_ADDR%, 1, 1, &h00
If MM.I2C Then Error qmc5883l.get_i2c_error$()

' Read 14 registers/bytes (0x00 .. 0x0D).
Local buf$
I2C Read qmc5883l.I2C_ADDR%, 0, 14, buf$
If MM.I2C Then Error qmc5883l.get_i2c_error$()

Local i%
For i% = 1 To 14
'    Print Hex$(Asc(Mid$(buf$, i%, 1)), 2) " ";
  Print Asc(Mid$(buf$, i%, 1));
Next
Print
End Sub


This is what I get when I run it



Edit: I have buzzed out the connections and they are all correct
Edited 2023-04-25 21:50 by lew247
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 12:14pm 25 Apr 2023
Copy link to clipboard 
Print this post

I don't get this
I tried the i2c detect program here
' i2cdetect like program

' Set Pins!
SetPin gp4, gp5, I2C

' Open first I2C
I2C open 100, 400


' Prep table
Print "     0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F"

' loop col/row
For y=0 To 7
Print Hex$(y,2);": ";
For x=0 To 15
 addr = y*16+x               ' calc address
 I2C write addr, 0, 1, &H00  ' write zerp to that adress
 If MM.I2C=0 Then            ' check for errors
   Print Hex$(addr,2);" ";   ' found one!
Else
  Print "-- ";               ' nothing here..
EndIf
Next x
Print
Next y

I2C close  ' clean up
and it's not detecting any i2c device
Yet the device is connected
SDA on the device to GP4 on the Pico and SCL to GP5 on the Pico
Tested the connections with a meter and they are connected
Edited 2023-04-25 22:19 by lew247
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3152
Posted: 12:44pm 25 Apr 2023
Copy link to clipboard 
Print this post

Your OPTION LIST doesn't show OPTION SYSTEM I2C sdapin, sclpin

(This is a change from earliest times.)

Try OPTION SYSTEM I2C GP4,GP5

(I think once you've used that, you don't need I2C OPEN.)

~
Edited 2023-04-25 22:45 by lizby
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3813
Posted: 12:48pm 25 Apr 2023
Copy link to clipboard 
Print this post

Would the program need I2C2 otherwise?

John
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9133
Posted: 12:58pm 25 Apr 2023
Copy link to clipboard 
Print this post

His program is correct. The issue is that the device isn't responding as evidenced by the i2c detect.
Do you have pullups on SDA and SCL?
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 01:49pm 25 Apr 2023
Copy link to clipboard 
Print this post

  matherp said  His program is correct. The issue is that the device isn't responding as evidenced by the i2c detect.
Do you have pullups on SDA and SCL?


No I don't have external pullups as i thought Pico had internal ones?
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6808
Posted: 01:58pm 25 Apr 2023
Copy link to clipboard 
Print this post

Even if the internal pullups are enabled, they are very high value.  That will slow down the I2C signals as they need the resistors for the rising edges. I usually use 4K7 but you can mess about with the value - I've gone down to 2K2 in the past.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 09:29am 27 Apr 2023
Copy link to clipboard 
Print this post

I've now added 4k7 pullup resistors and I'm still getting nothing when running the i2c scanner
I'm guessing either my compass that uses i2c is kaput or the pico is
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9133
Posted: 09:34am 27 Apr 2023
Copy link to clipboard 
Print this post

Are you sure you are connecting to the correct pins on the Pico? GP4 and GP5 are pins 6 and 7 with pin 6 as SDA
Edited 2023-04-27 19:35 by matherp
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 09:57am 27 Apr 2023
Copy link to clipboard 
Print this post

yes and I've metered them out to the sda and scl pins on the device
scl - scl and sda - sda

Edit: just tried RC7 and it's the same so it must be my hardware
Edited 2023-04-27 20:04 by lew247
 
Print this page


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

© JAQ Software 2024