/*********************************************
This program was produced by the
CodeWizardAVR V1.0.1.7b Light
Automatic Program Generator
© Copyright 1998-2001
Pavel Haiduc, HP InfoTech S.R.L.
http://infotech.ir.ro
e-mail:dhptechn@ir.ro , hpinfotech@mail.com

Project : 
Version : 
Date    : 13.07.2004
Author  : Beru AG
Company : 
Comments: 


Chip type           : AT90S8535
Clock frequency     : 8,000000 MHz
Memory model        : Small
Internal SRAM size  : 512
External SRAM size  : 0
Data Stack size     : 128
*********************************************/

#include <90s8535.h>
#define wr PORTB.0
#define rd PORTB.1
#define cd PORTB.2

void pause(void);
void PORTC_output(void);
void PORTC_input(void);
void out(char commando,char dataa, char datab);
void lcd_data(char daten);
void lcd_commando(char commando);
void lcd_status(void);
void autoreset(void);
void autowrite(void);
void bitset(char pix);
void t6963cinit(void);
void pset(char x,unsigned char y);
void cls (void);
void linie (char xa,char ya,char xb,char yb);
void LCD_PutPixel(unsigned char x, unsigned char y, unsigned char Set);
void LCD_Home_Graphics(void);
void LCD_Clear_Graphics(void);

void pause(void)
{
#asm
 nop
 nop
#endasm
}

void PORTC_output(void)
{
DDRC = 0xFF;
PORTC = 0;
}

void PORTC_input(void)
{
DDRC = 0x00;
PORTC = 0xFF;
}

void lcd_status(void)
{
 unsigned char lcd_status;
 PORTC_input();
 pause();
 cd = 1;
 pause();
 wr = 1;
 pause();
 
   do
   {          
      rd = 0;
      pause();
      lcd_status = PINC; 
      rd = 1;
      pause();
   }
   while ((lcd_status & 0x03) != 0x03);
 PORTC_output(); 
}

void out(char commando,char dataa, char datab)
{      
lcd_status();
pause();
rd = 1;
pause();
cd = 0;
pause();
PORTC = dataa;
pause();
wr = 0;
pause();
wr = 1;
pause();
PORTC = datab;
pause();
wr = 0;
pause();
wr = 1;
pause();
cd = 1;
pause();
PORTC = commando;
pause();
wr = 0;
pause();
wr = 1;
pause();
cd = 1;
pause();
}

void autoreset(void)
{
PORTC = 0xB2;
pause();
wr = 0;
pause();
wr = 1;
}

void autowrite(void)
{
PORTC = 0xB0;
pause();
wr = 0;
pause();
wr = 1;
}

void lcd_data(char daten)
{
lcd_status();
cd = 0;
pause();
PORTC = daten;
pause();
wr = 0;
pause();
wr = 1;
pause();
cd = 1;
}

void lcd_commando(char commando)
{
pause();
rd = 1;
pause();
cd = 1;
pause();
PORTC = commando;
pause();
wr = 0;
pause();
wr = 1;
pause();
cd = 1;
pause();
}

void LCD_PutPixel(unsigned char x, unsigned char y, unsigned char Set)
{
unsigned int XY;
unsigned char bitByte;

	XY=0x200;
	XY=XY+(y*40);
	XY=XY+(x/6);

	
        out(0x24, XY & 0x00FF, XY>>8);
	bitByte=5-(x % 6);
        
	if (0 != Set) 
	 {
          bitByte |= 0xF8; 
         } 
        else 
         {
          bitByte|=0xF0;
         }

	out(bitByte, 0, 0);		//0b1111SXXX , s is set/reset, xxx is bit number xxx 
					//(Each memorybyte i six graphics bits (pixels))
}


void bitset(char pix)
{
PORTC = pix | 0xF8;
pause();
wr = 0;
pause();
wr = 1;
}

//Display initialisieren
/*
void t6963cinit(void)
{
out(0x98, 0, 0);
out(0x80, 0, 0);
out(0x42, 0, 0);
out(0x43, 32, 32);
out(0x24, 0, 0);
}
*/
void t6963cinit(void)
{
PORTC = 15;

out(0x42, 0, 0);
out(0x43, 32, 32);
out(0x40, 0, 0);
out(0x41, 40, 0);
out(0x80, 0, 0);
out(0xA1, 0, 0);
out(0x21, 0, 0);
out(0x9B, 0, 0);
out(0x24, 0, 0);
}

//Pixel setzen
void pset(char x,unsigned char y)
{
char pix;
unsigned char temp;
temp = x & 0xF8;
temp = temp >> 3;
y = y << 5;
temp = temp + y;
y = temp >> 8;
out(0x24, temp, y);
pix = 7 - (x & 7);
bitset(pix);
}

//Clear Screen
void cls (void)
{
char i;
char j;
out(0x24, 0, 0);
autowrite();
for(j=0;j!=128;j++)
	{	
	 for(i=0;i!=32;i++)
			{
			 lcd_data(0);
			}
        }
autoreset();
} 

void LCD_Home_Graphics(void)
{
out(0x24, 0, 0);
}

void LCD_Clear_Graphics(void)
{
unsigned int i;

	LCD_Home_Graphics();
	
	for(i=0;i<4192;i++)
		{
	         out(0xC0, 0, 0);
		}
	
	LCD_Home_Graphics();
}

void linie (char xa,char ya,char xb,char yb)
{
char x;
char y;
float fx;
float fy;
float n;
fy = yb - ya;
fx = xb - xa;
n = fy / fx;
for(x=xa;x<=xb;x++)
	{
	y = n * x;
	y = y + ya;
	pset(x,y);
	}
}


//Hauptprogramm
void main(void)
{int i=0;
DDRB = 0xFF;
PORTB = 0;
PORTC_output();
t6963cinit();
LCD_Clear_Graphics();

while (1)
      {                  
       i = i + 1;
       LCD_PutPixel(8,i,1); 
       LCD_PutPixel(i,3,1);

       pset(8,i);
       pset(i,1);
      };
}
