Example code for Newhaven OLED display NHD-0216KZW/ 0220DZW/ 0420DZW - (4-bit) models.
#include <reg52.H>
//---------------------------------------------------------
sbit E = P3^4;
sbit D_I = P3^0;
sbit R_W = P3^7;
void Nybble();
void Delayms(int n){
int i;
int j;
for (i=0;i<n;i++)
for (j=0;j<1000;j++)
{;}
}
void command(char i){
P1 = i;
D_I =0;
R_W =0;
Nybble();
i = i<<4;
P1 = i;
Nybble();
}
void write(char i){
P1 = i;
D_I =1;
R_W =0;
Nybble();
i = i<<4;
P1 = i;
Nybble();
}
void Nybble()
{
E = 1;
Delayms(1);
E = 0;
}
void init(){
P1 = 0;
P3 = 0;
Delayms(15);
// P1 = 0x30;
Delayms(50);
Nybble();
Delayms(50);
Nybble();
Delayms(5);
Nybble();
Delayms(5);
P1= 0x20;
Nybble();
Delayms(5);
command(0x28);
Delayms(5);
command(0x10);
Delayms(5);
command(0x0C);
Delayms(5);
command(0x06);
Delayms(5);
}
void Output()
{
code char text1[] = {"Newhaven Display Int"};
code char text2[] = {"4 Lines 20 Character"};
code char text3[] = {" Yellow OLED "};
code char text4[] = {"4-bit initialization"};
int i;
command(0x02);
for (i=0;i<20;i++){
write(text1[i]);
}
command(0xc0);
for (i=0;i<20;i++){
write(text2[i]);
}
command(0x94);
for (i=0;i<20;i++){
write(text3[i]);
}
command(0xd4);
for (i=0;i<20;i++){
write(text4[i]);
}
}
void main(void) {
P1=0;
P3=0;
while(1){
Delayms(10);
init();
command(0x01);
command(0x02);
command(0x80);
Output();
Delayms(10);
}
}