//ICC-AVR application builder : 2005-05-30 ¿ÀÀü 1:48:51
// Target : M128
// Crystal: 11.059Mhz

#include <iom128v.h>
#include <macros.h>

#define MaxLine	2
#define MaxCol	16

#define ON  1
#define OFF 0
#define DELAYTIME 0
#define DELAYTIME1 0
#define CS1 100
#define CS2 200 


int OneUsCounter=0;
int OneMsCounter=0;


void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0xFF;
 PORTB = 0x00;
 DDRB  = 0x00;
 PORTC = 0x00; //m103 output only
 DDRC  = 0xFF;
 PORTD = 0x00;
 DDRD  = 0xFF;
 PORTE = 0x00;
 DDRE  = 0x00;
 PORTF = 0x00;
 DDRF  = 0x00;
 PORTG = 0x00;
 DDRG  = 0x00;
}

//TIMER1 initialize - prescale:1
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1uSec
// actual value:  0.995uSec (0.5%)
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0xFF; //setup
 TCNT1L = 0xF5;
 OCR1AH = 0x00;
 OCR1AL = 0x0B;
 OCR1BH = 0x00;
 OCR1BL = 0x0B;
 OCR1CH = 0x00;
 OCR1CL = 0x0B;
 ICR1H  = 0x00;
 ICR1L  = 0x0B;
 TCCR1A = 0x00;
 TCCR1B = 0x01; //start Timer
}

#pragma interrupt_handler timer1_ovf_isr:15
void timer1_ovf_isr(void)
{
 OneUsCounter++;
 if(OneUsCounter >=1000){
 	OneMsCounter++;
	OneUsCounter=0;
 }
 //TIMER1 has overflowed
 TCNT1H = 0xFF; //reload counter high value
 TCNT1L = 0xF5; //reload counter low value
}

void delay(int delaytime){
 OneMsCounter=0;
 OneUsCounter=0;
 while(OneMsCounter<=delaytime){}
}
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
 UCSR0B = 0x00; //disable while setting baud rate
 UCSR0A = 0x00;
 UCSR0C = 0x06;
 UBRR0L = 0x47; //set baud rate lo
 UBRR0H = 0x00; //set baud rate hi
 UCSR0B = 0x18;
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 XDIV  = 0x00; //xtal divider
 XMCRA = 0x00; //external memory
 port_init();
 timer1_init();
 uart0_init();

 MCUCR = 0x00;
 EICRA = 0x00; //extended ext ints
 EICRB = 0x00; //extended ext ints
 EIMSK = 0x00;
 TIMSK = 0x04; //timer interrupt sources
 ETIMSK = 0x00; //extended timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
 OneUsCounter=0;
 OneMsCounter=0;
}

void E_Pulse(void){
 char i;
 GLCD_E(ON);
 delay(DELAYTIME1);
 GLCD_E(OFF);
}

void Func_set(void){
 GLCD_RW(OFF);
 GLCD_RS(OFF);
 PORTC=0x38;
 E_Pulse();
}

void GLCD_RS(int onoff){
 if(onoff==ON)
 	PORTD = PORTD|0x04;
 else
 	PORTD = PORTD&0xFB;  
}

void GLCD_RW(int onoff){
 if(onoff==ON)
 	PORTD = PORTD|0x08;
 else
 	PORTD = PORTD&0xF7;  
}

void GLCD_E(int onoff){
 if(onoff==ON)
 	PORTD = PORTD|0x20;
 else
 	PORTD = PORTD&0xDF;  
}

void GLCD_DI(int onoff){
 if(onoff==ON)
 	PORTD = PORTD|0x10;
 else
 	PORTD = PORTD&0xEF;  
}

void GLCD_CS2(int onoff){
 if(onoff==ON)
 	PORTD = PORTD|0x02;
 else
 	PORTD = PORTD&0xFD;  
}

void GLCD_CS1(int onoff){
 if(onoff==ON)
 	PORTD = PORTD|0x01;
 else
 	PORTD = PORTD&0xFE;  
}

void GLCD_BackLight(int onoff){
 if(onoff==ON)
 	PORTD = PORTD|0x40;
 else
 	PORTD = PORTD&0xBF;  
}

void lcd_disp(char x, char y){
 GLCD_RS(OFF);
 GLCD_RW(OFF);
 if(y==1)y++;
 else if(y==2) y--;
 if(y<=1){
 	PORTC=y*20+x+0x80;
 }
 else{
 	y=2;
	PORTC=y*20+x+0xc0;
 }
 E_Pulse();
}
 
void write_lcd(char x, char y, char *str){
 lcd_disp(x,y);
 while(*str) write_char(*str++);
}

void Init_GLcd(void){
 int i;
 GLCD_RS(OFF);
 GLCD_RS(ON);
 GLCD_E(OFF);
 GLCD_CS1(OFF);
 GLCD_CS2(OFF);
 GLCD_DI(OFF);
}

void display(int onoff){
 if(onoff==ON){
 	GLCD_CS1(ON);
	GLCD_CS2(ON);
	TestGLCDcode(0,0,0x3F);
 }else{
 	GLCD_CS1(OFF);
	GLCD_CS2(OFF);
	TestGLCDcode(0,0,0x3E);
 }
}

void write_char(char s){
 GLCD_RS(ON);
 PORTC=s;
 E_Pulse();
}

void GLCD_write_char(char chip, char data){
 switch(chip){
 	case CS1 : GLCD_CS1(ON);
		 	   break;
	case CS2 : GLCD_CS2(ON);
		 	   break;
	default  : break;
 }
 GLCD_RW(OFF);
 write_char(data);
 GLCD_CS1(OFF);
 GLCD_CS2(OFF);
}


void TestGLCDcode(int RW, int DI, char data){
 GLCD_RW(RW);
 GLCD_DI(DI);
 write_char(data);
}


void TestGLCD(void){
 int i=0,j=0, which=0;
 char font7x5[2][5]={{0x41, 0x22, 0x1C, 0x22, 0x41}, //character 'X' 
 	  			  {0x7E, 0x09, 0x09, 0x09, 0x7E}};   //character 'A'
 
 for(j=0;j<8;j++){
    TestGLCDcode(0,0,0b10111000+j); 	
	which = j%2;
  for(i=0;i<64;i++){
 	TestGLCDcode(0,0,0x40+i);	
	TestGLCDcode(0,1,font7x5[which][i%5]);	
  }
 }	
}

void TestGLCDCLR(void){
 int i=0,j=0;
 
 for(j=0;j<8;j++){
    TestGLCDcode(0,0,0b10111000+j); 	
  for(i=0;i<64;i++){
 	TestGLCDcode(0,0,0x40+i);	
	TestGLCDcode(0,1,0x00);	
  }
 }	
}

void main(void){
 int i,j;
 init_devices();
 Init_GLcd();
 display(ON);
 GLCD_BackLight(ON);
 GLCD_CS1(ON);
 GLCD_CS2(ON);
 while(1){	
	GLCD_BackLight(OFF);
	delay(50);	
	GLCD_BackLight(ON);
	TestGLCDCLR();
	TestGLCD();    	
 }	 
}