
#include <REGX51.H>

#define CMD 0
#define DATA 1

void init3310(void);
void delay(unsigned int);
void delay1ms(void);
void sndcmd(unsigned char);
void glcdata(unsigned char);
void main(void);

void timer0(void) interrupt 1
{
	tmr0bit=1;
}

void delay1ms(void)
{
        TR0=0;
	TMOD |= 0x01;
	TF0 = 0;
	TL0=0x17;
	TH0=0xFC;
	TR0=1;
	while(tmr0bit==0);
	tmr0bit=0;
}

void delay(unsigned int timems)
{
	unsigned int i;
	for(i=0;i<timems;i++)
	delay1ms();
}

/*
1 --- Yellow ( vCC)
2 --- Green (P0.7)SCK
3 --- Black (P0.6)SDIN
4 --- Red (P0.5)D/C
5 --- Blue (P0.3)SCE
6 --- Yellow(GND)GND
7 --- Green(NC)VOUT
8 --- Black(P0.4)RES
*/

sbit SCLK=0x87;
sbit SDIN=0x86;
sbit DC=0x85;
sbit SCE=0x83;
sbit RST=0x84;

void init3310(void)
{
	P0=0xff;
	SCE=1;
	
	RST=1;
	RST=0;
	RST=1;

	SCE=0;

	RST=1;
	RST=0;
	RST=1;
	sndcmd(0x21);//PD=0,V=0,H=1
	sndcmd(0x10);//bias system, BS2=BS1=BS0=0
	sndcmd(0x90);//Vop value as per datasheet
	sndcmd(0x04);//temp coeff=0
	
	//extended addressing complete
	
	sndcmd(0x20);//switching to ordinary mode H=1
	sndcmd(0x0C);//D=1,E=0-normal mode
	sndcmd(0x40);//Y addr=0
	sndcmd(0x80);//X addr=0
	SCE=1;//chip is deselected
}

void sndcmd(unsigned char command)
{
	unsigned char cntr,value;
	
	SCLK=1;
	RST=1;//chip is not at reset
	SCE=0;//chip is enabled
	DC=0;//command is to be sent
	
	SCLK=1;//sclk is started with negative edge
	SCLK=0;

	for(cntr=0;cntr<=7;cntr++)
	{
		value=(0x80 & command);
			
		if (value==0x80) SDIN=1;
		else			 SDIN=0;

		SCLK=1;//at this positive edge, the SDIN is sampled
		SCLK=0;//again SCLK=0 for next cycle

		command<<=1;
	 }

	 SCE=1;//chip is deselected
}

void glcdata(unsigned char databyte)
{
		unsigned char cntr,value;
	
	SCLK=1;
	RST=1;//chip is not at reset
	SCE=0;//chip is enabled
	DC=1;//data is to be sent
	
	SCLK=1;//sclk is started with negative edge
	SCLK=0;

	for(cntr=0;cntr<=0;cntr++)
	{
		value=(0x80 & databyte);
			
		if (value==0x80) SDIN=1;
		else			 SDIN=0;

		SCLK=1;//at this positive edge, the SDIN is sampled
		SCLK=0;//again SCLK=0 for next cycle

		databyte<<=1;
	 }

	 SCE=1;//chip is deselected
}
	
void main(void)
{
	IE=0x82;
	delay(10);
	setcursor(2,1);

	init3310();
	glcdata(0x0f);//writes something on the screen
	glcdata(0x00);
	glcdata(0xff);
	SCLK=1;
	
	while(1);
}
