Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Oled spi in C

Status
Not open for further replies.
The hardware SPI interface is running at 4-MHz now with just a minor change to the code...

C:
/************************************************************************
 *   main setup                                                         *
 ************************************************************************/
   void main(void)
   { ANSELA = 0;                    // Analog off (digital I/O)
     ANSELC = 0;                    //  "
     TRISA = 0;                     //
     TRISC = 0;                     //
     OSCCON = 0b01111000;           // INTOSC 16-MHz
 /*                                                                     *
  *  initialize MSSP module SPI mode 0 (CKE=1/CKP=0, Fosc/4)            *
  *                                                                     */
     SSP1STAT = 0x40;               // CKE = 1
     SSP1CON1 = 0x20;               // SPI Master, Fosc/4, CKP = 0
 
Last edited:
Research suggests, and experiments seem to confirm, that we can trim the initialization sequence by taking advantage of SSD1306 controller defaults. The SSD1306 default display orientation with these 0.96" displays is connector at bottom of display. All of the examples I've seen use the 'A1' and 'C8' commands to flip the orientation on these displays to connector at top of display.

C:
/************************************************************************
 *   init command sequence for 0.96" SSD1306 SPI 128x64 OLED display    *
 ************************************************************************/
   const unsigned char ssdcfg[] = {
  // 0xAE,                  // oled_displayoff . . . . . . . (default)
  // 0xD5, 0x80,            // oled_displayclockdiv & param  (default)
  // 0xA8, 0x3F,            // oled_setmultiplex & param . . (default)
  // 0xD3, 0x00,            // oled_setdisplayoffset & param (default)
  // 0x40,                  // oled_setstartline . . . . . . (default)
     0x8D, 0x14,            // oled_chargepump & param
     0x20, 0x00,            // oled_memorymode 'horizontal'
  // 0xA1,                  // oled_segremap (A0/A1, flip horizontally)
  // 0xC8,                  // oled_comscandec (C0/C8, flip vertically)
  // 0xDA, 0x12,            // oled_setcompins  . . . . . .  (default)
  // 0x81, 0x7F,            // oled_setcontrast, 0-255 . . . (default)
     0xD9, 0xF1,            // oled_setprecharge
     0xDB, 0x40,            // oled_setvcomdetect
  // 0xA4,                  // oled_displayallon_resume . .  (default)
  // 0xA6,                  // oled_normaldisplay . . . . .  (default)
     0xAF                   // oled_displayon
   };                       //

OLED 0.96 SPI #2.png
 
Last edited:
Just received a pair of 0.96" OLED I2C displays and I tested them on a 12F1840 (below) using hardware (MSSP) I2C... I love these displays...

OLED 0.96 I2C #1.png
 
Last edited:
The PCB labels are upside down again :D

I presume you know that you can print the other way up just as easily?, it's purely a bit mapped display.

Handy displays I2C, as you need so few pins - while SPI is faster, it's fairly 'pin hungry'.
 
The PCB labels are upside down again :D
Yeah... you can initialize the display either way (oriented with connectors at top or bottom)...
I presume you know that you can print the other way up just as easily?, it's purely a bit mapped display.
Yes, Sir... Bit mapped graphics and I are old friends, even before authoring the firmware for the original EPG (Electronic Program Guide) back in the early 80's, which evolved over several years to become The TV Guide Channel.
Handy displays I2C, as you need so few pins - while SPI is faster, it's fairly 'pin hungry'.
While SPI code is smaller and faster, I really like the I2C two pin interface and it's relatively easy to mitigate speed issues with carefully fine-tuned software if speed becomes an issue. For example, it would be a breeze to re-design my 50-MHz Frequency Counter using one of these displays and a little 8-pin PIC...

Cheerful regards, Mike, K8LH
 
Last edited:
Mike, K8LH could you zip the whole code up I'd like to try this with xc8 this new forum kind of hard on the code you posted it show bit's and pieces
 
I only posted bits-n'-pieces. Sorry! Also, the code is kinda' crude... Which code do you want? I've got XC8 demo code for;

() 12F1840 + I2C 0.91" 128x32 OLED display (MPLAB 8.92)
() 12F1840 + I2C 0.96" 128x64 OLED display (MPLAB 8.92)
() 16F15325 + I2C 0.91" 128x32 OLED display (10x14 font demo only)(MPLAB X)

I purchased a few of these displays when they went on sale on AliExpress this last month. The little 0.91" 128x32 I2C OLED displays cost $1.71 each while the 0.96" 128x64 I2C OLED displays cost $1.81 each. I also ordered a 1.3" 128x64 OLED display for $3.03 which uses the SH1106 controller rather than the SSD1306. This one should be interesting because the SH1106 I2C interface supports 'reading' OLED display memory which could potentially eliminate the need for a 1024 byte buffer in the uC for certain types of graphic operations (slow as that may be).
 
Last edited:
() 12F1840 + I2C 0.96" 128x64 OLED display (MPLAB 8.92)
What you talking about you write great code and so does the rest. Me I'm still hacking lol getting better tho.
I got sick the meds I think mess with my thinking but at my money making job I can wire with my eyes closed. Landed a good job lot's of led lighting love the stuff it using a tcs witch is really PLC it plays the light's and run's the car washer plus lot's of motor VFD love this stuff.
 
Wow, sounds like a fun job... Here's the MPLAB 8.92 XC8 source for 12F1840 + 0.96" 128x64 I2C OLED display. Hope it helps...
 

Attachments

  • 12F1840_I2C_v1(128x64).c
    26 KB · Views: 207
Last edited:
slightly off-topic but... I really like these little mini solderless breadboards and this AliExpress vendor has them on sale at a very nice price... Similar breadboards from SparkFun or AdaFruit are around $4 each (ouch)...

mini breadboards.png
 
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top