/**********************************************************
*   Test program for LCD Siemens C55
*   ATmega168, q=32768, RC=8MHz
*   For IAR AVR Compiler
**********************************************************/

#include "iom168.h"
#include <inavr.h>

//=== Hardware depended defines ===
#define LCD_RST_DDR DDRD_Bit4
#define LCD_RST_PORT PORTD_Bit4
#define LCD_RST_PIN PIND_Bit4

#define LCD_CE_DDR DDRD_Bit5
#define LCD_CE_PORT PORTD_Bit5
#define LCD_CE_PIN PIND_Bit5

#define LCD_DC_DDR DDRD_Bit6
#define LCD_DC_PORT PORTD_Bit6
#define LCD_DC_PIN PIND_Bit6

#define LCD_VDD_DDR DDRD_Bit7
#define LCD_VDD_PORT PORTD_Bit7
#define LCD_VDD_PIN PIND_Bit7

#define LCD_MOSI_DDR DDRB_Bit3
#define LCD_MOSI_PORT PORTB_Bit3
#define LCD_MOSI_PIN PINB_Bit3

#define LCD_SCK_DDR DDRB_Bit5
#define LCD_SCK_PORT PORTB_Bit5
#define LCD_SCK_PIN PINB_Bit5

#define LCD_SS_DDR DDRB_Bit2
#define LCD_SS_PORT PORTB_Bit2
#define LCD_SS_PIN PINB_Bit2


//=== LCD function prototypes ===
void LSend ( unsigned char, char );
void LOn(void);
void LReset (void);
void LInitSiemensC55(void);
void LInitNokia3310(void);
void LInitPCF8812(void);
void LTestOut(void);
void SInit(void);
void Delay(unsigned int);
void Delay2(unsigned int);

//=========================================================
int main(void)
{ 
  PORTB=0;  // All port to out "0" (check your device for no conflict)
  DDRB=0xFF;
  PORTC=0;
  DDRC=0xFF;
  PORTD=0;
  DDRD=0xFF;

  SInit();
  LInitSiemensC55();
//  LInitNokia3310();
//  LInitPCF8812();
  LTestOut();

  while(1); // Stop program
}
//=========================================================

//=== Test Output to LCD ===
void LTestOut(void)
{ unsigned int i;
  for ( i=0; i<(51); i++ )
  {
    LSend(0x01, 1);
    LSend(0x01, 1);
    LSend(0x02, 1);
    LSend(0x02, 1);
    LSend(0x04, 1);
    LSend(0x04, 1);
    LSend(0x08, 1);
    LSend(0x08, 1);
    LSend(0x10, 1);
    LSend(0x10, 1);
    LSend(0x20, 1);
    LSend(0x20, 1);
    LSend(0x40, 1);
    LSend(0x40, 1);
    LSend(0x80, 1);
    LSend(0x80, 1);
  };
}

//=== Send byte to LCD ===
// cd: 1 - data, 0 - command
void LSend ( unsigned char data, char cd )
{
  Delay(1);
  if ( cd == 1 ) { LCD_DC_PORT = 1; }
    else { LCD_DC_PORT = 0; };
  Delay(1);
  LCD_CE_PORT = 0;
  Delay(1);
  SPDR = data;                //  Send data to display controller.
  while (!(SPSR&(1<<SPIF))); //  Wait until Tx register empty.
  Delay(1);
  LCD_CE_PORT = 1;
  Delay(1);
}

//=== Hardware reset LCD ===
void LReset(void)
{
  LCD_DC_PORT = 1;
  Delay(1);
  LCD_CE_PORT = 1;
  Delay(1);
  LCD_RST_PORT=0; //res=0
  Delay2(1);
  LCD_RST_PORT=1; //res=1
  Delay2(1);
}

//=== LCD VDD ON ===
void LOn(void)
{
  LCD_RST_PORT=0;
  LCD_RST_DDR=1;
  LCD_VDD_PORT=0;
  LCD_VDD_DDR=1;
  LCD_CE_DDR=1;
  LCD_DC_DDR=1;
  Delay2(100);
  LCD_VDD_PORT=1;
  Delay2(100);
}

//=== Init for Siemens C55/A55 LCD - OK! ===
void LInitSiemensC55(void)
{
  LOn();
  LReset();
  Delay(1000);
  
  LSend(0x21,0); // PowerON, ExtCommandSet
  LSend(0x09,0); // Internal HV-gen x3
  LSend(0xE0,0); // Set Vop
  LSend(0x16,0); // Bias n=2    //15
  LSend(0x06,0); // Temperature coeff 2
  LSend(0x20,0); // StandartCommandSet
  LSend(0x0C,0); // normal mode, display non-inverted
  Delay(1000);
}

//=== Nokia 3310 LCD init - OK! ===
void LInitNokia3310(void)
{
  LOn();
  LReset();
  Delay(1000);
  
  LSend(0x21,0); // PowerON, ExtCommandSet
  LSend(0xC8,0); // Set Vop
  LSend(0x06,0); // Temperature coeff 2
  LSend(0x13,0); // Bias set
  LSend(0x20,0); // StandartCommandSet
  LSend(0x0C,0); // normal mode, display non-inverted
}

//=== INIT LCD with PCF8812 chip. NOT tested! ===
void LInitPCF8812(void) // or not PCF8812? ;)
{
  LOn();
  LReset();
  Delay(1000);
  
  LSend(0x21,0); // PowerON, ExtCommandSet
  LSend(0x09,0); // Internal HV-gen x3
  LSend(0xE0,0); // Set Vop
  LSend(0x16,0); // Bias n=2    //15
  LSend(0x06,0); // Temperature coeff 2
  LSend(0x20,0); // StandartCommandSet
  LSend(0x0C,0); // normal mode, display non-inverted
  Delay(1000);
}

//=== SPI init ===
void SInit(void)
{
  PORTB=0x00;
  LCD_MOSI_DDR=1;
  LCD_SCK_DDR=1;
  LCD_SS_DDR=1; // SS Pin need set to output direction
  SPSR=0;
  SPCR=0x53;  // SCK=Fosc:128
  Delay2(50);
  unsigned char i=SPSR;
}

//=== Delay ===
void Delay(unsigned int delay)  //delay ~ in uS for CPUclk=8MHz
{ unsigned int i;
  for (i=delay; i>0; i--)
  { __watchdog_reset();
    __no_operation();
  };   
}

//=== Delay ===
void Delay2(unsigned int delay)  //delay ~ in mS for CPUclk=8MHz
{ unsigned int i;
  for (i=delay; i>0; i--)
  { Delay(1000);
  };   
}
