//************************************************************************************
//**  
//**  File name:     C:\Program Files\Matrix Multimedia\Flowcode V4\Omat\hitec\display_hitec2.c
//**  Generated by:  Flowcode v4.3.7.63
//**  Date:          Wednesday, January 12, 2011 10:54:20
//**  Licence:       Professional
//**  Registered to: Mika Silvennoinen
//**  
//**  
//**  http://www.matrixmultimedia.com
//************************************************************************************


#define MX_PIC

//Määrittää mikro-ohjaimen
#define P18F4685
#define MX_EE
#define MX_EE_TYPE3
#define MX_EE_SIZE 1024
#define MX_SPI
#define MX_SPI_C
#define MX_SPI_SDI 4
#define MX_SPI_SDO 5
#define MX_SPI_SCK 3
#define MX_UART
#define MX_UART_C
#define MX_UART_TX 6
#define MX_UART_RX 7
#define MX_I2C
#define MX_MI2C
#define MX_I2C_C
#define MX_I2C_SDA 4
#define MX_I2C_SCL 3
#define MX_PWM
#define MX_PWM_CNT 1
#define MX_PWM_TRIS1 trisc
#define MX_PWM_1 2

//funktiot
#define MX_CLK_SPEED 25000000
#ifdef _BOOSTC
#include <system.h>
#endif
#ifdef HI_TECH_C
#include <pic18.h>
#endif

//Konfigurointi data
#ifdef _BOOSTC
#pragma DATA 0x300000, 0xff
#endif
#ifdef _HI_TECH_C
//__CONFIG(, );
#endif
#ifdef _BOOSTC
#pragma DATA 0x300001, 0x2
#endif
#ifdef _HI_TECH_C
__CONFIG(1, 0x2ff);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300002, 0x11
#endif
#ifdef _HI_TECH_C
//__CONFIG(, );
#endif
#ifdef _BOOSTC
#pragma DATA 0x300003, 0x1f
#endif
#ifdef _HI_TECH_C
__CONFIG(2, 0x1f11);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300004, 0xff
#endif
#ifdef _HI_TECH_C
//__CONFIG(, );
#endif
#ifdef _BOOSTC
#pragma DATA 0x300005, 0x0
#endif
#ifdef _HI_TECH_C
__CONFIG(3, 0xff);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300006, 0xa0
#endif
#ifdef _HI_TECH_C
//__CONFIG(, );
#endif
#ifdef _BOOSTC
#pragma DATA 0x300007, 0xff
#endif
#ifdef _HI_TECH_C
__CONFIG(4, 0xffa0);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300008, 0x3f
#endif
#ifdef _HI_TECH_C
//__CONFIG(, );
#endif
#ifdef _BOOSTC
#pragma DATA 0x300009, 0xc0
#endif
#ifdef _HI_TECH_C
__CONFIG(5, 0xc03f);
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000a, 0x3f
#endif
#ifdef _HI_TECH_C
//__CONFIG(, );
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000b, 0xe0
#endif
#ifdef _HI_TECH_C
__CONFIG(6, 0xe03f);
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000c, 0x3f
#endif
#ifdef _HI_TECH_C
//__CONFIG(, );
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000d, 0x40
#endif
#ifdef _HI_TECH_C
__CONFIG(7, 0x403f);
#endif

//Sisäiset toiminnot
#include "C:\Program Files\Matrix Multimedia\Flowcode V4\FCD\internals.h"

//Makro-funktion kuvaukset
void FCM_spi_incoming();
void FCM_print_data();


//Muuttujakuvaukset
#define FCSZ_STR_TSENSOR 11
#define FCSZ_STR_VUOSI 5
#define FCSZ_STR_PAIVAYS 20
#define FCSZ_STR_TEMP 20
#define FCSZ_STR_TUNNIT 2
#define FCSZ_STR_STATUS_KOODI 10
#define FCSZ_TEMP_STR 20
#define FCSZ_STR_KUUKAUSI 3
#define FCSZ_TEMP_SENSOR_FP_STR 7
#define FCSZ_STR_PAKOLAMPO 11
#define FCSZ_STR_X 20
#define FCSZ_STR_Y 20
#define FCSZ_STR_EBER_POWER 8
#define FCSZ_STR_Z 20
#define FCSZ_STR_MINUUTIT 2
#define FCSZ_BURNER_RPM_INT_STR 20
#define FCSZ_STR_BURNER_RPM 20
#define FCSZ_STR_SEKUNNIT 2
#define FCSZ_STR_PWM 4
#define FCSZ_STR_PAIVA 2
short FCV_RETVAL;
char FCV_KK;
char FCV_LO_NIBBLE;
short FCV_PWM_BASIC;
short FCV_Z_AXEL;
short FCV_NAPPI_LOOP;
char FCV_STATUS;
char FCV_STR_TSENSOR[FCSZ_STR_TSENSOR];
char FCV_STR_VUOSI[FCSZ_STR_VUOSI];
char FCV_NAPPI;
short FCV_BURNER_RPM_INT;
short FCV_Y_AXEL;
char FCV_NAPPI_COUNT;
char FCV_TEMP_INT;
char FCV_DOWN;
char FCV_BURNER_RPML;
short FCV_VUOSI;
char FCV_STR_PAIVAYS[FCSZ_STR_PAIVAYS];
short FCV_X_AXEL;
char FCV_DATA1;
short FCV_EBER_CHECK_LOOP;
char FCV_EBER_POWER;
char FCV_STR_TEMP[FCSZ_STR_TEMP];
char FCV_STR_TUNNIT[FCSZ_STR_TUNNIT];
char FCV_RIVI1;
char FCV_STR_STATUS_KOODI[FCSZ_STR_STATUS_KOODI];
char FCV_MINSAT;
char FCV_INIT_RTC;
short FCV_X;
short FCV_RATIO_LEFT;
short FCV_DUTY_RIGHT;
short FCV_X_YLA;
char FCV_DATA2;
char FCV_TEMP_STR[FCSZ_TEMP_STR];
char FCV_STR_KUUKAUSI[FCSZ_STR_KUUKAUSI];
char FCV_RIVI2;
short FCV_Y;
short FCV_Y_YLA;
char FCV_DATAC;
char FCV_DATA3;
char FCV_FANS_ON;
char FCV_TEMP_SENSOR_FP_STR[FCSZ_TEMP_SENSOR_FP_STR];
char FCV_STR_PAKOLAMPO[FCSZ_STR_PAKOLAMPO];
char FCV_KUUKAUSI;
char FCV_RIVI3;
short FCV_TEMP;
char FCV_ACK;
short FCV_Z;
char FCV_IN;
char FCV_STR_X[FCSZ_STR_X];
char FCV_DATA4;
char FCV_TEMP_DEC;
char FCV_RIVI4;
short FCV_WTD_LOOP;
char FCV_SEKAT;
char FCV_READ_ADR;
char FCV_STR_Y[FCSZ_STR_Y];
char FCV_DATA5;
short FCV_BURNER_RPM;
char FCV_STATUS_CODE;
char FCV_STR_EBER_POWER[FCSZ_STR_EBER_POWER];
char FCV_RIVI5;
char FCV_TEMP_SENSOR;
char FCV_BURNERH;
char FCV_RX1_IDHI;
char FCV_STR_Z[FCSZ_STR_Z];
char FCV_DATA6;
char FCV_STR_MINUUTIT[FCSZ_STR_MINUUTIT];
char FCV_RIVI6;
char FCV_FLAME_SENSORH;
char FCV_MANUAL_MODE;
short FCV_PAKOLAMPO;
char FCV_MINUUTIT;
char FCV_RIVI7;
short FCV_RATIO_RIGHT;
char FCV_TP_LOOP;
char FCV_RX1_IDLO;
char FCV_BURNER_DUTY;
char FCV_RIVI8;
short FCV_X_BASIC;
char FCV_BURNER_RPM_INT_STR[FCSZ_BURNER_RPM_INT_STR];
char FCV_SUUNTA;
short FCV_E_PULSE;
char FCV_POT_MODE;
char FCV_STR_BURNER_RPM[FCSZ_STR_BURNER_RPM];
char FCV_STR_SEKUNNIT[FCSZ_STR_SEKUNNIT];
char FCV_HI_NIBBLE;
short FCV_Y_BASIC;
char FCV_BURNERL;
char FCV_VIKAKOODI_NUMERO;
char FCV_RIVI;
char FCV_UP;
char FCV_EBER_ON;
char FCV_SEKUNNIT;
char FCV_TUNNIT;
char FCV_FLAME_SENSORL;
char FCV_ECU_SW;
short FCV_Z_BASIC;
char FCV_LIGHT_LEVEL;
short FCV_DUTY_LEFT;
short FCV_X_ALA;
char FCV_STR_PWM[FCSZ_STR_PWM];
char FCV_FAULT_1;
char FCV_SCREEN;
short FCV_Y_ALA;
char FCV_WRITE_ADR;
char FCV_COOLANT_LEVEL;
char FCV_FAULT_2;
char FCV_BURNER_RPMH;
char FCV_STR_PAIVA[FCSZ_STR_PAIVA];
short FCV_Z_ALA;
char FCV_PRINT_LOOP;
char FCV_PAIVA;
char FCV_OVH_SENSOR;
char FCV_NUMBER_OF_FAULTCODE;
char FCV_MENU;


//Defines:
//Error Reading Code For 128x64 gLCD Flowcode Component::DefinesCode



//Custom2(0): //Makro-funktion kuvaukset

void FCD_Custom20_GetDefines();
void FCD_Custom20_LCD_Init();
void FCD_Custom20_LCD_Clear();
void FCD_Custom20_LCD_PlotPixel(char x, char y, char colour);
void FCD_Custom20_LCD_Draw_Line(char x, char y, char x2, char y2, char colour);
void FCD_Custom20_LCD_Draw_Rect(char x, char y, char width, char height, char colour);
void FCD_Custom20_LCD_Print_String(char* String, char MSZ_String, char X, char Y, char Font, char Transparent, char Colour);
void FCD_Custom20_LCD_Print_Number(short Number, char X, char Y, char Font, char Transparent, char Colour);
void FCD_Custom20_LCD_Invert_Rect(char x, char y, char width, char height);
//Defines:

/**** Macro Substitutions ****
portc = RTS Port
trisc = RTS Data Direction
portc = CTS Port
trisc = CTS Data Direction
0 = RTS Pin
4 = CTS Pin
1 = UART Selection		(0-SW / 1-UART1 / 2-UART2)
0 = Flow Control		(0-Off / 1-On)
0 = Debug Enable		(0-Off / 1-On)
0 = Echo Enable		(0-Off / 1-On)
4 = UART TXSTA Value
162 = UART SPBRG Value
RS232_1115200 = Unique ID
Unused = Bitbanged Receive Port Register
Unused = Bitbanged Receive Data Direction Register
Unused = Bitbanged Receive Pin
Unused = Bitbanged Transmit Pin
59 = Bitbanged BAUD Rate Delay
0 = Timout	Selection	(0-Legacy / 1-MS Timeout)
0 = Data Size			(0-8 bits / 1-9 bits / 2-7 bits & Only available on BitBanged components)
0 = Parity Enable		(0-No Parity / 1-Odd Parity / 2-Even Parity)
0 = Legacy Return		(0-Legacy mode return 255 / 1-New mode return MSB err flags)
Unused = Bitbanged Transmit Port Register
Unused = Bitbanged Transmit Data Direction Register
******************************/

#define RS232_1115200_RTS_PORT			portc
#define RS232_1115200_RTS_TRIS			trisc
#define RS232_1115200_CTS_PORT			portc
#define RS232_1115200_CTS_TRIS			trisc
#define RS232_1115200_RTS_PIN			0
#define RS232_1115200_CTS_PIN			4
#define RS232_1115200_UART				1
#define RS232_1115200_TOUT  			0
#define RS232_1115200_DATASIZE			0
#define RS232_1115200_PARITY			0
#define RS232_1115200_LEGACY_RV			0

#if (0 == 1)
  	#define RS232_1115200_HARDWARE
#endif

#if (0 == 1)
  	#define RS232_1115200_DEBUG
#endif

#if (0 == 1)
  	#define RS232_1115200_ECHO
#endif

#if (RS232_1115200_UART == 0)
	#define RS232_1115200_RX_PORT		Unused
	#define RS232_1115200_RX_TRIS		Unused
	#define RS232_1115200_SW_RX		Unused
	#define RS232_1115200_SW_TX		Unused
	#define RS232_1115200_SW_BAUD		59
	#define RS232_1115200_TX_PORT		Unused
	#define RS232_1115200_TX_TRIS		Unused
#else
	#define RS232_1115200_TXSTA_VAL  	4
	#define RS232_1115200_SPBRG_VAL  	162
	#define RS232_1115200_SW_BAUD		0
#endif

#if (RS232_1115200_LEGACY_RV == 0)
	#if (RS232_1115200_DATASIZE == 1)
		#ifdef _BOOSTC
			#pragma error "Chip does not have second UART capability"
		#endif
		#ifdef HI_TECH_C
			#error "Chip does not have second UART capability"
		#endif
	#endif
#endif

#define RS232_1115200_STATUS_LOOP		0
#define RS232_1115200_STATUS_TIMEOUT	1
#define RS232_1115200_STATUS_RXBYTE	2





//RS232(0): //Makro-funktion kuvaukset

void FCD_RS2320_SendRS232Char(short nChar);
void FCD_RS2320_SendRS232String(char* String, char MSZ_String);
short FCD_RS2320_ReceiveRS232Char(short nTimeout);
void FCD_RS2320_ReceiveRS232String(char* FCR_RETVAL, char FCR_RETVAL_SIZE, char nTimeout, char NumBytes);
void FCD_RS2320_RS232_Delay(char mode);
//Defines:

/**** Macro Substitutions ****
199 = Timer 2 Rollover Value
0x05 = Timer 2 Prescaler Value
%c = Unused
0 = Alternate Pin FCD_PWM0_Enable
******************************/




//PWM(0): //Makro-funktion kuvaukset

void FCD_PWM0_Enable(char nIdx);
void FCD_PWM0_Disable(char nIdx);
void FCD_PWM0_SetDutyCycle(char nIdx, char nDuty);
void FCD_PWM0_ChangePeriod(char nPeriodVal, char nPrescalerVal);
void FCD_PWM0_SetDutyCycle10bit(char nIdx, short nDuty);



//Custom2(0): //Makro-toteutukset


void FCD_Custom20_GetDefines()
{
	
	}  //Dummy end of function to allow defines to be added correctly

	#include <string.h>

	//Common defines
	#define MX_DATA_PORT 		portd
	#define MX_DATA_TRIS		trisd
	#define MX_CONTROL_PORT		portc
	#define MX_CONTROL_TRIS		trisc
	#define MX_CS1		      5
	#define MX_CS2			4
	#define MX_DAT_INST		0
	#define MX_ENABLE			3
	#define MX_READ_WRITE		1


	//Static defines
	#define MX_CHIP1			0x00
	#define MX_CHIP2			0x01
	#define MX_CHIP3			0x02
	#define MX_LCD_ON			0x3F
	#define MX_LCD_OFF			0x3E
	#define MX_LCD_SET_ADD		0x40
	#define MX_LCD_SET_PAGE		0xB8
	#define MX_LCD_DISP_START	0xC0
	#define MX_BLACK			0xFF
	#define MX_WHITE			0x00


	//ASCII Pixel data
	#ifdef _BOOSTC
	rom char* ASCII1 = {0x00 , 0x00 , 0x00 , 0x00 , 0x00,       // space // 32  - 43
						0x00 , 0x06 , 0x5F , 0x06 , 0x00,       // !
	                              0x07 , 0x03 , 0x00 , 0x07 , 0x03,       // ''
						0x07 , 0x05 , 0x07 , 0x00 , 0x00,       // # aste
						0x7D , 0x12 , 0x11 , 0x12 , 0x7D,        // $ Ä
						0x63 , 0x13 , 0x08 , 0x64 , 0x63,       // %
						0x36 , 0x49 , 0x56 , 0x20 , 0x50,       // &
						0x00 , 0x07 , 0x03 , 0x00 , 0x00,       // '
						0x00 , 0x3E , 0x41 , 0x00 , 0x00,       // (
						0x00 , 0x41 , 0x3E , 0x00 , 0x00,       // )
						0x08 , 0x3E , 0x1C , 0x3E , 0x08,       // *
						0x08 , 0x08 , 0x3E , 0x08 , 0x08};      // +
	rom char* ASCII2 = {0x00 , 0xE0 , 0x60 , 0x00 , 0x00,       // , // 44 - 55
						0x08 , 0x08 , 0x08 , 0x08 , 0x08,       // -
						0x00 , 0x60 , 0x60 , 0x00 , 0x00,       // .
						0x20 , 0x10 , 0x08 , 0x04 , 0x02,       // /
						0x3E , 0x51 , 0x49 , 0x45 , 0x3E,       // 0
						0x00 , 0x42 , 0x7F , 0x40 , 0x00,       // 1
						0x62 , 0x51 , 0x49 , 0x49 , 0x46,       // 2
						0x22 , 0x49 , 0x49 , 0x49 , 0x36,       // 3
						0x18 , 0x14 , 0x12 , 0x7F , 0x10,       // 4
						0x2F , 0x49 , 0x49 , 0x49 , 0x31,       // 5
						0x3C , 0x4A , 0x49 , 0x49 , 0x30,       // 6
						0x01 , 0x71 , 0x09 , 0x05 , 0x03};      // 7
	rom char* ASCII3 = {0x36 , 0x49 , 0x49 , 0x49 , 0x36,       // 8 // 56 - 67
						0x06 , 0x49 , 0x49 , 0x29 , 0x1E,       // 9
						0x00 , 0x6C , 0x6C , 0x00 , 0x00,        // :
						0x00 , 0xEC , 0x6C , 0x00 , 0x00,       // ;
						0x08 , 0x14 , 0x22 , 0x41 , 0x00,       // <
						0x24 , 0x24 , 0x24 , 0x24 , 0x24,       // =
						0x00 , 0x41 , 0x22 , 0x14 , 0x08,       // >
						0x02 , 0x01 , 0x59 , 0x09 , 0x06,       // ?
						0x00 , 0x00 , 0x40 , 0x00 , 0x00,                  // @ piste pieni
						0x7E , 0x09 , 0x09 , 0x09 , 0x7E,       // A
						0x7F , 0x49 , 0x49 , 0x49 , 0x36,       // B
						0x3E , 0x41 , 0x41 , 0x41 , 0x22};      // C
	rom char* ASCII4 = {0x7F , 0x41 , 0x41 , 0x41 , 0x3E,       // D // 68 - 79
						0x7F , 0x49 , 0x49 , 0x49 , 0x41,       // E
						0x7F , 0x09 , 0x09 , 0x09 , 0x01,       // F
						0x3E , 0x41 , 0x49 , 0x49 , 0x7A,       // G
						0x7F , 0x08 , 0x08 , 0x08 , 0x7F,       // H
						0x00 , 0x41 , 0x7F , 0x41 , 0x00,       // I
						0x30 , 0x40 , 0x40 , 0x40 , 0x3F,       // J
						0x7F , 0x08 , 0x14 , 0x22 , 0x41,       // K
						0x7F , 0x40 , 0x40 , 0x40 , 0x40,       // L
						0x7F , 0x02 , 0x04 , 0x02 , 0x7F,       // M
						0x7F , 0x02 , 0x04 , 0x08 , 0x7F,       // N
						0x3E , 0x41 , 0x41 , 0x41 , 0x3E};      // O
	rom char* ASCII5 = {0x7F , 0x09 , 0x09 , 0x09 , 0x06,       // P // 80 - 91
						0x3E , 0x41 , 0x51 , 0x21 , 0x5E,       // Q
						0x7F , 0x09 , 0x09 , 0x19 , 0x66,       // R
						0x26 , 0x49 , 0x49 , 0x49 , 0x32,       // S
						0x01 , 0x01 , 0x7F , 0x01 , 0x01,       // T
						0x3F , 0x40 , 0x40 , 0x40 , 0x3F,       // U
						0x1F , 0x20 , 0x40 , 0x20 , 0x1F,       // V
						0x3F , 0x40 , 0x3C , 0x40 , 0x3F,       // W
						0x63 , 0x14 , 0x08 , 0x14 , 0x63,       // X
						0x07 , 0x08 , 0x70 , 0x08 , 0x07,       // Y
						0x71 , 0x49 , 0x45 , 0x43 , 0x00,       // Z
						0x38 , 0x45 , 0x44 , 0x45 , 0x38,};      // [ ö
	rom char* ASCII6 = {0x02 , 0x04 , 0x08 , 0x10 , 0x20,       // \ // 92 - 103
						0x20 , 0x55 , 0x54 , 0x55 , 0x78,       // ] ä
						0x04 , 0x02 , 0x01 , 0x02 , 0x04,       // ^
						0x80 , 0x80 , 0x80 , 0x80 , 0x80,       // _
						0x00 , 0x03 , 0x07 , 0x00 , 0x00,       // `
						0x20 , 0x54 , 0x54 , 0x54 , 0x78,       // a
						0x7F , 0x44 , 0x44 , 0x44 , 0x38,       // b
						0x38 , 0x44 , 0x44 , 0x44 , 0x28,       // c
						0x38 , 0x44 , 0x44 , 0x44 , 0x7F,       // d
						0x38 , 0x54 , 0x54 , 0x54 , 0x18,       // e
						0x08 , 0x7E , 0x09 , 0x09 , 0x00,       // f
						0x18 , 0xA4 , 0xA4 , 0xA4 , 0x7C};      // g
	rom char* ASCII7 = {0x7F , 0x04 , 0x04 , 0x78 , 0x00,       // h // 104 - 115
						0x00 , 0x00 , 0x7D , 0x00 , 0x00,       // i
						0x40 , 0x80 , 0x84 , 0x7D , 0x00,       // j
						0x7F , 0x10 , 0x28 , 0x44 , 0x00,       // k
						0x00 , 0x00 , 0x7F , 0x40 , 0x00,       // l
						0x7C , 0x04 , 0x18 , 0x04 , 0x78,       // m
						0x7C , 0x04 , 0x04 , 0x78 , 0x00,       // n
						0x38 , 0x44 , 0x44 , 0x44 , 0x38,       // o
						0xFC , 0x44 , 0x44 , 0x44 , 0x38,       // p
						0x38 , 0x44 , 0x44 , 0x44 , 0xFC,       // q
						0x44 , 0x78 , 0x44 , 0x04 , 0x08,       // r
						0x08 , 0x54 , 0x54 , 0x54 , 0x20};      // s
	rom char* ASCII8 = {0x04 , 0x3E , 0x44 , 0x24 , 0x00,       // t // 116 - 126
						0x3C , 0x40 , 0x20 , 0x7C , 0x00,       // u
						0x1C , 0x20 , 0x40 , 0x20 , 0x1C,       // v
						0x3C , 0x60 , 0x30 , 0x60 , 0x3C,       // w
						0x6C , 0x10 , 0x10 , 0x6C , 0x00,       // x
						0x9C , 0xA0 , 0x60 , 0x3C , 0x00,       // y
						0x64 , 0x54 , 0x54 , 0x4C , 0x00,       // z
						0x08 , 0x3E , 0x41 , 0x41 , 0x00,       // {
						0x00 , 0x00 , 0x7F , 0x00 , 0x00,       // |
						0x00 , 0x41 , 0x41 , 0x3E , 0x08,       // }
						0x02 , 0x01 , 0x02 , 0x01 , 0x00};      // ~

	#endif
	#ifdef HI_TECH_C
	const char ASCII1[] = {0x00 , 0x00 , 0x00 , 0x00 , 0x00,       // space // 32  - 43
						0x00 , 0x06 , 0x5F , 0x06 , 0x00,       // !
	                              0x07 , 0x03 , 0x00 , 0x07 , 0x03,       // ''
						0x07 , 0x05 , 0x07 , 0x00 , 0x00,       // # aste
						0x7D , 0x12 , 0x11 , 0x12 , 0x7D,        // $ Ä
						0x63 , 0x13 , 0x08 , 0x64 , 0x63,       // %
						0x36 , 0x49 , 0x56 , 0x20 , 0x50,       // &
						0x00 , 0x07 , 0x03 , 0x00 , 0x00,       // '
						0x00 , 0x3E , 0x41 , 0x00 , 0x00,       // (
						0x00 , 0x41 , 0x3E , 0x00 , 0x00,       // )
						0x08 , 0x3E , 0x1C , 0x3E , 0x08,       // *
						0x08 , 0x08 , 0x3E , 0x08 , 0x08};      // +
	const char ASCII2[] = {0x00 , 0xE0 , 0x60 , 0x00 , 0x00,       // , // 44 - 55
						0x08 , 0x08 , 0x08 , 0x08 , 0x08,       // -
						0x00 , 0x60 , 0x60 , 0x00 , 0x00,       // .
						0x20 , 0x10 , 0x08 , 0x04 , 0x02,       // /
						0x3E , 0x51 , 0x49 , 0x45 , 0x3E,       // 0
						0x00 , 0x42 , 0x7F , 0x40 , 0x00,       // 1
						0x62 , 0x51 , 0x49 , 0x49 , 0x46,       // 2
						0x22 , 0x49 , 0x49 , 0x49 , 0x36,       // 3
						0x18 , 0x14 , 0x12 , 0x7F , 0x10,       // 4
						0x2F , 0x49 , 0x49 , 0x49 , 0x31,       // 5
						0x3C , 0x4A , 0x49 , 0x49 , 0x30,       // 6
						0x01 , 0x71 , 0x09 , 0x05 , 0x03};      // 7
	const char ASCII3[] = {0x36 , 0x49 , 0x49 , 0x49 , 0x36,       // 8 // 56 - 67
						0x06 , 0x49 , 0x49 , 0x29 , 0x1E,       // 9
						0x00 , 0x6C , 0x6C , 0x00 , 0x00,        // :
						0x00 , 0xEC , 0x6C , 0x00 , 0x00,       // ;
						0x08 , 0x14 , 0x22 , 0x41 , 0x00,       // <
						0x24 , 0x24 , 0x24 , 0x24 , 0x24,       // =
						0x00 , 0x41 , 0x22 , 0x14 , 0x08,       // >
						0x02 , 0x01 , 0x59 , 0x09 , 0x06,       // ?
						0x00 , 0x00 , 0x40 , 0x00 , 0x00,                  // @ piste pieni
						0x7E , 0x09 , 0x09 , 0x09 , 0x7E,       // A
						0x7F , 0x49 , 0x49 , 0x49 , 0x36,       // B
						0x3E , 0x41 , 0x41 , 0x41 , 0x22};      // C
	const char ASCII4[] = {0x7F , 0x41 , 0x41 , 0x41 , 0x3E,       // D // 68 - 79
						0x7F , 0x49 , 0x49 , 0x49 , 0x41,       // E
						0x7F , 0x09 , 0x09 , 0x09 , 0x01,       // F
						0x3E , 0x41 , 0x49 , 0x49 , 0x7A,       // G
						0x7F , 0x08 , 0x08 , 0x08 , 0x7F,       // H
						0x00 , 0x41 , 0x7F , 0x41 , 0x00,       // I
						0x30 , 0x40 , 0x40 , 0x40 , 0x3F,       // J
						0x7F , 0x08 , 0x14 , 0x22 , 0x41,       // K
						0x7F , 0x40 , 0x40 , 0x40 , 0x40,       // L
						0x7F , 0x02 , 0x04 , 0x02 , 0x7F,       // M
						0x7F , 0x02 , 0x04 , 0x08 , 0x7F,       // N
						0x3E , 0x41 , 0x41 , 0x41 , 0x3E};      // O
	const char ASCII5[] = {0x7F , 0x09 , 0x09 , 0x09 , 0x06,       // P // 80 - 91
						0x3E , 0x41 , 0x51 , 0x21 , 0x5E,       // Q
						0x7F , 0x09 , 0x09 , 0x19 , 0x66,       // R
						0x26 , 0x49 , 0x49 , 0x49 , 0x32,       // S
						0x01 , 0x01 , 0x7F , 0x01 , 0x01,       // T
						0x3F , 0x40 , 0x40 , 0x40 , 0x3F,       // U
						0x1F , 0x20 , 0x40 , 0x20 , 0x1F,       // V
						0x3F , 0x40 , 0x3C , 0x40 , 0x3F,       // W
						0x63 , 0x14 , 0x08 , 0x14 , 0x63,       // X
						0x07 , 0x08 , 0x70 , 0x08 , 0x07,       // Y
						0x71 , 0x49 , 0x45 , 0x43 , 0x00,       // Z
						0x38 , 0x45 , 0x44 , 0x45 , 0x38,};      // [ ö
	const char ASCII6[] = {0x02 , 0x04 , 0x08 , 0x10 , 0x20,       // \ // 92 - 103
						0x20 , 0x55 , 0x54 , 0x55 , 0x78,       // ] ä
						0x04 , 0x02 , 0x01 , 0x02 , 0x04,       // ^
						0x80 , 0x80 , 0x80 , 0x80 , 0x80,       // _
						0x00 , 0x03 , 0x07 , 0x00 , 0x00,       // `
						0x20 , 0x54 , 0x54 , 0x54 , 0x78,       // a
						0x7F , 0x44 , 0x44 , 0x44 , 0x38,       // b
						0x38 , 0x44 , 0x44 , 0x44 , 0x28,       // c
						0x38 , 0x44 , 0x44 , 0x44 , 0x7F,       // d
						0x38 , 0x54 , 0x54 , 0x54 , 0x18,       // e
						0x08 , 0x7E , 0x09 , 0x09 , 0x00,       // f
						0x18 , 0xA4 , 0xA4 , 0xA4 , 0x7C};      // g
	const char ASCII7[] = {0x7F , 0x04 , 0x04 , 0x78 , 0x00,       // h // 104 - 115
						0x00 , 0x00 , 0x7D , 0x00 , 0x00,       // i
						0x40 , 0x80 , 0x84 , 0x7D , 0x00,       // j
						0x7F , 0x10 , 0x28 , 0x44 , 0x00,       // k
						0x00 , 0x00 , 0x7F , 0x40 , 0x00,       // l
						0x7C , 0x04 , 0x18 , 0x04 , 0x78,       // m
						0x7C , 0x04 , 0x04 , 0x78 , 0x00,       // n
						0x38 , 0x44 , 0x44 , 0x44 , 0x38,       // o
						0xFC , 0x44 , 0x44 , 0x44 , 0x38,       // p
						0x38 , 0x44 , 0x44 , 0x44 , 0xFC,       // q
						0x44 , 0x78 , 0x44 , 0x04 , 0x08,       // r
						0x08 , 0x54 , 0x54 , 0x54 , 0x20};      // s
	const char ASCII8[] = {0x04 , 0x3E , 0x44 , 0x24 , 0x00,       // t // 116 - 126
						0x3C , 0x40 , 0x20 , 0x7C , 0x00,       // u
						0x1C , 0x20 , 0x40 , 0x20 , 0x1C,       // v
						0x3C , 0x60 , 0x30 , 0x60 , 0x3C,       // w
						0x6C , 0x10 , 0x10 , 0x6C , 0x00,       // x
						0x9C , 0xA0 , 0x60 , 0x3C , 0x00,       // y
						0x64 , 0x54 , 0x54 , 0x4C , 0x00,       // z
						0x08 , 0x3E , 0x41 , 0x41 , 0x00,       // {
						0x00 , 0x00 , 0x7F , 0x00 , 0x00,       // |
						0x00 , 0x41 , 0x41 , 0x3E , 0x08,       // }
						0x02 , 0x01 , 0x02 , 0x01 , 0x00};      // ~

	#endif


	//Global Variables
	char mx_page;
	char mx_currentx;
	char mx_currenty;


	//Internal Prototypes
	void MX_LCD_Enable (void);
	void MX_Write_Command(char cmd, char chip);
	void GotoXY(char x, char y);
	void MX_WriteData(char data);
	char MX_ReadData(void);


	//Internal Functions
	void MX_LCD_Enable (void)
	{
		set_bit(MX_CONTROL_PORT, MX_ENABLE);
		//delay_10us(1);								//Min delay 450ns
		nop();
		nop();
		nop();
		nop();
		nop();	
	                nop();
		nop();
		nop();
		nop();
		nop(); 
	                nop();
		nop();
		nop();
		nop();
		nop(); 
	                nop();
		nop();
		nop();
		nop();
		nop();
	                nop();
		nop();
		nop(); 
	                nop();
		nop();
		nop();
		nop();
		nop();
	      	
	      
		clear_bit(MX_CONTROL_PORT, MX_ENABLE);
		//delay_10us(1);								//Min delay 1.5us
		nop();
		nop();
		nop();
		nop();
		nop();
	                nop();
		nop();
		nop();
		nop();
		nop();
	               nop();
		nop();
		nop();
		nop();
		nop();	
	                nop();
		nop();
		nop();
		nop();
		nop(); 
	                nop();
		nop();
		nop();
		nop();
		nop();
	                nop();
		nop();
		nop(); 
	                nop();
		nop();
		nop();
		nop();
		nop();
	      
	      
	}

	void MX_Write_Command(char cmd, char chip)
	{
		if(chip == MX_CHIP1)						//Select Chip 1
		{
			clear_bit(MX_CONTROL_PORT, MX_CS1);
	            clear_bit(MX_CONTROL_PORT, MX_CS2);
			
		}

	      else if(chip == MX_CHIP2)					//Select Chip 2
		{
			
	            clear_bit(MX_CONTROL_PORT, MX_CS2);
			set_bit(MX_CONTROL_PORT, MX_CS1);
		}
	      
		else if(chip == MX_CHIP3)					//Select Chip 3
		{

	            clear_bit(MX_CONTROL_PORT, MX_CS1);
			set_bit(MX_CONTROL_PORT, MX_CS2);
		}
		clear_bit(MX_CONTROL_PORT, MX_READ_WRITE);	//Write Command
		clear_bit(MX_CONTROL_PORT, MX_DAT_INST);	//Instruction code
		MX_DATA_TRIS = 0x00;						//Configure data port as output
		MX_DATA_PORT = cmd;
		MX_LCD_Enable();
		MX_DATA_PORT = 0x00;
	}


	void MX_GotoXY(char x, char y)
	{
		char chip = MX_CHIP1;
		char cmd;

		if(x > 191) x = 0;							//Ensure that coordinates are legal
		if(y > 63)  y = 0;

		mx_currentx = x;
		mx_currenty = y;
		mx_page = y / 8;

		if(x >= 64 && x < 128)							//Select the right chip
		{
			x = x - 64;
			chip = MX_CHIP2;
		}
	     
	      if(x > 127 )								//Select the right chip
		{
			x = x - 128;
			chip = MX_CHIP3;
		}


		cmd = MX_LCD_SET_PAGE | mx_page;			//Set y address on both chips
		MX_Write_Command(cmd, MX_CHIP1);
		MX_Write_Command(cmd, MX_CHIP2);
	      MX_Write_Command(cmd, MX_CHIP3);

		cmd = MX_LCD_SET_ADD | x;					//Set x address on active chip
		MX_Write_Command(cmd, chip);
	}





	void MX_WriteData(char data)
	{
		char displayData, yOffset, cmdPort;

		if(mx_currentx >= 192)
			return;

		if(mx_currentx < 64)
		{
			clear_bit(MX_CONTROL_PORT, MX_CS1);			//Select chip 1   
	            clear_bit(MX_CONTROL_PORT, MX_CS2);
			
		}
	      
	      else if(mx_currentx >= 64 && mx_currentx < 128)
		{
			clear_bit(MX_CONTROL_PORT, MX_CS2);			//Select chip 2
			set_bit(MX_CONTROL_PORT, MX_CS1);
		}


		else if(mx_currentx >= 128)
		{
			clear_bit(MX_CONTROL_PORT, MX_CS1);			//Select chip 3
			set_bit(MX_CONTROL_PORT, MX_CS2);
		}

		if(mx_currentx == 64)							//Chip 2 X Address = 0
			MX_Write_Command(MX_LCD_SET_ADD, MX_CHIP2); 

	      else if(mx_currentx == 128)							//Chip 3 X Address = 0
			MX_Write_Command(MX_LCD_SET_ADD, MX_CHIP3);

		set_bit(MX_CONTROL_PORT, MX_DAT_INST);			//Configure to send data
		clear_bit(MX_CONTROL_PORT, MX_READ_WRITE);		//Configure to write data
		MX_DATA_TRIS = 0x00;							//Configure data port to Output
		yOffset = mx_currenty % 8;

		if(yOffset != 0) 								//First page?
		{
			cmdPort = MX_CONTROL_PORT;					//Save command port
			displayData = MX_ReadData();
			MX_CONTROL_PORT = cmdPort;					//Restore command port
			MX_DATA_TRIS = 0x00;						//Data port is output
			displayData = displayData | (data << yOffset);
			MX_DATA_PORT = displayData;					//Write data
			MX_LCD_Enable();							//Enable

			// second page
			MX_GotoXY(mx_currentx, (mx_currenty + 8));
			displayData = MX_ReadData();
			MX_CONTROL_PORT = cmdPort;					//Restore command port
			MX_DATA_TRIS = 0x00;						//Data port is output
			displayData = displayData | (data >> (8-yOffset));
			MX_DATA_PORT = displayData;					//Write data
			MX_LCD_Enable();							//Enable
			MX_GotoXY(mx_currentx + 1, (mx_currenty - 8));
		}
		else
		{
			MX_DATA_PORT = data;						//Write data
			MX_LCD_Enable();							//Enable
			mx_currentx = mx_currentx + 1;
		}
		MX_DATA_PORT = 0x00;
	}


	char MX_ReadData(void)
	{
		char data, i;

		MX_DATA_PORT = 0x00;
		MX_DATA_TRIS = 0xFF;							//Data port is input
		set_bit(MX_CONTROL_PORT, MX_READ_WRITE);		//Configure to read data
		set_bit(MX_CONTROL_PORT, MX_DAT_INST);			//Configure to send data

		if(mx_currentx < 64)
		{
			clear_bit(MX_CONTROL_PORT, MX_CS1);			//Select chip 1 
	            clear_bit(MX_CONTROL_PORT, MX_CS2);
			
		}
	      else if(mx_currentx >= 64 && mx_currentx < 128)
	      
		{
			clear_bit(MX_CONTROL_PORT, MX_CS2);			//Select chip 2
			set_bit(MX_CONTROL_PORT, MX_CS1);
		}


		else if(mx_currentx >= 128)
		{
			clear_bit(MX_CONTROL_PORT, MX_CS1);			//Select chip 3
			set_bit(MX_CONTROL_PORT, MX_CS2);
		}

	       


		set_bit(MX_CONTROL_PORT, MX_ENABLE);			//Set the enable bit
		for (i=0; i<3; i++);
		clear_bit(MX_CONTROL_PORT, MX_ENABLE);			//Clear the enable bit
		for (i=0; i<8; i++);							//Min 1.5us delay
		set_bit(MX_CONTROL_PORT, MX_ENABLE);			//Set the enable bit
		for (i=0; i<15; i++);							//Min 450ns delay

		data = MX_DATA_PORT;							//Read Data
		clear_bit(MX_CONTROL_PORT, MX_ENABLE);			//Clear the enable bit
		MX_DATA_TRIS = 0x00;							//Configure port as output
		MX_GotoXY(mx_currentx, mx_currenty);
		return data;
	}


	// Dummy function to close the defines section off
	void CUSTOM_Dummy_Function();
	void CUSTOM_Dummy_Function()
	{

}

void FCD_Custom20_LCD_Init()
{
	
		mx_currentx = 0;
		mx_currenty = 0;
		mx_page = 0;

		MX_DATA_TRIS = 0x00;								//Convert data port to output
		MX_DATA_PORT = 0x00;

		clear_bit(MX_CONTROL_TRIS, MX_ENABLE);				//Convert control pins to output
		clear_bit(MX_CONTROL_TRIS, MX_DAT_INST);
		clear_bit(MX_CONTROL_TRIS, MX_CS1);
		clear_bit(MX_CONTROL_TRIS, MX_CS2);
		clear_bit(MX_CONTROL_TRIS, MX_READ_WRITE);

		set_bit(MX_CONTROL_PORT, MX_ENABLE);
		clear_bit(MX_CONTROL_PORT, MX_READ_WRITE);			//Write Command
		clear_bit(MX_CONTROL_PORT, MX_DAT_INST);			//Instruction code
		set_bit(MX_CONTROL_PORT, MX_CS1);					//Select Chip 1
		clear_bit(MX_CONTROL_PORT, MX_CS2);

		MX_Write_Command(MX_LCD_ON, MX_CHIP1);				//Switch on LCD controller chips
		MX_Write_Command(MX_LCD_ON, MX_CHIP2);
	      MX_Write_Command(MX_LCD_ON, MX_CHIP3);

		MX_Write_Command(MX_LCD_DISP_START, MX_CHIP1);		//Configure the display start address
		MX_Write_Command(MX_LCD_DISP_START, MX_CHIP2);
	      MX_Write_Command(MX_LCD_DISP_START, MX_CHIP3);

		FCD_Custom20_LCD_Clear();
		MX_GotoXY(0,0);

}

void FCD_Custom20_LCD_Clear()
{
		FCD_Custom20_LCD_Draw_Rect(0, 0, 191, 63, MX_WHITE);
}

void FCD_Custom20_LCD_PlotPixel(char x, char y, char colour)
{
		char data, mod_y;

		mod_y = y%8;
		MX_GotoXY(x, y - mod_y);
		data = MX_ReadData();							//Read 8 pixels from display
		if (colour)
			data = data | (0x01 << mod_y);				//Set Pixel
		else
			data = data & (~(0x01 << mod_y));			//Clear Pixel
		MX_WriteData(data);
}

void FCD_Custom20_LCD_Draw_Line(char x, char y, char x2, char y2, char colour)
{
		int C1, M1;
		signed int D1 = 0;
		signed int Pixelx = x2 - x;    //number of X-pixels;
		signed int Pixely = y2 - y;    //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 (x != x2)
			{
				FCD_Custom20_LCD_PlotPixel(x, y, colour);    //ForePlot line
				x = x + Xinc;
				D1 = D1 + M1;
				if (D1 > Pixelx)
				{
					y = y + Yinc;
					D1 = D1 - C1;
				}
			}
		}
		else
		{
			C1 = 2 * Pixely;
			M1 = 2 * Pixelx;
			while (y != y2)
			{
				FCD_Custom20_LCD_PlotPixel(x, y, colour);    //ForePlot Line
				y = y + Yinc;
				D1 = D1 + M1;
				if (D1 > Pixely)
				{
					x = x + Xinc;
					D1 = D1 - C1;
				}
			}
			FCD_Custom20_LCD_PlotPixel(x, y, colour);
		}
}

void FCD_Custom20_LCD_Draw_Rect(char x, char y, char width, char height, char colour)
{
		char mask, pageOffset, h, i, data;

		if(colour)
			colour = 255;

		height = height + 1;

		pageOffset = y % 8;
		y = y - pageOffset;
		mask = 0xFF;

		if(height < (8 - pageOffset))
		{
			mask = mask >> (8-height);
			h = height;
		}
		else
			h = (8 - pageOffset);

		mask = mask << pageOffset;

		MX_GotoXY(x, y);

		for(i=0; i<=width; i++)
		{
			data = MX_ReadData();
			if(colour)
				data = data | mask;
			else
				data = data & (~mask);

			MX_WriteData(data);
		}

		while((h + 8) <= height)
		{
			h = h + 8;
			y = y + 8;
			MX_GotoXY(x, y);

			for(i=0; i<=width; i++)
				MX_WriteData(colour);
		}

		if(h < height)
		{
			mask = ~(0xFF << (height - h));
			MX_GotoXY(x, (y + 8));

			for(i=0; i<=width; i++)
			{
				data = MX_ReadData();

				if(colour)
					data = data | mask;
				else
					data = data & (~mask);

				MX_WriteData(data);
			}
		}
}

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

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

		if (Colour)
			inv_colour = 0;

	    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
	    {
	        Fontwidth = 3;
	        Fontheight = 3;
	    }
	    if (Font == 4)                         //Double Height and Width Sizes
	    {
	        Fontwidth = 4;
	        Fontheight = 4;
	    }


	    xcount = 0;
	    for (i=0;i<MSZ_String;i++)         //Start at beginning of string and work along
	    {
	        if (String[i] == 0)
	        	return;
	        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_Custom20_LCD_PlotPixel(X + xcount, Y + ycount, Colour);
	                        }
	                        else if (Transparent == 0)
	                        {
	                            FCD_Custom20_LCD_PlotPixel(X+xcount, Y+ycount, inv_colour);
	                        }
	                        ycount++;
	                    }
	                }
	                xcount++;
	            }
	        }
	    }
}

void FCD_Custom20_LCD_Print_Number(short Number, char X, char Y, char Font, char Transparent, char Colour)
{
		char string_length;

		if (Number < -10000	)
			string_length = 6;

		else if(Number > 10000 || Number < -1000)
			string_length = 5;

		else if(Number > 1000 || Number < -100)
			string_length = 4;

		else if(Number > 100 || Number < -10)
			string_length = 3;

		else if(Number > 10 || Number < -1)
			string_length = 2;

		else
			string_length = 1;

		char temp_str[6];

		FCI_TOSTRING(Number,temp_str,string_length);									//Convert number to String
		FCD_Custom20_LCD_Print_String(temp_str, string_length, X, Y, Font, Transparent, Colour);		//Send string to Print function
}

void FCD_Custom20_LCD_Invert_Rect(char x, char y, char width, char height)
{
		char mask, pageOffset, h, i, data, tmpData;
		height++;

		pageOffset = y % 8;
		y = y - pageOffset;
		mask = 0xFF;
		if(height < (8 - pageOffset))
		{
			mask = mask >> (8-height);
			h = height;
		}
		else
		{
			h = 8 - pageOffset;
		}
		mask = mask << pageOffset;

		MX_GotoXY(x, y);
		for(i=0; i<=width; i++)
		{
			data = MX_ReadData();
			tmpData = ~data;
			data = (tmpData & mask) | (data & ~mask);
			MX_WriteData(data);
		}

		while((h + 8) <= height)
		{
			h = h + 8;
			y = y + 8;
			MX_GotoXY(x, y);

			for(i=0; i<=width; i++)
			{
				data = MX_ReadData();
				MX_WriteData(~data);
			}
		}

		if(h < height)
		{
			mask = ~(0xFF << (height-h));
			MX_GotoXY(x, y+8);

			for(i=0; i<=width; i++)
			{
				data = MX_ReadData();
				tmpData = ~data;
				data = (tmpData & mask) | (data & ~mask);
				MX_WriteData(data);
			}
		}
}



//RS232(0): //Makro-toteutukset


void FCD_RS2320_SendRS232Char(short nChar)
{
	
	#if (RS232_1115200_UART == 0)

		char dMask;
		char idx;
		char count = 8;

		#ifdef RS232_1115200_HARDWARE
			//wait until CTS is low
			while (( RS232_1115200_CTS_PORT & (1 << RS232_1115200_CTS_PIN) ) != 0);
		#endif

		#if(RS232_1115200_DATASIZE == 1)
			count = 9;
		#endif
		#if(RS232_1115200_DATASIZE == 2)
			count = 7;
		#endif

		clear_bit( RS232_1115200_TX_PORT, RS232_1115200_SW_TX);			// Send Start bit
		FCD_RS2320_RS232_Delay(0);

		for (idx = 0; idx < count; idx++)
		{
			dMask = nChar & 0x01;					// Mask off data bit

			if (dMask)
				set_bit( RS232_1115200_TX_PORT, RS232_1115200_SW_TX);
			else
				clear_bit( RS232_1115200_TX_PORT, RS232_1115200_SW_TX);

			FCD_RS2320_RS232_Delay(0);
			nChar = nChar >> 1;						// Move to next data bit
		}

		set_bit( RS232_1115200_TX_PORT, RS232_1115200_SW_TX);				// Send Stop bit
		FCD_RS2320_RS232_Delay(0);

	#endif

	#if (RS232_1115200_UART == 1)

		#ifndef MX_UART
			#ifdef _BOOSTC
				#pragma error "Chip does not have second UART capability"
			#endif
			#ifdef HI_TECH_C
				#error "Chip does not have second UART capability"
			#endif
		#endif

		st_bit(txsta, TXEN);
		while (ts_bit(pir1, TXIF) == 0);

		#ifdef RS232_1115200_HARDWARE
			//wait until CTS is low
			while (( RS232_1115200_CTS_PORT & (1 << RS232_1115200_CTS_PIN) ) != 0);
		#endif

		#if(RS232_1115200_DATASIZE == 1)
			if (test_bit(nChar, 8))
				st_bit(txsta, TX9D);
			else
				cr_bit(txsta, TX9D);
		#endif

		txreg = nChar;

	#endif

	#if (RS232_1115200_UART == 2)

		#ifndef MX_UART2
			#ifdef _BOOSTC
				#pragma error "Chip does not have second UART capability"
			#endif
			#ifdef HI_TECH_C
				#error "Chip does not have second UART capability"
			#endif
		#endif

		st_bit(txsta2, TXEN);
		while (ts_bit(pir3, TX2IF) == 0);

		#ifdef RS232_1115200_HARDWARE
			//wait until CTS is low
			while (( RS232_1115200_CTS_PORT & (1 << RS232_1115200_CTS_PIN) ) != 0);
		#endif

		#if( RS232_1115200_DATASIZE == 1)
			if (test_bit(nChar, 8))
				st_bit(txsta2, TX9D);
			else
				cr_bit(txsta2, TX9D);
		#endif

		txreg2 = nChar;

	#endif

}

void FCD_RS2320_SendRS232String(char* String, char MSZ_String)
{
		char idx;
		for(idx = 0; idx < MSZ_String; idx++)
		{
			#ifdef _BOOSTC
				if (String[idx] == 0)
					break;
				else FCD_RS2320_SendRS232Char(String[idx]);
			#endif
			#ifdef HI_TECH_
				if (*String == 0)
					break;
				else FCD_RS2320_SendRS232Char(*String);
				String++;
			#endif
		}
}

short FCD_RS2320_ReceiveRS232Char(short nTimeout)
{
	
		char delay1 = 0;
		char delay2 = 0;
		char regcheck = 0;
		char dummy = 0;
		short retVal = 512;
		char bWaitForever = 0;
		char rxStatus = RS232_1115200_STATUS_LOOP;
		char idx;
		char count = 8;

		#if ( RS232_1115200_LEGACY_RV == 0)
			retVal = 255;
		#endif

		#if ( RS232_1115200_UART == 1)
			#ifndef MX_UART
				#ifdef _BOOSTC
					#pragma error "Chip does not have second UART capability"
				#endif
				#ifdef HI_TECH_C
					#error "Chip does not have second UART capability"
				#endif
			#endif
		#endif

		#if ( RS232_1115200_UART == 2)
			#ifndef MX_UART2
				#ifdef _BOOSTC
					#pragma error "Chip does not have second UART capability"
				#endif
				#ifdef HI_TECH_C
					#error "Chip does not have second UART capability"
				#endif
			#endif
		#endif

		#ifdef RS232_1115200_HARDWARE
			//ready to accept data
			clear_bit( RS232_1115200_RTS_PORT, RS232_1115200_RTS_PIN);
		#endif

		if (nTimeout == 255)
			bWaitForever = 1;

		#if ( RS232_1115200_UART == 1)
			st_bit(rcsta, CREN);
		#endif

		#if ( RS232_1115200_UART == 2)
			st_bit(rcsta2, CREN);
		#endif

		while (rxStatus == RS232_1115200_STATUS_LOOP)
		{
			if (bWaitForever == 0)
			{
				//don't wait forever, so do timeout thing...
				if (nTimeout == 0)
				{
					rxStatus = RS232_1115200_STATUS_TIMEOUT;
				}
				else
				{
					if ( RS232_1115200_TOUT)
					{
						delay_us(10);
						delay1 = delay1 + 1;
						if(delay1 == 100)
						{
							nTimeout = nTimeout - 1;
							delay1 = 0;
						}
					}
					else
					{
						//decrement timeout
						delay1 = delay1 - 1;
						if (delay1 == 0)
						{
							nTimeout = nTimeout - 1;
						}
					}
				}
			}

			#if ( RS232_1115200_UART == 0)
				regcheck = test_bit(RS232_1115200_RX_PORT, RS232_1115200_SW_RX);	//Test for start bit
				if (regcheck == 0)
					rxStatus = RS232_1115200_STATUS_RXBYTE;
			#endif

			#if ( RS232_1115200_UART == 1)
				regcheck = ts_bit(pir1, RCIF);
				if (regcheck != 0)
					rxStatus = RS232_1115200_STATUS_RXBYTE;
			#endif

			#if ( RS232_1115200_UART == 2)
				regcheck = ts_bit(pir3, RC2IF);
				if (regcheck != 0)
					rxStatus = RS232_1115200_STATUS_RXBYTE;
			#endif
		}

		if (rxStatus == RS232_1115200_STATUS_RXBYTE)
		{
			#if ( RS232_1115200_UART > 0)

				#if ( RS232_1115200_UART == 1)
					regcheck = ts_bit(rcsta, FERR);
				#endif

				#if ( RS232_1115200_UART == 2)
					regcheck = ts_bit(rcsta2, FERR);
				#endif

				if (regcheck != 0)
				{
					#if ( RS232_1115200_UART == 1)
						dummy = rcreg;      //need to read the rcreg to clear FERR
					#endif

					#if ( RS232_1115200_UART == 2)
						dummy = rcreg2;      //need to read the rcreg to clear FERR
					#endif

					#ifdef RS232_1115200_DEBUG
						FCD_RS2320_SendRS232Char('<');
						FCD_RS2320_SendRS232Char('F');
						FCD_RS2320_SendRS232Char('E');
						FCD_RS2320_SendRS232Char('R');
						FCD_RS2320_SendRS232Char('R');
						FCD_RS2320_SendRS232Char('>');
					#endif

					#if ( RS232_1115200_LEGACY_RV == 1)
						retVal = 0x400;					//Framing Error Flag
					#endif
				}
				else
				{
					#if ( RS232_1115200_UART == 1)
						regcheck = ts_bit(rcsta, OERR);
					#endif

					#if ( RS232_1115200_UART == 2)
						regcheck = ts_bit(rcsta2, OERR);
					#endif

					if (regcheck != 0)
					{
						//need to read the rcreg to clear error
						#if ( RS232_1115200_UART == 1)
							cr_bit(rcsta, CREN);
							st_bit(rcsta, CREN);
						#endif

						#if ( RS232_1115200_UART == 2)
							cr_bit(rcsta2, CREN);
							st_bit(rcsta2, CREN);
						#endif

						#ifdef RS232_1115200_DEBUG
							FCD_RS2320_SendRS232Char('<');
							FCD_RS2320_SendRS232Char('O');
							FCD_RS2320_SendRS232Char('E');
							FCD_RS2320_SendRS232Char('R');
							FCD_RS2320_SendRS232Char('R');
							FCD_RS2320_SendRS232Char('>');
						#endif

						#if ( RS232_1115200_LEGACY_RV == 1)
							retVal = 0x800;					//Overrun Error Flag
						#endif

					}
					else
					{
						retVal = 0;

						#if ( RS232_1115200_UART == 1)

							#if( RS232_1115200_DATASIZE == 1)
								if(ts_bit(rcsta, RX9D));
									retVal = 0x100;
							#endif
							retVal = retVal | rcreg; 			//no error, so rx byte is valid

						#endif

						#if ( RS232_1115200_UART == 2)

							#if( RS232_1115200_DATASIZE == 1)
								if(ts_bit(rcsta2, RX9D));
									retVal = 0x100;
							#endif
							retVal = retVal | rcreg2; 			//no error, so rx byte is valid

						#endif

						#ifdef RS232_1115200_ECHO
							FCD_RS2320_SendRS232Char(retVal);
						#endif
					}
				}
			#else
				#if( RS232_1115200_DATASIZE == 1)
					count = 9;
				#endif

				FCD_RS2320_RS232_Delay(1);
				for (idx = 0; idx < count; idx++)
				{
					retVal = retVal >> 1;

					if(count == 9)
					{
						if (test_bit( RS232_1115200_RX_PORT, RS232_1115200_SW_RX))
							retVal = retVal | 0x100;
					}
					else
					{
						if (test_bit( RS232_1115200_RX_PORT, RS232_1115200_SW_RX))
							retVal = retVal | 0x80;
					}
					FCD_RS2320_RS232_Delay(1);
				}

				#ifdef RS232_1115200_ECHO
					FCD_RS2320_SendRS232Char(retVal);
				#endif

			#endif
		}
		#ifdef RS232_1115200_HARDWARE
			//not ready to accept data
			set_bit( RS232_1115200_RTS_PORT, RS232_1115200_RTS_PIN);
		#endif

		return (retVal);
}

void FCD_RS2320_ReceiveRS232String(char* FCR_RETVAL, char FCR_RETVAL_SIZE, char nTimeout, char NumBytes)
{
		char idx;
		short in;

		#if ( RS232_1115200_LEGACY_RV == 0 )
			#define RS232_TO 	255
		#else
			#define RS232_TO 	256
		#endif

		if (NumBytes > FCR_RETVAL_SIZE)
			NumBytes = FCR_RETVAL_SIZE;

		for (idx = 0; idx < NumBytes; idx++)
		{
			in = FCD_RS2320_ReceiveRS232Char(nTimeout);
			if(in < RS232_TO)
				FCR_RETVAL[idx] = in & 0xFF;
			else
				break;
		}

		if (idx < FCR_RETVAL_SIZE)
			FCR_RETVAL[idx] = 0;

		#undef RS232_TO
}

void FCD_RS2320_RS232_Delay(char mode)
{
		unsigned int iterations;
		unsigned int delay = RS232_1115200_SW_BAUD;

		if (mode)
			delay = delay + 1;

		for (iterations = 0; iterations < delay; iterations++);
}



//PWM(0): //Makro-toteutukset


void FCD_PWM0_Enable(char nIdx)
{
	
	  //error checking
	  #ifndef MX_PWM
	  		#ifdef _BOOSTC
	  			#pragma error "This chip does not have PWM capability"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error This chip does not have PWM capability
			#endif
	  #else
	    #ifndef MX_PWM_CNT
	  		#ifdef _BOOSTC
	  			#pragma error "FCD file error (no MX_PWM_CNT)"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error FCD file error (no MX_PWM_CNT)
			#endif
	    #endif
	    #if (MX_PWM_CNT < 1)
	  		#ifdef _BOOSTC
	  			#pragma error "FCD file error (MX_PWM_CNT < 1)"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error FCD file error (MX_PWM_CNT < 1)
			#endif
	    #endif
	    #if (MX_PWM_CNT > 2)
	  		#ifdef _BOOSTC
	  			#pragma error "FCD file error (MX_PWM_CNT > 2)"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error FCD file error (MX_PWM_CNT > 2)
			#endif
	    #endif
	    #ifndef MX_PWM_TRIS1
	  		#ifdef _BOOSTC
	  			#pragma error "FCD file error (no MX_PWM_TRIS1)"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error FCD file error (no MX_PWM_TRIS1)
			#endif
	    #endif
	    #ifndef MX_PWM_1
	  		#ifdef _BOOSTC
	  			#pragma error "FCD file error (no MX_PWM_1)"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error FCD file error (no MX_PWM_1)
			#endif
	    #endif
	    #if (MX_PWM_CNT == 2)
	      #ifndef MX_PWM_TRIS2
	  		#ifdef _BOOSTC
	  			#pragma error "FCD file error (no MX_PWM_TRIS2)"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error FCD file error (no MX_PWM_TRIS2)
			#endif
	      #endif
	      #ifndef MX_PWM_2
	  		#ifdef _BOOSTC
	  			#pragma error "FCD file error (no MX_PWM_2)"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error FCD file error (no MX_PWM_2)
			#endif
	      #endif
	    #endif
	    #if (0 == 1)
	      #ifndef MX_PWM_TRIS1a
				#ifdef _BOOSTC
					#pragma error "PWM component error (using alternative, but no MX_PWM_TRIS1a)"
				#endif
				#ifdef HI_TECH_C
					#error PWM component error (using alternative, but no MX_PWM_TRIS1a)
				#endif
	        #define MX_PWM_ALT_ERROR
	      #endif
	      #ifndef MX_PWM_1a
				#ifdef _BOOSTC
					#pragma error "PWM component error (using alternative, but no MX_PWM_1a)"
				#endif
				#ifdef HI_TECH_C
					#error PWM component error (using alternative, but no MX_PWM_1a)
				#endif
	        #define MX_PWM_ALT_ERROR
	      #endif
	    #endif
	    #if (0 == 2)
	      #ifndef MX_PWM_TRIS2a
				#ifdef _BOOSTC
					#pragma error "PWM component error (using alternative, but no MX_PWM_TRIS2a)"
				#endif
				#ifdef HI_TECH_C
					#error PWM component error (using alternative, but no MX_PWM_TRIS2a)
				#endif
	        #define MX_PWM_ALT_ERROR
	      #endif
	      #ifndef MX_PWM_2a
				#ifdef _BOOSTC
					#pragma error "PWM component error (using alternative, but no MX_PWM_2a)"
				#endif
				#ifdef HI_TECH_C
					#error PWM component error (using alternative, but no MX_PWM_2a)
				#endif
	        #define MX_PWM_ALT_ERROR
	      #endif
	    #endif
	  #endif

	 #ifdef MX_PWM
	    pr2 = 199;
	    t2con = 0x05;

	  #if (MX_PWM_CNT >= 1)
	    if (nIdx == 1)
	    {
	        ccp1con = 0x0C;

	      #if (0 == 1)
	       #ifndef MX_PWM_ALT_ERROR
	        clear_bit(MX_PWM_TRIS1a, MX_PWM_1a);
	       #endif
	      #else
	        clear_bit(MX_PWM_TRIS1, MX_PWM_1);
	      #endif
		}
	  #endif

	  #if (MX_PWM_CNT >= 2)
	    if (nIdx == 2)
	    {
	        ccp2con = 0x0C;
	      #if (0 == 2)
	       #ifndef MX_PWM_ALT_ERROR
	        clear_bit(MX_PWM_TRIS2a, MX_PWM_2a);
	       #endif
	      #else
	        clear_bit(MX_PWM_TRIS2, MX_PWM_2);
	      #endif
	   }
	  #endif
	 #endif

	 #ifdef MX_PWM_ALT_ERROR
	   #undef MX_PWM_ALT_ERROR
	 #endif

}

void FCD_PWM0_Disable(char nIdx)
{
	
	  //error checking
	  #ifndef MX_PWM
	  		#ifdef _BOOSTC
	  			#pragma error "This chip does not have PWM capability"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error This chip does not have PWM capability
			#endif
	  #endif

	 #ifdef MX_PWM
	  #if (MX_PWM_CNT >= 1)
	    if (nIdx == 1)
	    {
	        ccp1con = 0x00;
	    }
	  #endif

	  #if (MX_PWM_CNT >= 2)
	    if (nIdx == 2)
	    {
	        ccp2con = 0x00;
	    }
	  #endif
	 #endif

}

void FCD_PWM0_SetDutyCycle(char nIdx, char nDuty)
{
	
	  //error checking
	  #ifndef MX_PWM
	  		#ifdef _BOOSTC
	  			#pragma error "This chip does not have PWM capability"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error This chip does not have PWM capability
			#endif
	  #endif

	 #ifdef MX_PWM
	  #if (MX_PWM_CNT >= 1)
	    if (nIdx == 1)
	    {
	        ccpr1l = nDuty;
	    }
	  #endif

	  #if (MX_PWM_CNT >= 2)
	    if (nIdx == 2)
	    {
	        ccpr2l = nDuty;
	    }
	  #endif
	 #endif

}

void FCD_PWM0_ChangePeriod(char nPeriodVal, char nPrescalerVal)
{
	
	  //error checking
	  #ifndef MX_PWM
	  		#ifdef _BOOSTC
	  			#pragma error "This chip does not have PWM capability"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error This chip does not have PWM capability
			#endif
	  #endif

	  #ifdef MX_PWM
	    pr2 = nPeriodVal;

	    switch (nPrescalerVal)
	    {
	        case 1:
	            t2con = 0x04;
	            break;

	        case 4:
	            t2con = 0x05;
	            break;

	        case 16:
	            t2con = 0x06;
	            break;
	    }
	  #endif

}

void FCD_PWM0_SetDutyCycle10bit(char nIdx, short nDuty)
{
	
		char nCCPxCONtemp;

	  //error checking
	  #ifndef MX_PWM
	  		#ifdef _BOOSTC
	  			#pragma error "This chip does not have PWM capability"
	  		#endif
	  		#ifdef HI_TECH_C
	  			#error This chip does not have PWM capability
			#endif
	  #endif

	 #ifdef MX_PWM
	  #if (MX_PWM_CNT >= 1)
	    if (nIdx == 1)
	    {
	        ccpr1l = (nDuty & 0x3FC) >> 2;

	    	nDuty = (nDuty & 0x03) << 4;
	    	nCCPxCONtemp = (ccp1con & 0xCF) | nDuty;
	    	ccp1con = nCCPxCONtemp;
	    }

	  #endif

	  #if (MX_PWM_CNT >= 2)
	    if (nIdx == 2)
	    {
	        ccpr2l = (nDuty & 0x3FC) >> 2;

	    	nDuty = (nDuty & 0x03) << 4;
	    	nCCPxCONtemp = (ccp2con & 0xCF) | nDuty;
	    	ccp2con = nCCPxCONtemp;
	    }

	  #endif
	 #endif

}

//Makro-toteutukset

void FCM_spi_incoming()
{
	
	//tarkastusbitti
	//Kutsu komponenttimakro: temp=RS232(0)::ReceiveRS232Char(40)
	FCV_TEMP = FCD_RS2320_ReceiveRS232Char(40);


	//Kommentti:
	//Status
	//1= käynnistys
	//2= running
	//3 = fault


	//tarkastusbitti ok?
	//Päätös: temp = 212?
	if (FCV_TEMP == 212)
	{
		//1 faultcode
		//Kutsu komponenttimakro: data1=RS232(0)::ReceiveRS232Char(40)
		FCV_DATA1 = FCD_RS2320_ReceiveRS232Char(40);


		//Kommentti:
		//koodit
		//vikakoodit
		//1 = ei liekkiä
		//2 = lv maissa
		//3 = lv irti
		//4 = ls maissa
		//5 = ls irti
		//6 = yks maissa
		//7 = yks irti
		//8 = ls ja yks ero liian suuri
		//9 = burner speed
		//vaiheet
		//1 = diag
		//2 = hehku
		//3 = poltin on
		//4 = pa pumppu on
		//5 = syttyminen varmennett


		//2 phase
		//Kutsu komponenttimakro: data2=RS232(0)::ReceiveRS232Char(40)
		FCV_DATA2 = FCD_RS2320_ReceiveRS232Char(40);


		//3 power
		//Kutsu komponenttimakro: data3=RS232(0)::ReceiveRS232Char(40)
		FCV_DATA3 = FCD_RS2320_ReceiveRS232Char(40);


		//4 temp_sens
		//Kutsu komponenttimakro: data4=RS232(0)::ReceiveRS232Char(40)
		FCV_DATA4 = FCD_RS2320_ReceiveRS232Char(40);


		//5 ovh sens
		//Kutsu komponenttimakro: data5=RS232(0)::ReceiveRS232Char(40)
		FCV_DATA5 = FCD_RS2320_ReceiveRS232Char(40);


		//6 pako hi
		//Kutsu komponenttimakro: data6=RS232(0)::ReceiveRS232Char(40)
		FCV_DATA6 = FCD_RS2320_ReceiveRS232Char(40);


		//7 pako lo
		//Kutsu komponenttimakro: ack=RS232(0)::ReceiveRS232Char(40)
		FCV_ACK = FCD_RS2320_ReceiveRS232Char(40);


	}


}

void FCM_print_data()
{
	
	//Kutsu komponenttimakro
	//Kutsu komponenttimakro: Custom2(0)::LCD_Print_String(str_x, 0, rivi1, 2, 0, 1)
	FCD_Custom20_LCD_Print_String(FCV_STR_X,FCSZ_STR_X, 0, FCV_RIVI1, 2, 0, 1);


	//Kutsu komponenttimakro
	//Kutsu komponenttimakro: Custom2(0)::LCD_Print_String(str_y, 0, rivi3, 2, 0, 1)
	FCD_Custom20_LCD_Print_String(FCV_STR_Y,FCSZ_STR_Y, 0, FCV_RIVI3, 2, 0, 1);


	//Kutsu komponenttimakro
	//Kutsu komponenttimakro: Custom2(0)::LCD_Print_String(str_z, 0, rivi5, 2, 0, 1)
	FCD_Custom20_LCD_Print_String(FCV_STR_Z,FCSZ_STR_Z, 0, FCV_RIVI5, 2, 0, 1);


}

void main()
{
	
	//alustus
	adcon1 = 0x0F;
cmcon = 0x07;

	
	#if (RS232_1115200_UART == 0)
		set_bit(RS232_1115200_RX_TRIS, RS232_1115200_SW_RX);				// Receive pin is a input
		clear_bit(RS232_1115200_TX_TRIS, RS232_1115200_SW_TX);			// Transmit pin is a output
		set_bit(RS232_1115200_TX_PORT, RS232_1115200_SW_TX);				// Transmit pin is default high
	#endif

	#if (RS232_1115200_UART == 1)
		txsta = RS232_1115200_TXSTA_VAL;   					// 8-bit, async, low speed, off
		spbrg = RS232_1115200_SPBRG_VAL;   					// set the baud rate
		rcsta = 0;                    				// 8-bit, disabled

		if(RS232_1115200_DATASIZE == 1)
		{
			st_bit(txsta, TX9);   					// 9-bit TX
			st_bit(rcsta, RX9);                    	// 9-bit RX
		}

		st_bit(rcsta, SPEN);         				// turn on serial interface
	#endif

	#if (RS232_1115200_UART == 2)

		txsta2 = RS232_1115200_TXSTA_VAL;   					// 8-bit, async, low speed, off
		spbrg2 = RS232_1115200_SPBRG_VAL;   					// set the baud rate
		rcsta2 = 0;                    				// 8-bit, disabled

		if(RS232_1115200_DATASIZE == 1)
		{
			st_bit(txsta2, TX9);   					// 9-bit TX
			st_bit(rcsta2, RX9);                    // 9-bit RX
		}

		st_bit(rcsta2, SPEN);         				// turn on serial interface
	#endif

	#ifdef RS232_1115200_HARDWARE
		set_bit( RS232_1115200_CTS_TRIS, RS232_1115200_CTS_PIN);			//CTS is an input
		clear_bit( RS232_1115200_RTS_TRIS, RS232_1115200_RTS_PIN);		//RTS is an output
		set_bit( RS232_1115200_RTS_PORT, RS232_1115200_RTS_PIN); 			//not ready to accept data
	#endif


	//Keskeytyksen alustuskoodi
	


	//Kommentti:
	//Based on fcu  10.02 no set rtc br96.fcf, just replaced the pwm functions
	// with sent can. Added pulse encoder
	//Messages 
	//Fan cont 
	//ID hi 20 lo 1 (32)
	//bytes
	//0 = front light on (1)
	//1 = pwm left
	//2 = pwm right
	//3 = Light level
	//Incoming
	//Id hi 15 lo 1
	//bytes
	//0 = Coolant level


	//C-koodi
	//C-koodi:
	OSCCON = 0x60;
	OSCTUNE = 0x40;


	//Kutsu komponenttimakro
	//Kutsu komponenttimakro: PWM(0)::Enable(1)
	FCD_PWM0_Enable(1);


	//Kutsu komponenttimakro
	//Kutsu komponenttimakro: PWM(0)::SetDutyCycle(1, 160)
	FCD_PWM0_SetDutyCycle(1, 160);


	//Kutsu komponenttimakro
	//Kutsu komponenttimakro: Custom2(0)::LCD_Init
	FCD_Custom20_LCD_Init();


	//aseta rivitys
	//Laskutoimitus:
	//  rivi1 = 0
	//  rivi2 = 8
	//  rivi3 = 16
	//  rivi4 = 24
	//  rivi5 = 32
	//  rivi6 = 40
	//  rivi7 = 48
	//  rivi8 = 56
	FCV_RIVI1 = 0;
	FCV_RIVI2 = 8;
	FCV_RIVI3 = 16;
	FCV_RIVI4 = 24;
	FCV_RIVI5 = 32;
	FCV_RIVI6 = 40;
	FCV_RIVI7 = 48;
	FCV_RIVI8 = 56;
	

	//muuttujat
	//Laskutoimitus:
	//  data1 = 0
	//  data2 = 0
	//  data3 = 255
	//  data4 = 255
	//  data5 = 255
	//  data6 = 0b11111111
	FCV_DATA1 = 0;
	FCV_DATA2 = 0;
	FCV_DATA3 = 255;
	FCV_DATA4 = 255;
	FCV_DATA5 = 255;
	FCV_DATA6 = 255;
	

	//Viive
	//Viive: 10 ms
	delay_ms(10);


	//Kutsu komponenttimakro
	//Kutsu komponenttimakro: Custom2(0)::LCD_Clear
	FCD_Custom20_LCD_Clear();


	//Kutsu komponenttimakro
	//Kutsu komponenttimakro: RS232(0)::ReceiveRS232Char(0)
	FCD_RS2320_ReceiveRS232Char(0);


	//print  fan softa
	//Kutsu komponenttimakro: Custom2(0)::LCD_Print_String(" Eber puhallinohjain ", 0, 0, 0, 0, 1)
	FCD_Custom20_LCD_Print_String(" Eber puhallinohjain ",21, 0, 0, 0, 0, 1);


	//versio
	//Kutsu komponenttimakro: Custom2(0)::LCD_Print_String("  Ver 9.3     ", 0, rivi3, 2, 0, 1)
	FCD_Custom20_LCD_Print_String("  Ver 9.3     ",14, 0, FCV_RIVI3, 2, 0, 1);


	//Viive
	//Viive: 100 ms
	delay_ms(100);


	//print ecu softa
	//Kutsu komponenttimakro: Custom2(0)::LCD_Print_String("   Eber controller   ", 0, rivi5, 0, 0, 1)
	FCD_Custom20_LCD_Print_String("   Eber controller   ",21, 0, FCV_RIVI5, 0, 0, 1);


	//ecu versio
	//Kutsu komponenttimakro: Custom2(0)::LCD_Print_String("  Ver 1.2     ", 0, rivi7, 2, 0, 1)
	FCD_Custom20_LCD_Print_String("  Ver 1.2     ",14, 0, FCV_RIVI7, 2, 0, 1);


	//Viive
	//Viive: 1 s
	delay_s(1);


	//Kutsu komponenttimakro
	//Kutsu komponenttimakro: Custom2(0)::LCD_Clear
	FCD_Custom20_LCD_Clear();


	//Kutsu komponenttimakro
	//Kutsu komponenttimakro: Custom2(0)::LCD_Clear
	FCD_Custom20_LCD_Clear();


	//rs232 in
	//Keskeytys: Salli rs232_incoming
	st_bit(intcon, GIE);
	PEIE = 1; // Enable peripheral interrupts
	GIE = 1; // Enable global interrupts
	RCIE = 1; // Enable USART receive interrupts


	//mainloop
	//Silmukka: While 1
	while (1)
	{
		//Laskutoimitus
		//Laskutoimitus:
		//  z_axel = (data5 << 2) + (data6 / 64)
		//  temp = data6 MOD 64
		//  y_axel = (data4 << 2) + (temp / 16)
		//  temp = data6 MOD 16
		//  x_axel = (data3 << 2) + (temp / 4)
		FCV_Z_AXEL = (FCV_DATA5 << 2) + (FCV_DATA6 / 64);
		FCV_TEMP = FCV_DATA6 % 64;
		FCV_Y_AXEL = (FCV_DATA4 << 2) + (FCV_TEMP / 16);
		FCV_TEMP = FCV_DATA6 % 16;
		FCV_X_AXEL = (FCV_DATA3 << 2) + (FCV_TEMP / 4);
		

		//Merkkijonon käsittely
		//Merkkijonon käsittely:
		//  str_x = ToString$(x_axel)
		//  str_x = str_x + " "
		//  str_y = ToString$(y_axel)
		//  str_y = str_y + " "
		//  str_z = ToString$(z_axel)
		//  str_z = str_z + " "
		FCI_TOSTRING(FCV_X_AXEL,FCV_STR_X,FCSZ_STR_X);
		FCI_CONCATENATE(FCV_STR_X,FCSZ_STR_X," ",1,FCV_STR_X,FCSZ_STR_X);
		FCI_TOSTRING(FCV_Y_AXEL,FCV_STR_Y,FCSZ_STR_Y);
		FCI_CONCATENATE(FCV_STR_Y,FCSZ_STR_Y," ",1,FCV_STR_Y,FCSZ_STR_Y);
		FCI_TOSTRING(FCV_Z_AXEL,FCV_STR_Z,FCSZ_STR_Z);
		FCI_CONCATENATE(FCV_STR_Z,FCSZ_STR_Z," ",1,FCV_STR_Z,FCSZ_STR_Z);
		

		//Kutsu makro
		//Kutsu makro: print_data
		FCM_print_data();


		//Viive
		//Viive: 500 ms
		delay_ms(255);
		delay_ms(245);


		if ((1) == 0) break;
	}


	mainendloop: goto mainendloop;
}

void MX_INTERRUPT_MACRO(void)
{
	//Handler code for [rs232_incoming]
	if (pir1 & (1 << RCIF))
	{
	FCM_spi_incoming(); // call selected macro
	clear_bit(pir1, RCIF); // clear interrupt
	}

}



