//Author: Vivekanand Dhakane
//updated on: 10 June 2020
//https://oledtutorials.blogspot.com/2020/06/tutorial-2-using-glcd-font-creator-for.html
#include <Wire.h>
#define OLED_I2c_ADDRESS 0x3d// 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// INVERT B/W = 0xA7
#define PAGE_ADDRESSING_MODE 0x02 //
#define Char_Verticle_Pages_Required 2
#define Char_Horizontal_Columns_Required 16
/*
//---- 2LINE CHESS BOARD -----
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 };
//===================
*/
//---------- Big 2 ------------
byte data[] = {0x0D, 0x00, 0x00, 0x00, 0x00, 0x0C, 0xE0, 0x0E, 0xF0,
0x06, 0xF8, 0x03, 0xFC, 0x03, 0xDC, 0x03, 0xCE, 0x03,
0xC7, 0x03, 0xC3, 0x83, 0xC3, 0xFF, 0xC1, 0x7E, 0xC0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
//=================
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++) //(int i = 0; i < Char_Verticle_Pages_Required; i++)
{
setCursor(j, i);
writeData(data[j * Char_Verticle_Pages_Required + 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
}