SSD1306 oled problems

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Hey! I'm trying to get my ssd1306 oled working with atmega328, but it doesn't do anything. The codes are in the codes.zip

This is the first time I'm using the SPI by directly setting the registers, so I quess there might be some kind of problem with setting the registers.

I've been debugging it the whole day without any luck, hope someone could figure out what's wrong with the code.

 

-Regards Teemu

Attachment(s): 

This topic has a solution.
Last Edited: Fri. Jul 14, 2017 - 10:59 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Since you are using an ATmega328,  I suggest that you start with Arduino libraries and examples for your OLED.

 

You also need to study how to set and clear individual PORT bits using C (or C++).

void oled::command(uint8_t ocom) {
	PORTD |= (1<<cs);           //sets CS bit in PORTD i.e. deselect
	PORTD &= (0<<dc);           //clears complete PORT i.e. select OLED, DC=0
	PORTD &= (0<<cs);           //clears complete PORT i.e. nothing different
	SPDR = ocom;                //start the SPI transfer
	while(!(SPSR & (1<<SPIF))); //wait till transfer is complete
	PORTD |= (1<<cs);           //sets CS bit i.e. deselect OLED
	PORTD |= (1<<dc);           //sets DC bit
}

I suggest that you write two methods:  lcd_cmd(ocom) and lcd_dat(odat)

Then everything builds from these.

 

David.

Last Edited: Thu. Jul 13, 2017 - 10:00 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I had a look at the Arduino SPI/SPI{h,cpp}

 

Arduino sends and receives a byte in the same function call : if you want to reuse your library, this might be an interesting feature

Arduino makes sure SS pin (the true one) is high before SPI starts (to ensure AVRs does not become a slave) ; telibs spi:: begin does not.

It might be surprising one has to wait one millisecond at each SPI transaction (interest of SPI is its speed; at this rate, I2C is way faster....); if you want your library to be general purpose, this is very weird

Each pin manipulation is outside the spi-transfer function, allowing to have multiple spi slaves....

 

edited : read Davi Prentice answer, added  https://github.com/arduino/Arduino/tree/master/hardware/arduino/avr/libraries/SPI/src  adress to make OP's life easier

Last Edited: Thu. Jul 13, 2017 - 12:46 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I suggest that you write two methods:  lcd_cmd(ocom) and lcd_dat(odat)

This is general advice for TFT, LCD controllers.   However the SSD1306 is more forgiving than other controllers.

 

I have got your code working on a SSD1306.   Note that my wiring is different to yours.  i.e. CS on PB2, DC on PB1,

I have used descriptive macros for the bit-twiddling.   Note how you clear a bit in C++.

 

Life is much easier with Arduino.

 

David.

 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wow, thank you! So was the problem only with clearing the bit?

I would use Arduino IDE, but it consumes so much memory with the code made to support any kind of microcontrollers

that I can't do almost anything on it after I finished coding the library

for the SSD1306.

 

-Teemu