Sample code for LCD COG (Chip On Glass) NHD-C160100DiZ with ST7528i.
/*****************************************************/
/*
NHD-C160100DiZ.c
Program for writing to Newhaven Display Graphic COG - I2C interface
(c)2009 Newhaven Display International, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
/*****************************************************/
#include <REG52.H>
#include "picture.h"
/*****************************************************/
/*****************************************************/
sbit SDA = P1^0; //Serial data
sbit SCL = P3^4; //Serial clock
sbit RST = P3^7; //Reset
/*****************************************************/
const char Slave = 0x7E; //slave address
const char Comsend = 0x00;
const char Datasend = 0x40;
/*****************************************************/
void delay(unsigned int n) //Delay subroutine
{
unsigned int i,j;
for (i=0;i<n;i++)
for (j=0;j<350;j++)
{;}
}
/*****************************************************/
void I2C_out(unsigned char j) //I2C Output
{
int n;
unsigned char d;
d=j;
for(n=0;n<8;n++){ //send 8 bits
if((d&0x80)==0x80) //get only the MSB
SDA=1; //if 1, then SDA=1
else
SDA=0; //if 0, then SDA=0
d=(d<<1); //shift data byte left
SCL = 0;
SCL = 1; //clock in data
SCL = 0;
}
SCL = 1;
while(SDA==1){ //wait here until ACK
SCL=0;
SCL=1;
}
SCL=0;
}
/*****************************************************/
void I2C_Start(void)
{
SCL=1;
SDA=1;
SDA=0;
SCL=0;
}
/*****************************************************/
void I2C_Stop(void)
{
SDA=0;
SCL=0;
SCL=1;
SDA=1;
}
/*****************************************************/
void Show(unsigned char *text)
{
int n,i;
char page=0xB0; //first page
for(i=0;i<13;i++){ //100 pixels = 12.5 pages
I2C_Start();
I2C_out(Slave);
I2C_out(Comsend);
I2C_out(page);
I2C_out(0x10); //column address Y9:Y6
I2C_out(0x01); //column address Y5:Y2
I2C_Stop();
I2C_Start();
I2C_out(Slave);
I2C_out(Datasend);
for(n=0;n<160;n++){
I2C_out(*text); //send data 4 times for grayscaling
I2C_out(*text);
I2C_out(*text);
I2C_out(*text);
++text; //point to next byte of data
}
I2C_Stop();
page++; //move to next page
}
}
/****************************************************
* Initialization *
*****************************************************/
void init_LCD()
{
I2C_Start();
I2C_out(Slave);
I2C_out(Comsend);
I2C_out(0x48); //partial display duty ratio
I2C_out(0x64); // 1/100 duty
I2C_out(0xA0); //ADC select
I2C_out(0xC8); //SHL select
I2C_out(0x44); //initial Com0 register
I2C_out(0x00); //scan from Com0
I2C_out(0xAB); //OSC on
I2C_out(0x26); //
I2C_out(0x81); //set electronic volume
I2C_out(0x1C); //vopcode=0x1C
I2C_out(0x56); //set 1/11 bias
I2C_out(0x64); //3x
delay(2);
I2C_out(0x2C); //
I2C_out(0x66); //5x
delay(2);
I2C_out(0x2E); //
delay(2);
I2C_out(0x2F); //power control
I2C_out(0xF3); //bias save circuit
I2C_out(0x00); //
I2C_out(0x96); //frc and pwm
I2C_out(0x38); //external mode
I2C_out(0x75); //
I2C_out(0x97); //3frc, 45 pwm
I2C_out(0x80); //start 16-level grayscale settings
I2C_out(0x00); //
I2C_out(0x81); //
I2C_out(0x00); //
I2C_out(0x82); //
I2C_out(0x00); //
I2C_out(0x83); //
I2C_out(0x00); //
I2C_out(0x84); //
I2C_out(0x06); //
I2C_out(0x85); //
I2C_out(0x06); //
I2C_out(0x86); //
I2C_out(0x06); //
I2C_out(0x87); //
I2C_out(0x06); //
I2C_out(0x88); //
I2C_out(0x0B); //
I2C_out(0x89); //
I2C_out(0x0B); //
I2C_out(0x8A); //
I2C_out(0x0B); //
I2C_out(0x8B); //
I2C_out(0x0B); //
I2C_out(0x8C); //
I2C_out(0x10); //
I2C_out(0x8D); //
I2C_out(0x10); //
I2C_out(0x8E); //
I2C_out(0x10); //
I2C_out(0x8F); //
I2C_out(0x10); //
I2C_out(0x90); //
I2C_out(0x15); //
I2C_out(0x91); //
I2C_out(0x15); //
I2C_out(0x92); //
I2C_out(0x15); //
I2C_out(0x93); //
I2C_out(0x15); //
I2C_out(0x94); //
I2C_out(0x1A); //
I2C_out(0x95); //
I2C_out(0x1A); //
I2C_out(0x96); //
I2C_out(0x1A); //
I2C_out(0x97); //
I2C_out(0x1A); //
I2C_out(0x98); //
I2C_out(0x1E); //
I2C_out(0x99); //
I2C_out(0x1E); //
I2C_out(0x9A); //
I2C_out(0x1E); //
I2C_out(0x9B); //
I2C_out(0x1E); //
I2C_out(0x9C); //
I2C_out(0x23); //
I2C_out(0x9D); //
I2C_out(0x23); //
I2C_out(0x9E); //
I2C_out(0x23); //
I2C_out(0x9F); //
I2C_out(0x23); //
I2C_out(0xA0); //
I2C_out(0x27); //
I2C_out(0xA1); //
I2C_out(0x27); //
I2C_out(0xA2); //
I2C_out(0x27); //
I2C_out(0xA3); //
I2C_out(0x27); //
I2C_out(0xA4); //
I2C_out(0x2B); //
I2C_out(0xA5); //
I2C_out(0x2B); //
I2C_out(0xA6); //
I2C_out(0x2B); //
I2C_out(0xA7); //
I2C_out(0x2B); //
I2C_out(0xA8); //
I2C_out(0x2F); //
I2C_out(0xA9); //
I2C_out(0x2F); //
I2C_out(0xAA); //
I2C_out(0x2F); //
I2C_out(0xAB); //
I2C_out(0x2F); //
I2C_out(0xAC); //
I2C_out(0x32); //
I2C_out(0xAD); //
I2C_out(0x32); //
I2C_out(0xAE); //
I2C_out(0x32); //
I2C_out(0xAF); //
I2C_out(0x32); //
I2C_out(0xB0); //
I2C_out(0x35); //
I2C_out(0xB1); //
I2C_out(0x35); //
I2C_out(0xB2); //
I2C_out(0x35); //
I2C_out(0xB3); //
I2C_out(0x35); //
I2C_out(0xB4); //
I2C_out(0x38); //
I2C_out(0xB5); //
I2C_out(0x38); //
I2C_out(0xB6); //
I2C_out(0x38); //
I2C_out(0xB7); //
I2C_out(0x38); //
I2C_out(0xB8); //
I2C_out(0x3A); //
I2C_out(0xB9); //
I2C_out(0x3A); //
I2C_out(0xBA); //
I2C_out(0x3A); //
I2C_out(0xBB); //
I2C_out(0x3A); //
I2C_out(0xBC); //
I2C_out(0x3C); //
I2C_out(0xBD); //
I2C_out(0x3C); //
I2C_out(0xBE); //
I2C_out(0x3C); //
I2C_out(0xBF); //
I2C_out(0x3C); //end grayscale settings
I2C_out(0x38); //
I2C_out(0x74); //
I2C_out(0xAF); //display on
I2C_Stop();
}
/*****************************************************/
/*****************************************************/
int main(void)
{
P1 = 0;
P3 = 0;
RST = 1;
delay(3);
while(1) //continue
{
init_LCD();
delay(2);
Show(picture1);
delay(3000);
Show(picture2);
delay(3000);
}
}