//---------------------------------------------------------
/*
Program for writing to Newhaven Display Slim OLEDs based on US2066 controller.
Pick one up today in the Newhaven Display shop!
------> https://newhavendisplay.com/oled-displays/
This code is written for the Arduino Mega. Copyright (c) 2024 - Newhaven Display International, Inc.
Newhaven Display invests time and resources providing this open source code.
Please support Newhaven Display by purchasing products from Newhaven Display!
*/
//---------------------------------------------------------
int DC = 30, E = 31, RES = 32, CS = 33;
//DB4-DB7 connected to pins 26-29 on Arduino MEGA
// R/W signal tied LOW (always write)
unsigned char text1[] = "Newhaven Display----";
unsigned char text2[] = "Slim OLED Display---";
unsigned char text3[] = "4x20 Characters-----";
unsigned char text4[] = "4-bit Parallel------";
void writeNibble(unsigned char nibble) {
PORTA = (PORTA & 0x0F) | (nibble & 0xF0); // Mask and set upper nibble
digitalWrite(E, HIGH);
delayMicroseconds(1);
digitalWrite(E, LOW);
}
void writeByte(unsigned char byte, bool isData) {
digitalWrite(DC, isData ? HIGH : LOW); // Set DC for data/command
writeNibble(byte & 0xF0); // Send upper nibble
writeNibble((byte << 4) & 0xF0); // Send lower nibble
}
void command(unsigned char c) {
writeByte(c, false);
delayMicroseconds(40); // Small delay for command
}
void data(unsigned char d) {
writeByte(d, true);
delayMicroseconds(40); // Small delay for data
}
void output() {
command(0x01);
delay(2);
for (int i = 0; i < 20; i++) data(text1[i]);
command(0xA0);
for (int i = 0; i < 20; i++) data(text2[i]);
command(0xC0);
for (int i = 0; i < 20; i++) data(text3[i]);
command(0xE0);
for (int i = 0; i < 20; i++) data(text4[i]);
}
void setup() {
DDRA = 0xF0; // Configure upper 4 bits of PORTA as output
PORTA = 0x00;
DDRC = 0xFF;
PORTC = 0x00;
pinMode(RES, OUTPUT);
pinMode(DC, OUTPUT);
pinMode(E, OUTPUT);
digitalWrite(RES, HIGH);
delay(10);
command(0x2A);
command(0x71);
data(0x00);
command(0x28);
command(0x08);
command(0x2A);
command(0x79);
command(0xD5);
command(0x70);
command(0x78);
command(0x09);
command(0x06);
command(0x72);
data(0x00);
command(0x2A);
command(0x79);
command(0xDA);
command(0x10);
command(0xDC);
command(0x00);
command(0x81);
command(0x7F);
command(0xD9);
command(0xF1);
command(0xDB);
command(0x40);
command(0x78);
command(0x28);
command(0x01);
command(0x80);
command(0x0C);
delay(100);
}
void loop() {
output();
while(1);
}