//*******************************************************************************
//
// Test SED 1520 LCD Ansteuerung
// 4.8.03 untere Bilschirmhälfte blinkt, ...es tut sich was
//
//******************************************************************************

#include  <msp430x14x.h>

#define A0 0x01;
#define RW 0x02;
#define E1 0x04;
#define E2 0x08;

//;------------------------------------------------------------------------------
//; Konstanten für Display Komandos
//;------------------------------------------------------------------------------

#define LCD_SET_COLUMN      0x00 // Set current column
#define LCD_ADC_NORMAL      0xa0 // Set normal RAM <-> SEG
#define LCD_ADC_REVERSE     0xa1 // Set reverse RAM <-> SEG
#define LCD_ALL_PIXELS_OFF  0xa4 // Disable 'all pixels lit'
#define LCD_ALL_PIXELS_ON   0xa5 // Enable 'all pixels lit'
#define LCD_DISPLAY_NORMAL  0xa6 // Set normal LCD display
#define LCD_DISPLAY_REVERSE 0xa7 // Set reverse LCD display
#define LCD_DUTY_SELECT     0xa9 // Duty select
#define LCD_DISPLAY_OFF     0xae // Disable LCD display
#define LCD_DISPLAY_ON      0xaf // Enable LCD display
#define LCD_SET_PAGE        0xb8 // last 2 bits - page
#define LCD_SET_STARTLINE   0xc0 // Set start line (last 5 bits)
#define LCD_RMW_ON          0xe0 // Enable 'Read/Modify/Write'
#define LCD_RESET           0xe2 // Internal reset
#define LCD_RMW_OFF         0xee // Disable 'Read/Modify/Write'
// Enter 'power save' mode: LCD_DISPLAY_OFF and LCD_ALL_PIXELS_ON

#define LCDDATAin P4IN
#define LCDDATAout P4OUT
#define LCDDATAdir P4DIR
#define LCDCOM P5OUT
#define LCDCOMdir P5DIR

void delay2(unsigned int d);
void LCD_out(char rs, char dat) ;
void LCD_Initialize();

 char MyChar = 0;
 char AddChar = 0;
int lcd_error = 0;
 unsigned int i;

void main(void)
{ 
  WDTCTL = WDTPW + WDTHOLD;             // Stop watchdog timer
  LCDDATAdir = 0xff;                        // output direction
  LCDCOMdir = 0xff;
  P1DIR = 0xff;  
  LCD_Initialize();  
  //LCD_out( 0 , char dat) 
  
   
  LCD_out( 0 ,(LCD_SET_STARTLINE | 0 ));
  LCD_out( 0 ,(LCD_SET_PAGE | 0 )); 
  LCD_out( 0 ,(LCD_SET_COLUMN | 0 )); 
  
  
////



  WDTCTL = WDTPW + WDTHOLD;             // Stop watchdog timer
/***********************************************************  
 * Quartzoszillator-Initialisierung aus  FET140_UART01
 * Programmbeispiel
 *
 *
 ************************************************************/
  BCSCTL1 |= XTS;                       // ACLK = LFXT1 = HF XTAL
  do 
  {
  IFG1 &= ~OFIFG;                       // Clear OSCFault flag
  for (i = 0xFF; i > 0; i--);           // Time for flag to set
  }
  while ((IFG1 & OFIFG) == OFIFG);      // OSCFault flag still set?                
  BCSCTL2 |= SELM1+SELM0;               // MCLK = LFXT1 (safe)

  P1DIR  = 0xC0;                        // Set P1.6, P1.7 to output direction
  P1SEL  = 0x40;                        // P1.6 = TA1
  P5OUT  = 0xC0;                        // I2C High, LCD ein, alle anderen Low 
  P5DIR  = 0xFF;                        // Alles Ausgang

  TACTL = SSEL0 + TASSEL1 + TACLR;      // TACLK, Clear Tar
  CCR0 = 256-1;                         // PWM Period
  CCTL1 = OUTMOD_7;                     // CCR1 reset/set
// Zur Zeit zwischen -5 und -6.5 V einstellbar,
// Einstellbereich sollte bei -4.5 V beginnen,
// Schaltungsanpassung erforderlich  
  CCR1 = 100;                             // CCR1 PWM duty cycle = -5.0 V

  TACTL |= MC0;                         // Start Timer_A in up mode


////  
  
  for ( AddChar = 0; AddChar <= 254; AddChar++)
  {
    P1OUT ^= 0x01;   //blink !! 
  
  for (MyChar = 0;MyChar<=254; MyChar++)                              
  {

    //LCD_out( 0 ,LCD_ALL_PIXELS_OFF); 
   //  P1OUT ^= 0x01;   //blink !!  
   // delay2(30000);

    
    
    LCD_out( 0x0c, MyChar + AddChar ); 
    
    LCD_out( 0 ,(LCD_SET_PAGE | (MyChar/64) ));
    LCD_out( 0 ,(LCD_SET_COLUMN | (MyChar&0x3f) ));
   // delay2(30000);
  }
  }
}
 
void LCD_delete()
{
char theDelChar=0;
 for ( ;theDelChar <= 0x3f;theDelChar++)                              
  {

    //LCD_out( 0 ,LCD_ALL_PIXELS_OFF);
   //  P1OUT ^= 0x01;   //blink !!  
   // delay2(30000);
 
    LCD_out( 0 ,(LCD_SET_COLUMN | (theDelChar) ));
    
    LCD_out( 0 ,(LCD_SET_PAGE | (0) ));
    LCD_out( 0x0c, 0x00 ); 
    //delay2(1000);
    LCD_out( 0 ,(LCD_SET_COLUMN | (theDelChar) ));
    LCD_out( 0 ,(LCD_SET_PAGE | (1) ));
    LCD_out( 0x0c, 0x00 ); 
    //delay2(1000);
    LCD_out( 0 ,(LCD_SET_COLUMN | (theDelChar) ));
    LCD_out( 0 ,(LCD_SET_PAGE | (2) ));
    LCD_out( 0x0c, 0x00 ); 
   // delay2(1000);
    LCD_out( 0 ,(LCD_SET_COLUMN | (theDelChar) ));
    LCD_out( 0 ,(LCD_SET_PAGE | (3) ));
    LCD_out( 0x0c, 0x00 );   
   // delay2(1000);
  
  }

}
// LCD_SendCommand(LCD_SETPAGE | 3);
//   LCD_SendCommand(LCD_SETCOLUMN | 30);

void LCD_Initialize()
{
  
  LCD_out(0,LCD_RESET);
  delay2(10000);
  LCD_out(0,LCD_DISPLAY_ON);
  LCD_out(0,LCD_ADC_NORMAL); 
 
  //LCD_out(0,LCD_ALL_PIXELS_OFF);
  LCD_out(0,LCD_DUTY_SELECT | 0x01);
  LCD_delete();
  
}

void LCD_out(char rs, char dat)  // rs> dann Commando, sonst ist rs der Chip
{
  char getdat;
  char dummy;
  char try_count;

  LCDDATAdir = 0x0;                        
  LCDCOMdir = 0xff;

  LCDCOM &= ~A0;          // Read status
  LCDCOM &= ~E1;          
  LCDCOM &= ~E2;          
 
  
 /* LCDCOM |= RW;           //
  //LCDDATAout &= 0x00;     //

  try_count = 0;
  do 
  {
    LCDCOM |= E1;   
    getdat = LCDDATAin;
    LCDCOM &= ~E1; 
    try_count++;
    if (try_count>100) 
    {
      LCDDATAdir = 0xff;
      lcd_error = 1;
      return;
    }
  } 
  while (getdat & 0x80);
  // jetzt kann geschrieben werden

*/
  LCDDATAdir = 0xff;
  //lcd_rs = rs;
  LCDDATAout = dat;
  LCDCOM &= ~RW;
  LCDCOM &= ~A0;
  if (rs)
  {
    LCDCOM |= A0;
  } 
  else
  {
    rs = 0x04 | 0x08 ;
  }

  LCDCOM |= rs; 
  delay2(10);
  LCDCOM &= ~rs;
  delay2(10);


}


/////////////////////////////////////////////////////////////////
// selects a page (=row) on both sed1520s
//

void selectpage (int page)
{  
    //LCD_out((E1 + E2), (LCD_SET_PAGE + (page & 3))) ;
}

/////////////////////////////////////////////////////////////////
// selects a column on the sed1520s specified by chip
//

void selectcolumn (int column, int chip)
{
    LCD_out (chip,(column & 0x7F));
}



void delay2(unsigned int d) 
{
  int i;
  for (i = 0; i<d; i++) {
    int x = 0;
  }
}
