//********************************************
//
// 	File: 		GLCD_CAVR.c
//	Compiler: 	CodeVisionAVR
//      Datum:		23.01.2004 
//	Controller:	AVR
//	Routinen für die Text (und Graphik) Ausgabe 
//	auf einem 128*64 Graphik-Display mit HD61202 Controller (und compartible
//
//********************************************

// Portbelegungen 
#define P_CHIP1     PORTB.3
#define P_CHIP2     PORTB.4
#define P_RESET     PORTB.5
#define P_RW        PORTB.1
#define P_DI        PORTB.0
#define P_ENABLE    PORTB.2

#define DATA 	    PORTD

#define HIGH    1
#define LOW	0    

#include "char.h";
unsigned char current_line =0, current_page =0;

unsigned char get_chip (void)
{
if (current_line<=63) return 0;
else return 1;
}

void LCD_on (void)
{
P_ENABLE = LOW;
P_CHIP1 = HIGH;
P_CHIP2 = HIGH;
P_DI 	= LOW;
P_RW	= LOW;
P_ENABLE = HIGH;
DATA =0x3F;
P_ENABLE = LOW;
}


void send_instr (unsigned char data)
{
P_ENABLE = LOW;
if (get_chip() ==0)
	{
	P_CHIP1 = HIGH;
	P_CHIP2 = LOW;
	}
else  
	{
	P_CHIP1 = LOW;
	P_CHIP2 = HIGH;
	}
P_DI 	= LOW;
P_RW	= LOW;
P_ENABLE = HIGH;
DATA = data;
P_ENABLE = LOW;
}

void send_data (unsigned char data)
{
P_ENABLE = LOW;
if (get_chip() ==0)
	{
	P_CHIP1 = HIGH;
	P_CHIP2 = LOW;
	}
else  
	{
	P_CHIP1 = LOW;
	P_CHIP2 = HIGH;
	}
P_DI 	= HIGH;
P_RW	= LOW;
P_ENABLE = HIGH;
DATA = data;
P_ENABLE = LOW;
}

void set_pos (unsigned char page,unsigned char line)
{
unsigned char line2;

current_line = line; 
current_page = page;

if (line<=63) 
	{
	line |= 0x40;
	page |= 0xB8;

	send_instr (line);
	send_instr (page);

	line2 = current_line;
	current_line =64;

	send_instr (0x00|0x40);
	send_instr (page);
 
	current_line = line2;
	}

else
	{
	line -= 64;

	line |= 0x40;
	page |= 0xB8;

	send_instr (line);
	send_instr (page);
	}
}


void LCD_init (void)
{
unsigned char page,col;

for (page=0;page<8;page++)
	{
	set_pos(page,0);	
	for (col=0;col<=127;col++)
		{	        		
		send_data(0x00);
		current_line ++;
		}
	}	
set_pos(0,0);
}

void print_char (int c)
{
int i;
 for(i=0; i<5; i++)
	{	
	send_data(Font5x7[((c - 0x20) * 5) + i]);
	current_line ++;
	}
send_data(0x00);
current_line ++;
}

void print_string(char *data)
{
int i,x;
x = strlen(data);

for(i=0;i<x; i++)
	{
	print_char(*data);
	++data;
	}
}



void print_screen(void)
{
unsigned char page,col;
int x=0;
for (page=0;page<8;page++)
	{
	set_pos(page,0);	
	for (col=0;col<=127;col++)
		{	        		
		send_data(screen[x]);
		current_line ++;
		x++;
		}
	}	
set_pos(0,0);

}
 
void paint(char page, char start,char stop, char data)
{
int x;
set_pos(page,start);
for (x=0;x<=stop;x++)
 	{	
 	send_data(data);
	current_line ++;
  	}
}
