////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
OSRAM OLED graphical LCD routines 
CCS compiler

TechDesign Electronics (c) March-July 2007. 

www.techdesign.be
info@techdesign.be
*/
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


BYTE CONST TABLE7[240]=				{0x00,0x00,0x00,0x00,0x00,	// 20 space	 		ASCII table for NOKIA LCD: 96 rows * 5 bytes= 480 bytes
						0x00,0x00,0x5f,0x00,0x00,	// 21 !
						0x00,0x07,0x00,0x07,0x00,	// 22 "
						0x14,0x7f,0x14,0x7f,0x14,	// 23 #
						0x24,0x2a,0x7f,0x2a,0x12,	// 24 $
						0x23,0x13,0x08,0x64,0x62,	// 25 %
						0x36,0x49,0x55,0x22,0x50,	// 26 &
						0x00,0x05,0x03,0x00,0x00,	// 27 '
						0x00,0x1c,0x22,0x41,0x00,	// 28 (
						0x00,0x41,0x22,0x1c,0x00,	// 29 )
						0x14,0x08,0x3e,0x08,0x14,	// 2a *
						0x08,0x08,0x3e,0x08,0x08,	// 2b +
						0x00,0x50,0x30,0x00,0x00,	// 2c ,
						0x08,0x08,0x08,0x08,0x08,	// 2d -
						0x00,0x60,0x60,0x00,0x00,	// 2e .
						0x20,0x10,0x08,0x04,0x02,	// 2f /
						0x3e,0x51,0x49,0x45,0x3e,	// 30 0
						0x00,0x42,0x7f,0x40,0x00,	// 31 1
						0x42,0x61,0x51,0x49,0x46,	// 32 2
						0x21,0x41,0x45,0x4b,0x31,	// 33 3
						0x18,0x14,0x12,0x7f,0x10,	// 34 4
						0x27,0x45,0x45,0x45,0x39,	// 35 5
						0x3c,0x4a,0x49,0x49,0x30,	// 36 6
						0x01,0x71,0x09,0x05,0x03,	// 37 7
						0x36,0x49,0x49,0x49,0x36,	// 38 8
						0x06,0x49,0x49,0x29,0x1e,	// 39 9
						0x00,0x36,0x36,0x00,0x00,	// 3a :
						0x00,0x56,0x36,0x00,0x00,	// 3b ;
						0x08,0x14,0x22,0x41,0x00,	// 3c <
						0x14,0x14,0x14,0x14,0x14,	// 3d =
						0x00,0x41,0x22,0x14,0x08,	// 3e >
						0x02,0x01,0x51,0x09,0x06,	// 3f ?
						0x32,0x49,0x79,0x41,0x3e,	// 40 @
						0x7e,0x11,0x11,0x11,0x7e,	// 41 A
						0x7f,0x49,0x49,0x49,0x36,	// 42 B
						0x3e,0x41,0x41,0x41,0x22,	// 43 C
						0x7f,0x41,0x41,0x22,0x1c,	// 44 D
						0x7f,0x49,0x49,0x49,0x41,	// 45 E
						0x7f,0x09,0x09,0x09,0x01,	// 46 F
						0x3e,0x41,0x49,0x49,0x7a,	// 47 G
						0x7f,0x08,0x08,0x08,0x7f,	// 48 H
						0x00,0x41,0x7f,0x41,0x00,	// 49 I
						0x20,0x40,0x41,0x3f,0x01,	// 4a J
						0x7f,0x08,0x14,0x22,0x41,	// 4b K
						0x7f,0x40,0x40,0x40,0x40,	// 4c L
						0x7f,0x02,0x0c,0x02,0x7f,	// 4d M
						0x7f,0x04,0x08,0x10,0x7f,	// 4e N
						0x3e,0x41,0x41,0x41,0x3e};	// 4f O


BYTE CONST TABLE8[240]=				{0x7f,0x09,0x09,0x09,0x06,	// 50 P
						0x3e,0x41,0x51,0x21,0x5e,	// 51 Q
						0x7f,0x09,0x19,0x29,0x46,	// 52 R
						0x46,0x49,0x49,0x49,0x31,	// 53 S
						0x01,0x01,0x7f,0x01,0x01,	// 54 T
						0x3f,0x40,0x40,0x40,0x3f,	// 55 U
						0x1f,0x20,0x40,0x20,0x1f,	// 56 V
						0x3f,0x40,0x38,0x40,0x3f,	// 57 W
						0x63,0x14,0x08,0x14,0x63,	// 58 X
						0x07,0x08,0x70,0x08,0x07,	// 59 Y
						0x61,0x51,0x49,0x45,0x43,	// 5a Z
						0x00,0x7f,0x41,0x41,0x00,	// 5b [
						0x02,0x04,0x08,0x10,0x20,	// 5c \
						0x00,0x41,0x41,0x7f,0x00,	// 5d ]
						0x04,0x02,0x01,0x02,0x04,	// 5e ^
						0x40,0x40,0x40,0x40,0x40,	// 5f _
						0x00,0x01,0x02,0x04,0x00,	// 60 `
						0x20,0x54,0x54,0x54,0x78,	// 61 a
						0x7f,0x48,0x44,0x44,0x38,	// 62 b
						0x38,0x44,0x44,0x44,0x20,	// 63 c
						0x38,0x44,0x44,0x48,0x7f,	// 64 d
						0x38,0x54,0x54,0x54,0x18,	// 65 e
						0x08,0x7e,0x09,0x01,0x02,	// 66 f
						0x0c,0x52,0x52,0x52,0x3e,	// 67 g
						0x7f,0x08,0x04,0x04,0x78,	// 68 h
						0x00,0x44,0x7d,0x40,0x00,	// 69 i
						0x20,0x40,0x44,0x3d,0x00,	// 6a j 
						0x7f,0x10,0x28,0x44,0x00,	// 6b k
						0x00,0x41,0x7f,0x40,0x00,	// 6c l
						0x7c,0x04,0x18,0x04,0x78,	// 6d m
						0x7c,0x08,0x04,0x04,0x78,	// 6e n
						0x38,0x44,0x44,0x44,0x38,	// 6f o
						0x7c,0x14,0x14,0x14,0x08,	// 70 p
						0x08,0x14,0x14,0x18,0x7c,	// 71 q
						0x7c,0x08,0x04,0x04,0x08,	// 72 r
						0x48,0x54,0x54,0x54,0x20,	// 73 s
						0x04,0x3f,0x44,0x40,0x20,	// 74 t
						0x3c,0x40,0x40,0x20,0x7c,	// 75 u
						0x1c,0x20,0x40,0x20,0x1c,	// 76 v
						0x3c,0x40,0x30,0x40,0x3c,	// 77 w
						0x44,0x28,0x10,0x28,0x44,	// 78 x
						0x0c,0x50,0x50,0x50,0x3c,	// 79 y
						0x44,0x64,0x54,0x4c,0x44,	// 7a z
						0x00,0x08,0x36,0x41,0x00,	// 7b {
						0x00,0x00,0x7f,0x00,0x00,	// 7c |
						0x00,0x41,0x36,0x08,0x00,	// 7d }
						0x10,0x08,0x08,0x10,0x08,	// 7e ~
						0x78,0x46,0x41,0x46,0x78};	// 7f ¦
				

//


BYTE CONST TABLE9[195]=				{0x1e,0x21,0x61,0x21,0x12,	// 80 Ç		// extended ascii (selected chars)
						0x3c,0x41,0x40,0x21,0x7c,	// 81 ü
						0x38,0x54,0x56,0x55,0x18,	// 82 é
						0x20,0x55,0x55,0x55,0x78,	// 83 â
						0x20,0x55,0x54,0x55,0x78,	// 84 ä
						0x20,0x55,0x56,0x54,0x78,	// 85 à
						0x20,0x54,0x55,0x54,0x78,	// 86 å
						0x1c,0x22,0x62,0x22,0x10,	// 87 ç
						0x38,0x55,0x55,0x55,0x18,	// 88 ê
						0x38,0x55,0x54,0x55,0x18,	// 89 ë
						0x38,0x54,0x55,0x56,0x18,	// 8a è
						0x00,0x45,0x7c,0x41,0x00,	// 8b ï
						0x00,0x45,0x7d,0x41,0x00,	// 8c î
						0x00,0x45,0x7e,0x40,0x00,	// 8d ì
						0x78,0x15,0x14,0x15,0x78,	// 8e Ä
						0x78,0x14,0x15,0x14,0x78,	// 8f Å

						0x7c,0x54,0x56,0x55,0x44,	// 90 É
						0x24,0x54,0x78,0x54,0x48,	// 91 æ
						0x7e,0x09,0x7e,0x49,0x49,	// 92 Æ
						0x38,0x45,0x45,0x45,0x38,	// 93 ô
						0x38,0x45,0x44,0x45,0x38,	// 94 ö
						0x38,0x45,0x46,0x44,0x38,	// 95 ò
						0x3c,0x41,0x41,0x21,0x7c,	// 96 û
						0x3c,0x41,0x42,0x20,0x7c,	// 97 ù
						0x0c,0x51,0x50,0x51,0x3c,	// 98 ÿ
						0x38,0x45,0x44,0x45,0x38,	// 99 Ö
						0x3c,0x41,0x40,0x41,0x3c,	// 9a Ü
						0x00,0x00,0x00,0x00,0x00,	// 9b 
						0x20,0x48,0x3e,0x49,0x62,	// 9c £
						0x00,0x00,0x00,0x00,0x00,	// 9d 
						0x00,0x00,0x00,0x00,0x00,	// 9e 
						0x40,0x68,0x3e,0x09,0x02,	// 9f ƒ

						0x20,0x54,0x56,0x55,0x78,	// a0 á
						0x00,0x44,0x7e,0x41,0x00,	// a1 í
						0x38,0x44,0x46,0x45,0x38,	// a2 ó
						0x3c,0x40,0x42,0x21,0x7c,	// a3 ú
						0x7c,0x09,0x05,0x05,0x78,	// a4 ñ
						0x7c,0x09,0x11,0x21,0x7c,	// a5 Ñ

						0x7e,0x25,0x25,0x26,0x18};	// a6 ß




char charrow,charsele,charposi,chardat; 		




void 	dt_lcd_init(void);
void 	dt_lcd_clear_screen(void);
void 	dt_lcd_write_command(char bytefor_command);
void 	dt_lcd_write_data(char bytefor_data);

void 	dt_lcd_write_dorc(char bytefor);

void 	dt_lcd_gotoxy(byte x_col, byte y_row);
void 	dt_lcd_erase_y(byte y_row);

void 	dt_lcd_printchar(byte cvar);
void 	dt_lcd_printchar_inv(byte cvar2);

void 	table_to_dt_lcd(void);
void 	table_to_dt_lcd_inv(void);

void 	ascii_extended_corr(void);

void 	dt_lcd_print_stereo(void);
void 	dt_lcd_print_status_a(void);
void 	dt_lcd_print_status_b(void);
void 	dt_lcd_print_status_c(void);
void 	dt_lcd_print_status_d(void);
void 	dt_lcd_print_status_e(void);
void 	dt_lcd_print_status_f(void);
void 	dt_lcd_print_status_g(void);

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void dt_lcd_init(void)
{
dt_cs=1;
dt_sclk=1;

dt_rst = 0;
delay_ms(10);
dt_rst = 1;
delay_ms(1);

dt_lcd_write_command(0xe2);	// reset
	
dt_lcd_write_command(0xa0);	// ADC select: normal
dt_lcd_write_command(0xc0);	// SHL select:
dt_lcd_write_command(0xb2);	// LCD bias select: was b2

dt_lcd_write_command(0x2c);	// Voltage converter ON
delay_ms(1);
dt_lcd_write_command(0x2e);	// Voltage regulator ON
delay_ms(1);
dt_lcd_write_command(0x2f);	// Voltage follower ON

dt_lcd_write_command(0x25);	// Regulator resistor select 0x20..0x27 was 25
dt_lcd_write_command(0x81);	// Reference voltage register set
dt_lcd_write_command(0x20);	// Reference voltage 0..63	was 28


dt_lcd_write_command(0x40);	// initial display line 0x40..0x7f
dt_lcd_write_command(0xaf);	// display ON
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void dt_lcd_clear_screen(void)
{
char yy;


// RAM set to zero
for (yy=0;yy!=8;yy++){
				dt_lcd_gotoxy(0,yy);
				printf(dt_lcd_printchar,"                        ");
				//printf("yy=%u ",yy);
						}



}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void dt_lcd_write_command(char bytefor_command)
{
dt_cs=0;
//nop;nop;nop;nop;

dt_rs=0;
dt_lcd_write_dorc(bytefor_command);

dt_cs=1;
}
/////////////////////////////////////////////////////////////////////////////////
void dt_lcd_write_data(char bytefor_data)
{
dt_cs=0;
//nop;nop;nop;nop;

dt_rs=1;
dt_lcd_write_dorc(bytefor_data);	// write display data


dt_cs=1;
}
//////////////////////////////////////////////////////////////////////////////////
void dt_lcd_write_dorc(char bytefor)			// serial write data or command subroutine
{
char caa;
for (caa=8;caa>0;caa--) {
		dt_sclk=0;
		//delay_us(2);
		if ((bytefor&0x80)==0){dt_sid=0;}
		else {dt_sid=1;}
		//nop;nop;
		dt_sclk=1;
		bytefor=bytefor<<1;
		//nop;nop;
					}
}
//////////////////////////////////////////////////////////////////////////////////
void dt_lcd_gotoxy(byte x_col, byte y_row)		// Nokia LCD 3310 Position cursor
{
char hi_col,lo_col;

//x_col+=0x24;

lo_col= (x_col&0x0f);
//printf(" lo_col=%u ",lo_col);
hi_col= ((x_col>>4)&0x0f)|0x10;
//printf(" hi_col=%u ",hi_col);

dt_lcd_write_command(hi_col);		// set higher column address
dt_lcd_write_command(lo_col);		// set lower column address	


dt_lcd_write_command(0xb0|(y_row&0x0f));	// set GDDRAM page address 0..7 (for 8 rows)
//printf(" y_row=%u ",y_row);

}
//////////////////////////////////////////////////////////////////////////////////
void dt_lcd_erase_y(byte y_row)
{

dt_lcd_gotoxy(0,y_row);
printf(dt_lcd_printchar,"                     ");
dt_lcd_write_data(0x00);
dt_lcd_write_data(0x00);

}
//////////////////////////////////////////////////////////////////////////////////

void dt_lcd_printchar(byte cvar)					// Write 1 character to LCD 
{
charsele=cvar;
table_to_dt_lcd();
}
//////////////////////////////////////////////////////////////////////////////////
void dt_lcd_printchar_inv(byte cvar2)
{
charsele=cvar2;
table_to_dt_lcd_inv();

}
//////////////////////////////////////////////////////////////////////////////////
void table_to_dt_lcd(void)	// extract ascii from tables & write to LCD
{


if (charsele<0x20)return;
else if (charsele>0x7f){ascii_extended_corr();}

if (charsele<0x20)return;
if (charsele>0xa6)return;

for (charrow=0;charrow<5;charrow++) {		// 5 bytes
	
if (charsele<0x50){charposi=(((charsele&0xff)-0x20)*5);chardat=TABLE7[(charposi+charrow)];}						// use TABLE7
else if (charsele<0x80){charposi=(((charsele&0xff)-0x50)*5);chardat=TABLE8[(charposi+charrow)];}				// use TABLE8							
else if (charsele<0xa7){charposi=(((charsele&0xff)-0x80)*5);chardat=TABLE9[(charposi+charrow)]; }				// use TABLE9							
		
	dt_lcd_write_data(chardat);				// send data to nokia	
										}

	dt_lcd_write_data(0x00);					// 	1 byte (always blank)

}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void table_to_dt_lcd_inv(void)
{
if (charsele<0x20)return;
if (charsele>0x7f){ascii_extended_corr();}

if (charsele<0x20)return;
if (charsele>0xa6)return;

for (charrow=0;charrow<5;charrow++) {		// 5 bytes
	
if (charsele<0x50){charposi=(((charsele&0xff)-0x20)*5);chardat=TABLE7[(charposi+charrow)];}						// use TABLE7
else if (charsele<0x80){charposi=(((charsele&0xff)-0x50)*5);chardat=TABLE8[(charposi+charrow)];}				// use TABLE8							
else if (charsele<0xa7){charposi=(((charsele&0xff)-0x80)*5);chardat=TABLE9[(charposi+charrow)];}				// use TABLE9							
		
	chardat= 255-chardat;
	dt_lcd_write_data(chardat);				// send data to nokia	
										}

	dt_lcd_write_data(0xff);					// 	1 byte (always blank)



}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ascii_extended_corr(void)
{
//printf( "charsele_in= %x ",charsele);

if(charsele==0xc7){charsele=0x80;}			// Ç
else if(charsele==0xfc){charsele=0x81;}		// ü
else if(charsele==0xe9){charsele=0x82;}		// é
else if(charsele==0xe2){charsele=0x83;}		// 
else if(charsele==0xe4){charsele=0x84;}		// 
else if(charsele==0xe0){charsele=0x85;}		// 
else if(charsele==0xe5){charsele=0x86;}		//
else if(charsele==0xe7){charsele=0x87;}		// 
else if(charsele==0xea){charsele=0x88;}		// 
else if(charsele==0xeb){charsele=0x89;}		// 
else if(charsele==0xe8){charsele=0x8a;}		// 
else if(charsele==0xef){charsele=0x8b;}		// 
else if(charsele==0xee){charsele=0x8c;}		// 
else if(charsele==0xec){charsele=0x8d;}		// 
else if(charsele==0xc4){charsele=0x8e;}		// 
else if(charsele==0xc5){charsele=0x8f;}		// Å

else if(charsele==0xc9){charsele=0x90;}		// É
else if(charsele==0xe6){charsele=0x91;}		// æ
else if(charsele==0xc6){charsele=0x92;}		// Æ
else if(charsele==0xf4){charsele=0x93;}		// 
else if(charsele==0xf6){charsele=0x94;}		// 
else if(charsele==0xf2){charsele=0x95;}		// 
else if(charsele==0xfb){charsele=0x96;}		//
else if(charsele==0xf9){charsele=0x97;}		// 
else if(charsele==0xff){charsele=0x98;}		// 
else if(charsele==0xd6){charsele=0x99;}		// 
else if(charsele==0xdc){charsele=0x9a;}		// 
else if(charsele==0xf8){charsele=0x9b;}		// 
else if(charsele==0xa3){charsele=0x9c;}		// 
else if(charsele==0xd8){charsele=0x9d;}		// 
else if(charsele==0xd7){charsele=0x9e;}		// 
else if(charsele==0x83){charsele=0x9f;}		// ƒ

else if(charsele==0xe1){charsele=0xa0;}		// á
else if(charsele==0xed){charsele=0xa1;}		// í
else if(charsele==0xf3){charsele=0xa2;}		// ó
else if(charsele==0xfa){charsele=0xa3;}		// 
else if(charsele==0xf1){charsele=0xa4;}		// 
else if(charsele==0xd1){charsele=0xa5;}		// Ñ

else if(charsele==0xdf){charsele=0xa6;}		// ß

//printf( "charsele_out= %x ",charsele);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void dt_lcd_print_stereo(void)
{
dt_lcd_write_data(0x00);
dt_lcd_write_data(0x00);
dt_lcd_write_data(0x00);
dt_lcd_write_data(0x1c);
dt_lcd_write_data(0x22);
dt_lcd_write_data(0x41);

dt_lcd_write_data(0x41);
dt_lcd_write_data(0x5d);
dt_lcd_write_data(0x22);
dt_lcd_write_data(0x5d);
dt_lcd_write_data(0x41);
dt_lcd_write_data(0x41);

dt_lcd_write_data(0x22);
dt_lcd_write_data(0x1c);
dt_lcd_write_data(0x00);
dt_lcd_write_data(0x00);
dt_lcd_write_data(0x00);
dt_lcd_write_data(0x00);

}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void dt_lcd_print_status_a(void)
{
dt_lcd_write_data(0x3e);
dt_lcd_write_data(0x41);
dt_lcd_write_data(0x49);
dt_lcd_write_data(0x45);
dt_lcd_write_data(0x3e);
dt_lcd_write_data(0x00);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void dt_lcd_print_status_b(void)
{
dt_lcd_write_data(0x3e);
dt_lcd_write_data(0x41);
dt_lcd_write_data(0x49);
dt_lcd_write_data(0x49);
dt_lcd_write_data(0x3e);
dt_lcd_write_data(0x00);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void dt_lcd_print_status_c(void)
{
dt_lcd_write_data(0x3e);
dt_lcd_write_data(0x41);
dt_lcd_write_data(0x49);
dt_lcd_write_data(0x51);
dt_lcd_write_data(0x3e);
dt_lcd_write_data(0x00);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void dt_lcd_print_status_d(void)
{
dt_lcd_write_data(0x3e);
dt_lcd_write_data(0x41);
dt_lcd_write_data(0x79);
dt_lcd_write_data(0x41);
dt_lcd_write_data(0x3e);
dt_lcd_write_data(0x00);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void dt_lcd_print_status_e(void)
{
dt_lcd_write_data(0x3e);
dt_lcd_write_data(0x51);
dt_lcd_write_data(0x49);
dt_lcd_write_data(0x41);
dt_lcd_write_data(0x3e);
dt_lcd_write_data(0x00);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void dt_lcd_print_status_f(void)
{
dt_lcd_write_data(0x3e);
dt_lcd_write_data(0x49);
dt_lcd_write_data(0x49);
dt_lcd_write_data(0x41);
dt_lcd_write_data(0x3e);
dt_lcd_write_data(0x00);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void dt_lcd_print_status_g(void)
{
dt_lcd_write_data(0x3e);
dt_lcd_write_data(0x45);
dt_lcd_write_data(0x49);
dt_lcd_write_data(0x41);
dt_lcd_write_data(0x3e);
dt_lcd_write_data(0x00);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void dt_lcd_print_status_h(void)
{
dt_lcd_write_data(0x3e);
dt_lcd_write_data(0x41);
dt_lcd_write_data(0x4f);
dt_lcd_write_data(0x41);
dt_lcd_write_data(0x3e);
dt_lcd_write_data(0x00);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
