Code example for Newhaven OLED display NHD-1.5-128128UGC3
//-------------------------------------------------------------------------- //send Data to OLED //-------------------------------------------------------------------------- void oled_Data_128128RGB(unsigned char Data) { int i; GPIO_Write(GPIOB, Data); for(i=0;i<10000;i++){;} GPIO_SetBits(GPIOC, RS); for(i=0;i<10000;i++){;} GPIO_ResetBits(GPIOC, CS1); for(i=0;i<10000;i++){;} GPIO_ResetBits(GPIOC, RW); for(i=0;i<10000;i++){;} GPIO_SetBits(GPIOC, E1); for(i=0;i<10000;i++){;} GPIO_ResetBits(GPIOC, E1); for(i=0;i<10000;i++){;} GPIO_SetBits(GPIOC, RW); for(i=0;i<10000;i++){;} GPIO_SetBits(GPIOC, CS1); for(i=0;i<10000;i++){;} } //-------------------------------------------------------------------------- //send Command to OLED //-------------------------------------------------------------------------- void oled_Command_128128RGB(unsigned char Data) { int i; GPIO_Write(GPIOB, Data); for(i=0;i<4000;i++){;} GPIO_ResetBits(GPIOC, RS); for(i=0;i<4000;i++){;} GPIO_ResetBits(GPIOC, CS1); for(i=0;i<4000;i++){;} GPIO_ResetBits(GPIOC, RW); for(i=0;i<4000;i++){;} GPIO_SetBits(GPIOC, E1); for(i=0;i<4000;i++){;} GPIO_ResetBits(GPIOC, E1); for(i=0;i<4000;i++){;} GPIO_SetBits(GPIOC, RW); for(i=0;i<4000;i++){;} GPIO_SetBits(GPIOC, CS1); for(i=0;i<4000;i++){;} } //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Initialization //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= void OLED_Init_128128RGB(void) { int i,j; GPIO_ResetBits(GPIOC, RES);graphic_delay(500000);GPIO_SetBits(GPIOC, RES);graphic_delay(500000);UILCD_SetCursor(0,0);UILCD_DisplayString("GO "); oled_Command_128128RGB(0xFD); //Command lock setting oled_Data_128128RGB(0x12); //unlock oled_Command_128128RGB(0xFD); //Command lock setting oled_Data_128128RGB(0xB1); //unlock oled_Command_128128RGB(0xAE); oled_Command_128128RGB(0xB3); //clock & frequency oled_Data_128128RGB(0xF1); //clock=Diviser+1 frequency=fh oled_Command_128128RGB(0xCA); //Duty oled_Data_128128RGB(0x7F); //OLED _END+1 oled_Command_128128RGB(0xA2); //Display offset oled_Data_128128RGB(0x00); oled_Command_128128RGB(0xA1); //Set display start line oled_Data_128128RGB(0x00); //0x00 start line oled_Command_128128RGB(0xA0); //Set Re-map, color depth oled_Data_128128RGB(0xA0); //8-bit 262K oled_Command_128128RGB(0xB5); //set GPIO oled_Data_128128RGB(0x00); //disabled oled_Command_128128RGB(0xAB); //Function Set oled_Data_128128RGB(0x01); //8-bit interface, internal VDD regulator oled_Command_128128RGB(0xB4); //set VSL oled_Data_128128RGB(0xA0); //external VSL oled_Data_128128RGB(0xB5); oled_Data_128128RGB(0x55); oled_Command_128128RGB(0xC1); //Set contrast current for A,B,C oled_Data_128128RGB(0x8a); //Color A //8a oled_Data_128128RGB(0x51); //Color B //51 oled_Data_128128RGB(0x8a); //Color C //8a oled_Command_128128RGB(0xC7); //Set master contrast oled_Data_128128RGB(0x0F); // //oled_Command_128128RGB(0xB9); //use linear grayscale LUT oled_Command_128128RGB(0xB8); oled_Data_128128RGB(0x02); oled_Data_128128RGB(0x03); oled_Data_128128RGB(0x04); oled_Data_128128RGB(0x05); oled_Data_128128RGB(0x06); oled_Data_128128RGB(0x07); oled_Data_128128RGB(0x08); oled_Data_128128RGB(0x09); oled_Data_128128RGB(0x0a); oled_Data_128128RGB(0x0b); oled_Data_128128RGB(0x0c); oled_Data_128128RGB(0x0d); oled_Data_128128RGB(0x0e); oled_Data_128128RGB(0x0f); oled_Data_128128RGB(0x10); oled_Data_128128RGB(0x11); oled_Data_128128RGB(0x12); oled_Data_128128RGB(0x13); oled_Data_128128RGB(0x15); oled_Data_128128RGB(0x17); oled_Data_128128RGB(0x19); oled_Data_128128RGB(0x1b); oled_Data_128128RGB(0x1d); oled_Data_128128RGB(0x1f); oled_Data_128128RGB(0x21); oled_Data_128128RGB(0x23); oled_Data_128128RGB(0x25); oled_Data_128128RGB(0x27); oled_Data_128128RGB(0x2a); oled_Data_128128RGB(0x2d); oled_Data_128128RGB(0x30); oled_Data_128128RGB(0x33); oled_Data_128128RGB(0x36); oled_Data_128128RGB(0x39); oled_Data_128128RGB(0x3c); oled_Data_128128RGB(0x3f); oled_Data_128128RGB(0x42); oled_Data_128128RGB(0x45); oled_Data_128128RGB(0x48); oled_Data_128128RGB(0x4c); oled_Data_128128RGB(0x50); oled_Data_128128RGB(0x54); oled_Data_128128RGB(0x58); oled_Data_128128RGB(0x5c); oled_Data_128128RGB(0x60); oled_Data_128128RGB(0x64); oled_Data_128128RGB(0x68); oled_Data_128128RGB(0x6c); oled_Data_128128RGB(0x70); oled_Data_128128RGB(0x74); oled_Data_128128RGB(0x78); oled_Data_128128RGB(0x7d); oled_Data_128128RGB(0x82); oled_Data_128128RGB(0x87); oled_Data_128128RGB(0x8c); oled_Data_128128RGB(0x91); oled_Data_128128RGB(0x96); oled_Data_128128RGB(0x9b); oled_Data_128128RGB(0xa0); oled_Data_128128RGB(0xa5); oled_Data_128128RGB(0xaa); oled_Data_128128RGB(0xaf); oled_Data_128128RGB(0xb4); oled_Command_128128RGB(0xB1); //Set pre & dis-charge oled_Data_128128RGB(0x32); //pre=1h, dis=1h oled_Command_128128RGB(0xBB); //Set precharge voltage of color A,B,C oled_Data_128128RGB(0x07); // oled_Command_128128RGB(0xB2); //display enhancement oled_Data_128128RGB(0xa4); oled_Data_128128RGB(0x00); oled_Data_128128RGB(0x00); oled_Command_128128RGB(0xB6); //precharge period oled_Data_128128RGB(0x01); oled_Command_128128RGB(0xBE); //Set VcomH oled_Data_128128RGB(0x07); oled_Command_128128RGB(0xA6); //Normal display oled_Command_128128RGB(0x15); //set column start and end addresses oled_Data_128128RGB(0x00); // oled_Data_128128RGB(0x7F); // oled_Command_128128RGB(0x75); //set row start and end addresses oled_Data_128128RGB(0x00); // oled_Data_128128RGB(0x7F); // oled_Command_128128RGB(0x5C); //write to RAM command for(i=0;i<128;i++) { for(j=0;j<128;j++) { oled_Data_128128RGB(0x00); oled_Data_128128RGB(0x00); oled_Data_128128RGB(0x00); } } oled_Command_128128RGB(0xAF); //Display on } //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Demonstration //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= int oled_128128RGB(void) { char filename[17] = "OLED_128128_";char filetype[4] = ".bmp";char filenumber[1] = "0"; int result,i,j,k,counter;char image = 0x30;char write_data,datum;UINT blen = sizeof(Buff);memset(Buff,3,sizeof(Buff)); int column, row; unsigned char red,green1,green2,blue,byte1,byte2; filename[12]='\0';filenumber[0] = image;strncat(filename,filenumber,1);strncat(filename,filetype,4); GPIO_SetBits(GPIOB, BS1); GPIO_ResetBits(GPIOB, BS0); OLED_Init_128128RGB(); while(1){ UILCD_SetCursor(0,0);UILCD_DisplayString("Reading SD Card ");UILCD_SetCursor(1,0);UILCD_DisplayString(" ");UILCD_SetCursor(1,0);UILCD_DisplayString(filename); if(openSD(filename)==0x30){;}seekSD(54); column = 0x00; byte1 = 0x00; byte2 = 0x00; oled_Command_128128RGB(0x15); //set column start and end addresses oled_Data_128128RGB(column); // oled_Data_128128RGB(0x7F); // oled_Command_128128RGB(0x75); //set row start and end addresses oled_Data_128128RGB(0x00); // oled_Data_128128RGB(0x7F); // oled_Command_128128RGB(0x5C); //write to RAM command for (i=0;i<16384;i++) //for each 24-bit pixel...128*128=16384 { f_read(&File1, &red, 1, &blen); //read the red 8-bits f_read(&File1, &green1, 1, &blen); //read the green 8-bits f_read(&File1, &blue, 1, &blen); //read the blue 8-bits /*red = (red & 0xF8); //keep R7..R3 in D7..D3 green2 = (green1 << 3) & 0xE0; //shift up for G4..G2 in D7..D5 green1 = (green1 >> 5) & 0x07; //shift down for G7..G5 in D2..D0 blue = (blue >> 3) & 0x1F; //shift down for B7..B3 in D4..D0 byte1 = (red | green1); byte2 = (green2 | blue); oled_Data_128128RGB(byte1); oled_Data_128128RGB(byte2);*/ red = red >> 2; green1 = green1 >> 2; blue = blue >> 2; oled_Data_128128RGB(red); oled_Data_128128RGB(green1); oled_Data_128128RGB(blue); } result = f_close(&File1);if (result != FR_OK) {UILCD_SetCursor(0,0);UILCD_DisplayString("File not Closed ");while(1);return result;} /*graphic_delay(555555); oled_Command_128128RGB(0x96); oled_Data_128128RGB(0x01); oled_Data_128128RGB(0x00); oled_Data_128128RGB(0x7F); oled_Data_128128RGB(0x00); oled_Data_128128RGB(0x01); oled_Command_128128RGB(0x9F);*/ UILCD_SetCursor(0,0);UILCD_DisplayString("Press SEL button");UILCD_SetCursor(1,0);UILCD_DisplayString("for next file. "); while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2)==1){;} //oled_Command_128128RGB(0x9E); image++; if(image==0x3A){image=0x41;} filename[12]='\0'; filenumber[0] = image; strncat(filename,filenumber,1); strncat(filename,filetype,4); if(openSD(filename)==0x30) {image=0x30;} filename[12]='\0'; filenumber[0] = image; strncat(filename,filenumber,1); strncat(filename,filetype,4); }return 1;}