////     MOTOROLA    |      MICROCHIP      |           CCS 
////----------------------------------------------------------------------- 
////   SPI Mode 0,0  |   CKP = 0, CKE = 1  |  SPI_L_TO_H | SPI_XMIT_L_TO_H 
////   SPI Mode 0,1  |   CKP = 0, CKE = 0  |  SPI_L_TO_H 
////   SPI Mode 1,0  |   CKP = 1, CKE = 1  |  SPI_H_TO_L 
////   SPI Mode 1,1  |   CKP = 1, CKE = 0  |  SPI_H_TO_L | SPI_XMIT_L_TO_H

//// Marcus Russell
//// Russell Evolutions Inc.
//// Nokia 6100 LCD with Epson S1D15G10 Controller
////  sample initialization program for PIC16LF872 in CCS C  3-14-07
////
//// Based on Spark Fun Electronics Code by Owen Osborn 1-22-05
////   with thanks to David Carne of SFE for DISCTL help
////   and user 'Ttelmah' from CCS C forum for SSPEN bit idea for 9-bit hardware SPI
////
//// You are free to use this code for any purpose.
////
////
//// START OF HEADER FILE ////

#include <18f4550.h>

#fuses xt,NOWDT,NOPROTECT,NOLVP

#use delay(clock=4000000)
#define LCD_RESET   PIN_B3
#define LCD_CS   PIN_B2
#define SPI_CLK   PIN_B1
//#define SPI_DI   PIN_C4 //unused
#define SPI_DO   PIN_C7

#bit SSPEN = 0xFC6.5   // bit to turn on/off hardware SPI

// Epson S1D15G10 Command Set


////  START OF MAIN .C SOURCE FILE ////

//#include "<path to .h file cut from above>"
#use fast_io(C)
#use fast_io(B)

void spi_command(int);
void spi_data(int);
void pset(unsigned char color, unsigned char x, unsigned char y);


void main()
{

   int16 i;
	int k;

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_XMIT_L_TO_H|SPI_CLK_DIV_4);
   set_tris_c(0x00);
   set_tris_b(0xF0);
   // all outputs, can change to mask only hardware SPI bits if needed

   // reset display

   output_low (SPI_CLK);
   output_low (SPI_DO);
   output_high (LCD_CS);
   output_low (LCD_RESET);
   delay_ms(150);
   output_high (LCD_RESET);
   delay_ms(150);

   //Philips init

	delay_ms(150);    
	spi_command(0x11); //standby off
	delay_ms(150);
	spi_command(0x29); //display on
	delay_ms(150);
	spi_command(0x03); //booster on
	delay_ms(2500);



	
	for(;;)
	{
		spi_command(0x29);
	}
}


/*
	//SAMSUNG - itt a paramétereket is utasításként kell küldeni!

	spi_command(0x2c); //standby off
	delay_ms(150);

	spi_command(0x02); //OSC ON
	spi_command(0x01);
	delay_ms(150);


	spi_command(0x26); //DC-DC and AMP on
	spi_command(0x0f);
	delay_ms(150);

	spi_command(0x2A); //contrast 0-255
	spi_command(0xff);
	delay_ms(150);

	spi_command(0x51); // display on
	delay_ms(150);



  while (1) {  // main program loop.  just blinks A0 to show chip is running
      output_toggle(PIN_A0);
      delay_ms(500);
   }
}

*/

/*
	//EPSON

    spi_command(0xd1);  // internal oscialltor ON
    delay_ms(150);

    spi_command(0x94);  // sleep out
    delay_ms(150);

    spi_command(0x81);  // electronic volume, this is the contrast/brightness
    spi_data(0x23);   // volume (contrast) setting - fine tuning
    spi_data(0x03);   // internal resistor ratio - coarse adjustment
    delay_ms(150);

    spi_command(0x20);  // power ctrl
    spi_data(0x0f);      //everything on, no external reference resistors
    delay_ms(150);
	
	spi_command(0xaf);		//display on
    delay_ms(150);

  	while (1) {  // main program loop.  just blinks A0 to show chip is running
      output_toggle(PIN_A0);
      delay_ms(500);
   	}



*/

void spi_command(int dat){

   output_low(LCD_CS);      // enable chip
   SSPEN = 0;               // shut off hardware SPI allowing direct access to SPI in/out pins
   output_low (SPI_DO);     // output low on data out (9th bit low = command)
   output_high (SPI_CLK);
   delay_cycles(1);         // send clock pulse
   output_low (SPI_CLK);
   SSPEN=1;                 // turn hardware SPI back on
   spi_write(dat);          // make PIC do the work for the command byte
   output_high(LCD_CS);     // disable

}

void spi_data(int dat){

   output_low(LCD_CS);      // enable chip
   SSPEN = 0;               // turn off hardware SPI allowing us direct access to SPI in/out pins
   output_high (SPI_DO);    // output high on data out (9th bit high = data)
   output_high (SPI_CLK);
   delay_cycles(1);         // send clock pulse
   output_low (SPI_CLK);
   SSPEN=1;                 // turn hardware SPI back on
   spi_write(dat);          // make PIC do the work for the data byte
   output_high(LCD_CS);     // disable
}
