SSD1306 OLED (72x40) I2C display from scratch


+ Reply to Thread
Results 1 to 40 of 74

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default SSD1306 OLED (72x40) I2C display from scratch

    Thanks a lot guys 👍

    I'll give it a try this week-end.

    And thanks Richard for condensing the information here
    Roger

  2. #2
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default SSD1306 OLED (72x40) I2C display from scratch

    Only garbage at this time...but it's alive

    Name:  72x40_first_life.png
Views: 13434
Size:  501.7 KB
    Roger

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,133


    Did you find this post helpful? Yes | No

    Default Re: SSD1306 OLED (42x40) I2C display from scratch

    Seems that the character size is too big to fit there..

    Ioannis

  4. #4
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default Re: SSD1306 OLED (42x40) I2C display from scratch

    ...still struggeling with the ssd1306's datasheet to understand the "page" concept and how to display things.

    This is the simplest and shortest code I'm working with for now, composed of different sources I found in this forum and also on ARDUINO's one:

    Code:
    ' ====== DESCRIPTION ===============================================================================
    ' Use mini OLED 72x40 pixel display
    
    ' ====== FUSES =====================================================================================
    ' PIC 16F690 Fuses
    ' Internal oscillator  (activate OSCCON register)
    @ __config _FCMEN_OFF &_IESO_OFF &_CPD_OFF &_WDT_OFF &_INTRC_OSC_NOCLKOUT &_BOR_OFF &_CP_OFF &_PWRTE_OFF &_MCLRE_OFF
    
    @ ERRORLEVEL -306
    @ ERRORLEVEL -202
    
    ' ====== REGISTERS =================================================================================
    '             76543210
    OPTION_REG = %10000000 'PORT A&B Pull-Ups disabled (see WPUA & WPUB)
    OSCCON     = %01100000 'Internal RC set to 4MHZ
    'ANSEL      = %00000000 'Select analog inputs Channels 0 to 7 (AN2 + AN4 selected)
    'ANSELH     = %00000000 'Select analog inputs Channels 8 to 11
    'ADCON0     = %10000000 'A/D Module (Bit5:2 select channels 0:11)
    'ADCON1     = %00000000 'A/D control register
    'CM1CON0    = %00000000 'Comparator1 Module is OFF
    'CM2CON0    = %00000000 'Comparator2 Module is OFF
    'INTCON     = %00000000 'INTerrupts CONtrol (TMR0 OFF)
    'TRISA      = %00000000 'Set Input/Output (0 to 5)
    'PORTA      = %00000000 'Ports High/Low (0 to 5)
    'TRISB      = %00000000 'Set Input/Output (4 to 7)
    'PORTB      = %00000000 'Ports High/Low (4 to 7)
    'TRISC      = %00000000 'Set Input/Output (0 to 7)
    'PORTC      = %00000000 'Ports High/Low (0 to 7)
    
    ' ====== DEFINES ===================================================================================
    DEFINE OSC 4
    'DEFINE NO_CLRWDT 1   'Don't waste cycles clearing WDT  
    'DEFINE HSER_CLOERR 1 'Automatic clear overrun error 
    
    ' ====== VARIABLES =================================================================================
    I2CDevice   var byte
    
    SDA         VAR PORTB.4 ' I2C Data
    SCL         VAR PORTB.6 ' I2C Clock
    
    TEXT        VAR BYTE[20]
    LENGTH      VAR BYTE[2]
    DC          VAR Byte    ' "DATA OR COMMAND", $40=DATA; $0=COMMAND --> changed from bit to byte
    LCD_DATA    VAR BYTE   
    COM         VAR BYTE    ' COMMAND
    
    I           VAR BYTE
    J           VAR BYTE
    X           VAR BYTE  'LCD POSITION X(0 TO 127)
    Y           VAR BYTE  'LCD POSITION Y(0 TO 7)
    
    ' ====== INITIALIZE VARIABLES ======================================================================
    I2CDevice = $78
    
    '======= SSD1306 I2C OLED initialization ===========================================================
    COM = $AE : GOSUB SEND_COMMAND ' turn off oled panel
    COM = $D5 : GOSUB SEND_COMMAND ' set display clock divide ratio/oscillator frequency
    COM = $80 : GOSUB SEND_COMMAND ' set divide ratio
    COM = $A8 : GOSUB SEND_COMMAND ' set multiplex ratio
    COM = $27 : GOSUB SEND_COMMAND ' 1/40 duty
    COM = $D3 : GOSUB SEND_COMMAND ' set display offset
    COM = $00 : GOSUB SEND_COMMAND ' not offset
    COM = $AD : GOSUB SEND_COMMAND ' Internal IREF Setting	
    COM = $30 : GOSUB SEND_COMMAND ' --
    COM = $8D : GOSUB SEND_COMMAND ' set Charge Pump enable/disable
    COM = $14 : GOSUB SEND_COMMAND ' set(0x10) disable
    COM = $40 : GOSUB SEND_COMMAND ' set start line address
    COM = $A6 : GOSUB SEND_COMMAND ' set normal display
    COM = $A4 : GOSUB SEND_COMMAND ' Disable Entire Display On
    COM = $A1 : GOSUB SEND_COMMAND ' set segment re-map 128 to 0
    COM = $C8 : GOSUB SEND_COMMAND ' Set COM Output Scan Direction 64 to 0
    COM = $DA : GOSUB SEND_COMMAND ' set com pins hardware configuration
    COM = $12 : GOSUB SEND_COMMAND '
    COM = $81 : GOSUB SEND_COMMAND ' set contrast control register
    COM = $AF : GOSUB SEND_COMMAND ' 
    COM = $D9 : GOSUB SEND_COMMAND ' set pre-charge period
    COM = $22 : GOSUB SEND_COMMAND '
    COM = $DB : GOSUB SEND_COMMAND ' set vcomh
    COM = $20 : GOSUB SEND_COMMAND ' 
    COM = $AF : GOSUB SEND_COMMAND ' turn on oled panel    
    
    ' ====== PROGRAM ===================================================================================
    PAUSE 20
    GOSUB CLEAR_LCD
    GOSUB TITLE
    
    MAIN:
        PAUSE 1000
        GOTO MAIN
    END
    
    ' ====== CLEAR LCD =================================================================================
    CLEAR_LCD:
        FOR J = 0 TO 7 
            FOR I = 0 TO 71 '127
                LCD_DATA = $00 : GOSUB SEND_DATA
            NEXT I
        NEXT J                             
        RETURN 
        
    ' ====== SEND COMMAND ==============================================================================
    SEND_COMMAND:
        dc = $0
        I2CWrite SDA,SCL,I2CDevice,DC,[COM]
        dc = $40
        RETURN 
        
    ' ====== CLEAR LCD =================================================================================
    SEND_DATA:
        DC = $40
        I2CWrite SDA,SCL,I2CDevice,DC,[LCD_DATA]
        RETURN
        
    ' ====== SEND DATA =================================================================================
    FILL:
        FOR J = 0 TO 7
            FOR I = 0 TO 71 '127
                LCD_DATA = $FF : GOSUB SEND_DATA
            NEXT I
        NEXT J
        RETURN
        
    ' ====== TITLE =====================================================================================
    TITLE:
        X = 0 : Y = 0 : GOSUB SET_XY
        FOR I = 0 TO 71 '127
            LOOKUP I,[$00,$40,$60,$50,$7E,$50,$60,$40,$02,$06,$0E,$1E,$3E,$00,$00,$00,_
            $00,$00,$00,$00,$00,$1F,$3F,$60,$60,$60,$60,$60,$60,$3C,$1C,$00,_
            $00,$7F,$7F,$61,$61,$61,$61,$61,$61,$3F,$1E,$00,$00,$1E,$3F,$61,_
            $61,$61,$61,$61,$61,$38,$18,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
            $00,$00,$00,$00,$00,$00,$00,$1F,$3F,$60,$60,$60,$60,$60,$60,$3C,_
            $1C,$00,$00,$1E,$3F,$61,$61,$61,$61,$61,$61,$38,$18,$00,$00,$3F,_
            $7F,$60,$18,$06,$06,$18,$60,$7F,$3F,$00,$00,$00,$00,$00,$00,$00,_
            $00,$00,$00,$1C,$63,$41,$5D,$41,$5D,$41,$5D,$41,$5D,$41,$7F,$00],LCD_DATA
            lcd_data = lcd_data rev 8
            GOSUB SEND_DATA
        NEXT I
    
        X = 0 : Y = 1 : GOSUB SET_XY
        for i = 0 to 71 '127
            lookup i,[$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
            $00,$00,$00,$00,$00,$F8,$FC,$06,$06,$06,$C6,$C6,$C6,$FC,$F8,$00,_
            $00,$FE,$FE,$80,$80,$80,$80,$80,$80,$00,$00,$00,$00,$18,$1C,$86,_
            $86,$86,$86,$86,$86,$FC,$78,$00,$00,$00,$00,$00,$00,$00,$00,$00,_
            $00,$00,$00,$00,$00,$00,$00,$F8,$FC,$06,$06,$06,$C6,$C6,$C6,$FC,_
            $F8,$00,$00,$18,$1C,$86,$86,$86,$86,$86,$86,$FC,$78,$00,$00,$FE,_
            $FE,$00,$00,$00,$00,$00,$00,$FE,$FE,$00,$00,$00,$00,$00,$00,$00,_
            $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00],lcd_data
            lcd_data=lcd_data rev 8
            GOSUB SEND_DATA
        NEXT I
    
        X = 0 : Y = 3 : GOSUB SET_XY
        for i = 0 to 71 '127
            lookup i,[$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$3C,$7E,$C3,$C3,$C3,$C3,_
            $C3,$C3,$71,$30,$00,$00,$7F,$FF,$C3,$C3,$C3,$C3,$C3,$C3,$C3,$C3,_
            $00,$00,$7F,$FF,$C1,$C1,$C1,$C1,$C1,$C1,$FF,$7F,$00,$00,$7F,$FF,_
            $C3,$C3,$C3,$C3,$C3,$C3,$7E,$3C,$00,$00,$3F,$7F,$C0,$C0,$C0,$C0,_
            $C0,$C0,$70,$30,$00,$00,$FF,$FF,$03,$03,$03,$03,$03,$03,$FF,$FF,_
            $00,$00,$C0,$C0,$C0,$C0,$FF,$FF,$C0,$C0,$C0,$C0,$00,$00,$FF,$FF,_
            $0C,$0C,$03,$03,$00,$00,$FF,$FF,$00,$00,$3F,$7F,$C0,$C0,$C0,$C1,_
            $C1,$C1,$79,$39,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00],lcd_data
            lcd_data=lcd_data rev 8
            GOSUB SEND_DATA
        NEXT I
    
        X = 0 : Y = 4 : GOSUB SET_XY
        for i = 0 to 71 '127
            lookup i,[$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$30,$38,$0C,$0C,$0C,$0C,_
            $0C,$0C,$F8,$F0,$00,$00,$F8,$FC,$0C,$0C,$0C,$0C,$0C,$0C,$0C,$0C,_
            $00,$00,$FC,$FC,$80,$80,$80,$80,$80,$80,$FC,$FC,$00,$00,$FC,$FC,_
            $80,$C0,$60,$30,$18,$0C,$04,$00,$00,$00,$F0,$F8,$0C,$0C,$0C,$0C,_
            $0C,$0C,$38,$30,$00,$00,$FC,$FC,$00,$00,$00,$00,$00,$00,$FC,$FC,_
            $00,$00,$0C,$0C,$0C,$0C,$FC,$FC,$0C,$0C,$0C,$0C,$00,$00,$FC,$FC,_
            $00,$00,$00,$00,$C0,$C0,$FC,$FC,$00,$00,$F0,$F8,$0C,$0C,$0C,$8C,_
            $8C,$8C,$F8,$F0,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00],lcd_data
            lcd_data=lcd_data rev 8
            GOSUB SEND_DATA
        NEXT I
    
        X = 0 : Y = 5 : GOSUB SET_XY
        for i = 0 to 71 '127
            lookup i,[$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$1F,$3F,$30,$30,$30,$30,_
            $30,$30,$30,$30,$00,$00,$0F,$1F,$30,$30,$30,$30,$30,$30,$1F,$0F,_
            $00,$00,$1F,$3F,$30,$30,$30,$30,$30,$30,$1F,$0F,$00,$00,$00,$00,_
            $00,$00,$00,$0F,$1F,$30,$30,$30,$30,$30,$30,$1C,$0C,$00,$00,$30,_
            $30,$30,$30,$3F,$3F,$30,$30,$30,$30,$00,$00,$0F,$1F,$30,$30,$30,_
            $30,$30,$30,$1E,$0E,$00,$00,$3F,$3F,$03,$03,$00,$00,$00,$00,$3F,_
            $3F,$00,$00,$1F,$3F,$30,$30,$30,$30,$30,$30,$3F,$1F,$00,$00,$3F,_
            $3F,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00],lcd_data
            lcd_data=lcd_data rev 8
            GOSUB SEND_DATA
        NEXT I
    
        X = 0 : Y = 6 : GOSUB SET_XY
        for i = 0 to 71 '127
            lookup i,[$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$FF,$FF,$C0,$C0,$C0,$C0,_
            $C0,$C0,$00,$00,$00,$00,$FC,$FE,$03,$03,$03,$03,$03,$03,$FE,$FC,_
            $00,$00,$FF,$FF,$E0,$F0,$D8,$CC,$C6,$C3,$81,$00,$00,$00,$00,$00,_
            $00,$00,$00,$0C,$8E,$C3,$C3,$C3,$C3,$C3,$C3,$7E,$3C,$00,$00,$03,_
            $03,$03,$03,$FF,$FF,$03,$03,$03,$03,$00,$00,$FC,$FE,$03,$03,$03,_
            $63,$63,$63,$7E,$7C,$00,$00,$FF,$FF,$00,$00,$C0,$C0,$30,$30,$FF,_
            $FF,$00,$00,$FF,$FF,$60,$60,$60,$60,$60,$60,$FF,$FF,$00,$00,$FF,_
            $FF,$03,$03,$03,$03,$03,$03,$03,$03,$00,$00,$00,$00,$00,$00,$00],lcd_data
            lcd_data=lcd_data rev 8
            GOSUB SEND_DATA
        NEXT I
        RETURN
        
    '======= SET_XY ====================================================================================
    SET_XY:
        COM = $21 : GOSUB SEND_COMMAND
        COM = X   : GOSUB SEND_COMMAND
        COM = 71  : GOSUB SEND_COMMAND
        COM = $22 : GOSUB SEND_COMMAND
        COM = Y   : GOSUB SEND_COMMAND
        COM = 41  : GOSUB SEND_COMMAND
        RETURN


    Seems that the character size is too big to fit there..
    Originally, this code is made for a larger display so I need to adapt it.
    Roger

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,133


    Did you find this post helpful? Yes | No

    Default Re: SSD1306 OLED (42x40) I2C display from scratch

    Is it worth it the effort to spend much time on this LCD's?

    It tried it and seems a lot of waste of time. Also too small for my age

    Ioannis

  6. #6
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default SSD1306 OLED (72x40) I2C display from scratch

    Up to now, I couldn't find any pixel map for the 72x40 display so I'm relying on information for the 128x64 one.

    This is the information I have found in the 72x40 datasheet:
    Name:  72x40_segments-coms.png
Views: 13497
Size:  11.8 KB

    Can anybody tell me how I should convert this in "pages" if I have to do so? I don't understand how data has to be displayed...

    This is the example from the 128x64's datasheet (to be found in attachment):
    Name:  128x64.png
Views: 13649
Size:  71.4 KB
    Attached Images Attached Images
    Roger

Similar Threads

  1. big char oled display
    By mombasa in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 19th September 2020, 07:02
  2. SSD1306 start display problem
    By harryweb in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 14th December 2016, 19:16
  3. Cannot drive I2C Oled :(
    By elcrcp in forum mel PIC BASIC Pro
    Replies: 21
    Last Post: - 20th August 2016, 12:19
  4. OLED Display Noise problem
    By gunayburak in forum mel PIC BASIC Pro
    Replies: 24
    Last Post: - 5th July 2016, 10:15
  5. Help With OLED Display / 128X64 SSD1306
    By Denner in forum General
    Replies: 6
    Last Post: - 25th May 2013, 15:40

Members who have read this thread : 23

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts