Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vivekanandRdhakane/226ab69b26f6d856ff24035534bb2a3f to your computer and use it in GitHub Desktop.
Save vivekanandRdhakane/226ab69b26f6d856ff24035534bb2a3f to your computer and use it in GitHub Desktop.
//Author: Vivekanand Dhakane
//updated on: 10 June 2020
#include <Wire.h>
#define OLED_I2c_ADDRESS 0x3C
// registers Addresses
#define COMMAND_REG 0x80 // B1000 0000
#define DATA_REG 0x40 // B0100 0000
// commands
#define ON_CMD 0xAF
#define NORMAL_DISPLAY_CMD 0xA6
#define PAGE_ADDRESSING_MODE 0x02
byte data[]= {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
void setup() {
oled_begin();
setCursor(0, 0);
//send data in array to display
for(int i=0; i<8;i++)
{
writeData(data[i]);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
void oled_begin()
{
Wire.begin(); // join i2c bus (address optional for master)
turnON();
NormalDisplayMode();
setPageMode();
writeCommand(0x8d); //Charge Pump
writeCommand(0x14);
clearFullDisplay();
}
void writeCommand(byte command) {
Wire.beginTransmission(OLED_I2c_ADDRESS); // begin transmitting (OLED_I2c_ADDRESS=0x3C)
Wire.write(COMMAND_REG); // Command Reg Address (COMMAND_REG =0x80 )
Wire.write(command); // Command to be excuted
Wire.endTransmission(); // stop transmitting
}
void writeData(byte data) {
Wire.beginTransmission(OLED_I2c_ADDRESS); // begin transmitting (OLED_I2c_ADDRESS=0x3C)
Wire.write(DATA_REG); //Data Reg Address (DATA_REG =0x40)
Wire.write(data); //Write data
Wire.endTransmission(); // stop transmitting
}
void turnON() {
writeCommand(ON_CMD);
}
void NormalDisplayMode() {
writeCommand(NORMAL_DISPLAY_CMD);
}
void setPageMode() {
byte addressingMode = PAGE_ADDRESSING_MODE;
writeCommand(0x20); //set addressing mode
writeCommand(PAGE_ADDRESSING_MODE); //set page addressing mode
}
//clearFullDisplay()
// In this function we are setting X,Y=0
// then write data on 128 column and
// then change page and do same till 7th page
void clearFullDisplay() {
for (byte page = 0; page < 8; page++) {
setCursor(0, page);
for (byte column = 0; column < 128; column++) { //clear all columns
writeData(0x00);
}
}
setCursor(0, 0);
}
void setCursor(byte X, byte Y) {
writeCommand(0x00 + (X & 0x0F)); //set column lower address
writeCommand(0x10 + ((X >> 4) & 0x0F)); //set column higher address
writeCommand(0xB0 + Y); //set page address
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment