Example code for Newhaven OLED graphic display NHD-1.69-160128UGC3
//--------------------------------------------------------------------------
//send Data to OLED
//--------------------------------------------------------------------------
void oled_Data_160128RGB(unsigned char Data)
{
//GPIO_SetBits(GPIOC, E1);
GPIO_ResetBits(GPIOC, CS1);
GPIO_SetBits(GPIOC, RS);
GPIO_Write(GPIOB, Data);
GPIO_ResetBits(GPIOC, RW);
short_delay(10);GPIO_SetBits(GPIOC, E1);short_delay(10);
GPIO_ResetBits(GPIOC, E1);
//GPIO_SetBits(GPIOC, RW);
}
//--------------------------------------------------------------------------
//send Command to OLED
//--------------------------------------------------------------------------
void oled_Command_160128RGB(unsigned char Data)
{
//GPIO_SetBits(GPIOC, E1);
GPIO_ResetBits(GPIOC, CS1);
GPIO_ResetBits(GPIOC, RS);
GPIO_Write(GPIOB, Data);
GPIO_ResetBits(GPIOC, RW);
short_delay(10);GPIO_SetBits(GPIOC, E1);short_delay(10);
GPIO_ResetBits(GPIOC, E1);
//GPIO_SetBits(GPIOC, RW);
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Initialization
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void OLED_Init_160128RGB(void)
{
int i,j;
GPIO_ResetBits(GPIOC, RES);graphic_delay(500000);GPIO_SetBits(GPIOC, RES);graphic_delay(500000);
oled_Command_160128RGB(0x04);// Set Normal Driving Current
oled_Data_160128RGB(0x03);// Disable Oscillator Power Down
graphic_delay(2000);
oled_Command_160128RGB(0x04); // Enable Power Save Mode
oled_Data_160128RGB(0x00); // Set Normal Driving Current
graphic_delay(2000); // Disable Oscillator Power Down
oled_Command_160128RGB(0x3B);
oled_Data_160128RGB(0x00);
oled_Command_160128RGB(0x02);
oled_Data_160128RGB(0x01); // Set EXPORT1 Pin at Internal Clock
// Oscillator operates with external resister.
// Internal Oscillator On
oled_Command_160128RGB(0x03);
oled_Data_160128RGB(0x90); // Set Clock as 90 Frames/Sec
oled_Command_160128RGB(0x80);
oled_Data_160128RGB(0x01); // Set Reference Voltage Controlled by External Resister
oled_Command_160128RGB(0x08);// Set Pre-Charge Time of Red
oled_Data_160128RGB(0x04);
oled_Command_160128RGB(0x09);// Set Pre-Charge Time of Green
oled_Data_160128RGB(0x05);
oled_Command_160128RGB(0x0A);// Set Pre-Charge Time of Blue
oled_Data_160128RGB(0x05);
oled_Command_160128RGB(0x0B);// Set Pre-Charge Current of Red
oled_Data_160128RGB(0x9D);
oled_Command_160128RGB(0x0C);// Set Pre-Charge Current of Green
oled_Data_160128RGB(0x8C);
oled_Command_160128RGB(0x0D);// Set Pre-Charge Current of Blue
oled_Data_160128RGB(0x57);
oled_Command_160128RGB(0x10);// Set Driving Current of Red
oled_Data_160128RGB(0x56);//45
oled_Command_160128RGB(0x11);// Set Driving Current of Green
oled_Data_160128RGB(0x4D);//34
oled_Command_160128RGB(0x12);// Set Driving Current of Blue
oled_Data_160128RGB(0x46);//33
oled_Command_160128RGB(0x13);
oled_Data_160128RGB(0xa0); // Set Color Sequence D[15:0]=[RRRRR:GGGGGG:BBBBB]
// Alternative Gate Pin Configuration
// Scan from G0 to G127
// Column Address 0 Mapped to S0
// One Screen Mode
// Normal Display
oled_Command_160128RGB(0x14);
oled_Data_160128RGB(0x01); // Set MCU Interface Mode
oled_Command_160128RGB(0x16);
oled_Data_160128RGB(0x76);
// Set Horizontal Address Increment
// Vertical Address Increment
// The data is continuously written horizontally.
// Enable 8-bit Bus Interface
// 65,536 Colors Mode (0x66)
// * 262,144 Colors Mode (0x76)
oled_Command_160128RGB(0x20);
oled_Data_160128RGB(0x00); // Shift Mapping RAM Counter
oled_Command_160128RGB(0x21);
oled_Data_160128RGB(0x00); // Shift Mapping RAM Counter
oled_Command_160128RGB(0x28);
oled_Data_160128RGB(0x7F); // 1/128 Duty (0x0F~0x7F)
oled_Command_160128RGB(0x29);
oled_Data_160128RGB(0x00); // Set Mapping RAM Display Start Line (0x00~0x7F)
oled_Command_160128RGB(0x06);
oled_Data_160128RGB(0x01); // Display On (0x00/0x01)
oled_Command_160128RGB(0x05); // Disable Power Save Mode
oled_Data_160128RGB(0x00); // Set All Internal Register Value as Normal Mode
// oled_Command_160128RGB(0x06);
//oled_Data_160128RGB(0x00); // Display Off (0x00/0x01)
oled_Command_160128RGB(0x15);
oled_Data_160128RGB(0x00); // Set RGB Interface Polarity as Active Low
// Dot Clock Polarity Sampled as Rising Edge
// Disable Vertical Synchronization Output on VSYNCO Pin
//oled_Command_160128RGB(0xFD);
//Set_Gray_Scale_Table(); // Set Pulse Width for Gamma Table
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Demonstration
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
int oled_160128RGB(void)
{
char filename[17] = "OLED_160128_";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 short RGB16[16384];
unsigned char red,green,blue;
memset(RGB16,0x0000,sizeof(RGB16));
filename[12]='\0';filenumber[0] = image;strncat(filename,filenumber,1);strncat(filename,filetype,4);
GPIO_SetBits(GPIOB, BS1);
GPIO_ResetBits(GPIOB, BS0);
OLED_Init_160128RGB();
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);
oled_Command_160128RGB(0x17); //set column start address
oled_Data_160128RGB(0x00); //
oled_Command_160128RGB(0x18); //set column end address
oled_Data_160128RGB(0x9F); //
oled_Command_160128RGB(0x19); //set row start address
oled_Data_160128RGB(0x00); //
oled_Command_160128RGB(0x1A); //set row end address
oled_Data_160128RGB(0x7F); //
oled_Command_160128RGB(0x22); //write to RAM command
for (i=0;i<20480;i++) //for each 24-bit pixel...160*128=20480
{
f_read(&File1, &red, 1, &blen); //read the red 8-bits
oled_Data_160128RGB(red);
f_read(&File1, &green, 1, &blen); //read the green 8-bits
oled_Data_160128RGB(green);
f_read(&File1, &blue, 1, &blen); //read the blue 8-bits
oled_Data_160128RGB(blue);
/*red=red>>3; //shift down to 5-bits
green=green>>2; //shift down to 6-bits
blue=blue>>3; //shift down to 5-bits
RGB16[i]= (RGB16[i] | red); //put red 5-bits into int
RGB16[i]= (RGB16[i] << 6); //move red bits over, make room for green
RGB16[i]= (RGB16[i] | green); //put green 6-bits into int
RGB16[i]= (RGB16[i] << 5); //move red and green bits over, make room for blue
RGB16[i]= (RGB16[i] | blue); //put blue 5-bits into int
*/
}
result = f_close(&File1);if (result != FR_OK) {UILCD_SetCursor(0,0);UILCD_DisplayString("File not Closed ");while(1);return result;}
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){;}
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);
oled_Command_160128RGB(0x3D);//Screen saver timer
oled_Data_160128RGB(0x00);
oled_Command_160128RGB(0x3C);//Screen saver timer
oled_Data_160128RGB(0x00);
oled_Command_160128RGB(0x3B);//Screen saver
oled_Data_160128RGB(0x00);
oled_Command_160128RGB(0x42);//Screen saver
oled_Data_160128RGB(0x00);
}return 1;}