int const clk = 0;//9; //D0,SCL,CLK,SCK - Clock - PB0
int const data = 1;//10;//D1,SDA,MOSI - Data - PB1
int const res = 2;//11,RES - Reset - PB2//можно сразу к +
int const dc = 3;//12;DC - Data/Command - PB3
int const cs = 4;//13;CS - Chip Select - PB4
// Initialisation sequence for OLED module
int const InitLen = 23;
unsigned char Init[InitLen] = {
0xAE, // Display off
0xD5, // Set display clock
0x80, // Recommended value
0xA8, // Set multiplex
0x3F,
0xD3, // Set display offset
0x00,
0x40, // Zero start line
0x8D, // Charge pump
0x14,
0x20, // Memory mode
0x02, // Page addressing
0xA1, // 0xA0/0xA1 flip horizontally
0xC8, // 0xC0/0xC8 flip vertically
0xDA, // Set comp ins
0x12,
0x81, // Set contrast
0x7F,
0xD9, // Set pre charge
0xF1,
0xDB, // Set vcom detect
0x40,
0xA6 // Normal (0xA7=Inverse)
};
// Character set - stored in program memory
const uint8_t CharMap[96][6] PROGMEM = {
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x5F, 0x00, 0x00, 0x00 },
{ 0x00, 0x07, 0x00, 0x07, 0x00, 0x00 },
{ 0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00 },
{ 0x24, 0x2A, 0x7F, 0x2A, 0x12, 0x00 },
{ 0x23, 0x13, 0x08, 0x64, 0x62, 0x00 },
{ 0x36, 0x49, 0x56, 0x20, 0x50, 0x00 },
{ 0x00, 0x08, 0x07, 0x03, 0x00, 0x00 },
{ 0x00, 0x1C, 0x22, 0x41, 0x00, 0x00 },
{ 0x00, 0x41, 0x22, 0x1C, 0x00, 0x00 },
{ 0x2A, 0x1C, 0x7F, 0x1C, 0x2A, 0x00 },
{ 0x08, 0x08, 0x3E, 0x08, 0x08, 0x00 },
{ 0x00, 0x80, 0x70, 0x30, 0x00, 0x00 },
{ 0x08, 0x08, 0x08, 0x08, 0x08, 0x00 },
{ 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 },
{ 0x20, 0x10, 0x08, 0x04, 0x02, 0x00 },
{ 0x3E, 0x51, 0x49, 0x45, 0x3E, 0x00 },
{ 0x00, 0x42, 0x7F, 0x40, 0x00, 0x00 },
{ 0x72, 0x49, 0x49, 0x49, 0x46, 0x00 },
{ 0x21, 0x41, 0x49, 0x4D, 0x33, 0x00 },
{ 0x18, 0x14, 0x12, 0x7F, 0x10, 0x00 },
{ 0x27, 0x45, 0x45, 0x45, 0x39, 0x00 },
{ 0x3C, 0x4A, 0x49, 0x49, 0x31, 0x00 },
{ 0x41, 0x21, 0x11, 0x09, 0x07, 0x00 },
{ 0x36, 0x49, 0x49, 0x49, 0x36, 0x00 },
{ 0x46, 0x49, 0x49, 0x29, 0x1E, 0x00 },
{ 0x00, 0x00, 0x14, 0x00, 0x00, 0x00 },
{ 0x00, 0x40, 0x34, 0x00, 0x00, 0x00 },
{ 0x00, 0x08, 0x14, 0x22, 0x41, 0x00 },
{ 0x14, 0x14, 0x14, 0x14, 0x14, 0x00 },
{ 0x00, 0x41, 0x22, 0x14, 0x08, 0x00 },
{ 0x02, 0x01, 0x59, 0x09, 0x06, 0x00 },
{ 0x3E, 0x41, 0x5D, 0x59, 0x4E, 0x00 },
{ 0x7C, 0x12, 0x11, 0x12, 0x7C, 0x00 },
{ 0x7F, 0x49, 0x49, 0x49, 0x36, 0x00 },
{ 0x3E, 0x41, 0x41, 0x41, 0x22, 0x00 },
{ 0x7F, 0x41, 0x41, 0x41, 0x3E, 0x00 },
{ 0x7F, 0x49, 0x49, 0x49, 0x41, 0x00 },
{ 0x7F, 0x09, 0x09, 0x09, 0x01, 0x00 },
{ 0x3E, 0x41, 0x41, 0x51, 0x73, 0x00 },
{ 0x7F, 0x08, 0x08, 0x08, 0x7F, 0x00 },
{ 0x00, 0x41, 0x7F, 0x41, 0x00, 0x00 },
{ 0x20, 0x40, 0x41, 0x3F, 0x01, 0x00 },
{ 0x7F, 0x08, 0x14, 0x22, 0x41, 0x00 },
{ 0x7F, 0x40, 0x40, 0x40, 0x40, 0x00 },
{ 0x7F, 0x02, 0x1C, 0x02, 0x7F, 0x00 },
{ 0x7F, 0x04, 0x08, 0x10, 0x7F, 0x00 },
{ 0x3E, 0x41, 0x41, 0x41, 0x3E, 0x00 },
{ 0x7F, 0x09, 0x09, 0x09, 0x06, 0x00 },
{ 0x3E, 0x41, 0x51, 0x21, 0x5E, 0x00 },
{ 0x7F, 0x09, 0x19, 0x29, 0x46, 0x00 },
{ 0x26, 0x49, 0x49, 0x49, 0x32, 0x00 },
{ 0x03, 0x01, 0x7F, 0x01, 0x03, 0x00 },
{ 0x3F, 0x40, 0x40, 0x40, 0x3F, 0x00 },
{ 0x1F, 0x20, 0x40, 0x20, 0x1F, 0x00 },
{ 0x3F, 0x40, 0x38, 0x40, 0x3F, 0x00 },
{ 0x63, 0x14, 0x08, 0x14, 0x63, 0x00 },
{ 0x03, 0x04, 0x78, 0x04, 0x03, 0x00 },
{ 0x61, 0x59, 0x49, 0x4D, 0x43, 0x00 },
{ 0x00, 0x7F, 0x41, 0x41, 0x41, 0x00 },
{ 0x02, 0x04, 0x08, 0x10, 0x20, 0x00 },
{ 0x00, 0x41, 0x41, 0x41, 0x7F, 0x00 },
{ 0x04, 0x02, 0x01, 0x02, 0x04, 0x00 },
{ 0x40, 0x40, 0x40, 0x40, 0x40, 0x00 },
{ 0x00, 0x03, 0x07, 0x08, 0x00, 0x00 },
{ 0x20, 0x54, 0x54, 0x78, 0x40, 0x00 },
{ 0x7F, 0x28, 0x44, 0x44, 0x38, 0x00 },
{ 0x38, 0x44, 0x44, 0x44, 0x28, 0x00 },
{ 0x38, 0x44, 0x44, 0x28, 0x7F, 0x00 },
{ 0x38, 0x54, 0x54, 0x54, 0x18, 0x00 },
{ 0x00, 0x08, 0x7E, 0x09, 0x02, 0x00 },
{ 0x18, 0xA4, 0xA4, 0x9C, 0x78, 0x00 },
{ 0x7F, 0x08, 0x04, 0x04, 0x78, 0x00 },
{ 0x00, 0x44, 0x7D, 0x40, 0x00, 0x00 },
{ 0x20, 0x40, 0x40, 0x3D, 0x00, 0x00 },
{ 0x7F, 0x10, 0x28, 0x44, 0x00, 0x00 },
{ 0x00, 0x41, 0x7F, 0x40, 0x00, 0x00 },
{ 0x7C, 0x04, 0x78, 0x04, 0x78, 0x00 },
{ 0x7C, 0x08, 0x04, 0x04, 0x78, 0x00 },
{ 0x38, 0x44, 0x44, 0x44, 0x38, 0x00 },
{ 0xFC, 0x18, 0x24, 0x24, 0x18, 0x00 },
{ 0x18, 0x24, 0x24, 0x18, 0xFC, 0x00 },
{ 0x7C, 0x08, 0x04, 0x04, 0x08, 0x00 },
{ 0x48, 0x54, 0x54, 0x54, 0x24, 0x00 },
{ 0x04, 0x04, 0x3F, 0x44, 0x24, 0x00 },
{ 0x3C, 0x40, 0x40, 0x20, 0x7C, 0x00 },
{ 0x1C, 0x20, 0x40, 0x20, 0x1C, 0x00 },
{ 0x3C, 0x40, 0x30, 0x40, 0x3C, 0x00 },
{ 0x44, 0x28, 0x10, 0x28, 0x44, 0x00 },
{ 0x4C, 0x90, 0x90, 0x90, 0x7C, 0x00 },
{ 0x44, 0x64, 0x54, 0x4C, 0x44, 0x00 },
{ 0x00, 0x08, 0x36, 0x41, 0x00, 0x00 },
{ 0x00, 0x00, 0x77, 0x00, 0x00, 0x00 },
{ 0x00, 0x41, 0x36, 0x08, 0x00, 0x00 },
{ 0x02, 0x01, 0x02, 0x04, 0x02, 0x00 },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 }
};
int Line, Column, Scroll;
int thisByte = 33;
void setup() {
//clk = 0;//9; //D0,SCL,CLK,SCK - Clock - PB0
//data = 1;//10;//D1,SDA,MOSI - Data - PB1
//res = 2;//11 - PB2
//dc = 3;//12; //-Data/Command - PB3
//cs = 4;//13;//-Chip Select - PB4
// Define pins
// pinMode(11, OUTPUT); digitalWrite(11,HIGH);//res
// pinMode(12, OUTPUT); digitalWrite(12,HIGH);//dc
// pinMode(9, OUTPUT); digitalWrite(9,HIGH);//clk
// pinMode(10, OUTPUT);//data
// pinMode(13, OUTPUT); digitalWrite(13,HIGH);//cs
PORTB |= (1 << res);/// Set bits - high
DDRB |= (1 << res);// Set bits - output
PORTB &= ~(1 << data);// Clear bit - low
PORTB |= (1 << cs) | (1 << dc) | (1 << clk);/// Set bits - high
DDRB |= (1 << data) | (1 << cs) | (1 << dc) | (1 << clk);// Set bits - output
InitDisplay();
ClearDisplay();
Command(0xAF); // Display on
// Print('q');Print('w');Print('e');Print('r');Print('t');Print('y');
}
void loop() {
//"ASCII Table"
if (thisByte < 126){
Print(thisByte);Print(' ');
thisByte++;
}
delay(1000);
}
// Write a data byte to the display
void Data(uint8_t d) {
PINB = 1<<cs; // cs low
for (uint8_t bit = 0x80; bit; bit >>= 1) {
PINB = 1<<clk; // clk low
if (d & bit) PORTB = PORTB | (1<<data); else PORTB = PORTB & ~(1<<data);
PINB = 1<<clk; // clk high
}
PINB = 1<<cs; // cs high
}
// Write a command byte to the display
void Command(uint8_t c) {
PINB = 1<<dc; // dc low
Data(c);
PINB = 1<<dc; // dc high
}
void InitDisplay () {
for (uint8_t c=0; c<InitLen; c++) Command(Init[c]);
}
// Character terminal **********************************************
void Clear256() {
uint8_t b = 0;
do {
PINB = 1<<clk; // clk low
b++;
PINB = 1<<clk; // clk high
} while (b != 0);
}
// Optimised for fast scrolling
void ClearLine (int line) {
Command(0xB0 + line);
Command(0x00); // Column start low
Command(0x00); // Column start high
PINB = 1<<cs; // cs low
PORTB = PORTB & ~(1<<data); // data low
Clear256(); Clear256(); Clear256(); Clear256();
PINB = 1<<cs; // cs high
}
void ClearDisplay () {
for (uint8_t p=0; p < 8; p++) ClearLine(p);
Line = Scroll;
Column = 0;
}
// Clears the top line, then scrolls the display up by one line
void ScrollDisplay () {
ClearLine(Scroll);
Scroll = (Scroll + 1) & 0x07;
Command(0xD3);
Command(Scroll << 3);
}
// Plots a character; line = 0 to 7; column = 0 to 20
void PlotChar(char c, int line, int column) {
column = column*6;
Command(0xB0 + ((line + Scroll) & 0x07));
Command(0x00 + (column & 0x0F)); // Column start low
Command(0x10 + (column >> 4)); // Column start high
for (uint8_t col = 0 ; col < 6; col++) {
Data(pgm_read_byte(&CharMap[c-32][col]));
}
}
// Prints a character, handling control characters
void Print(char c) {
c = c & 0x7F; // Ignore top bit
if (c >= 32) {
PlotChar(c, Line, Column++);
if (Column > 20) {
Column = 0;
if (Line == 7) ScrollDisplay(); else Line++;
}
}
// Return character
else if (c == 13) {
Column = 0;
if (Line == 7) ScrollDisplay(); else Line++;
}
}