OLED Hardware SPI pins.

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Ceddy
 
Posts: 2
Joined: Sat Apr 19, 2014 10:03 pm

OLED Hardware SPI pins.

Post by Ceddy »

Hello, I've purchased the 128x32 SPI OLEDhttps://www.adafruit.com/products/661

In the examples it has software SPI(default) pins as 9, 10, 11, 12, 13.

And hardware SPI would be 9, 10, 6, 7, 8. (9,10 carried over from software SPI)

I'm using a Arduino Nano, and by my pinout it seems they might have software and hardware SPI switched in the comments. Am I correct?
(Anyone know what board this was written for?)


Most important, looking at Adafruit_SSD1306.cpp, I'm not sure how hardware SPI is selected versus software SPI.
Can anyone give me some help with this?

Thanks.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: OLED Hardware SPI pins.

Post by adafruit_support_mike »

The hardware SPI pins on an Arduino Uno are 11 (MOSI), 12 (MISO), and 13 (SCK). Pin 10 is often used as CS, but and digital pin can do the same job, so there's no such thing as a 'hardware CS pin'.

The Nano's hardware SPI pins are the same: http://arduino.cc/en/Main/ArduinoBoardNano

The OLED also uses a "data/command" signal, which accounts for the extra pins.

To select hardware or software SPI, you call the initializer with MOSI and SCK pin numbers (or not):

Code: Select all

//  constructor for software SPI - we indicate MOSI, SCLK, DataCommand, Reset, ChipSelect
Adafruit_SSD1306::Adafruit_SSD1306(int8_t SID, int8_t SCLK, int8_t DC, int8_t RST, int8_t CS) : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) {
  cs = CS;
  rst = RST;
  dc = DC;
  sclk = SCLK;
  sid = SID;
  hwSPI = false;
}

// constructor for hardware SPI - we indicate DataCommand, ChipSelect, Reset 
Adafruit_SSD1306::Adafruit_SSD1306(int8_t DC, int8_t RST, int8_t CS) : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) {
  dc = DC;
  rst = RST;
  cs = CS;
  hwSPI = true;
}

Arduprog
 
Posts: 4
Joined: Tue May 27, 2014 2:57 am

Re: OLED Hardware SPI pins.

Post by Arduprog »

Hello,

I'm wondering about the comment in the sample program:

Code: Select all

// If using software SPI (the default case):
#define OLED_MOSI   9
#define OLED_CLK   10
#define OLED_DC    11
#define OLED_CS    12
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

/* Uncomment this block to use hardware SPI
#define OLED_DC     6
#define OLED_CS     7
#define OLED_RESET  8
Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS);
...
I'm using the same OLED like Ceddy with the Arduino pro mini 3.3V with the same SPI-Pinout as the UNO.

I thought the pins 9, 10, 11, 12, 13 are the Hardware SPI (because of the label MISO, MOSI ...), not a software SPI like in the sample code.

Can you give me a clearification?

2)
I want to use a second SPI-LCD. How can I select an output to the wanted display?
I have all pins on the same port except pin 12 (CS) - here I want to use an other Port (like CS_2 or so).
But how can I differ when coding

Code: Select all

display.setcursor(10,50);
display.println(0xDEADBEEF, HEX);
etc. to give that to the right display?
There is no command like "setOLED(1)" or anything else.

Thank you very much in advance and thanks for your very nice products! I really like it much!

Joachim

Ceddy
 
Posts: 2
Joined: Sat Apr 19, 2014 10:03 pm

Re: OLED Hardware SPI pins.

Post by Ceddy »

Joachim,

If you call Adafruit_SSD1306 display() with 3 parameters it will enable hardware spi, and use the default pins for MOSI (=9), CLK (=10), etc.

If you call with 5 parameters it will enable software spi. MOSI, CLK, etc. can any pin you want.

In their example, the software spi pins, just happen to be the default hardware spi pins.

The calling with different number of parameters, I believe is a C++ object oriented thing. It confused me at first.

To have two OLED displays I believe you would have to call Adafruit_SSD1306 display() twice, something like:

Code: Select all

Adafruit_SSD1306 display1(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS1);
Adafruit_SSD1306 display2(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS2);
Having OLED_CS1 and OLED_CS2 #define as different pins.

If you wanted hardware spi, you would just remove OLED_MOSI and OLED_CLK.

To use the two displays you would:

Code: Select all

display1.setcursor(10,50);
display1.println(0xDEADBEEF, HEX);

display2.setcursor(10,50);
display2.println(0xDEADBEEF, HEX);
One issue you may run into with two displays is you will have two frame buffers.
So either 2 x 512 = 1024 bytes ram
or 2 x 1024 = 2048 bytes ram.
Depending on which OLED you have.
RAM might get tight..

Arduprog
 
Posts: 4
Joined: Tue May 27, 2014 2:57 am

Re: OLED Hardware SPI pins.

Post by Arduprog »

Ceddy, thank you for your fast reply.

"..In their example, the software spi pins, just happen to be the default hardware spi pins..."

This is a bit confusing for me. What are then their hardware spi pins 6, 7 and 8?

Thanks a lot for your coding examples. I will try this out and this will surely help me out of the deep water!

By the way: frame buffer is a good point I have to think about. I just want to display text. What is the frame buffer for the OLED128x32?

User avatar
paulwb
 
Posts: 23
Joined: Fri Apr 11, 2014 1:04 am

Re: OLED Hardware SPI pins.

Post by paulwb »

I came across this post while trying to find out how to wire a 128 x 64 OLED for hardware SPI (it is the second Google result for "adafruit oled hardware spi"). I had some trouble connecting the knowledge presented here with what wires should go where. The following may be redundant to most, but it was how my tired brain finally got it:

For hardware SPI with an Adafruit OLED, then:

Code: Select all

  
/* 
For Hardware SPI:
 GND goes to ground
 Vin goes to 5V
 DATA to hardware MOSI = 11
 CLK to hardware SCK = 13
 D/C to 6 (user choice, defined below)
 CS to 7 (user choice, defined below)
 RST to 8 (user choice, defined below)
 Hardware MISO (12) unused.
 */

[...]

//For Hardware SPI
#define OLED_DC     6
#define OLED_CS     7
#define OLED_RESET  8
Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS);
Let me know if I made a mistake and I'll edit my post.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: OLED Hardware SPI pins.

Post by adafruit_support_mike »

Arduprog wrote:This is a bit confusing for me. What are then their hardware spi pins 6, 7 and 8?
Technically those aren't hardware SPI.

The only three pins that count as true 'hardware SPI' are MOSI, MISO, and SCK. All the others are either optional or can't be done in hardware.

The CS pin can't be handled in hardware, for instance, because you need a separate CS signal for every SPI device on the bus. Sending CS LOW is how you tell a specific device to listen to what's happening on MOSI and MISO, so you can't handle multiple devices with a 'hardware CS' pin. The Data/Command signal is optional, as while the Reset signal is common, it isn't strictly necessary.

More importantly, putting those signals in hardware wouldn't do any good. They don't have timing or speed requirements like MOSI, MISO, and SCK, and you don't have to write a lot of code to make them work. Any digital pin can do any of those jobs, and you can send them HIGH and LOW in your code as efficiently as calling for the hardware to do it.
Arduprog wrote:By the way: frame buffer is a good point I have to think about. I just want to display text. What is the frame buffer for the OLED128x32?
The display doesn't know the difference between text and graphics. All it knows is that it has an array that contains 4096 pixels and the code tells it to turn various sets of them on and off.

The framebuffer contains one bit for each pixel, for a total of 512 bytes.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”