//************************************************************************************
//**  
//**  File name:     C:\Documents and Settings\Administrator\Desktop\EB-058.c
//**  Generated by:  Flowcode v4.4.10.66
//**  Date:          Thursday, January 13, 2011 15:13:58
//**  Licence:       
//**  
//**        ***DEMO VERSION***
//**  
//**  
//**  http://www.matrixmultimedia.com
//************************************************************************************


#define MX_AVR

//Defines for microcontroller
#define MX_AVR
#define MX_EE
#define MX_EE_SIZE 1024
#define MX_SPI
#define MX_SPI_B
#define MX_SPI_SDI 6
#define MX_SPI_SDO 5
#define MX_SPI_SCK 7
#define MX_UART
#define MX_UART_ID
#define MX_UART_UCSRC
#define MX_UART_D
#define MX_UART_TX 1
#define MX_UART_RX 0
#define MX_MI2C
#define MX_I2C_C
#define MX_I2C_SDA 1
#define MX_I2C_SCL 0
#define MX_PWM
#define MX_PWM_PORT PORTD
#define MX_PWM_CNT 2
#define MX_PWM_TRIS1 DDRD
#define MX_PWM_1 5
#define MX_PWM_TRIS2 DDRD
#define MX_PWM_2 4
#define MX_PWMTYPE 1

//Functions
#define F_CPU 20000000UL
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <avr\io.h>
#include <avr\interrupt.h>
#include <avr\eeprom.h>
#include <avr\wdt.h>
#include <MX_util\delay.h>
#include <MX_util\bit_cmds.h>

//Configuration data
#pragma DATA 0x0, 0xdf
#pragma DATA 0x1, 0xff
#pragma DATA 0x2, 0xff

//Internal functions
#include "C:\Program Files\Matrix Multimedia\Flowcode AVR V4\FCD\internals.h"

//Macro function declarations


//Variable declarations


//Defines:

/**** Macro % Substitutions ****
a = gLCD Port Register
b = gLCD Data Direction Register
c = Reset Pin
d = Chip Select Pin
e = Serial Clock Pin
f = Serial Data Pin
g = Default Background Colour
h = Default Foreground Colour
i = Size of display
j - q = ASCII character bitmap arrays
1 = LCD Type
******************************/

#include <string.h>

//Common Defines
#define MX_GFXLCD_PORT       PORTA            //PORTC   I/O port
#define MX_GFXLCD_DDR        DDRA            //DDRC    I/O direction register
#define MX_GFXLCD_RS         3            //3       Reset
#define MX_GFXLCD_CS         2            //2       Chip Select
#define MX_GFXLCD_SCLK       1            //1       Serial Clock
#define MX_GFXLCD_SDATA      0            //0       Serial Data
#define MX_GFXLCD_BCOL       252            //255     Background Color - White
#define MX_GFXLCD_FCOL       0            //0       Foreground Color - Black
#define MX_GFXLCD_SIZE       0            //0       0 = 132x132
#define MX_GCXLCD_TYPE	 	 1			   //0       0 = EB043, 1 = EB057

//ASCII Pixel data
const char ASCII1[] =
{
    0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,       // 32 = <space>
    0x00 , 0x06 , 0x5F , 0x06 , 0x00 ,       // 33 = !
    0x07 , 0x03 , 0x00 , 0x07 , 0x03 ,       // 34 = "
    0x24 , 0x7E , 0x24 , 0x7E , 0x24 ,       // 35 = #
    0x24 , 0x2B , 0x6A , 0x12 , 0x00 ,       // 36 = $
    0x63 , 0x13 , 0x08 , 0x64 , 0x63 ,       // 37 = %
    0x36 , 0x49 , 0x56 , 0x20 , 0x50 ,       // 38 = &
    0x00 , 0x07 , 0x03 , 0x00 , 0x00 ,       // 39 = '
    0x00 , 0x3E , 0x41 , 0x00 , 0x00 ,       // 40 = (
    0x00 , 0x41 , 0x3E , 0x00 , 0x00 ,       // 41 = )
    0x08 , 0x3E , 0x1C , 0x3E , 0x08 ,       // 42 = *
    0x08 , 0x08 , 0x3E , 0x08 , 0x08         // 43 = +
};

const char ASCII2[] =
{
    0x00 , 0xE0 , 0x60 , 0x00 , 0x00 ,       // 44 = ,
    0x08 , 0x08 , 0x08 , 0x08 , 0x08 ,       // 45 = -
    0x00 , 0x60 , 0x60 , 0x00 , 0x00 ,       // 46 = .
    0x20 , 0x10 , 0x08 , 0x04 , 0x02 ,       // 47 = /
    0x3E , 0x51 , 0x49 , 0x45 , 0x3E ,       // 48 = 0
    0x00 , 0x42 , 0x7F , 0x40 , 0x00 ,       // 49 = 1
    0x62 , 0x51 , 0x49 , 0x49 , 0x46 ,       // 50 = 2
    0x22 , 0x49 , 0x49 , 0x49 , 0x36 ,       // 51 = 3
    0x18 , 0x14 , 0x12 , 0x7F , 0x10 ,       // 52 = 4
    0x2F , 0x49 , 0x49 , 0x49 , 0x31 ,       // 53 = 5
    0x3C , 0x4A , 0x49 , 0x49 , 0x30 ,       // 54 = 6
    0x01 , 0x71 , 0x09 , 0x05 , 0x03         // 55 = 7
};

const char ASCII3[] =
{
    0x36 , 0x49 , 0x49 , 0x49 , 0x36 ,       // 56 = 8
    0x06 , 0x49 , 0x49 , 0x29 , 0x1E ,       // 57 = 9
    0x00 , 0x6C , 0x6C , 0x00 , 0x00 ,       // 58 = :
    0x00 , 0xEC , 0x6C , 0x00 , 0x00 ,       // 59 = ;
    0x08 , 0x14 , 0x22 , 0x41 , 0x00 ,       // 60 = <
    0x24 , 0x24 , 0x24 , 0x24 , 0x24 ,       // 61 = =
    0x00 , 0x41 , 0x22 , 0x14 , 0x08 ,       // 62 = >
    0x02 , 0x01 , 0x59 , 0x09 , 0x06 ,       // 63 = ?
    0x3E , 0x41 , 0x5D , 0x55 , 0x1E ,       // 64 = @
    0x7E , 0x09 , 0x09 , 0x09 , 0x7E ,       // 65 = A
    0x7F , 0x49 , 0x49 , 0x49 , 0x36 ,       // 66 = B
    0x3E , 0x41 , 0x41 , 0x41 , 0x22         // 67 = C
};

const char ASCII4[] =
{
    0x7F , 0x41 , 0x41 , 0x41 , 0x3E ,       // 68 = D
    0x7F , 0x49 , 0x49 , 0x49 , 0x41 ,       // 69 = E
    0x7F , 0x09 , 0x09 , 0x09 , 0x01 ,       // 70 = F
    0x3E , 0x41 , 0x49 , 0x49 , 0x7A ,       // 71 = G
    0x7F , 0x08 , 0x08 , 0x08 , 0x7F ,       // 72 = H
    0x00 , 0x41 , 0x7F , 0x41 , 0x00 ,       // 73 = I
    0x30 , 0x40 , 0x40 , 0x40 , 0x3F ,       // 74 = J
    0x7F , 0x08 , 0x14 , 0x22 , 0x41 ,       // 75 = K
    0x7F , 0x40 , 0x40 , 0x40 , 0x40 ,       // 76 = L
    0x7F , 0x02 , 0x04 , 0x02 , 0x7F ,       // 77 = M
    0x7F , 0x02 , 0x04 , 0x08 , 0x7F ,       // 78 = N
    0x3E , 0x41 , 0x41 , 0x41 , 0x3E         // 79 = O
};

const char ASCII5[] =
{
    0x7F , 0x09 , 0x09 , 0x09 , 0x06 ,       // 80 = P
    0x3E , 0x41 , 0x51 , 0x21 , 0x5E ,       // 81 = Q
    0x7F , 0x09 , 0x09 , 0x19 , 0x66 ,       // 82 = R
    0x26 , 0x49 , 0x49 , 0x49 , 0x32 ,       // 83 = S
    0x01 , 0x01 , 0x7F , 0x01 , 0x01 ,       // 84 = T
    0x3F , 0x40 , 0x40 , 0x40 , 0x3F ,       // 85 = U
    0x1F , 0x20 , 0x40 , 0x20 , 0x1F ,       // 86 = V
    0x3F , 0x40 , 0x3C , 0x40 , 0x3F ,       // 87 = W
    0x63 , 0x14 , 0x08 , 0x14 , 0x63 ,       // 88 = X
    0x07 , 0x08 , 0x70 , 0x08 , 0x07 ,       // 89 = Y
    0x71 , 0x49 , 0x45 , 0x43 , 0x00 ,       // 90 = Z
    0x00 , 0x7F , 0x41 , 0x41 , 0x00         // 91 = [
};

const char ASCII6[] =
{
    0x02 , 0x04 , 0x08 , 0x10 , 0x20 ,       // 92 = <backslash>
    0x00 , 0x41 , 0x41 , 0x7F , 0x00 ,       // 93 = ]
    0x04 , 0x02 , 0x01 , 0x02 , 0x04 ,       // 94 = ^
    0x80 , 0x80 , 0x80 , 0x80 , 0x80 ,       // 95 = _
    0x00 , 0x03 , 0x07 , 0x00 , 0x00 ,       // 96 = `
    0x20 , 0x54 , 0x54 , 0x54 , 0x78 ,       // 97 = a
    0x7F , 0x44 , 0x44 , 0x44 , 0x38 ,       // 98 = b
    0x38 , 0x44 , 0x44 , 0x44 , 0x28 ,       // 99 = c
    0x38 , 0x44 , 0x44 , 0x44 , 0x7F ,       // 100 = d
    0x38 , 0x54 , 0x54 , 0x54 , 0x18 ,       // 101 = e
    0x08 , 0x7E , 0x09 , 0x09 , 0x00 ,       // 102 = f
    0x18 , 0xA4 , 0xA4 , 0xA4 , 0x7C         // 103 = g
};

const char ASCII7[] =
{
    0x7F , 0x04 , 0x04 , 0x78 , 0x00 ,       // 104 = h
    0x00 , 0x00 , 0x7D , 0x00 , 0x00 ,       // 105 = i
    0x40 , 0x80 , 0x84 , 0x7D , 0x00 ,       // 106 = j
    0x7F , 0x10 , 0x28 , 0x44 , 0x00 ,       // 107 = k
    0x00 , 0x00 , 0x7F , 0x40 , 0x00 ,       // 108 = l
    0x7C , 0x04 , 0x18 , 0x04 , 0x78 ,       // 109 = m
    0x7C , 0x04 , 0x04 , 0x78 , 0x00 ,       // 110 = n
    0x38 , 0x44 , 0x44 , 0x44 , 0x38 ,       // 111 = o
    0xFC , 0x44 , 0x44 , 0x44 , 0x38 ,       // 112 = p
    0x38 , 0x44 , 0x44 , 0x44 , 0xFC ,       // 113 = q
    0x44 , 0x78 , 0x44 , 0x04 , 0x08 ,       // 114 = r
    0x08 , 0x54 , 0x54 , 0x54 , 0x20         // 115 = s
};

const char ASCII8[] =
{
    0x04 , 0x3E , 0x44 , 0x24 , 0x00 ,       // 116 = t
    0x3C , 0x40 , 0x20 , 0x7C , 0x00 ,       // 117 = u
    0x1C , 0x20 , 0x40 , 0x20 , 0x1C ,       // 118 = v
    0x3C , 0x60 , 0x30 , 0x60 , 0x3C ,       // 119 = w
    0x6C , 0x10 , 0x10 , 0x6C , 0x00 ,       // 120 = x
    0x9C , 0xA0 , 0x60 , 0x3C , 0x00 ,       // 121 = y
    0x64 , 0x54 , 0x54 , 0x4C , 0x00 ,       // 122 = z
    0x08 , 0x3E , 0x41 , 0x41 , 0x00 ,       // 123 = {
    0x00 , 0x00 , 0x7F , 0x00 , 0x00 ,       // 124 = |
    0x00 , 0x41 , 0x41 , 0x3E , 0x08 ,       // 125 = }
    0x02 , 0x01 , 0x02 , 0x01 , 0x00         // 126 = ~
};

#define MX_GFXLCD_CMD       0        //A "0" tells the display: now a Command is following
#define MX_GFXLCD_PARAM     1        //A "1" tells the display: now a Parameter is following


//Global Variables

#if MX_GCXLCD_TYPE == 0

	//EB043 ONLY

	//Color Data
	char GFX_Fore_Color;
	char GFX_Back_Color;

#else


	//EB057 ONLY

	//I2C I/O Expander Command Defines
	#define DDR	0x00
	#define PORT	0x12
	#define CONF	0x0A

	//Pin Definitions
	#define CS	0x08
	#define RESET	0x10
	#define RS	0x04
	#define RW	0x02
	#define EN	0x01

	//global variables
	char Data = 0;
	char Control = RESET | CS;

	//LCD Command Defines
	#define LCD_Data		Control = Control | RS;			//Set DC
	#define LCD_Command		Control = Control & ~RS;		//Clear DC
	#define LCD_Read		Control = Control | RW;			//Set DC
	#define LCD_Write		Control = Control & ~RW;		//Clear DC

	//SPI COMPONENT DEFINES
	#define SPI_SPR		1
	#define SPI_CPOL	0
	#define SPI_CPHA	0

  #ifdef MX_SPI_A
	#define SPI_CS_PORT PORTA
	#define SPI_CS_DDR DDRA
  #endif
  #ifdef MX_SPI_B
	#define SPI_CS_PORT PORTB
	#define SPI_CS_DDR DDRB
  #endif
  #ifdef MX_SPI_C
	#define SPI_CS_PORT PORTC
	#define SPI_CS_DDR DDRC
  #endif
  #ifdef MX_SPI_D
	#define SPI_CS_PORT PORTD
	#define SPI_CS_DDR DDRD
  #endif
  #ifdef MX_SPI_E
	#define SPI_CS_PORT PORTE
	#define SPI_CS_DDR DDRE
  #endif
  #ifdef MX_SPI_F
	#define SPI_CS_PORT PORTF
	#define SPI_CS_DDR DDRF
  #endif
  #ifdef MX_SPI_G
	#define SPI_CS_PORT PORTG
    #define SPI_CS_DDR DDRG
  #endif
  #ifdef MX_SPI_H
	#define SPI_CS_PORT PORTH
	#define SPI_CS_DDR DDRH
  #endif
  #ifdef MX_SPI_I
	#define SPI_CS_PORT PORTI
	#define SPI_CS_DDR DDRI
  #endif
  #ifdef MX_SPI_J
	#define SPI_CS_PORT PORTJ
	#define SPI_CS_DDR DDRJ
  #endif
  #ifdef MX_SPI_K
	#define SPI_CS_PORT PORTK
	#define SPI_CS_DDR DDRK
  #endif
  #ifdef MX_SPI_L
	#define SPI_CS_PORT PORTL
	#define SPI_CS_DDR DDRL
  #endif

	short GFX_Fore_Color;
	short GFX_Back_Color;

#endif



//Color Data
char DataInit[20] = {0x00,0x02,0x04,0x06,0x09,0x0B,0x0D,0x0F,0x00,0x02,0x04,0x06,0x09,0x0B,0x0D,0x0F,0x00,0x04,0x0B,0x0F};




//gLCD(0): //Macro function declarations

static void FCD_gLCD0_gLCD_Window(char X1, char Y1, char X2, char Y2);
static void FCD_gLCD0_gLCD_SendByte(char Command, char Lcdout);
static void FCD_gLCD0_Init();
static void FCD_gLCD0_Clear();
static void FCD_gLCD0_Print(char* String, char MSZ_String, char X, char Y, char Font, char Transparent);
static void FCD_gLCD0_PrintNumber(short Number, char X, char Y, char Font, char Transparent);
static void FCD_gLCD0_Plot(char X1, char Y1);
static void FCD_gLCD0_DrawLine(char X1, char Y1, char X2, char Y2);
static void FCD_gLCD0_DrawBox(char X1, char Y1, char X2, char Y2, char Transparent, char Solid);
static void FCD_gLCD0_SetForeColor(char Red, char Green, char Blue);
static void FCD_gLCD0_SetBackColor(char Red, char Green, char Blue);
static void FCD_gLCD0_SPI_Init();
static void FCD_gLCD0_SPI_Send_Char(char Char);
static void FCD_gLCD0_Set_Data_Direction(char mode);
static void FCD_gLCD0_StrobeE();
static void FCD_gLCD0_Update_Slave();
static void FCD_gLCD0_CommandWithData(char Command, char Data);
static void FCD_gLCD0_CommandWith2Data(char Command, char Data1, char Data2);



//gLCD(0): //Macro implementations


static void FCD_gLCD0_gLCD_Window(char X1, char Y1, char X2, char Y2)
{
	    FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_CMD, 0x2A);        //Column adress
	    FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_PARAM, X1);        //X-Position upper left corner
	    FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_PARAM, X2);        //y-Position upper left corner
	    FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_CMD, 0x2B);        //Page adressing
	    FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_PARAM, Y1);        //x-Position of lower right corner
	    FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_PARAM, Y2);        //y-Position of lower right corner
	    FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_CMD, 0x2C);
}

static void FCD_gLCD0_gLCD_SendByte(char Command, char Lcdout)
{
	
	   #if MX_GCXLCD_TYPE == 0

			//EB043 ONLY
			char i;

			clear_bit(MX_GFXLCD_PORT, MX_GFXLCD_CS);              //MX_GFXLCD_CS 0 start of sequence
			clear_bit(MX_GFXLCD_PORT, MX_GFXLCD_SCLK);            //Clock 0

			if (Command == MX_GFXLCD_CMD)
			{
				clear_bit(MX_GFXLCD_PORT, MX_GFXLCD_SDATA);       //0 = MX_GFXLCD_CMD
			}
			else
			{
				set_bit(MX_GFXLCD_PORT, MX_GFXLCD_SDATA);         //1 = MX_GFXLCD_PARAMeter
			}

			set_bit(MX_GFXLCD_PORT, MX_GFXLCD_SCLK);

			for (i=0;i<8;i++)
			{
				clear_bit(MX_GFXLCD_PORT, MX_GFXLCD_SCLK);        //Clock 0

				if (Lcdout & 0x80)
					set_bit(MX_GFXLCD_PORT, MX_GFXLCD_SDATA);     //Output MSB

				else
					clear_bit(MX_GFXLCD_PORT, MX_GFXLCD_SDATA);

				set_bit(MX_GFXLCD_PORT, MX_GFXLCD_SCLK);          //Clock 1
				Lcdout = Lcdout << 1;                             //Shift Data to left

			}
			set_bit(MX_GFXLCD_PORT, MX_GFXLCD_CS);                //MX_GFXLCD_CS to 1 at end of sequence

		#else

			//EB057 ONLY
			if (Command == MX_GFXLCD_CMD)
			{
				LCD_Command;
			}
			else
			{
				LCD_Data;
			}
			LCD_Write;
			Data = Lcdout;
			FCD_gLCD0_StrobeE();

		#endif
}

static void FCD_gLCD0_Init()
{
	
		#if MX_GCXLCD_TYPE == 0

			//EB043 ONLY
			char i;

			GFX_Fore_Color = MX_GFXLCD_FCOL;
			GFX_Back_Color = MX_GFXLCD_BCOL;

			set_bit(MX_GFXLCD_DDR, MX_GFXLCD_RS);		   //Configure I/O
			set_bit(MX_GFXLCD_DDR, MX_GFXLCD_CS);
			set_bit(MX_GFXLCD_DDR, MX_GFXLCD_SDATA);
			set_bit(MX_GFXLCD_DDR, MX_GFXLCD_SCLK);
			clear_bit(MX_GFXLCD_PORT, MX_GFXLCD_RS);       //Reset LCD

			delay_ms(10);                                  //Always wait after a Reset

			set_bit(MX_GFXLCD_PORT, MX_GFXLCD_RS);         //Startup LCD
			set_bit(MX_GFXLCD_PORT, MX_GFXLCD_CS);         //Select LCD
			set_bit(MX_GFXLCD_PORT, MX_GFXLCD_SDATA);      //Set Data Pin High
			set_bit(MX_GFXLCD_PORT, MX_GFXLCD_SCLK);       //Set Clock Pin High
			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_CMD, 0x01);

			delay_ms(10);                                  //Wait after Reset

			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_CMD, 0x11);            //Sleep Out
			delay_ms(40);

			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_CMD, 0x29);            //Display On
			delay_ms(40);

			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_CMD, 0x3A);            //Interface Pixel Format....
			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_PARAM, 0x02);          //....data format: 8 bit for each Pixel
			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_CMD, 0x20);            //No inveMX_GFXLCD_RSion, 0x21 would be inverted display (different coloMX_GFXLCD_RS!)
			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_CMD, 0x2D);            //RGB-Table for the 256 out of 4096 color mode

			for (i=0;i<20;i++)
			{
				FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_PARAM, DataInit[i]);    //read predefined color table for 256 color mode
			}

		#else

			char Red, Green, Blue;

			GFX_Fore_Color = MX_GFXLCD_FCOL;
			GFX_Back_Color = MX_GFXLCD_BCOL;

			//Convert Foreground to 16-bit colour
			Green = (GFX_Fore_Color >> 2) & 0x07;
			Green = Green << 2;
			Red = (GFX_Fore_Color >> 5) & 0x07;
			Red = Red << 3;
			Blue = (GFX_Fore_Color) & 0x03;
			Blue = Blue << 3;
			GFX_Fore_Color = Green | (Red << 5);
			GFX_Fore_Color = GFX_Fore_Color | (Blue << 11);

			//Convert Background to 16-bit colour
			Green = (GFX_Back_Color >> 2) & 0x07;
			Green = Green << 2;
			Red = (GFX_Back_Color >> 5) & 0x07;
			Red = Red << 3;
			Blue = (GFX_Back_Color) & 0x03;
			Blue = Blue << 3;
			GFX_Back_Color = Green | (Red << 5);
			GFX_Back_Color = GFX_Back_Color | (Blue << 11);

			//EB057 Initialise
			FCD_gLCD0_SPI_Init();								//Initialise SPI hardware
			delay_us(10);
			FCD_gLCD0_Set_Data_Direction(0);
			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_CMD, 0x01);		//Software Reset
			delay_us(100);
			FCD_gLCD0_CommandWithData(0xFD, 0x12);			//Unlock Basic Commands
			FCD_gLCD0_CommandWithData(0xFD, 0xB3);			//Unlock Supplementary Commands
			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_CMD, 0x10);		//Sleep In
			FCD_gLCD0_CommandWithData(0xD2, 0x10);			//Set Display Clock
			FCD_gLCD0_CommandWithData(0xCA, 0x7F);			//Set Multiplex Ratio
			FCD_gLCD0_CommandWithData(0xC8, 0x00);			//Set Display Offset
			FCD_gLCD0_CommandWith2Data(0x36, 0x88, 0x01);		//Memory Access Control
			FCD_gLCD0_CommandWithData(0x3A, 0x05);			//Pixel Format
			FCD_gLCD0_CommandWithData(0x35, 0x00);			//Tearing Effect Line On
			FCD_gLCD0_CommandWithData(0xB3, 0x03);			//Function Selection
			FCD_gLCD0_CommandWithData(0xCC, 0xB0);			//-Enable External VSL
			FCD_gLCD0_CommandWithData(0xD6, 0x07);			//-High Power Protection
			FCD_gLCD0_CommandWithData(0xBA, 0x43);			//Set Contrast A
			FCD_gLCD0_CommandWithData(0xBB, 0x55);			//Set Contrast B
			FCD_gLCD0_CommandWithData(0xBC, 0x87);			//Set Contrast C
			FCD_gLCD0_CommandWith2Data(0xD1, 0x00, 0x00);		//-Set Contrast Compensation
			FCD_gLCD0_CommandWithData(0x51, 0xF0);			//Write Luminence
			FCD_gLCD0_CommandWithData(0xE3, 0x02);			//Grey Scale Enhance
			FCD_gLCD0_CommandWithData(0xCD, 0x32);			//Phase Length
			FCD_gLCD0_CommandWithData(0xBD, 0x09);			//Precharge Voltage
			FCD_gLCD0_CommandWith2Data(0xD0, 0x04, 0x3F);		//-Pre Charge Compensation
			FCD_gLCD0_CommandWithData(0xCE, 0x0B);			//2nd Precharge Period
			FCD_gLCD0_CommandWithData(0xCF, 0x03);			//2nd Precharge Speed
			FCD_gLCD0_CommandWithData(0xD3, 0x04);			//Set VCOMH
			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_CMD, 0x29);		//Disable Pixels On / Off
			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_CMD, 0x20);		//Display Inversion Off
			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_CMD, 0x13);		//Display Normal Mode
			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_CMD, 0x11);		//Sleep Out
			delay_ms(200);
		#endif

	    FCD_gLCD0_Clear();
}

static void FCD_gLCD0_Clear()
{
	
	    char X,Y;
	    short Pixel,Count;

	   	#if MX_GCXLCD_TYPE == 0
	   		X = 132;                        	//# of X-Pixel
	   		Y = 132;                           	//# of Y-Pixel
	   	#else
	   		X = 128;                        	//# of X-Pixel
	   		Y = 128;                           	//# of Y-Pixel
		#endif

	   	FCD_gLCD0_gLCD_Window(0,0,(X-1),(Y-1));			//define window
	    Pixel = X * Y;                   //# of total pixel

		for (Count=0;Count<Pixel;Count++)
		{
			//Plot each pixel in the defined output window
			#if MX_GCXLCD_TYPE == 0
				FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_PARAM, GFX_Back_Color);
			#else
				FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_PARAM, GFX_Back_Color);
				FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_PARAM, GFX_Back_Color>>8);
			#endif
		}


}

static void FCD_gLCD0_Print(char* String, char MSZ_String, char X, char Y, char Font, char Transparent)
{
	    char xpix, ypix, pos_Str, length_Str, count, xcount, ycount, height, width, i;
	    char Fontwidth = 1;                //First we assume small font
	    char Fontheight = 1;

	    char temp[6];
	    temp[5] = 0x00;                        //Spacing Line

	    length_Str = strlen(String);       //Get length of string

	    if (Font == 1)                     //Double Height Sizes //Double Width
	    {
	        Fontwidth = 2;
	    }
	    if (Font == 2)                         //Double Height and Width Sizes
	    {
	        Fontwidth = 2;
	        Fontheight = 2;
	    }
	    if (Font == 3)                         //Double Height Sizes
	    {
	        Fontheight = 2;
	    }


	    xcount = 0;

	    for (i=0;i<length_Str;i++)         //Start at beginning of string and work along
	    {

	        pos_Str = String[i] - 32;      //Calculate place in ASCII memory

	        for (count = 0; count < 5; count++)        //Use correct buffer location
	        {
	            if (pos_Str < 12)
	            {
	                temp[count]=ASCII1[(pos_Str*5)+count];
	            }
	            else if (pos_Str < 24)
	            {
	                temp[count]=ASCII2[((pos_Str - 12)*5)+count];
	            }
	            else if (pos_Str < 36)
	            {
	                temp[count]=ASCII3[((pos_Str - 24)*5)+count];
	            }
	            else if (pos_Str < 48)
	            {
	                temp[count]=ASCII4[((pos_Str - 36)*5)+count];
	            }
	            else if (pos_Str < 60)
	            {
	                temp[count]=ASCII5[((pos_Str - 48)*5)+count];
	            }
	            else if (pos_Str < 72)
	            {
	                temp[count]=ASCII6[((pos_Str - 60)*5)+count];
	            }
	            else if (pos_Str < 84)
	            {
	                temp[count]=ASCII7[((pos_Str - 72)*5)+count];
	            }
	            else if (pos_Str < 95)
	            {
	                temp[count]=ASCII8[((pos_Str - 84)*5)+count];
	            }
	        }

	        for (xpix=0;xpix<6;xpix++)    //For 6 ASCII bytes 0 - 5
	        {
	            for (width=0;width<Fontwidth;width++)
	            {
	                ycount = 0;
	                for (ypix=0;ypix<8;ypix++)    //For 8 data bits in bytes 0 - 7
	                {
	                    for (height=0;height<Fontheight;height++)
	                    {
	                        if (test_bit(temp[xpix],ypix))
	                        {
	                            FCD_gLCD0_Plot(X + xcount, Y + ycount);
	                        }
	                        else if (Transparent == 0)
	                        {
	                            FCD_gLCD0_gLCD_Window(X+xcount+1, Y+ycount+1, X+xcount+1, Y+ycount+1);
								#if MX_GCXLCD_TYPE == 0
									FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_PARAM, GFX_Back_Color);
								#else
									FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_PARAM, GFX_Back_Color);
									FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_PARAM, GFX_Back_Color>>8);
								#endif
	                        }
	                        ycount++;
	                    }
	                }
	                xcount++;
	            }
	        }
	    }
}

static void FCD_gLCD0_PrintNumber(short Number, char X, char Y, char Font, char Transparent)
{
		char temp_str[6];
		FCI_TOSTRING(Number,temp_str,6);					//Convert number to String
		FCD_gLCD0_Print (temp_str, 6, X, Y, Font, Transparent);			//Send string to FCD_gLCD0_Print function
}

static void FCD_gLCD0_Plot(char X1, char Y1)
{
	    X1 = X1 + 1;
	    Y1 = Y1 + 1;
	    FCD_gLCD0_gLCD_Window(X1,Y1,X1,Y1);                            //Creates output window of 1 pixel

	    #if MX_GCXLCD_TYPE == 0
			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_PARAM, GFX_Fore_Color);      //set color of pixel
		#else
			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_PARAM, GFX_Fore_Color);      //set color of pixel
			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_PARAM, GFX_Fore_Color>>8);   //set color of pixel
		#endif
}

static void FCD_gLCD0_DrawLine(char X1, char Y1, char X2, char Y2)
{
	    int C1, M1;
	    signed int D1 = 0;
	    signed int Pixelx = X2 - X1;    //number of X-pixels;
	    signed int Pixely = Y2 - Y1;    //Number of Y-pixels
	    signed char Xinc = 1;
	    signed char Yinc = 1;

	    if (Pixelx < 0)        //If negative X direction
	    {
	        Xinc = -1;
	        Pixelx = Pixelx * -1;
	    }
	    if (Pixely < 0)        //If negative Y direction
	    {
	        Yinc = -1;
	        Pixely = Pixely * -1;
	    }
	    if (Pixely <= Pixelx)
	    {
	        C1 = 2 * Pixelx;
	        M1 = 2 * Pixely;
	        while (X1 != X2)
	        {
	            FCD_gLCD0_Plot(X1, Y1);    //ForePlot line
	            X1 = X1 + Xinc;
	            D1 = D1 + M1;
	            if (D1 > Pixelx)
	            {
	                Y1 = Y1 + Yinc;
	                D1 = D1 - C1;
	            }
	        }
	    }
	    else
	    {
	        C1 = 2 * Pixely;
	        M1 = 2 * Pixelx;
	        while (Y1 != Y2)
	        {
	            FCD_gLCD0_Plot(X1, Y1);    //ForePlot Line
	            Y1 = Y1 + Yinc;
	            D1 = D1 + M1;
	            if (D1 > Pixely)
	            {
	                X1 = X1 + Xinc;
	                D1 = D1 - C1;
	            }
	        }
	        FCD_gLCD0_Plot(X1, Y1);
	    }
}

static void FCD_gLCD0_DrawBox(char X1, char Y1, char X2, char Y2, char Transparent, char Solid)
{
	    char X,Y;
	    short Pixel,Count,Color;

	    FCD_gLCD0_DrawLine(X1, Y1, X2, Y1);    //draw border
	    FCD_gLCD0_DrawLine(X2, Y1, X2, Y2);
	    FCD_gLCD0_DrawLine(X1, Y2, X2, Y2);
	    FCD_gLCD0_DrawLine(X1, Y1, X1, Y2);

	    if (!Transparent)
	    {
	        X1 = X1 + 2;    //srink box to make room for border
	        Y1 = Y1 + 2;
	        FCD_gLCD0_gLCD_Window(X1,Y1,X2,Y2);        //define window

	        X = X2 - X1;
	        X++;                            //# of X-Pixel
	        Y = Y2 - Y1;
	        Y++;                               //# of Y-Pixel
	        Pixel = X * Y;                   //# of total pixel

	        if (Solid)
	            Color = GFX_Fore_Color;
	        else
	            Color = GFX_Back_Color;

	        for (Count=0;Count<Pixel;Count++)
	        {
	            FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_PARAM, Color);    //ForePlot each pixel in the defined output window (=box)
	        }
	    }
}

static void FCD_gLCD0_SetForeColor(char Red, char Green, char Blue)
{
	
		#if MX_GCXLCD_TYPE == 0

			if (Red > 7)
				Red = 7;

			if (Green > 7)
				Green = 7;

			if (Blue > 3)
				Blue = 3;

			Green = Green << 2;
			Red = Red << 5;
			GFX_Fore_Color = (Blue | Red) | Green;

		#else

			if (Red > 0x3F)
				Red = 0x3F;

			if (Green > 0x1F)
				Green = 0x1F;

			if (Blue > 0x1F)
				Blue = 0x1F;

			GFX_Fore_Color = Green | (Red << 5);
			GFX_Fore_Color = GFX_Fore_Color | (Blue << 11);

		#endif

}

static void FCD_gLCD0_SetBackColor(char Red, char Green, char Blue)
{
	
		#if MX_GCXLCD_TYPE == 0

			if (Red > 7)
				Red = 7;

			if (Green > 7)
				Green = 7;

			if (Blue > 3)
				Blue = 3;

			Green = Green << 2;
			Red = Red << 5;

			GFX_Back_Color = (Blue | Red | Green);

		#else

			if (Red > 0x3F)
				Red = 0x3F;

			if (Green > 0x1F)
				Green = 0x1F;

			if (Blue > 0x1F)
				Blue = 0x1F;

			GFX_Back_Color = Green | (Red << 5);
			GFX_Back_Color = GFX_Fore_Color | (Blue << 11);

		#endif
}

static void FCD_gLCD0_SPI_Init()
{
	
	#if MX_GCXLCD_TYPE == 1

		//GetSPI_InitCode
	  #ifndef MX_SPI
		#error "Chip does not have SPI capability"
	  #endif

		//initialise spi
		clear_bit(SPI_CS_DDR,MX_SPI_SDI);
		set_bit(SPI_CS_DDR,MX_SPI_SDO);
		set_bit(SPI_CS_DDR,MX_SPI_SCK);
		set_bit(SPI_CS_DDR, MX_GFXLCD_CS);

		//disable SPI devices
		set_bit(SPI_CS_PORT, MX_GFXLCD_CS);

	  #ifdef MX_USI
		USICR = (1 << USIWM0);
	  #else
		SPCR = ((1 << SPE) | (1 << MSTR) | SPI_CPOL | SPI_CPHA | SPI_SPR);
	  #endif
	#endif

}

static void FCD_gLCD0_SPI_Send_Char(char Char)
{
	
	#if MX_GCXLCD_TYPE == 1

		//GetSPI_Send_CharCode
	  #ifndef MX_SPI
		#error "Chip does not have SPI capability"
	  #endif

		//send the Char
	  #ifdef MX_USI
		unsigned int i;
		USISR = 0;
	 	USIDR = Char;
		for (i = 0; i < 8; i++)
		{
			USICR = (1 << USIWM0) | (1 << USITC);
			delay_us(1);
			USICR = (1 << USIWM0) | (1 << USITC) | (1 << USICLK);
			delay_us(1);
		}
	  #else

		SPDR = Char;

		//wait until sspbuf is empty
		while ((SPSR & (1 << SPIF)) == 0);
	  #endif


	#endif

}

static void FCD_gLCD0_Set_Data_Direction(char mode)
{
	
		#if MX_GCXLCD_TYPE == 1

			//Configure Non Incremental and pair swapping
			clear_bit(SPI_CS_PORT, MX_GFXLCD_CS);			//CS Low

			FCD_gLCD0_SPI_Send_Char(0x40);
			FCD_gLCD0_SPI_Send_Char(CONF);
			FCD_gLCD0_SPI_Send_Char(0x20);
			FCD_gLCD0_SPI_Send_Char(0x20);

			set_bit(SPI_CS_PORT, MX_GFXLCD_CS);				//CS High

			//Configure DDRs
			delay_us(10);
			clear_bit(SPI_CS_PORT, MX_GFXLCD_CS);			//CS Low

			FCD_gLCD0_SPI_Send_Char(0x40);
			FCD_gLCD0_SPI_Send_Char(DDR);
			if(mode)
				FCD_gLCD0_SPI_Send_Char(0xFF);
			else
				FCD_gLCD0_SPI_Send_Char(0x00);
			FCD_gLCD0_SPI_Send_Char(0x00);

			set_bit(SPI_CS_PORT, MX_GFXLCD_CS);				//CS High

		#endif

}

static void FCD_gLCD0_StrobeE()
{
	
		#if MX_GCXLCD_TYPE == 1

			Control = Control & ~CS;		//Clear CS
			Control = Control | EN;			//Set EN
			FCD_gLCD0_Update_Slave ();
			Control = Control & ~EN;		//Clear EN
			Control = Control | CS;			//Set CS
			FCD_gLCD0_Update_Slave ();

		#endif

}

static void FCD_gLCD0_Update_Slave()
{
	
		#if MX_GCXLCD_TYPE == 1

			clear_bit(SPI_CS_PORT, MX_GFXLCD_CS);			//CS Low

			FCD_gLCD0_SPI_Send_Char(0x40);
			FCD_gLCD0_SPI_Send_Char(PORT);
			FCD_gLCD0_SPI_Send_Char(Control);
			FCD_gLCD0_SPI_Send_Char(Data);

			set_bit(SPI_CS_PORT, MX_GFXLCD_CS);				//CS High

		#endif

}

static void FCD_gLCD0_CommandWithData(char Command, char Data)
{
	
		#if MX_GCXLCD_TYPE == 1

			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_CMD, Command);
			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_PARAM, Data);

		#endif

}

static void FCD_gLCD0_CommandWith2Data(char Command, char Data1, char Data2)
{
	
		#if MX_GCXLCD_TYPE == 1

			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_CMD, Command);
			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_PARAM, Data1);
			FCD_gLCD0_gLCD_SendByte(MX_GFXLCD_PARAM, Data2);

		#endif

}

//Macro implementations

int main()
{
	
	//Initialisation
	MCUSR=0x00;
	wdt_disable();


	//Interrupt initialisation code
	
	//Call Component Macro
	//Call Component Macro: gLCD(0)::Init
	FCD_gLCD0_Init();


	//Call Component Macro
	//Call Component Macro: gLCD(0)::Print("Hello World", 5, 5, 0, 0)
	FCD_gLCD0_Print("Hello World",11, 5, 5, 0, 0);


	mainendloop: goto mainendloop;
}



