News:

Let's find out together what makes a PIC Tick!

Main Menu

I2C address confusion

Started by charliecoutas, Today at 12:46 PM

Previous topic - Next topic

charliecoutas

I am confused (yet again). When using I2C, if a chip has an I2C address of, say, $052 (a Time Of Flight jobbie - VL53L0X). What address do I put in the I2C command?

Is it I2CRead SDA, SCL, $52, ......?  And is a write I2COut SDA, SCL, $53, ...?

Or do I have to take the binary value, %01010010 and shift it left 1: %10100100=$E4 and use that?

Do I have to set the bottom bit of the address for a read or does the I2C command do it?

Charlie
 


RGV250

Hi Charlie,
I think you have to shift everything left by one. Then set bit 0 for read or write.
I am on a mobile so can't elaborate.

Bob

Pepe

Example

;-------------------------------------------------------------------------------
Device = 16F1827
Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF, BOREN_OFF, CLKOUTEN_OFF, IESO_OFF, FCMEN_OFF
Config2 WRT_OFF, PLLEN_OFF, STVREN_OFF, LVP_OFF
;-------------------------------------------------------------------------------

Declare Xtal 16
Declare Optimiser_Level = 3
Declare Watchdog off
Declare Dead_Code_Remove = 1
Declare Create_Coff On
Declare Hserial_Baud = 9600
Declare HRSOut_Pin = PORTB.2

Declare Hbus_Bitrate = 400
Declare HSCL_Pin = PORTB.4
Declare HSDA_Pin = PORTB.1

;------------------------------------------------
; LCD
;------------------------------------------------
Declare LCD_Interface 4
Declare LCD_DTPin PORTA.0
Declare LCD_ENPin PORTA.6
Declare LCD_RSPin PORTA.7
Declare LCD_Lines 2
Declare LCD_DataUs 50
Declare LCD_CommandUs 2000

Symbol CalBtn PORTB.0

;------------------------------------------------
; VL53L0X ADDR
;------------------------------------------------
Symbol W_ADDRESS = $52
Symbol R_ADDRESS = $53

;------------------------------------------------
; Variables
;------------------------------------------------
Dim dist As Word = $ffff
Dim Distance As SWord
Dim tmp As SWord
Dim DatoSensor[12] As Byte
Dim offset As SWord        ; puede ser negativo
Dim prom As Word
Dim dat As Word
Dim i As Byte
Dim SPAD As Word
Dim Signal As Word
Dim Ambient As Word



;------------------------------------------------
; Oscilador
;------------------------------------------------
OSCCON = %01111000
Do : Loop While OSCSTAT.0 = 0

TRISA = %00000000
TRISB = %00000011          ' RB1 SDA, RB0 BOTÓN

offset = ERead 0   ' cargar offset guardado
If offset = $FFFF Then offset = 0
Cls
Print At 1,1,"VL53L0X to calib"
Print At 2,2,"Press button"
DelayMS 1500
VL53_Init

Cls
Print At 1,1,"Dist:"

;====================================================================
;                          BUCLE PRINCIPAL
;====================================================================
Do
    '------------------------------------------
    '    SI SE PRESIONA EL BOTON = CALIBRAR
    '------------------------------------------
    If CalBtn = 0 Then
                       calibrar
                  Else
                       medir
     EndIf
     
     DelayMS 100
Loop

;====================================================================
Sub medir                       
    '------------------------------------------
    '      LECTURA NORMAL DEL SENSOR
    '------------------------------------------
    leer
    Distance = prom       

    If Distance <> dist Then
                             dist =  Distance
                             tmp = Distance + offset
                             If tmp > 0 Then
                                             Distance = tmp
                                        Else
                                             Distance = 0
                             EndIf
                             Print At 1,7,Dec Distance," mm  "
                             HSerOutLn ["Dist:",Dec Distance," mm "]
                             HSerOutLn [ "SPAD: ",Hex4 SPAD,  " Signal: ",Dec Signal, " Ambient: ",Dec Ambient]
    EndIf                             
   
EndSub

;====================================================================
Sub calibrar
Cls
Print At 1,1,"Calibrating..."
Print At 2,1,"Put at 10 cm"

Do
Loop While CalBtn = 0

leer

' Calcular offset:
' offset = distancia_real(100mm) � medicion_promedio
offset = 100 - prom

' Guardar en EEPROM
EWrite  0, [offset]

Cls
Print At 1,1,"OFFSET= ",SDec offset," mm"
DelayMS 2000
Cls
Print At 1,1,"Dist:"
dist = $ffff
EndSub

;====================================================================
Sub leer
  prom = 0
    For i = 1 To 4
        dat=  VL53Lx()
        If dat<>0 Then
                       prom = prom + dat
                  Else
                       Inc i
        EndIf
        DelayMS 30
    Next
    prom = prom / 4
EndSub   
;====================================================================


;====================================================================
;             LECTURA VL53L0X  (SIN CAMBIOS)
;====================================================================
Proc VL53Lx() ,Word
    HBusOut W_ADDRESS,[$00,$01]
    DelayMS 5
    HBusOut W_ADDRESS,[$14]
    DelayMS 33
    HBusIn R_ADDRESS,[Str DatoSensor]
    If DatoSensor[0]= $5E Then
                              Result = DatoSensor[10]*256 + DatoSensor[11]
                              ' Effective SPAD Return Count
                              SPAD = DatoSensor[2]*256 + DatoSensor[3]

                              ' Signal Rate
                              Signal = DatoSensor[6]*256 + DatoSensor[7]

                              ' Ambient Rate
                              Ambient = DatoSensor[8]*256 + DatoSensor[9]

                         Else
                              Result= 0
    EndIf
EndProc
;====================================================================

Sub VL53_Init
    '-----------------------------------------
    ' 1) Apagar control automático interno
    '-----------------------------------------
    HBusOut W_ADDRESS, [$88,$00]
    DelayMS 10
   
    '-----------------------------------------
    ' 2) Entrar en modo de configuración secreta
    '-----------------------------------------
    HBusOut W_ADDRESS, [$80,$01]
    HBusOut W_ADDRESS, [$FF,$01]
    HBusOut W_ADDRESS, [$00,$00]

    '-----------------------------------------
    ' 3) Patchear reloj interno (registro oculto $91)
    '-----------------------------------------
    HBusOut W_ADDRESS, [$91,$3C]

    '-----------------------------------------
    ' 4) Salir de modo secreto
    '-----------------------------------------

    HBusOut W_ADDRESS, [$00,$01]
    HBusOut W_ADDRESS, [$FF,$00]
    HBusOut W_ADDRESS, [$80,$00]

EndSub


charliecoutas

Bob. Pepe, many thanks.

Charlie

▲