Teensy 3.2, SSD1306 + Hardware SPI

NuttyMonk

Well-known member
Hi all,

i've been trying to get hardware SPI to work for my SSD1306 (Adafruit Monochrome 1.3" 128x64 OLED graphic display - STEMMA QT / Qwiic) display on a Teensy 3.2 but with no luck. I've been trawling the posts on this forum and looking for alternative pinouts for the 3.2 but with no luck so far. It doesn't help that pins can have different names. Here is the latest code i used.

Code:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <elapsedMillis.h>
#include <EEPROM.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Comment out above, uncomment this block to use hardware SPI
#define OLED_DC     9
#define OLED_CS     10
#define OLED_RESET  14
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &SPI, OLED_DC, OLED_RESET, OLED_CS);

void setup() {
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // the code then goes on to use the display the same as when i was using it with I2C
}

At the moment i'm just trying to get the hardware SPI working so i can cut down on the time it takes to update a screen. Currently it's 35,000 microseconds (uS) for each update, want to cut that down as much as possible while keeping everything working fine.

I have issues with the screen giving odd looking results sometimes as i have a clock running (using elapsedMillis) which updates the screen on each step (currently 4 times a second) but when i try updating the screen based on external input as well (buttons, encoders), the two updates interfere with each other even though they are both updated in the same function. No matter how i deal with this issue, i still want hardware SPI working with the screen to see the differences, and i have spare pins available to do it.

Any advice on the correct pinout or problems i have with my code would be appreciated.

Cheers

NM
 
p.s. i've just realised that i might need to hook up more than 3 pins from the Teensy 3.2 and the SSD1306. I still have enough free pins on the Teensy 3.2 in my project but am even more confused which other pins i should be using (MOSI, MISO etc)

Cheers

NM
 
p.s. i've just realised that i might need to hook up more than 3 pins from the Teensy 3.2 and the SSD1306. I still have enough free pins on the Teensy 3.2 in my project but am even more confused which other pins i should be using (MOSI, MISO etc)

Cheers

NM

You need 5 or 4 pins connected
MOSI
SCK
CS
D/C
Reset (can be optional, depends on library)

The SPI hardware will dictate MOSI & SCK. You pick the rest of the pins from your available GPIO and define them in your code.
 
You need 5 or 4 pins connected
MOSI
SCK
CS
D/C
Reset (can be optional, depends on library)

The SPI hardware will dictate MOSI & SCK. You pick the rest of the pins from your available GPIO and define them in your code.

Am i correct in saying that these are how the pins are labelled on the Teensy 3.2?

MOSI = DOUT (Pin 11)
SCK = SCK (Pin 13)

Can i pick another SCK pin such as pin 14? If so would i have to change my MOSI pin to pin 7? I'd rather not have the LED on pin 13 being used all of the time if possible.

Are there any pins i should/shouldn't use for the other 3? Are there any conflicts which i should be aware of when picking them?

Thanks for your help TeenWolf, this issue has been bugging me all day.

NM
 
Am i correct in saying that these are how the pins are labelled on the Teensy 3.2?

MOSI = DOUT (Pin 11)
SCK = SCK (Pin 13)

Can i pick another SCK pin such as pin 14? If so would i have to change my MOSI pin to pin 7? I'd rather not have the LED on pin 13 being used all of the time if possible.

Are there any pins i should/shouldn't use for the other 3? Are there any conflicts which i should be aware of when picking them?

Thanks for your help TeenWolf, this issue has been bugging me all day.

NM

Yes, that is right for pins 11 and 13.
If there is a way to specify the alternate pin 14 for SCK in the software, then that can be used.
If you chnage one alternate pin that is independant of other optional ALT pins, so DOUT/MOSI can stay on pin 11.
 
Quick look seems the Adafruit lib doesn't include pin changes.

In the Teensy SPI library is this:
Code:
	// Teensy 3.x can use alternate pins for these 3 SPI signals.
	void setMOSI(uint8_t pin);

Do a forum search this went by recently indicating when the pin change should be made for setMOSI() BEFORE the SPI interface begins.

SPI.H already included so: SPI.setMOSI(14);
would be first line to add. Not sure if an overt SPI.begin() is needed AFTER that as in the other posts, or if that is part of the SSD1306 library.
 
Those pins worked a treat. The display updates in 3.4 milliseconds now. It was 35 milliseconds when i was using I2C, Massive difference.

Being able to pick the SPI pins will also help when creating the PCB.

Cheers

NM
 
Back
Top