Created
June 20, 2020 08:36
-
-
Save vivekanandRdhakane/005ea9ab23964aa88758f3fe9f214659 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 | |
#define Char_Verticle_Pages_Required 2 | |
#define Char_Horizontal_Columns_Required 16 | |
byte data[] = {0x33, 0x33, 0x33, 0x33, 0xCC, 0xCC, 0xCC, | |
0xCC, 0x33, 0x33, 0x33, 0x33, 0xCC, 0xCC, | |
0xCC, 0xCC, 0x33, 0x33, 0x33, 0x33, 0xCC, | |
0xCC, 0xCC, 0xCC, 0x33, 0x33, 0x33, 0x33, | |
0xCC, 0xCC, 0xCC, 0xCC }; | |
void setup() { | |
oled_begin(); | |
setCursor(0, 0); | |
//send data in array to display | |
for (int j = 0; j < Char_Horizontal_Columns_Required; j++) | |
{ | |
for (int i = 0; i < Char_Verticle_Pages_Required; i++) | |
{ | |
setCursor(j,i); | |
writeData(data[j*Char_Verticle_Pages_Required+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