News:

;) This forum is the property of Proton software developers

Main Menu

Lidar Lite. VL53L1X sensor and OLED display running off a 12F675.

Started by david, Sep 09, 2023, 10:12 PM

Previous topic - Next topic

david

I have to admit I really struggled with this one until I finally figured out how to correctly load the sensor's 16 bit register addresses.   I could use 0x010F to access the device's ID but I couldn't use 0x0087 to load a register as the high byte was truncated and the low byte would try to load in to the sensor's high byte register.   Anyway....after many days of being tempted to use a small hammer to solve my debug problems I finally got there.
The code is separated to show the sensor set up and the OLED code which is a bit ugly because the little 12F675 doesn't support string functions and I've used double sized font but the whole thing squeezes into just over 1k.  (20 bytes left to store your favourite MP3s)
If you only want the serial out you can remove the OLED code.  If you want to use a device with hardware I2C just change BusOut/BusIn to HBusOut/HBusIn and set up its SSPCON.


' Lidar3 and OLED readout with double size numbers.

'
'                  Vin <1 U 8> Gnd   
'    RS232 Out  GPIO.5 <2   7> GPIO.0  Interrupt in (if used)   
'    Spare I/O  GPIO.4 <3   6> GPIO.1  SCL    (pullups on sensor and OLED)
'   Spare input GPIO.3 <4   5> GPIO.2  SDA              "


Device = 12F675
Config CPD_OFF,CP_OFF,BODEN_OFF,MCLRE_OFF,PWRTE_ON,WDT_OFF,INTRC_OSC_NOCLKOUT
Set_OSCCAL             ' Reads and sets manufacture value
Declare Xtal = 4 'Internal OSC
TRISIO = %00000111     '
All_Digital=true     '
Declare SCL_Pin GPIO.1
Declare SDA_Pin GPIO.2
Symbol Lidar_W= $52  'OLED address and write
Symbol Lidar_R= $53  'OLED address and read
Symbol OLED_W= $78  'OLED address + write   
Symbol OLED_R= $79  'OLED address + read     

    Dim ID As Word
    Dim Distance As Word    'value returned from sensor in mm
    Dim m As Byte
    Dim n As Byte      'number of digits in Distance value. (Lt to Rt)
    Dim Col As Byte    'columns of font data
    Dim Pge As Byte    'pixels
    Dim index As Byte  'index number in Cdata array
    Dim number As Byte 'single digit value from result
    Dim pix As Byte
    DelayMS 100
   
    'initialize display
   BusOut OLED_W,[$80,$AE,_      'display OFF
                        $00,$20,$00,_  'Mem mode  Horiz addressing
                        $00,$81,$7F,_  'contrast control reg/value
                        $80,$A1,_      'set segment remap
                        $80,$A4,_      'graphic RAM displayed
                        $80,$A6,_      'display norm/reverse
                        $00,$A8,$3F,_  'multiplex ratio/value
                        $80,$C8,_      'com scan direction
                        $00,$D5,$90,_  'set osc division/value
                        $00,$D9,$22,_  'set precharge period/value
                        $00,$DA,$12,_  'set Com pins/value
                        $00,$DB,$30,_  'set vcomh/value
                        $00,$8D,$14,_  'set charge pump enable/value
                        $80,$AF]       'display ON
                        DelayMS 5
                       
   For pix=0 To 255      '512, 256
     BusOut OLED_W,[$40,$00,$00,$00,$00]  'write zero down each font col
   Next pix
   DelayMS 100 
   
   BusOut OLED_W,[$00,$DA,16]  'Alt COM mode.
   BusOut OLED_W,[$00,$D6,1]   'Enable zoom mode.                         
   
'******* write config data to Lidar sensor *****************************
   
'config code               0x2D  E   F   0   1   2   3   4   5   6   7   8   9   A   B  0x3C
    BusOut Lidar_W,$00,[$2D,$00,$01,$01,$01,$02,$00,$02,$08,$00,$08,$10,$01,$01,$00,$00,$00,_
'                         0x3D                                                         0x4C
                           $00,$FF,$00,$0F,$00,$00,$00,$00,$00,$20,$0B,$00,$00,$02,$0A,$21,_
'                         0x4D                                                         0x5C
                           $00,$00,$05,$00,$00,$00,$00,$C8,$00,$00,$38,$FF,$01,$00,$08,$00,_
'                         0x5D                                                         0x6C
                           $00,$01,$DB,$0F,$01,$F1,$0D,$01,$68,$00,$80,$08,$B8,$00,$00,$00,_
'                         0x6D                                                         0x7C
                           $00,$0F,$89,$00,$00,$00,$00,$00,$00,$00,$01,$0F,$0D,$0E,$0E,$00,_
'                         0x7D                                     0X87                           
                           $00,$02,$C7,$FF,$9B,$00,$00,$00,$01,$00,$00]
   
   
    BusOut Lidar_W,$00,[$86,$01]   'clear interrpt     
    DelayMS 1
    BusOut Lidar_W,$00,[$87,$40]   'system mode start auto ranging
       
main:
    'While PORTA.0=0 :Wend      'wait for high interrupt from sensor
    DelayMS 200                 'OR -use a delay
 
    BusOut Lidar_W,$00,[$96]    'get value in mm
    BusIn Lidar_R,[Distance]
    DelayMS 1
   
    BusOut Lidar_W,$00,[$86,$01]   'clear int
    DelayMS 1
   
    BusOut Lidar_W,$00,[$87,$40]   'stop ranging
    DelayMS 1
   
    SerOut GPIO.5,84,[Dec Distance,13,10]    'optional serial out
   
'********* End of Lidar routine ****************************   
   
    BusOut OLED_W,[$00,$21,0,127]  'Reset column range, pointer to zero
    BusOut OLED_W,[$00,$22,1,1]    'Reset Page range, pointer to 3
    If Distance <100 Then n=1      '2 digits  10-99
    If Distance >999 Then n=3      '4 digits 1000 up
    If Distance >99 And Distance <1000 Then n=2  '3 digits 100-999
    GoSub printing                'jump to printing routine
    DelayMS 200   
   
    GoTo main                     'loop back to Lidar

'*********** Print routine  *********************************
printing:
      For n=n To 0 Step -1             'indexing the look up table
        number=Dig Distance,n          'individual digits
        For Col=0 To 4                 '5 bytes per character
          index= CRead8 Fonts [number*5+Col]
          BusOut OLED_W,[$40,index]   'print values into columns
          BusOut OLED_W,[$40,index]   'repeat above line for double width. 
        Next Col                   
         BusOut OLED_W,[$40,$00]      'print blank spacer column.
         BusOut OLED_W,[$40,$00]      'repeat blank spacer column.  .
      Next n
     
      BusOut OLED_W,[$40,$F8,$08,$70,$08,$F0]  'first "m"
      BusOut OLED_W,[$40,$F8,$08,$70,$08,$F0]
      BusOut OLED_W,[$40,$00]     'print blank spacer columns.
      BusOut OLED_W,[$40,$00]
      BusOut OLED_W,[$40,$F8,$08,$70,$08,$F0]  'second "m"
      BusOut OLED_W,[$40,$F8,$08,$70,$08,$F0]
     
       For m=0 To 35
          BusOut OLED_W,[$40,$00]     'print blank characters.
       Next m   
       Return

'*********** pruned font table ********************       
'                         Col0 Col1 Col2 Col3 Col4
Dim Fonts As Code=As Byte  $7C, $A2, $92, $8A, $7C,_      ' char 0    Dec 48
                           $00, $84, $FE, $80, $00,_      ' char 1        49
                           $C4, $A2, $92, $92, $8C,_      ' char 2        50
                           $44, $82, $92, $92, $6C,_      ' char 3        51
                           $18, $14, $12, $FE, $10,_      ' char 4        52
                           $9E, $92, $92, $92, $62,_      ' char 5        53
                           $7C, $92, $92, $92, $64,_      ' char 6        54
                           $06, $02, $E2, $12, $0E,_      ' char 7        55
                           $6C, $92, $92, $92, $6C,_      ' char 8        56
                           $4C, $92, $92, $92, $7C        ' char 9        57
                          '$F8, $08, $70, $08, $F0        ' char m       
                           
                               
End

Hopefully this will save others many frustrating days trawling through code examples on the web that all closely follow the ST api.

Cheers,
David

joesaliba

Thank you for the code David,

What type of OLEd you used, 0.95", 96 * 64?

Joe

david

Hello Joe,
The display I have is 0.96" 128x64 and based on the SSD1315 chip.  The standard font is pretty small so I've used a combination of printing double width and using the chip's zoom function which is double height to achieve a 2x2 font size.

Cheers,
David