Graphical LCD

void WriteCmd (uint8_t cmd)
{
 SPI_PORT&=~(1<<SPI_DC_PIN);    
 // clear pin B1=DC; 0=command, 1=data
 SPDR = cmd; // initiate transfer
 while(!(SPSR&(1<<SPIF)));
 SPI_PORT|=(1<<SPI_DC_PIN);     
 // set pin return DC high
}
void WriteByte (uint8_t b)
{
 SPDR = b; // initiate transfer
 while(!(SPSR&(1<<SPIF)));
}
void InitDisplay(void)
{
     WriteCmd(SWRESET);      // SWRESET  = 0x01
     _delay_ms(1);           // 1mS is enough
     WriteCmd(SWRESET);      // SWRESET  = 0x01
     _delay_ms(150);         // wait 150mS for reset to finish
     WriteCmd(SLPOUT);       // take display out of sleep mode, SLPOUT =  0x11
     _delay_ms(150);         // wait 150mS for TFT driver circuits
     WriteCmd(COLMOD);       // select color mode, COLMOD  = 0x3A
     WriteByte(0x05);        // mode 5 = 16bit pixels (RGB565)
     WriteCmd(DISPON);       // turn display on, DISPON = 0x29
}
void Write565 (int data, unsigned int count)
{
WriteCmd(RAMWR); // memory write command, RAMWR = 0x2C
for (;count>0;count--)
	{
      SPDR = data >> 8 ;   // write upper 8 bits
      while(!(SPSR&(1<<SPIF)));   
	  SPDR = data & 0xFF;  // write lower 8 bit
      while(!(SPSR&(1<<SPIF)));
	}
}
void SetAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1)
{
  WriteCmd(CASET); // set column range (x0,x1), CASET = 0x2A
  WriteWord(x0);
  WriteWord(x1);
  WriteCmd(RASET); // set row range (y0,y1), RASET = 0x2B
  WriteWord(y0);
  WriteWord(y1);
}
void DrawPixel (uint8_t x, uint8_t y, int color) //Pixel On/Off
{
	SetAddrWindow(x,y,x,y);
	Write565(color,1);
}
const uint8_t FONT_CHARS[9][5] PROGMEM ={
     { 0x3E, 0x51, 0x49, 0x45, 0x3E }, // 0
     { 0x00, 0x42, 0x7F, 0x40, 0x00 }, // 1
     { 0x42, 0x61, 0x51, 0x49, 0x46 }, // 2
     { 0x21, 0x41, 0x45, 0x4B, 0x31 }, // 3
     { 0x18, 0x14, 0x12, 0x7F, 0x10 }, // 4
     { 0x27, 0x45, 0x45, 0x45, 0x39 }, // 5
     { 0x3C, 0x4A, 0x49, 0x49, 0x30 }, // 6
     { 0x01, 0x71, 0x09, 0x05, 0x03 }, // 7
     { 0x36, 0x49, 0x49, 0x49, 0x36 }, // 8
     { 0x06, 0x49, 0x49, 0x29, 0x1E }, // 9
}
void PutCh (char ch, uint8_t x, uint8_t y, int color)
{
  int pixel;
  uint8_t row, col, bit, data, mask = 0x01;            
  SetAddrWindow(x,y,x+4,y+6);
  WriteCmd(RAMWR);
  for (row=0; row<7;row++)
      {
      for (col=0; col<5;col++)
           {
             data = pgm_read_byte(&(FONT_CHARS[ch][col]));
             bit = data & mask;
             if (bit==0) pixel=BLACK;
             else pixel=color;
             WriteWord(pixel);
           } mask <<= 1;
      }
}
void ClearScreen(void)
{
  SetAddrWindow(0,0,XMAX,YMAX); // set window to entire display
  WriteCmd(RAMWR);              // memory write command, RAMWR = 0x2C
  for (unsigned int i=40960;i>0;--i) // byte count = 128*160*2
	{
	   SPDR = 0 ;
       while(!(SPSR&(1<<SPIF)));
	}
}
void SetOrientation(int degrees)
{
  uint8_t arg;
  switch (degrees)
	{
	case 90: arg = 0x60; break;
	case 180: arg = 0xC0; break;
	case 270: arg = 0xA0; break;
	default: arg = 0x00; break;
	}
  WriteCmd(MADCTL); //axis control, MADCTL = 0x36
  WriteByte(arg);
}
/***************************************************************/
/********      Subeer Kumar Sarkar                    **********/
/********      Electrical & Electronic Engineer       **********/
/***************************************************************/
/********   Program for ghraphical display interface  **********/
/***************************************************************/
/***************************************************************/
  #include<avr/io.h>
  #include<util/delay.h>
  #include"TFTLCD.h"
  int main(void)
  {
    init_spi();    //Initilize SPI
    InitDisplay(); // initialize TFT controller
    _delay_ms(500);
    ClearScreen();
    SetOrientation(90);// value should 90,180,270
    DDRB|=(1<<DDB7);   //For load 
    char *str = "Subeer Kumar Sarkar"; // text to display
    GotoXY(2,1); // position text cursor
    WriteString(str,GREEN); // display text
    char *ptr = "RUET EEE 091109"; // text to display
    WriteStringXY(2,2,ptr,MAGENTA); // display text 
    DrawRect(12,8,126,24,RED);
    while(1)
	   {
		PORTB|=(1<<PB7);  
		FillCircle(20,40,12,GREEN);
		WriteStringXY(6,4,"LOAD ON ",BLUE); // display text
		_delay_ms(3000);
		PORTB&=~(1<<PB7);  
		FillCircle(20,40,12,RED);
		WriteStringXY(6,4,"LOAD OFF",BLUE); // display text
		_delay_ms(3000);
	}
 return 0;
}