new SSD1306S Library

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
bitfogav
Registered User
Registered User
Posts: 169
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

new SSD1306S Library

Post by bitfogav » Sun Sep 13, 2020 9:12 am

Is there any example code for the new SSD1306S.bas Module just added to the compiler library?...

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: new SSD1306S Library

Post by Jerry Messina » Sun Sep 13, 2020 10:52 am

If you look in the folder 'Samples\NewExamples' there's 'main_GLCD_ssd1306s.bas', which is one of the files I used to test the library.

It's not really a demo program and there's stuff in there that you won't need (like mplab debugging) but it has the basic setup for various hardware configurations.

bitfogav
Registered User
Registered User
Posts: 169
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: new SSD1306S Library

Post by bitfogav » Sun Sep 13, 2020 10:58 am

I looked in the Samples folder but didn't look in the NewExamples folder.. :roll: Thanks Jerry

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: new SSD1306S Library

Post by Jerry Messina » Sun Sep 13, 2020 11:20 am

Some of the setup can be a bit confusing now that the new modules use the standard libs for their interface code,
but at least that way they'll work with any device that the std libs support, and also allow using PPS.

main_GLCD_ST7565RS.bas has an example of setting up hdw using the 27K40, SPI, and PPS.

Let me know if you need any help.

bitfogav
Registered User
Registered User
Posts: 169
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: new SSD1306S Library

Post by bitfogav » Thu Sep 17, 2020 8:25 pm

Thanks for this Jerry I have a 128x64 I2C Oled working like a dream with the module.. Thanks again..

Here is a demo I have been running

Code: Select all

Device = 18F26K22
Clock = 64

// OLED I2C 128x64
//  4   SDA             mssp SDA 
//  3   SCL             mssp SCL  
//  2   5V              VCC
//  1   GND             GND

Include "intosc.bas"            // setup intosc based on 'clock='
#option DIGITALIO_INIT = true   // automatically call setalldigital at startup
Include "setdigitalio.bas"

// select SSD1306 serial mode driver
#option GLCD_MODEL = SSD1306S
// interface selection... I2C, I2C2, SI2C, SPI, SPI2, or SSPI
#option GLCD_INTERFACE = I2C   'I2C2 'SPI2

// set any #options required by the selected interface .bas module.
#if (GLCD_INTERFACE = I2C)
  #option I2C_BAUD = 400
  'include "i2c.bas"                // module name shown for ref only
#elseif (GLCD_INTERFACE = I2C2)
  #option I2C2_SCL = PORTB.1         // K22 SCL2
  #option I2C2_SDA = PORTB.2         // K22 SDA2
  #option I2C2_BAUD = 400
  'include "i2c2.bas"
#elseif (GLCD_INTERFACE = SI2C)
  #option SI2C_SCL = PORTB.1         // K22 SCL2
  #option SI2C_SDA = PORTB.2         // K22 SDA2
  #option SI2C_BAUD = 400
  'include "si2c.bas"
#elseif (GLCD_INTERFACE = SPI)
  'include "spi.bas"
#elseif (GLCD_INTERFACE = SPI2)
  #option SPI2_SCK = PORTB.1         // SPI clock
  #option SPI2_SDO = PORTB.3         // SPI data out
  #option SPI2_SDI = PORTB.2         // SPI data in (not used, remap as GLCD_DC below)
  #option SPI2_DEFAULT_CLOCK = %1010    // use SPI CLK = FOSC/8 **NOT SUPPORTED BY ALL DEVICES**
  'include "spi2.bas"
#elseif (GLCD_INTERFACE = SSPI)
  #option SSPI_SCK = PORTB.1         // SPI clock
  #option SSPI_SDO = PORTB.3         // SPI data out
  #option SSPI_SDI = PORTB.2         // SPI data in (not used, remap as GLCD_DC below)
  'include "sspi.bas"
#endif
// addtl SPI control pins (unused pins will not be initialized)
#option GLCD_DC = PORTB.2       // D/C data/command pin: data=1, command=0
#option GLCD_CS = PORTB.0       // SPI CS chip select pin (active-low)
#option GLCD_RST = PORTA.0      // reset pin (either mode, optional)

// addtl I2C settings
#option GLCD_I2C_ADDR = $78     // ssd1306 DC_SA0 GND (note: this seems to be the default)
'#option GLCD_I2C_ADDR = $7A    // ssd1306 DC_SA0 high, floating (unconnected)

// we'll call GLCD.Initialize (allows for PPS setup)
#option GLCD_INITIALIZE = false
// we're already setting digitalio pin mode here, so it's not required in GLCD
#option GLCD_SETALLDIGITAL = false

#option GLCD_WIDTH = 128            // SSD1306 max 128
#option GLCD_HEIGHT = 64            // SSD1306 max 64
Include "math.bas"
Include "GLCD.bas"
Include "graphics.bas"

Dim i As Byte
Dim x As Byte
Dim c As Byte
Dim y As Float

// start of prog...
GLCD.Initialize()
GLCD.SetContrast()     // default
GLCD.Cls()

// main Loop...
While True
  // lines
  GLCD.Cls()
  For i = 0 To 63 Step 2
    GLCD.Line(0, i, 127, 63-i)
  Next
  For i = 127 To 0 Step -2
    GLCD.Line(i, 0, 127-i, 63)
  Next
  DelayMS(2000)

  // cross lines
  GLCD.Cls()
  GLCD.Rectangle(0, 0, 127, 63)
  For i = 0 To 63 Step +4
    GLCD.Line(0, i, i*2, 63)
  Next
  For i = 0 To 63 Step +4
    GLCD.Line(127, 63-i, 127-(i*2), 0)
  Next
  DelayMS(2000)

  // squares
  GLCD.Cls()
  For i = 0 To 9
    GLCD.Rectangle(i*3, i*3, 127-(i*3), 63-(i*3))
  Next
  DelayMS(2000)

  // circles
  GLCD.Cls()
  x = 0
  For i = 0 To 24
    x = x + 3
    GLCD.Circle(64, 32, x)
    DelayMS(1)
  Next
  DelayMS(2000)

  // sin wave
  GLCD.Cls()
  GLCD.Rectangle(0, 0, 127, 63)
  GLCD.Line(0, 31, 127, 31)
  GLCD.Line(63, 0, 63, 63)
  For c = 0 To 3 
    For i = 0 To 127 
      y=i*0.04974188368183839294232518690191
      GLCD.SetPixel(i, (sin(y)*28)+31)
      DelayMS(10)
    Next
  Next
  DelayMS(2000)
Wend
Youtube link to video demo

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: new SSD1306S Library

Post by Jerry Messina » Fri Sep 18, 2020 11:33 am

Cool video!

Just in case anyone's interested, in many cases you can simplify the test code and just use the defaults for most of the #options.
For a K22 and MSSP1 I2C, that might look something like...

Code: Select all

Device = 18F26K22
Clock = 64

Include "intosc.bas"            // setup intosc based on 'clock='

// select SSD1306 serial mode driver
#option GLCD_MODEL = SSD1306S
// interface selection... I2C, I2C2, SI2C, SPI, SPI2, or SSPI
#option GLCD_INTERFACE = I2C
#option I2C_BAUD = 400
#option GLCD_I2C_ADDR = $78     // ssd1306 DC_SA0 GND (note: this seems to be the default)

#option GLCD_WIDTH = 128            // SSD1306 max 128
#option GLCD_HEIGHT = 64            // SSD1306 max 64
Include "GLCD.bas"
Include "graphics.bas"

Include "math.bas"
That will automatically initialize the GLCD, and as part of that will call SetAllDigital and I2C.Open for you.
If you let it automatically call GLCD.Initialize() as part of the startup, you can/should remove the call in the main program.

Also, FYI...
All the display controllers I've looked at so far are "write-only" when you use the serial I2C/SPI interface.
The GLCD driver uses a good chunk of ram (1K) to act as a display buffer to get around that, so there's still applications where your standalone driver might be a better choice.

Post Reply