//************************************************************************************
//**  
//**  File name:     C:\Pic 16\ssd1289 test 16bit.c
//**  Generated by:  Flowcode v4.4.13.69
//**  Date:          Tuesday, December 20, 2011 13:20:35
//**  Licence:       Professional
//**  Registered to: Martin
//**  
//**  
//**  http://www.matrixmultimedia.com
//************************************************************************************


#define MX_PIC_16BIT

//Defines for microcontroller
#define P30F4013
#define MX_CLKS_PER_INST 4
#define MX_EE
#define MX_EE_SIZE 1024
#define MX_SPI1
#define MX_SPI1_PORT_F
#define MX_SPI1_SDI 2
#define MX_SPI1_SDO 3
#define MX_SPI1_SCK 6
#define MX_UART1
#define MX_UART1_PORT_F
#define MX_UART2
#define MX_UART2_PORT_F
#define MX_MI2C1
#define MX_I2C1_PORT_F
#define MX_I2C1_SDA 2
#define MX_I2C1_SCL 3
#define MX_PWM
#define MX_PWM_PORT_D
#define MX_PWM_CNT 4
#define MX_PWM_1 0
#define MX_PWM_2 1
#define MX_PWM_3 2
#define MX_PWM_4 3
#define MX_ECAN

//Functions
#define MX_Fosc 120000000

#include "../support/dsPIC30F/h/p30fxxxx.h"
#include <stdlib.h>
#include <math.h>

#define _RESERVED1(x) int temp1 = (x);
#define _RESERVED2(x) int temp2 = (x);
#define _ICD(x) _FICD(x);

//Configuration data

//Internal functions
#include "C:\Program Files (x86)\Matrix Multimedia\Flowcode PIC24&dsPIC V4\FCD\internals.h"

//Macro function declarations


//Variable declarations
volatile short FCV_TEST;
volatile char FCV_X;
volatile char FCV_Y;
volatile char FCV_COUNT;
volatile char FCV_BLUE;
volatile char FCV_RED;
volatile char FCV_GREEN;


//Defines:
#include <string.h>

//Common defines
#define MX_DATA_HI_PORT 		LATB
#define MX_DATA_HI_TRIS		TRISB
#define MX_DATA_LO_PORT 		LATF
#define MX_DATA_LO_TRIS		TRISF
#define MX_CONTROL_PORT		LATD
#define MX_CONTROL_TRIS		TRISD
#define MX_CS	      9
#define MX_CD		3
#define MX_RD		8
#define MX_RW			2


// This is from custom glcd component
//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 = ~
};


// this is from glcd component

	//global variables
		short LCD_Back_Color;
		short LCD_Fore_Color;
		char LCD_Orientation = 0;


	

	



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

static void FCD_Custom20_GetDefines();
static void FCD_Custom20_LCD_Init();
static void FCD_Custom20_LCD_Set_Fore_Color(char Red, char Green, char Blue);
static void FCD_Custom20_LCD_Set_Back_Color(char Red, char Green, char Blue);
static void FCD_Custom20_LCD_Draw_Rect(short X1, short Y1, short Width, short Height, char Transparent);
static void FCD_Custom20_LCD_Print_String(char* String, char MSZ_String, short X, short Y, char Fontwidth, char Fontheight, char Transparent);
static void FCD_Custom20_LCD_Print_Number(short Number, short X, short Y, char Fontwidth, char Fontheight, char Transparent);
static void FCD_Custom20_LCD_Draw_Line(short X_Start, short Y_Start, short X_End, short Y_End);
static void FCD_Custom20_LCD_Display_Clear();
static void FCD_Custom20_LCD_Send_Cmd(char command);
static void FCD_Custom20_LCD_Sent_Data(char hi, char lo);
static void FCD_Custom20_LCD_Set_Pixel(char x, short y, short color);
static void FCD_Custom20_LCD_Set_Address(char x, short y);
static void FCD_Custom20_LCD_Set_Pixel_Color(char color_hi, char color_lo);
static void FCD_Custom20_LCD_Plot_Pixel(short x, short y);
static void FCD_Custom20_LCD_Window(short X, short Y, short Width, short Height);



//Custom2(0): //Macro implementations


static void FCD_Custom20_GetDefines()
{
	

}

static void FCD_Custom20_LCD_Init()
{
	
		MX_DATA_HI_TRIS = 0x00;								//Convert data port to output
		MX_DATA_HI_PORT = 0x00;
		MX_DATA_LO_TRIS = 0x00;								//Convert data port to output
		MX_DATA_LO_PORT = 0x00;

					
		clear_bit(MX_CONTROL_TRIS, MX_RD);  //Convert control pins to output
		clear_bit(MX_CONTROL_TRIS, MX_CS);
		clear_bit(MX_CONTROL_TRIS, MX_RW);
		clear_bit(MX_CONTROL_TRIS, MX_CD);


		FCD_Custom20_LCD_Send_Cmd(0x00);	//Start Oscillation
		FCD_Custom20_LCD_Sent_Data(0x00 ,0x01);
		delay_ms(3);


		FCD_Custom20_LCD_Send_Cmd(0x01);	//Driver Output Control
		FCD_Custom20_LCD_Sent_Data(0x23 ,0x3F );
	    delay_ms(3);
		

	    FCD_Custom20_LCD_Send_Cmd(0x02);	//LCD Drive AC Control
		FCD_Custom20_LCD_Sent_Data(0x06 , 0x00);
		delay_ms(3);
		
		

		FCD_Custom20_LCD_Send_Cmd(0x03);	//Power Control (1)
		FCD_Custom20_LCD_Sent_Data(0xA8 ,0xA6 );
		delay_ms(3);


		FCD_Custom20_LCD_Send_Cmd(0x07);	//Display Control
		FCD_Custom20_LCD_Sent_Data(0x00 , 0x33);
		delay_ms(3);


		FCD_Custom20_LCD_Send_Cmd(0x11);	//Entry Mode (R11h): Set Display color mode for 65k/262k color
		if (LCD_Orientation)
		FCD_Custom20_LCD_Sent_Data(0x68, 0x38);	//DFM1:DFM0=1:1 for 65k color (POR) 38 for
		else
		FCD_Custom20_LCD_Sent_Data(0x68, 0x20);	//DFM1:DFM0=1:1 for 65k color (POR) 38 for
	    delay_ms(3);	
		

		
		FCD_Custom20_LCD_Send_Cmd(0x0C);	//Power Control (2)
		FCD_Custom20_LCD_Sent_Data(0x00, 0x05);
		delay_ms(3);


		FCD_Custom20_LCD_Send_Cmd(0x0D);	//Power Control (3)
		FCD_Custom20_LCD_Sent_Data(0x30, 0x0B );
	    delay_ms(3);

		FCD_Custom20_LCD_Send_Cmd(0x0E);	//Power Control (4)
		FCD_Custom20_LCD_Sent_Data(0x20 ,0x00 );
	    delay_ms(3);

		FCD_Custom20_LCD_Send_Cmd(0x10);	//Exit Sleep Mode
		FCD_Custom20_LCD_Sent_Data(0x00 , 0x00);
		delay_ms(3);

		delay_ms(30);		//delay 30ms

		FCD_Custom20_LCD_Send_Cmd(0x1E);	//Power Control (5)
		FCD_Custom20_LCD_Sent_Data(0x00 , 0xA8);
		delay_ms(3);


		FCD_Custom20_LCD_Send_Cmd(0x44);	//Horizontal RAM address position start/end setup
		FCD_Custom20_LCD_Sent_Data(0xEF , 0x00);	//dec 239
		//dec 0, i.e. horizontal ranges from 0 -> 239
							//POR value is 0xEF00 anyway. This address must be set before RAM write
		delay_ms(3);
		
		FCD_Custom20_LCD_Send_Cmd(0x45);	//Vertical RAM address start position setting
		FCD_Custom20_LCD_Sent_Data(0x00 , 0x00);	//0x0000 = dec 0
		delay_ms(3);


		FCD_Custom20_LCD_Send_Cmd(0x46);	//Vertical RAM address end position setting
		FCD_Custom20_LCD_Sent_Data(0x01 , 0x3F);	//0x013F = dec 319
		delay_ms(3);


		FCD_Custom20_LCD_Send_Cmd(0x22);	//RAM write command
		LCD_Back_Color = 0x0000; // Back color to black
		delay_ms(3);

		FCD_Custom20_LCD_Display_Clear(); // init for white back color
		
}

static void FCD_Custom20_LCD_Set_Fore_Color(char Red, char Green, char Blue)
{
	
		

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

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

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

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

		
}

static void FCD_Custom20_LCD_Set_Back_Color(char Red, char Green, char Blue)
{
	
			if (Red > 0x1F)
				Red = 0x1F;

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

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

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


		



}

static void FCD_Custom20_LCD_Draw_Rect(short X1, short Y1, short Width, short Height, char Transparent)
{
	
	    char X,Y,color_hi,color_lo;
	    short Pixel,Count,Color;
		
	    
	  
	    if (Transparent)
		{
		  color_hi = LCD_Back_Color >> 8; //If transparent set color to back color
		  color_lo = LCD_Back_Color;
		}  
		else
		{
		  color_hi = LCD_Fore_Color >> 8;
		  color_lo = LCD_Fore_Color;
		}  
		
	        FCD_Custom20_LCD_Window (X1,Y1, Width, Height);
			FCD_Custom20_LCD_Send_Cmd(0x22);		//RAM write command		
			  
			Pixel = Width * Height;
			for(Count =0; Count < Pixel;Count ++)
			FCD_Custom20_LCD_Sent_Data(color_hi, color_lo);
			
			FCD_Custom20_LCD_Send_Cmd(0x44);	//Horizontal RAM address position start/end setup
		FCD_Custom20_LCD_Sent_Data(0xEF , 0x00);	//dec 239
		//dec 0, i.e. horizontal ranges from 0 -> 239
							//POR value is 0xEF00 anyway. This address must be set before RAM write
		
		FCD_Custom20_LCD_Send_Cmd(0x45);	//Vertical RAM address start position setting
		FCD_Custom20_LCD_Sent_Data(0x00 , 0x00);	//0x0000 = dec 0


		FCD_Custom20_LCD_Send_Cmd(0x46);	//Vertical RAM address end position setting
		FCD_Custom20_LCD_Sent_Data(0x01 , 0x3F);	//0x013F = dec 319

	   
}

static void FCD_Custom20_LCD_Print_String(char* String, char MSZ_String, short X, short Y, char Fontwidth, char Fontheight, char Transparent)
{
	    char  pos_Str, length_Str,  Fore_hi, Fore_lo, Back_hi, Back_lo;
	    short xpix, ypix,count, xcount, ycount, height, width, i;
		char position;
		Fore_hi = LCD_Fore_Color >>8;// colors to 8 bit here for speed
		Fore_lo = LCD_Fore_Color;
		Back_hi = LCD_Back_Color >>8;
	    Back_lo = LCD_Back_Color;
		
	    char temp[6];
	    temp[5] = 0x00;                        //Spacing Line

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

	  
		
	    xcount = 0;
	    for (i=0;i<length_Str;i++)         //Start at beginning of string and work along
	    {
		        FCD_Custom20_LCD_Window ((X+(6*Fontwidth)*i),Y, Fontwidth * 6, Fontheight * 8); //Calculate the place and size of "char window"
				FCD_Custom20_LCD_Send_Cmd(0x22);		//RAM write command	

	        pos_Str = *(String + i) - 32;      //Calculate place in ASCII_SMALL memory

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

	        for (xpix=0;xpix<6;xpix++)    //For 6 ASCII_SMALL 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_Custom20_LCD_Sent_Data(Fore_hi, Fore_lo);
	                        else if (Transparent == 0)
								FCD_Custom20_LCD_Sent_Data(Back_hi, Back_lo);

	                        ycount++;
	                    }
	                }
	                xcount++;
	            }
	        }
	    }
}

static void FCD_Custom20_LCD_Print_Number(short Number, short X, short Y, char Fontwidth, char Fontheight, char Transparent)
{
		char temp_str[6];
		FCI_TOSTRING(Number,temp_str,6);					//Convert number to String
		FCD_Custom20_LCD_Print_String(temp_str, 6, X, Y, Fontwidth, Fontheight, Transparent);		//Send string to Print function
}

static void FCD_Custom20_LCD_Draw_Line(short X_Start, short Y_Start, short X_End, short Y_End)
{
	    short X1,X2,Y1,Y2;
		if(LCD_Orientation)
		{
		X1 = X_Start;
		X2 = X_End;
		Y1 = Y_Start;
		Y2 = Y_End; // these are for portrait
		}
		else
		{
		X1 = 239 - Y_Start;
		X2 = 239 - Y_End;
		Y1 = X_Start;
		Y2 = X_End; // these are for landscape
		}
	    	
		
	    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;
	    }
		//Pixelx = Pixelx + 1; // add 1 to x count, otherwise print is 1 pix short to x dir
	    if (Pixely <= Pixelx)
	    {
	        C1 = 2 * Pixelx;
	        M1 = 2 * Pixely;
	        while (X1 != X2)
	        {
	            FCD_Custom20_LCD_Set_Pixel(X1, Y1,LCD_Fore_Color);    //ForePlot line y x to landsc
	            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_Custom20_LCD_Set_Pixel(X1, Y1,LCD_Fore_Color);    //ForePlot Line
	            Y1 = Y1 + Yinc;
	            D1 = D1 + M1;
	            if (D1 > Pixely)
	            {
	                X1 = X1 + Xinc;
	                D1 = D1 - C1;
	            }
	        }
	        FCD_Custom20_LCD_Set_Pixel(X1, Y1,LCD_Fore_Color);
	    }
}

static void FCD_Custom20_LCD_Display_Clear()
{
		char i,hiByte,lowByte;
		short j;
		if (LCD_Orientation)
		FCD_Custom20_LCD_Window(0,0,240,320); // window for display size
		else
		FCD_Custom20_LCD_Window(0,0,320,240); // window for display size

		hiByte  = LCD_Back_Color >>8; 
		lowByte = LCD_Back_Color;
	    FCD_Custom20_LCD_Set_Address(0 , 239);
		FCD_Custom20_LCD_Send_Cmd(0x22);		//RAM write command
		for(j=0;j<320;j++)
		{
			for(i=0;i<240;i++)
			{
				FCD_Custom20_LCD_Sent_Data(hiByte, lowByte);
				
			}
		}
}

static void FCD_Custom20_LCD_Send_Cmd(char command)
{
	    MX_DATA_HI_PORT = (command >> 4);
		MX_DATA_LO_PORT = command;
		clear_bit(MX_CONTROL_PORT,MX_CD);		//CD LOW for display data
		clear_bit(MX_CONTROL_PORT,MX_RW);
		set_bit(MX_CONTROL_PORT,MX_RD);	
		clear_bit(MX_CONTROL_PORT,MX_CS);	  //CS low for read		
		set_bit(MX_CONTROL_PORT,MX_CS);      //CS high
		
		
}

static void FCD_Custom20_LCD_Sent_Data(char hi, char lo)
{
	    short temp;
		temp = (hi << 4) + (lo >> 4);
		 
		 
		MX_DATA_HI_PORT = temp;
		MX_DATA_LO_PORT = lo;
		set_bit(MX_CONTROL_PORT,MX_CD);		//CD HIGH for display data
		clear_bit(MX_CONTROL_PORT,MX_RW);
		set_bit(MX_CONTROL_PORT,MX_RD);	
		clear_bit(MX_CONTROL_PORT,MX_CS);	//CS low for read	
		//asm nop;
		set_bit(MX_CONTROL_PORT,MX_CS);      //CS high
		
		
}

static void FCD_Custom20_LCD_Set_Pixel(char x, short y, short color)
{
		char hiByte, loByte;
	    hiByte = color >> 8;
		loByte = color;
		FCD_Custom20_LCD_Set_Address(x , y );		//set address
		FCD_Custom20_LCD_Set_Pixel_Color(hiByte, loByte);     //set pixel color
		

		
		
}

static void FCD_Custom20_LCD_Set_Address(char x, short y)
{
		char hiByte, loByte;

		FCD_Custom20_LCD_Send_Cmd(0x4E);		//set address in the 240 pixel direction
		FCD_Custom20_LCD_Sent_Data(0x00 , x);		//hiByte first fill all zero
				//0<x<239

		hiByte 	= y>>8;
		loByte = y;

		FCD_Custom20_LCD_Send_Cmd(0x4F);		//set address in the 320 pixel direction
		FCD_Custom20_LCD_Sent_Data(hiByte , loByte);

}

static void FCD_Custom20_LCD_Set_Pixel_Color(char color_hi, char color_lo)
{
	

		FCD_Custom20_LCD_Send_Cmd(0x22);		//RAM write command
		FCD_Custom20_LCD_Sent_Data(color_hi , color_lo);		//write color content here

		
		
}

static void FCD_Custom20_LCD_Plot_Pixel(short x, short y)
{
	

		char hiByte, loByte;
	    hiByte = LCD_Fore_Color >> 8;
		loByte = LCD_Fore_Color;
		if (LCD_Orientation)
		FCD_Custom20_LCD_Set_Address(x , y );	//set address
		else
		FCD_Custom20_LCD_Set_Address(239-y , x );	//set address
		
		FCD_Custom20_LCD_Set_Pixel_Color(hiByte, loByte);     //set pixel color
		

		
		
}

static void FCD_Custom20_LCD_Window(short X, short Y, short Width, short Height)
{
	
	short X_Start, Y_Start, X_End, Y_End, loopcount, loop;
	char hibyte, lobyte, Fore_hi, Fore_lo, Back_hi, Back_lo ;
	        Fore_hi = LCD_Fore_Color >>8;// colors to 8 bit here for speed
		    Fore_lo = LCD_Fore_Color;

	         if(LCD_Orientation)
			 {
			  X_Start = X;
			  X_End = (X + Width-1);
			  Y_Start = Y;
			  Y_End = (Y + Height-1);
			  }
			 
			 else
			 {
			  X_Start = 239 - (Y + (Height -1));//remove the start row
			  X_End = 239 - Y ; 
			  Y_Start = X;
			  Y_End = (X + Width - 1);//remove the start row
			  }
			  
			  
			     FCD_Custom20_LCD_Send_Cmd(0x44);	//Horizontal RAM address position start/end setup
		         FCD_Custom20_LCD_Sent_Data(X_End , X_Start);			 //dec 239   //dec 0, i.e. horizontal ranges from 0 -> 239
				  
				 hibyte = Y_Start >> 8;
				 lobyte = Y_Start; 
				 
		         FCD_Custom20_LCD_Send_Cmd(0x45);	//Vertical RAM address start position setting
		         FCD_Custom20_LCD_Sent_Data(hibyte , lobyte);	//0x0000 = dec 0
	 
	             hibyte = Y_End >> 8;
				 lobyte = Y_End;

	             FCD_Custom20_LCD_Send_Cmd(0x46);	//Vertical RAM address end position setting
		         FCD_Custom20_LCD_Sent_Data(hibyte , lobyte);	//0x013F = dec 319
				 
	             if(LCD_Orientation)
				 FCD_Custom20_LCD_Set_Address( X_Start, Y_Start);  //Set address counter to window first byte //
				 else
	             FCD_Custom20_LCD_Set_Address( X_End, Y_Start);  //Set address counter to window first byte //



				 
}

//Macro implementations

int main()
{
	
	//Initialisation
	ADPCFG = 0xffff;


	//Interrupt initialisation code
	
	//C-koodi
	//C Code:
	LCD_Orientation = 0;


	//Kutsu komponenttimakro
	//Call Component Macro: Custom2(0)::LCD_Init
	FCD_Custom20_LCD_Init();


	//Laskutoimitus
	//Calculation:
	//  x = 0
	//  y = 0
	FCV_X = 0;
	FCV_Y = 0;
	

	//Kutsu komponenttimakro
	//Call Component Macro: Custom2(0)::LCD_Set_Back_Color(0, 0, 0)
	FCD_Custom20_LCD_Set_Back_Color(0, 0, 0);


	//Laskutoimitus
	//Calculation:
	//  count = 0
	FCV_COUNT = 0;
	

	//Silmukka
	//Loop: While 1
	while (1)
	{
		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Set_Fore_Color(47, 91, 47)
		FCD_Custom20_LCD_Set_Fore_Color(47, 91, 47);


		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Print_String("Testing at", 0, 0, 5, 5, 0)
		FCD_Custom20_LCD_Print_String("Testing at",10, 0, 0, 5, 5, 0);


		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Print_String("Flowcode", 0, 40, 5, 5, 0)
		FCD_Custom20_LCD_Print_String("Flowcode",8, 0, 40, 5, 5, 0);


		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Print_Number(4, 60, 80, 16, 16, 0)
		FCD_Custom20_LCD_Print_Number(4, 60, 80, 16, 16, 0);


		//Laskutoimitus
		//Calculation:
		//  count = count + 1
		FCV_COUNT = FCV_COUNT + 1;
		

		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Print_Number(count, 80, 170, 8, 8, 0)
		FCD_Custom20_LCD_Print_Number(FCV_COUNT, 80, 170, 8, 8, 0);


		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Set_Fore_Color(0, 0, 47)
		FCD_Custom20_LCD_Set_Fore_Color(0, 0, 47);


		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Print_String("Testing at", 0, 0, 5, 5, 0)
		FCD_Custom20_LCD_Print_String("Testing at",10, 0, 0, 5, 5, 0);


		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Print_String("Flowcode", 0, 40, 5, 5, 0)
		FCD_Custom20_LCD_Print_String("Flowcode",8, 0, 40, 5, 5, 0);


		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Print_Number(4, 60, 80, 16, 16, 0)
		FCD_Custom20_LCD_Print_Number(4, 60, 80, 16, 16, 0);


		//Laskutoimitus
		//Calculation:
		//  count = count + 1
		FCV_COUNT = FCV_COUNT + 1;
		

		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Print_Number(count, 80, 170, 8, 8, 0)
		FCD_Custom20_LCD_Print_Number(FCV_COUNT, 80, 170, 8, 8, 0);


		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Set_Fore_Color(0, 91, 0)
		FCD_Custom20_LCD_Set_Fore_Color(0, 91, 0);


		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Print_String("Testing at", 0, 0, 5, 5, 0)
		FCD_Custom20_LCD_Print_String("Testing at",10, 0, 0, 5, 5, 0);


		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Print_String("Flowcode", 0, 40, 5, 5, 0)
		FCD_Custom20_LCD_Print_String("Flowcode",8, 0, 40, 5, 5, 0);


		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Print_Number(4, 60, 80, 16, 16, 0)
		FCD_Custom20_LCD_Print_Number(4, 60, 80, 16, 16, 0);


		//Laskutoimitus
		//Calculation:
		//  count = count + 1
		FCV_COUNT = FCV_COUNT + 1;
		

		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Print_Number(count, 80, 170, 8, 8, 0)
		FCD_Custom20_LCD_Print_Number(FCV_COUNT, 80, 170, 8, 8, 0);


		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Set_Fore_Color(47, 0, 0)
		FCD_Custom20_LCD_Set_Fore_Color(47, 0, 0);


		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Print_String("Testing at", 0, 0, 5, 5, 0)
		FCD_Custom20_LCD_Print_String("Testing at",10, 0, 0, 5, 5, 0);


		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Print_String("Flowcode", 0, 40, 5, 5, 0)
		FCD_Custom20_LCD_Print_String("Flowcode",8, 0, 40, 5, 5, 0);


		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Print_Number(4, 60, 80, 16, 16, 0)
		FCD_Custom20_LCD_Print_Number(4, 60, 80, 16, 16, 0);


		//Laskutoimitus
		//Calculation:
		//  count = count + 1
		FCV_COUNT = FCV_COUNT + 1;
		

		//Kutsu komponenttimakro
		//Call Component Macro: Custom2(0)::LCD_Print_Number(count, 80, 170, 8, 8, 0)
		FCD_Custom20_LCD_Print_Number(FCV_COUNT, 80, 170, 8, 8, 0);


	}


	mainendloop: goto mainendloop;
	return 0;
}



