News:

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

Main Menu

I2c help

Started by charliem, Nov 04, 2024, 10:29 AM

Previous topic - Next topic

charliem

Hello everyone,
 I was wondering if someone had a I2C hello world example they would share.
I am having a hard time understanding how the I2Cout Library works.
I want to see if I can display something using a 12F683 device.

Thanks.

John Lawton

When you say "display something" does this mean you wish to address an I2C display, in which case which one as I think there are a number of different ones available?

John

charliem

Hi John,
 Thanks for the reply.
I have a .96 i2c display.
It has 4 wires. I hope this helps narrow it down. I'm at work at the moment and don't have access to the display.
I just want to print hello world or something like that.
To help understand how the library works.

Thanks again.

JonW

The displays need a bit of setting up to print to them, have you tried a working library?  Here is Dompie's one OLED LIB

Atomix also has one on here and they both work well. 


charliem

Thanks I'll give it a try.

John Lawton

For a basic "Hello World" program, wouldn't it would be a lot easier to use a character based display?

John

charliem

Generally yes. My device is a 12F device and I have a i2c oled I was going to use.

Pepe

example i2c oled with demo Proteus

Device = 12F683
Declare Xtal = 8

Declare Optimiser_Level = 3
Declare Dead_Code_Remove = On
Declare Watchdog = Off
Declare SCL_Pin GPIO.1
Declare SDA_Pin GPIO.2

;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

;Device = 12F683

Config FOSC_INTOSCIO, WDTE_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF, BOREN_OFF, IESO_OFF, FCMEN_OFF

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------

Symbol OLED_W= $78   'OLED address + write     
Symbol OLED_R= $79   'OLED address + read       

Dim c As Byte
Dim d As Byte
Dim f As Byte
Dim i As Byte
     

Dim Fonts As Flash8 = $00,$00,$00,$00,$00,$00,_      'Graphic character 0
                      $FF,$FF,$FF,$FF,$FF,$FF,_      'Graphic character 1
                      $07,$07,$07,$00,$00,$00,_      'Graphic character 2
                      $00,$00,$00,$07,$07,$07,_      'Graphic character 3
                      $E0,$E0,$E0,$00,$00,$00,_      'Graphic character 4
                      $00,$00,$00,$E0,$E0,$E0,_      'Graphic character 5
                      $FF,$FF,$FF,$00,$00,$00,_      'Graphic character 6
                      $00,$00,$00,$FF,$FF,$FF,_      'Graphic character 7
                      $07,$07,$07,$07,$07,$07,_      'Graphic character 8
                      $E0,$E0,$E0,$E0,$E0,$E0,_      'Graphic character 9
                      $E0,$E0,$E0,$07,$07,$07,_      'Graphic character 10
                      $07,$07,$07,$E0,$E0,$E0,_      'Graphic character 11
                      $FF,$FF,$FF,$E0,$E0,$E0,_      'Graphic character 12
                      $E0,$E0,$E0,$FF,$FF,$FF,_      'Graphic character 13
                      $07,$07,$07,$FF,$FF,$FF,_      'Graphic character 14
                      $FF,$FF,$FF,$07,$07,$07,_      'Graphic character 15
                      $55,$AA,$55,$AA,$55,$AA,_      'Graphic character 16
                      $AA,$55,$AA,$55,$AA,$55,_      'Graphic character 17
                      $01,$01,$01,$01,$01,$01,_      'Graphic character 18
                      $80,$80,$80,$80,$80,$80,_      'Graphic character 19
                      $FF,$00,$00,$00,$00,$00,_      'Graphic character 20
                      $00,$00,$00,$00,$00,$FF,_      'Graphic character 21
                      $FF,$01,$01,$01,$01,$01,_      'Graphic character 22
                      $01,$01,$01,$01,$01,$FF,_      'Graphic character 23
                      $FF,$80,$80,$80,$80,$80,_      'Graphic character 24
                      $80,$80,$80,$80,$80,$FF,_      'Graphic character 25
                      $00,$00,$00,$00,$F0,$F0,_      'User defined character 26
                      $00,$00,$00,$00,$0F,$0F,_      'User defined character 27
                      $00,$00,$00,$00,$00,$00,_      'User defined character 28
                      $00,$00,$00,$00,$00,$00,_      'User defined character 29
                      $00,$00,$00,$00,$00,$00,_      'User defined character 30
                      $00,$00,$00,$00,$00,$00,_      'User defined character 31
                      $00,$00,$00,$00,$00,$00,_      '32 -   - 20       
                      $00,$00,$4F,$00,$00,$00,_      '33 - ! - 21
                      $00,$07,$00,$07,$00,$00,_      '34 - " - 22
                      $14,$7F,$14,$7F,$14,$00,_      '35 - # - 23
                      $24,$2A,$7F,$2A,$12,$00,_      '36 - $ - 24
                      $23,$13,$08,$64,$62,$00,_      '37 - % - 25
                      $36,$49,$55,$22,$50,$00,_      '38 - & - 26
                      $00,$05,$03,$00,$00,$00,_      '39 - ' - 27
                      $1C,$22,$41,$00,$00,$00,_      '40 - ( - 28
                      $00,$00,$41,$22,$1C,$00,_      '41 - ) - 29
                      $14,$08,$3E,$08,$14,$00,_      '42 - * - 2A
                      $08,$08,$3E,$08,$08,$00,_      '43 - + - 2B
                      $00,$50,$30,$00,$00,$00,_      '44 - , - 2C
                      $08,$08,$08,$08,$08,$00,_      '45 - - - 2D
                      $00,$60,$60,$00,$00,$00,_      '46 - . - 2E
                      $20,$10,$08,$04,$02,$00,_      '47 - / - 2F
                      $3E,$51,$49,$45,$3E,$00,_      '48 - 0 - 30
                      $00,$42,$7F,$40,$00,$00,_      '49 - 1 - 31
                      $42,$61,$51,$49,$46,$00,_      '50 - 2 - 32
                      $21,$41,$45,$4B,$31,$00,_      '51 - 3 - 33
                      $18,$14,$12,$7F,$10,$00,_      '52 - 4 - 34
                      $27,$45,$45,$45,$39,$00,_      '53 - 5 - 35
                      $3C,$4A,$49,$49,$30,$00,_      '54 - 6 - 36
                      $01,$71,$09,$05,$03,$00,_      '55 - 7 - 37
                      $36,$49,$49,$49,$36,$00,_      '56 - 8 - 38
                      $06,$49,$49,$49,$3E,$00,_      '57 - 9 - 39
                      $00,$36,$36,$00,$00,$00,_      '58 - : - 3A
                      $00,$56,$36,$00,$00,$00,_      '59 - ; - 3B
                      $08,$14,$22,$41,$00,$00,_      '60 - < - 3C
                      $14,$14,$14,$14,$14,$00,_      '61 - = - 3D
                      $00,$41,$22,$14,$08,$00,_      '62 - > - 3E
                      $02,$01,$51,$09,$06,$00,_      '63 - ? - 3F
                      $32,$49,$79,$41,$3E,$00,_      '64 - @ - 40
                      $7E,$11,$11,$11,$7E,$00,_      '65 - A - 41
                      $7F,$49,$49,$49,$36,$00,_      '66 - B - 42
                      $3E,$41,$41,$41,$22,$00,_      '67 - C - 43
                      $7F,$41,$41,$22,$1C,$00,_      '68 - D - 44
                      $7F,$49,$49,$49,$41,$00,_      '69 - E - 45
                      $7F,$09,$09,$09,$01,$00,_      '70 - F - 46
                      $3E,$41,$49,$49,$7A,$00,_      '71 - G - 47
                      $7F,$08,$08,$08,$7F,$00,_      '72 - H - 48
                      $00,$41,$7F,$41,$00,$00,_      '73 - I - 49
                      $20,$40,$41,$3F,$01,$00,_      '74 - J - 4A
                      $7F,$08,$14,$22,$41,$00,_      '75 - K - 4B
                      $7F,$40,$40,$40,$40,$00,_      '76 - L - 4C
                      $7F,$02,$0C,$02,$7F,$00,_      '77 - M - 4D
                      $7F,$04,$08,$10,$7F,$00,_      '78 - N - 4E
                      $3E,$41,$41,$41,$3E,$00,_      '79 - O - 4F
                      $7F,$09,$09,$09,$06,$00,_      '80 - P - 50
                      $3E,$41,$51,$21,$5E,$00,_      '81 - Q - 51
                      $7F,$09,$19,$29,$46,$00,_      '82 - R - 52
                      $46,$49,$49,$49,$31,$00,_      '83 - S - 53
                      $01,$01,$7F,$01,$01,$00,_      '84 - T - 54
                      $3F,$40,$40,$40,$3F,$00,_      '85 - U - 55
                      $1F,$20,$40,$20,$1F,$00,_      '86 - V - 56
                      $3F,$40,$38,$40,$3F,$00,_      '87 - W - 57
                      $63,$14,$08,$14,$63,$00,_      '88 - X - 58
                      $07,$08,$70,$08,$07,$00,_      '89 - Y - 59
                      $61,$51,$49,$45,$43,$00,_      '90 - Z - 5A
                      $7F,$41,$41,$00,$00,$00,_      '91 - [ - 5B
                      $02,$04,$08,$10,$20,$00,_      '92 - \ - 5C
                      $00,$00,$41,$41,$7F,$00,_      '93 - ] - 5D
                      $04,$02,$01,$02,$04,$00,_      '94 - ^ - 5E
                      $40,$40,$40,$40,$40,$00,_      '95 - _ - 5F
                      $00,$01,$02,$04,$00,$00,_      '96 - ` - 60
                      $20,$54,$54,$54,$78,$00,_      '97 - a - 61
                      $7F,$48,$44,$44,$38,$00,_      '98 - b - 62
                      $38,$44,$44,$44,$20,$00,_      '99 - c - 63
                      $38,$44,$44,$48,$7F,$00,_      '100  d - 64
                      $38,$54,$54,$54,$18,$00,_      '101  e - 65
                      $08,$7E,$09,$01,$02,$00,_      '102  f - 66
                      $0C,$52,$52,$52,$3E,$00,_      '103  g - 67
                      $7F,$08,$04,$04,$78,$00,_      '104  h - 68
                      $00,$44,$7D,$40,$00,$00,_      '105  i - 69
                      $00,$20,$40,$44,$3D,$00,_      '106  j - 6A
                      $7F,$10,$28,$44,$00,$00,_      '107  k - 6B
                      $00,$41,$7F,$40,$00,$00,_      '108  l - 6C
                      $7C,$04,$18,$04,$78,$00,_      '109  m - 6D
                      $7C,$08,$04,$04,$78,$00,_      '110  n - 6E
                      $38,$44,$44,$44,$38,$00,_      '111  o - 6F
                      $7C,$14,$14,$14,$08,$00,_      '112  p - 70
                      $08,$14,$14,$18,$7C,$00,_      '113  q - 71
                      $7C,$08,$04,$04,$08,$00,_      '114  r - 72
                      $48,$54,$54,$54,$20,$00,_      '115  s - 73
                      $04,$3F,$44,$40,$20,$00,_      '116  t - 74
                      $3C,$40,$40,$20,$7C,$00,_      '117  u - 75
                      $1C,$20,$40,$20,$1C,$00,_      '118  v - 76
                      $3C,$40,$30,$40,$3C,$00,_      '119  w - 77
                      $44,$28,$10,$28,$44,$00,_      '120  x - 78
                      $0C,$50,$50,$50,$3C,$00,_      '121  y - 79
                      $44,$64,$54,$4C,$44,$00,_      '122  z - 7A
                      $08,$36,$41,$00,$00,$00,_      '123  { - 7B
                      $00,$00,$7F,$00,$00,$00,_      '124  | - 7C
                      $00,$00,$41,$36,$08,$00,_      '125  } - 7D
                      $00,$08,$04,$08,$04,$00,_      '126  ~ - 7E     
                      $00,$3C,$26,$23,$26,$3C        '127  � - 7F

Dim texto[11] As Byte = "hello world"

OSCCON = $71

TRISIO = %00000101

  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

BusOut OLED_W,[$00,$DA,16]    'Alt COM mode.

Do
 clearscreen ()
 
 For c = 0 To 63

  For f = 0 To 7
   If c > 0 Then
                d = c - 1
                locate(d,f)
                BusOut OLED_W,[$40,0]
   EndIf
   
   locate(c,f)
   
   For i = 0 To 10
    printing( texto[i])
   Next
  Next
 
  DelayMS 100
 
 Next
Loop

End

Proc locate(x As Byte,y As Byte)
BusOut OLED_W,[$00,$21,x,127]
BusOut OLED_W,[$00,$22,y,127]
EndProc

Proc clearscreen ()
Dim pix As Byte
 locate(0,0)
 For pix = 0 To 255
  BusOut OLED_W,[$40,$00,$00,$00,$00]
 Next pix
EndProc

Proc printing(chr As Byte)
Dim col As Byte
Dim index As Byte
 For col = 0 To 5
  index= CRead8 Fonts[chr*6+col]
  BusOut OLED_W,[$40,index]
 Next col
EndProc

charliem

Thankyou PePe.

That was a lot of work. I didn't think that there was a easy way to do it.
Thanks again.

Charlie.

JonW

Charlie, don't be put off by the extensive listing Pepe posted (he is a proficient coder); that is where libraries come into play and why I pointed you to a library-based approach. 

This is Dompie's code with just Hello Charlie, I removed the comments to de-clutter the code (no disrespect Dompie).
You can see that using libraries (inc files), it dramatically simplifies the code structure. 


Demo SSDlite library
;*******************************************************************
;*  SSDlite_v1.20.bas - demo SSDlite library
;*******************************************************************


Device  =   12F1840                 ;
    ;Internal Oscillator 8Mhz and enable PLL (= 4 * 8 = 32Mhz)
OSCCONbits_SCS0 = 0 : OSCCONbits_SCS1 = 0
OSCCONbits_IRCF3 = 1 : OSCCONbits_IRCF2 = 1 : OSCCONbits_IRCF1 = 1 : OSCCONbits_IRCF0 = 0  ;8Mhz internal clock
OSCCONbits_SPLLEN = 1                       ;Enable PLL
DelayMS 1
Declare Xtal = 32

Declare Hints = false                       ;suppress HBUS legacy message
;-----------------------------------------------
;PORT and TRIS
;Register   %76543210
PORTA   =   %00000000
LATA    =   %00000000
TRISA   =   %00000000
;----------------------------------------------------
;I2C hardware or software, default I2C via Hardware (RA1 and RA2) but
$define SSDlite_I2C_Software            ;uncomment - Use bit-bashed I2C
    $ifdef SSDlite_I2C_Software
        Declare SCL_Pin = PORTA.4       ;SCL = Port.Pin
        Declare SDA_Pin = PORTA.5       ;SDA = Port.pin
    $endif
;-----------------------------------------------------
;SSDlite hijacked the Positron commands Print (At), Cursor and Cls
;-----------------------------------------------------

Dim     bRow        As Byte
Dim     bCol        As Byte
Dim     bScaleX     As Byte
Dim     bCnt        As Byte

Include "SSDlite_Def_v1.20.inc"
Include "SSDlite_Proc_v1.20.inc"







While 1=1
    Cls
    ScaleYX(1,1)                    ;Normal character
    Print At 1,1, "HELLO CHARLIE"

End







;===========================================================
;=============F U S E S=====================================
;===========================================================
Config1 FOSC_INTOSC,_   ;Oscillator Selection bits
        WDTE_OFF,_      ;Watchdog Timer Enable bit
        PWRTE_OFF,_     ;Power-up Timer Enable bit
        MCLRE_OFF,_     ;MCLR/VPP Pin Function Select bit
        CP_OFF,_        ;Code Protection bit
        CPD_OFF,_       ;Data Code Protection bit
        BOREN_OFF,_     ;Brown-out Reset Enable bits
        CLKOUTEN_OFF,_  ;Clock Out Enable bit
        IESO_OFF,_      ;Internal External Switchover bit
        FCMEN_OFF       ;Fail-Safe Clock Monitor Enable bit
Config2 WRT_OFF,_       ;Flash Memory Self-Write Protection bits
        PLLEN_OFF,_     ;PLL Enable bit UIT wordt in Software gedaan
        STVREN_ON,_     ;Stack Overflow/Underflow Reset Enable bit
        BORV_19,_       ;Brown-out Reset Voltage Selection bit
        LVP_OFF         ;Low-Voltage Programming Enable bit
;




charliem

Thanks for the reply.
Where can get that library?

Nevermind I found it. Thanks so much.

JonW

Just change the processor,port mapping and fuses to your chosen MCU.  Remember to put the .inc files in the same folder or in the main positron, including file location.

charliem

Thankyou for the help Jon.

Pepe

The example I gave was for pic 12f683 and I did not use the SSDlite library because it is not possible because it does not work because since it uses string variables, this pic does not support them.
If you can use another pic model then use the library that is easier.

charliem

Ok thanks Pepe.