//ICC-AVR application builder : 2007-12-21 ¿ÀÀü 10:36:07
//ICC-AVR application builder : 2007-12-21 ¿ÀÀü 9:11:27
// Target : M128
// Crystal: 16.000Mhz

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

#define Control (*(volatile unsigned char *)0x8001)
#define Data (*(volatile unsigned char *)0x8000)


#define LCD_EN_ON	(Control = (Controller |= 0x04))
#define LCD_EN_OFF	(Control = (Controller &= 0x0b))
#define LCD_RW_ON	(Control = (Controller |= 0x02))
#define LCD_RW_OFF	(Control = (Controller &= 0x0d))
#define LCD_RS_ON	(Control = (Controller |= 0x01))
#define LCD_RS_OFF	(Control = (Controller &= 0x0e))

#define	 SCR_L_SHFT	   0x18	  
#define	 SCR_R_SHFT	   0x1c

char Controller;


void port_init(void)
{

 PORTA = 0x00;
 DDRA  = 0x00;
 PORTB = 0x00;
 DDRB  = 0x00;
 PORTC = 0x00;
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
 PORTE = 0x00;
 DDRE  = 0x00;
 PORTF = 0x00;
 DDRF  = 0x00;
 PORTG = 0x00;
 DDRG  = 0x00;

}

//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();

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

void delay(int cnt)
{
  int i, j;
  for (i=0; i<cnt; i++)
    for (j=0; j<1000; j++) {;}
}

void Write_Controller(char value)
{
  LCD_EN_OFF; 
  delay(1);
  LCD_RS_OFF; 
  delay(1);
  LCD_RW_OFF; 
  delay(1);
  LCD_EN_ON;  
  delay(1);
  Data = value;   
  delay(1);
  LCD_EN_OFF; 
  delay(1);
}


void Write_Data(char value)
{
  LCD_EN_OFF; 
  delay(1);
  LCD_RS_ON; 
  delay(1);
  LCD_RW_OFF; 
  delay(1);
  LCD_EN_ON;  
  delay(1);
  Data = value;   
  delay(1);
  LCD_EN_OFF; 
  delay(1);
}

void lcd_init(){
  Write_Controller(0x38); 
  Write_Controller(0x0e); 
  Write_Controller(0x06); 
  }
void main(void)
{
  int count=0;  
  init_devices();
  lcd_init();
  Write_Controller(0x01);
  Write_Controller(0x02);
  
  Write_Data('h');
  Write_Data('i');
  Write_Data('~');
  Write_Data('p');
  Write_Data('a');
  Write_Data('r');
  Write_Data('k');
  
  Write_Controller(0xc0);
  Write_Data('T');
  Write_Data('h');
  Write_Data('a');
  Write_Data('n');
  Write_Data('k');
  Write_Data('!');
  while(1){
  if(count<9){
  Write_Controller(SCR_R_SHFT);
  delay(1000);
  count++;
  } else {
  Write_Controller(SCR_L_SHFT);
  delay(100);
  if(count>16){count = -1;}
  count++;
  }
  }
  }

















