void ArduiPi_OLED::begin( void ) { uint8_t multiplex; uint8_t chargepump; uint8_t compins; uint8_t contrast; uint8_t precharge; constructor(oled_width, oled_height); // Setup reset pin direction (used by both SPI and I2C) if (rst >= 0) { bcm2835_gpio_fsel(rst, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_write(rst, HIGH); // VDD (3.3V) goes high at start, lets just chill for a ms usleep(1000); // bring reset low bcm2835_gpio_write(rst, LOW); // wait 10ms usleep(10000); // bring out of reset bcm2835_gpio_write(rst, HIGH); } // depends on OLED type configuration if (oled_height == 32) { multiplex = 0x1F; compins = 0x02; contrast = 0x8F; } else { #ifdef SEEED_I2C if (oled_type == OLED_SEEED_I2C_96x96 ) { multiplex = 0x5F; compins = 0x12; contrast = 0x53; } else // So 128x64 #endif { multiplex = 0x3F; compins = 0x12; if (oled_type == OLED_SH1106_I2C_128x64) contrast = 0x80; else contrast = (vcc_type==SSD_External_Vcc?0x9F:0xCF); } } if (vcc_type == SSD_External_Vcc) { chargepump = 0x10; precharge = 0x22; } else { chargepump = 0x14; precharge = 0xF1; } #ifdef SEEED_I2C if (oled_type == OLED_SEEED_I2C_96x96 ) sendCommand(SSD1327_Set_Command_Lock, 0x12); // Unlock OLED driver IC MCU interface from entering command. i.e: Accept commands #endif sendCommand(SSD_Display_Off); sendCommand(SSD_Set_Muliplex_Ratio, multiplex); #ifdef SEEED_I2C if (oled_type == OLED_SEEED_I2C_96x96 ) { sendCommand(SSD1327_Set_Display_Clock_Div, 0x01); sendCommand(SSD1327_Set_Display_Start_Line , 0 ); sendCommand(SSD1327_Set_Display_Offset, 96 ); sendCommand(SSD_Set_Segment_Remap , 0x46); sendCommand(0xAB); // set vdd internal sendCommand(0x01); // sendCommand(0xB1); // Set Phase Length sendCommand(0X51); // sendCommand(0xB9); // sendCommand(0xBC); // set pre_charge voltage/VCOMH sendCommand(0x08); // (0x08); sendCommand(0xBE); // set VCOMH sendCommand(0X07); // (0x07); sendCommand(0xB6); // Set second pre-charge period sendCommand(0x01); // sendCommand(0xD5); // enable second precharge and enternal vsl sendCommand(0X62); // (0x62); // Set Normal Display Mode sendCommand(SSD1327_Normal_Display); // Row Address // Start 0 End 95 sendCommand(SSD1327_Set_Row_Address, 0, 95); // Column Address // Start from 8th Column of driver IC. This is 0th Column for OLED // End at (8 + 47)th column. Each Column has 2 pixels(segments) sendCommand(SSD1327_Set_Column_Address, 8, 0x37 ); // Map to horizontal mode sendCommand(0xA0); // remap to sendCommand(0x46); // Vertical mode // Init gray level for text. Default:Brightest White grayH= 0xF0; grayL= 0x0F; } else #endif // SEEED if (oled_type == OLED_SH1106_I2C_128x64) { sendCommand(SSD1306_Set_Lower_Column_Start_Address|0x02); /*set lower column address*/ sendCommand(SSD1306_Set_Higher_Column_Start_Address); /*set higher column address*/ sendCommand(SSD1306_Set_Start_Line); /*set display start line*/ sendCommand(SH1106_Set_Page_Address); /*set page address*/ sendCommand(SSD_Set_Segment_Remap|0x01); /*set segment remap*/ sendCommand(SSD1306_Normal_Display); /*normal / reverse*/ sendCommand(0xad); /*set charge pump enable*/ sendCommand(0x8b); /*external VCC */ sendCommand(0x30); /*0X30---0X33 set VPP 9V liangdu!!!!*/ sendCommand(SSD1306_Set_Com_Output_Scan_Direction_Remap); /*Com scan direction*/ sendCommand(SSD1306_Set_Display_Offset); /*set display offset*/ sendCommand(0x00); /* 0x20 */ sendCommand(SSD1306_Set_Display_Clock_Div); /*set osc division*/ sendCommand(0x80); sendCommand(SSD1306_Set_Precharge_Period); /*set pre-charge period*/ sendCommand(0x1f); /*0x22*/ sendCommand(SSD1306_Set_Com_Pins); /*set COM pins*/ sendCommand(0x12); sendCommand(SSD1306_Set_Vcomh_Deselect_Level); /*set vcomh*/ sendCommand(0x40); } else { sendCommand(SSD1306_Charge_Pump_Setting, chargepump); sendCommand(SSD1306_Set_Memory_Mode, 0x00); // 0x20 0x0 act like ks0108 sendCommand(SSD1306_Set_Display_Clock_Div, 0x80); // 0xD5 + the suggested ratio 0x80 sendCommand(SSD1306_Set_Display_Offset, 0x00); // no offset sendCommand(SSD1306_Set_Start_Line | 0x0); // line #0 // use this two commands to flip display sendCommand(SSD_Set_Segment_Remap | 0x1); sendCommand(SSD1306_Set_Com_Output_Scan_Direction_Remap); sendCommand(SSD1306_Set_Com_Pins, compins); sendCommand(SSD1306_Set_Precharge_Period, precharge); sendCommand(SSD1306_Set_Vcomh_Deselect_Level, 0x40); // 0x40 -> unknown value in datasheet sendCommand(SSD1306_Entire_Display_Resume); sendCommand(SSD1306_Normal_Display); // 0xA6 // Reset to default value in case of // no reset pin available on OLED, sendCommand( SSD_Set_Column_Address, 0, 127 ); sendCommand( SSD_Set_Page_Address, 0, 7 ); } sendCommand(SSD_Set_ContrastLevel, contrast); stopscroll(); // Empty uninitialized buffer clearDisplay(); // turn on oled panel sendCommand(SSD_Display_On); // wait 100ms usleep(100000); }
void Adafruit_SSD1306::begin(uint8_t vccstate, uint8_t i2caddr, bool reset) { _vccstate = vccstate; _i2caddr = i2caddr; // set pin directions if (sid != -1){ pinMode(dc, OUTPUT); pinMode(cs, OUTPUT); csport = portOutputRegister(digitalPinToPort(cs)); cspinmask = digitalPinToBitMask(cs); dcport = portOutputRegister(digitalPinToPort(dc)); dcpinmask = digitalPinToBitMask(dc); if (!hwSPI){ // set pins for software-SPI pinMode(sid, OUTPUT); pinMode(sclk, OUTPUT); clkport = portOutputRegister(digitalPinToPort(sclk)); clkpinmask = digitalPinToBitMask(sclk); mosiport = portOutputRegister(digitalPinToPort(sid)); mosipinmask = digitalPinToBitMask(sid); } if (hwSPI){ SPI.begin (); #ifdef __SAM3X8E__ SPI.setClockDivider (9); // 9.3 MHz #else SPI.setClockDivider (SPI_CLOCK_DIV2); // 8 MHz #endif } } else { // I2C Init Wire.begin(); #ifdef __SAM3X8E__ // Force 400 KHz I2C, rawr! (Uses pins 20, 21 for SDA, SCL) TWI1->TWI_CWGR = 0; TWI1->TWI_CWGR = ((VARIANT_MCK / (2 * 400000)) - 4) * 0x101; #endif } if (reset && rst != -1) { // Setup reset pin direction (used by both SPI and I2C) pinMode(rst, OUTPUT); digitalWrite(rst, HIGH); // VDD (3.3V) goes high at start, lets just chill for a ms delay(1); // bring reset low digitalWrite(rst, LOW); // wait 10ms delay(10); // bring out of reset digitalWrite(rst, HIGH); // turn on VCC (9V?) } #if defined SSD1306_128_32 // Init sequence for 128x32 OLED module ssd1306_command(SSD1306_DISPLAYOFF); // 0xAE ssd1306_command(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5 ssd1306_command(0x80); // the suggested ratio 0x80 ssd1306_command(SSD1306_SETMULTIPLEX); // 0xA8 ssd1306_command(0x1F); ssd1306_command(SSD1306_SETDISPLAYOFFSET); // 0xD3 ssd1306_command(0x0); // no offset ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0 ssd1306_command(SSD1306_CHARGEPUMP); // 0x8D if (vccstate == SSD1306_EXTERNALVCC) { ssd1306_command(0x10); } else { ssd1306_command(0x14); } ssd1306_command(SSD1306_MEMORYMODE); // 0x20 ssd1306_command(0x00); // 0x0 act like ks0108 ssd1306_command(SSD1306_SEGREMAP | 0x1); ssd1306_command(SSD1306_COMSCANDEC); ssd1306_command(SSD1306_SETCOMPINS); // 0xDA ssd1306_command(0x02); ssd1306_command(SSD1306_SETCONTRAST); // 0x81 ssd1306_command(0x8F); ssd1306_command(SSD1306_SETPRECHARGE); // 0xd9 if (vccstate == SSD1306_EXTERNALVCC) { ssd1306_command(0x22); } else { ssd1306_command(0xF1); } ssd1306_command(SSD1306_SETVCOMDETECT); // 0xDB ssd1306_command(0x40); ssd1306_command(SSD1306_DISPLAYALLON_RESUME); // 0xA4 ssd1306_command(SSD1306_NORMALDISPLAY); // 0xA6 #endif #if defined SSD1306_128_64 // Init sequence for 128x64 OLED module ssd1306_command(SSD1306_DISPLAYOFF); // 0xAE ssd1306_command(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5 ssd1306_command(0x80); // the suggested ratio 0x80 ssd1306_command(SSD1306_SETMULTIPLEX); // 0xA8 ssd1306_command(0x3F); ssd1306_command(SSD1306_SETDISPLAYOFFSET); // 0xD3 ssd1306_command(0x0); // no offset ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0 ssd1306_command(SSD1306_CHARGEPUMP); // 0x8D if (vccstate == SSD1306_EXTERNALVCC) { ssd1306_command(0x10); } else { ssd1306_command(0x14); } ssd1306_command(SSD1306_MEMORYMODE); // 0x20 ssd1306_command(0x00); // 0x0 act like ks0108 ssd1306_command(SSD1306_SEGREMAP | 0x1); ssd1306_command(SSD1306_COMSCANDEC); ssd1306_command(SSD1306_SETCOMPINS); // 0xDA ssd1306_command(0x12); ssd1306_command(SSD1306_SETCONTRAST); // 0x81 if (vccstate == SSD1306_EXTERNALVCC) { ssd1306_command(0x9F); } else { ssd1306_command(0xCF); } ssd1306_command(SSD1306_SETPRECHARGE); // 0xd9 if (vccstate == SSD1306_EXTERNALVCC) { ssd1306_command(0x22); } else { ssd1306_command(0xF1); } ssd1306_command(SSD1306_SETVCOMDETECT); // 0xDB ssd1306_command(0x40); ssd1306_command(SSD1306_DISPLAYALLON_RESUME); // 0xA4 ssd1306_command(SSD1306_NORMALDISPLAY); // 0xA6 #endif #if defined SSD1306_96_16 // Init sequence for 96x16 OLED module ssd1306_command(SSD1306_DISPLAYOFF); // 0xAE ssd1306_command(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5 ssd1306_command(0x80); // the suggested ratio 0x80 ssd1306_command(SSD1306_SETMULTIPLEX); // 0xA8 ssd1306_command(0x0F); ssd1306_command(SSD1306_SETDISPLAYOFFSET); // 0xD3 ssd1306_command(0x00); // no offset ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0 ssd1306_command(SSD1306_CHARGEPUMP); // 0x8D if (vccstate == SSD1306_EXTERNALVCC) { ssd1306_command(0x10); } else { ssd1306_command(0x14); } ssd1306_command(SSD1306_MEMORYMODE); // 0x20 ssd1306_command(0x00); // 0x0 act like ks0108 ssd1306_command(SSD1306_SEGREMAP | 0x1); ssd1306_command(SSD1306_COMSCANDEC); ssd1306_command(SSD1306_SETCOMPINS); // 0xDA ssd1306_command(0x2); //ada x12 ssd1306_command(SSD1306_SETCONTRAST); // 0x81 if (vccstate == SSD1306_EXTERNALVCC) { ssd1306_command(0x10); } else { ssd1306_command(0xAF); } ssd1306_command(SSD1306_SETPRECHARGE); // 0xd9 if (vccstate == SSD1306_EXTERNALVCC) { ssd1306_command(0x22); } else { ssd1306_command(0xF1); } ssd1306_command(SSD1306_SETVCOMDETECT); // 0xDB ssd1306_command(0x40); ssd1306_command(SSD1306_DISPLAYALLON_RESUME); // 0xA4 ssd1306_command(SSD1306_NORMALDISPLAY); // 0xA6 #endif if (reset && rst == -1) { ssd1306_command( 0x21, 0, 127 ); ssd1306_command( 0x22, 0, 7 ); stopscroll(); } ssd1306_command(SSD1306_DISPLAYON);//--turn on oled panel }