Paso 6: ¿Cómo dibujar un pixel?
Ahora que sabemos cómo funciona el OLED, podemos crear un programa que permite al usuario convertir en un píxel específico de la pantalla.
#include //Include Wire library for I2C communication#define HEIGHT 64 #define WIDTH 128 const int RST = 10; //Assign pin 10 for Reset int i; //Set variable i as integer static unsigned char array[1024]; //buffer array/> void setup() { pin_init(); //Initialize pins initialize_OLED(); //Initialize screen memset(array, 0, sizeof(array)); //Clear array draw_pixel(63,31); //Store pixel at (x,y) location Flush(); //Send data } void loop() { //Nothing happens here } void pin_init(){ Serial.begin(9600); //Set baud for serial transmission pinMode(RST, OUTPUT); //Set RST as output } void initialize_OLED(){ Wire.begin(); //Initialize I2C interface digitalWrite(RST, LOW); //Set reset pin low (active) delay(10); //Wait 100 ms digitalWrite(RST, HIGH); //Set reset pin high (inactive) Wire.beginTransmission(0x3D); // Start communication with slave Wire.write(0x00); //Command stream Wire.write(0xAE); //Set display Off Wire.write(0xD5); //Set display clock divide ratio/oscillator frequency Wire.write(0x80); Wire.write(0xA8); //Set multiplex ratio Wire.write(0x3F); Wire.write(0xD3); //Set display offset Wire.write(0x00); Wire.write(0x40); //Set display start line Wire.write(0x8D); //Set charge pump Wire.write(0x14); //VCC generated by internal DC/DC circuit Wire.write(0xA1); //Set segment re-map Wire.write(0xC0); //Set COM output scan direction Wire.write(0xDA); //Set COM pins hardware configuration Wire.write(0x12); Wire.write(0x81); //Set contrast control Wire.write(0xCF); Wire.write(0xD9); //Set pre-changed period Wire.write(0xF1); Wire.write(0xDB); //Set VCOMH Deselected level Wire.write(0x40); Wire.write(0xA4); //Set entire display on/off Wire.write(0xA6); //Set normal display Wire.write(0x20); //Set memory address mode Wire.write(0x00); //Horizontal Wire.write(0xAF); //Set display on Wire.endTransmission(); //End communicaiton with slave } void draw_pixel(int x, int y) if((x<0) || (x>=WIDTH) || (y<0) || (y>=HEIGHT)){ //Check for boundaries return; } else{ array[x+(y/8)*WIDTH] |= _BV((y%8)); //Store pixel in array } } void Flush(){ Wire.beginTransmission(0x3D); //Start communication with slave Wire.write(0x00); //Command stream Wire.write(0x00); //Set lower column start address for page addressing mode Wire.write(0x10); //Set higher column start address for page addressing mode Wire.write(0x40); //Set display start line Wire.endTransmission(); //End communication with slave unsigned char twbrbackup = TWBR; //Two wire bit rate register TWBR = 12; //Set to 400 kHz for(unsigned short q=0; q<(WIDTH*HEIGHT/8); q++){ Wire.beginTransmission(0x3D); //Start communication with slave Wire.write(0x40); //Data stream for(unsigned char w=0; w<16; w++){ Wire.write(array[q]); //Transmit data to be displayed q++; } q--; Wire.endTransmission(); //End communication with slave } TWBR = twbrbackup; }
La biblioteca de alambre permite a Arduino para comunicarse con dispositivos I2C. En la primera parte del código asignar el pin digital ya se ha mencionado a RST y crear un array para los datos que queremos mostrar. Entonces pin RST es inicializado por lo que es una salida. A continuación, se inicializa el OLED siguiendo el ejemplo mostrado en la pantalla hoja de datos en la página 15. Se recomienda que reinicie una vez el dispositivo antes de empezar a utilizarlo. Esto se hace estableciendo primero baja por algún tiempo y luego alta. Ya que este pin es baja activa, debe ser alta durante la operación normal.