Last active
June 10, 2020 11:15
-
-
Save vivekanandRdhakane/503906578d78fd222685e2013c87203d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 | |
void setup() { | |
oled_begin(); | |
setCursor(0, 0); | |
writeData(B11001100); | |
writeData(B00110011); | |
} | |
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