Created
July 14, 2017 22:03
-
-
Save HectorTorres/33d8f61c221a16db8bd1c5b05cf5a3e6 to your computer and use it in GitHub Desktop.
Arduino OLED display tutorial
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
/==========================================================// | |
// // | |
// OLED ssd1316 Test Code // | |
// Adafruit | |
// // | |
//==========================================================// | |
#include <avr/pgmspace.h> | |
#include <Wire.h> | |
//---------------Datos de la fuente y los graficos----------// | |
#include "data.h" | |
//==========================================================// | |
// Dirección I2C de la pantalla | |
#define OLED_address 0x3c | |
void setup() | |
{ | |
Wire.begin(); // Inicializa comunicación I2C | |
init_OLED(); // Inicializa el modulo OLED | |
reset_display(); // Resetea a la pantalla | |
} | |
// Loop principal | |
//==========================================================// | |
void loop() | |
{ | |
clear_display(); //Limpia la pantalla | |
sendCharXY('A',0,0); //Escribe un caracter en la posicion fila 0, columna 0. | |
sendCharXY('B',0,1); | |
sendCharXY('C',0,2); | |
sendCharXY('D',0,3); | |
sendCharXY('E',0,4); | |
sendCharXY('F',0,5); | |
sendCharXY('G',0,6); | |
sendCharXY('H',0,7); | |
sendCharXY('I',0,8); | |
sendCharXY('J',0,9); | |
sendCharXY('K',0,10); | |
sendCharXY('L',0,11); | |
sendCharXY('L',0,12); | |
sendCharXY('M',0,13); | |
sendCharXY('N',0,14); | |
sendCharXY('O',0,15); | |
sendCharXY('1',1,0); // Escribe un caracter en la posicion fila 1 columan 0 | |
sendCharXY('2',1,1); | |
sendCharXY('3',1,2); | |
sendCharXY('4',1,3); | |
sendCharXY('5',1,4); | |
sendCharXY('6',1,5); | |
sendCharXY('7',1,6); | |
sendCharXY('8',1,7); | |
sendCharXY('9',1,8); | |
sendCharXY('"',1,9); | |
sendCharXY('!',1,10); | |
sendCharXY('$',1,11); | |
sendCharXY('%',1,12); | |
sendCharXY('/',1,13); | |
sendCharXY('(',1,14); | |
sendCharXY(')',1,15); | |
sendStrXY("Herramientas Tec",2,0); // Escribe una cadena de caracteres en la fila 2 y columna 0 | |
sendStrXY("hetpro-store.com",3,0); | |
sendStrXY("Marcelino Garcia B.",4,0); | |
sendStrXY("1615-A, Guadalajara",5,0); | |
sendCharXY('A',7,0); | |
sendCharXY('B',7,1); | |
sendCharXY('C',7,2); | |
sendCharXY('D',7,3); | |
sendCharXY('E',7,4); | |
sendCharXY('F',7,5); | |
sendCharXY('G',7,6); | |
sendCharXY('H',7,7); | |
sendCharXY('I',7,8); | |
sendCharXY('J',7,9); | |
sendCharXY('K',7,10); | |
sendCharXY('L',7,11); | |
sendCharXY('L',7,12); | |
sendCharXY('M',7,13); | |
sendCharXY('N',7,14); | |
sendCharXY('O',7,15); | |
delay(4000); // Retardo de 4 segundos | |
reset_display(); // Reseteo del display | |
} | |
//==========================================================// | |
// Resets display depending on the actual mode. | |
static void reset_display(void) | |
{ | |
displayOff(); | |
clear_display(); | |
displayOn(); | |
} | |
//==========================================================// | |
// Turns display on. | |
void displayOn(void) | |
{ | |
sendcommand(0xaf); //display on | |
} | |
//==========================================================// | |
// Turns display off. | |
void displayOff(void) | |
{ | |
sendcommand(0xae); //display off | |
} | |
//==========================================================// | |
// Clears the display by sendind 0 to all the screen map. | |
static void clear_display(void) | |
{ | |
unsigned char i,k; | |
for(k=0;k<8;k++) | |
{ | |
setXY(k,0); | |
{ | |
for(i=0;i<128;i++) //clear all COL | |
{ | |
SendChar(0); //clear all COL | |
//delay(10); | |
} | |
} | |
} | |
} | |
//==========================================================// | |
static void printBigTime(char *string) | |
{ | |
int Y; | |
int lon = strlen(string); | |
if(lon == 3) { | |
Y = 0; | |
} else if (lon == 2) { | |
Y = 3; | |
} else if (lon == 1) { | |
Y = 6; | |
} | |
int X = 2; | |
while(*string) | |
{ | |
printBigNumber(*string, X, Y); | |
Y+=3; | |
X=2; | |
setXY(X,Y); | |
*string++; | |
} | |
} | |
//==========================================================// | |
// Prints a display big number (96 bytes) in coordinates X Y, | |
// being multiples of 8. This means we have 16 COLS (0-15) | |
// and 8 ROWS (0-7). | |
static void printBigNumber(char string, int X, int Y) | |
{ | |
setXY(X,Y); | |
int salto=0; | |
for(int i=0;i<96;i++) | |
{ | |
if(string == ' ') { | |
SendChar(0); | |
} else | |
SendChar(pgm_read_byte(bigNumbers[string-0x30]+i)); | |
if(salto == 23) { | |
salto = 0; | |
X++; | |
setXY(X,Y); | |
} else { | |
salto++; | |
} | |
} | |
} | |
//==========================================================// | |
// Actually this sends a byte, not a char to draw in the display. | |
// Display's chars uses 8 byte font the small ones and 96 bytes | |
// for the big number font. | |
static void SendChar(unsigned char data) | |
{ | |
Wire.beginTransmission(OLED_address); // begin transmitting | |
Wire.write(0x40);//data mode | |
Wire.write(data); | |
Wire.endTransmission(); // stop transmitting | |
} | |
//==========================================================// | |
// Prints a display char (not just a byte) in coordinates X Y, | |
// being multiples of 8. This means we have 16 COLS (0-15) | |
// and 8 ROWS (0-7). | |
static void sendCharXY(unsigned char data, int X, int Y) | |
{ | |
setXY(X, Y); | |
Wire.beginTransmission(OLED_address); // begin transmitting | |
Wire.write(0x40);//data mode | |
for(int i=0;i<8;i++) | |
Wire.write(pgm_read_byte(myFont[data-0x20]+i)); | |
Wire.endTransmission(); // stop transmitting | |
} | |
//==========================================================// | |
// Used to send commands to the display. | |
static void sendcommand(unsigned char com) | |
{ | |
Wire.beginTransmission(OLED_address); //begin transmitting | |
Wire.write(0x80); //command mode | |
Wire.write(com); | |
Wire.endTransmission(); // stop transmitting | |
} | |
//==========================================================// | |
// Set the cursor position in a 16 COL * 8 ROW map. | |
static void setXY(unsigned char row,unsigned char col) | |
{ | |
sendcommand(0xb0+row); //set page address | |
sendcommand(0x00+(8*col&0x0f)); //set low col address | |
sendcommand(0x10+((8*col>>4)&0x0f)); //set high col address | |
} | |
//==========================================================// | |
// Prints a string regardless the cursor position. | |
static void sendStr(unsigned char *string) | |
{ | |
unsigned char i=0; | |
while(*string) | |
{ | |
for(i=0;i<8;i++) | |
{ | |
SendChar(pgm_read_byte(myFont[*string-0x20]+i)); | |
} | |
*string++; | |
} | |
} | |
//==========================================================// | |
// Prints a string in coordinates X Y, being multiples of 8. | |
// This means we have 16 COLS (0-15) and 8 ROWS (0-7). | |
static void sendStrXY( char *string, int X, int Y) | |
{ | |
setXY(X,Y); | |
unsigned char i=0; | |
while(*string) | |
{ | |
for(i=0;i<8;i++) | |
{ | |
SendChar(pgm_read_byte(myFont[*string-0x20]+i)); | |
} | |
*string++; | |
} | |
} | |
//==========================================================// | |
// Inits OLED SSD1316 and draws logo at startup | |
static void init_OLED(void) | |
{ | |
sendcommand(0xae); //display off | |
sendcommand(0xa6); //Set Normal Display (default) | |
// Adafruit Init sequence for 128x64 OLED module | |
sendcommand(0xAE); //DISPLAYOFF | |
sendcommand(0xD5); //SETDISPLAYCLOCKDIV | |
sendcommand(0x80); // the suggested ratio 0x80 | |
sendcommand(0xA8); //SSD1306_SETMULTIPLEX | |
sendcommand(0x3F); | |
sendcommand(0xD3); //SETDISPLAYOFFSET | |
sendcommand(0x0); //no offset | |
sendcommand(0x40 | 0x0); //SETSTARTLINE | |
sendcommand(0x8D); //CHARGEPUMP | |
sendcommand(0x14); | |
sendcommand(0x20); //MEMORYMODE | |
sendcommand(0x00); //0x0 act like ks0108 | |
sendcommand(0xA0 | 0x1); //SEGREMAP //Rotate screen 180 deg | |
//sendcommand(0xA0); | |
sendcommand(0xC8); //COMSCANDEC Rotate screen 180 Deg | |
//sendcommand(0xC0); | |
sendcommand(0xDA); //0xDA | |
sendcommand(0x12); //COMSCANDEC | |
sendcommand(0x81); //SETCONTRAS | |
sendcommand(0xCF); // | |
sendcommand(0xd9); //SETPRECHARGE | |
sendcommand(0xF1); | |
sendcommand(0xDB); //SETVCOMDETECT | |
sendcommand(0x40); | |
sendcommand(0xA4); //DISPLAYALLON_RESUME | |
sendcommand(0xA6); //NORMALDISPLAY | |
clear_display(); | |
sendcommand(0x2e); // stop scroll | |
//----------------------------REVERSE comments----------------------------// | |
// sendcommand(0xa0); //seg re-map 0->127(default) | |
// sendcommand(0xa1); //seg re-map 127->0 | |
// sendcommand(0xc8); | |
// delay(1000); | |
//----------------------------REVERSE comments----------------------------// | |
// sendcommand(0xa7); //Set Inverse Display | |
// sendcommand(0xae); //display off | |
sendcommand(0x20); //Set Memory Addressing Mode | |
sendcommand(0x00); //Set Memory Addressing Mode ab Horizontal addressing mode | |
// sendcommand(0x02); // Set Memory Addressing Mode ab Page addressing mode(RESET) | |
setXY(0,0); | |
/* | |
for(int i=0;i<128*8;i++) // show 128* 64 Logo | |
{ | |
SendChar(pgm_read_byte(logo+i)); | |
} | |
*/ | |
sendcommand(0xaf); //display on | |
} |
Can you please share data.h header file.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what is the similar code for 128x32 OLED (including header file)