//************************************************************************************
//**  
//**  File name:     D:\projects 2013\pc data dev5.c
//**  Generated by:  Flowcode v4.2.3.58
//**  Date:          Thursday, March 07, 2013 12:15:34
//**  Licence:       Professional
//**  Registered to: 20ACM8
//**  
//**  
//**  http://www.matrixmultimedia.com
//************************************************************************************


#define MX_PIC

//Defines for microcontroller
#define P16F877A
#define MX_EE
#define MX_EE_TYPE2
#define MX_EE_SIZE 256
#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 2
#define MX_PWM_TRIS1 trisc
#define MX_PWM_1 2
#define MX_PWM_TRIS2 trisc
#define MX_PWM_2 1

//Functions
#define MX_CLK_SPEED 20000000
#ifdef _BOOSTC
#include <system.h>
#endif
#ifdef HI_TECH_C
#include <pic.h>
#endif

//Configuration data
#ifdef _BOOSTC
#pragma DATA 0x2007, 0x3f3a
#endif
#ifdef HI_TECH_C
__CONFIG(0x3f3a);
#endif

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

//Macro function declarations
void FCM_keypad();


//Variable declarations
#define FCSZ_STRING 20
#define FCSZ_STR1 20
char FCV_MEM;
char FCV_LEN;
char FCV_STRING[FCSZ_STRING];
char FCV_STR1[FCSZ_STR1];
short FCV_NUM;
char FCV_KEYPRESSED;
char FCV_SW;
char FCV_COUNT2;
short FCV_PACKET;
char FCV_NUMBER[4];
char FCV_COUNT;
short FCV_VALUE;


//Defines:

/**** Macro Substitutions ****
portd = D1 Port
trisd = D1 Data Direction
portd = D2 Port
trisd = D2 Data Direction
portd = D3 Port
trisd = D3 Data Direction
portd = D4 Port
trisd = D4 Data Direction
portd = RS Port
trisd = RS Data Direction
portd = E Port
trisd = E Data Direction
2 = Data 1_Pin
3 = Data 2 Pin
4 = Data 3 Pin
5 = Data 4 Pin
0 = RS Pin
1 = Enable Pin
LCD_328606 = Unique Component Reference Number
2 = Row Count
16 = Column Count
******************************/

	//component connections
	#define LCD_328606_PORT0    portd
	#define LCD_328606_TRIS0    trisd
	#define LCD_328606_PORT1    portd
	#define LCD_328606_TRIS1    trisd
	#define LCD_328606_PORT2    portd
	#define LCD_328606_TRIS2    trisd
	#define LCD_328606_PORT3    portd
	#define LCD_328606_TRIS3    trisd
	#define LCD_328606_PORT4    portd
	#define LCD_328606_TRIS4    trisd
	#define LCD_328606_PORT5    portd
	#define LCD_328606_TRIS5    trisd
	#define LCD_328606_BIT0    	2
	#define LCD_328606_BIT1    	3
	#define LCD_328606_BIT2    	4
	#define LCD_328606_BIT3    	5
	#define LCD_328606_RS      	0
	#define LCD_328606_E       	1
	#define LCD_328606_ROWCNT	2
	#define LCD_328606_COLCNT	16

	#ifdef _BOOSTC
	  #define LCD_328606_DELAY   delay_10us(10)
	#endif
	#ifdef _C2C_
	  #define LCD_328606_DELAY   delay_us(100)
	#endif
	#ifdef HI_TECH_C
	  #define LCD_328606_DELAY   __delay_us(120)
	#endif
	#ifndef LCD_328606_DELAY
	  #define LCD_328606_DELAY   delay_us(100)
	#endif




//LCDDisplay0: //Macro function declarations

void FCD_LCDDisplay0_RawSend(char in, char mask);
void FCD_LCDDisplay0_Start();
void FCD_LCDDisplay0_Clear();
void FCD_LCDDisplay0_PrintASCII(char Character);
void FCD_LCDDisplay0_Command(char in);
void FCD_LCDDisplay0_Cursor(char x, char y);
void FCD_LCDDisplay0_PrintNumber(short Number);
void FCD_LCDDisplay0_PrintString(char* String, char MSZ_String);
void FCD_LCDDisplay0_ScrollDisplay(char Direction, char Num_Positions);
void FCD_LCDDisplay0_ClearLine(char Line);
void FCD_LCDDisplay0_RAM_Write(char nIdx, char d0, char d1, char d2, char d3, char d4, char d5, char d6, char d7);
//Defines:

/**** Macro Substitutions ****
{'1','4','7','*','2','5','8','0','3','6','9','#'} = Key List (Numbers)
{1,4,7,10,2,5,8,0,3,6,9,11} = Key List (Characters)
trisc = Row Data Direction Register
portc = Row Port Register
3 = Number Of Columns
4 = Number Of Rows
trisc = Column Data Direction Register
portc = Column Port Register
8 = Column 1 mask
4 = Column 2 mask
2 = Column 3 mask
128 = Column 4 mask
128 = Row 1 mask
64 = Row 2 mask
32 = Row 3 mask
16 = Row 4 mask
******************************/




//KeyPad0: //Macro function declarations

char FCD_KeyPad0_GetKeypadNumber();
char FCD_KeyPad0_GetKeypadAscii();
//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
129 = UART SPBRG Value
RS232_328566 = Unique ID
Unused = Bitbanged Receive Port Register
Unused = Bitbanged Receive Data Direction Register
Unused = Bitbanged Receive Pin
Unused = Bitbanged Transmit Pin
120 = 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_328566_RTS_PORT			portc
#define RS232_328566_RTS_TRIS			trisc
#define RS232_328566_CTS_PORT			portc
#define RS232_328566_CTS_TRIS			trisc
#define RS232_328566_RTS_PIN			0
#define RS232_328566_CTS_PIN			4
#define RS232_328566_UART				1
#define RS232_328566_TOUT  			0
#define RS232_328566_DATASIZE			0
#define RS232_328566_PARITY			0
#define RS232_328566_LEGACY_RV			0

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

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

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

#if (RS232_328566_UART == 0)
	#define RS232_328566_RX_PORT		Unused
	#define RS232_328566_RX_TRIS		Unused
	#define RS232_328566_SW_RX		Unused
	#define RS232_328566_SW_TX		Unused
	#define RS232_328566_SW_BAUD		120
	#define RS232_328566_TX_PORT		Unused
	#define RS232_328566_TX_TRIS		Unused
#else
	#define RS232_328566_TXSTA_VAL  	4
	#define RS232_328566_SPBRG_VAL  	129
	#define RS232_328566_SW_BAUD		0
#endif

#if (RS232_328566_LEGACY_RV == 0)
	#if (RS232_328566_DATASIZE == 1)
		#pragma error "RS232 Component cannot combine 9-bit data mode with legacy return value of 255 please check the Component properties"
	#endif
#endif

#define RS232_328566_STATUS_LOOP		0
#define RS232_328566_STATUS_TIMEOUT	1
#define RS232_328566_STATUS_RXBYTE	2





//RS2320: //Macro function declarations

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);



//LCDDisplay0: //Macro implementations


void FCD_LCDDisplay0_RawSend(char in, char mask)
{
		unsigned char pt;

		clear_bit(LCD_328606_PORT0, LCD_328606_BIT0);
		clear_bit(LCD_328606_PORT1, LCD_328606_BIT1);
		clear_bit(LCD_328606_PORT2, LCD_328606_BIT2);
		clear_bit(LCD_328606_PORT3, LCD_328606_BIT3);
		clear_bit(LCD_328606_PORT4, LCD_328606_RS);
		clear_bit(LCD_328606_PORT5, LCD_328606_E);
		pt = ((in >> 4) & 0x0f);
		if (pt & 0x01)
		    set_bit(LCD_328606_PORT0, LCD_328606_BIT0);
		if (pt & 0x02)
		    set_bit(LCD_328606_PORT1, LCD_328606_BIT1);
		if (pt & 0x04)
		    set_bit(LCD_328606_PORT2, LCD_328606_BIT2);
		if (pt & 0x08)
		    set_bit(LCD_328606_PORT3, LCD_328606_BIT3);
		if (mask)
		    set_bit(LCD_328606_PORT4, LCD_328606_RS);
		LCD_328606_DELAY;
		set_bit (LCD_328606_PORT5, LCD_328606_E);
		LCD_328606_DELAY;
		clear_bit (LCD_328606_PORT5, LCD_328606_E);
		pt = (in & 0x0f);
		LCD_328606_DELAY;
		clear_bit(LCD_328606_PORT0, LCD_328606_BIT0);
		clear_bit(LCD_328606_PORT1, LCD_328606_BIT1);
		clear_bit(LCD_328606_PORT2, LCD_328606_BIT2);
		clear_bit(LCD_328606_PORT3, LCD_328606_BIT3);
		clear_bit(LCD_328606_PORT4, LCD_328606_RS);
		clear_bit(LCD_328606_PORT5, LCD_328606_E);
		if (pt & 0x01)
		    set_bit(LCD_328606_PORT0, LCD_328606_BIT0);
		if (pt & 0x02)
		    set_bit(LCD_328606_PORT1, LCD_328606_BIT1);
		if (pt & 0x04)
		    set_bit(LCD_328606_PORT2, LCD_328606_BIT2);
		if (pt & 0x08)
		    set_bit(LCD_328606_PORT3, LCD_328606_BIT3);
		if (mask)
		    set_bit(LCD_328606_PORT4, LCD_328606_RS);
		LCD_328606_DELAY;
		set_bit (LCD_328606_PORT5, LCD_328606_E);
		LCD_328606_DELAY;
		clear_bit (LCD_328606_PORT5, LCD_328606_E);
		LCD_328606_DELAY;
}

void FCD_LCDDisplay0_Start()
{
	
		clear_bit(LCD_328606_TRIS0, LCD_328606_BIT0);
		clear_bit(LCD_328606_TRIS1, LCD_328606_BIT1);
		clear_bit(LCD_328606_TRIS2, LCD_328606_BIT2);
		clear_bit(LCD_328606_TRIS3, LCD_328606_BIT3);
		clear_bit(LCD_328606_TRIS4, LCD_328606_RS);
		clear_bit(LCD_328606_TRIS5, LCD_328606_E);

		Wdt_Delay_Ms(12);

		FCD_LCDDisplay0_RawSend(0x33, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x33, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x32, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x2c, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x06, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x0c, 0);
		Wdt_Delay_Ms(2);

		//clear the display
		FCD_LCDDisplay0_RawSend(0x01, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x02, 0);
		Wdt_Delay_Ms(2);

}

void FCD_LCDDisplay0_Clear()
{
	
		FCD_LCDDisplay0_RawSend(0x01, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x02, 0);
		Wdt_Delay_Ms(2);

}

void FCD_LCDDisplay0_PrintASCII(char Character)
{
	
		FCD_LCDDisplay0_RawSend(Character, 0x10);

}

void FCD_LCDDisplay0_Command(char in)
{
	
		FCD_LCDDisplay0_RawSend(in, 0);
		Wdt_Delay_Ms(2);

}

void FCD_LCDDisplay0_Cursor(char x, char y)
{
	
	  #if (LCD_328606_ROWCNT == 1)
	    y=0x80;
	  #endif

	  #if (LCD_328606_ROWCNT == 2)
		if (y==0)
			y=0x80;
		else
			y=0xc0;
	  #endif

	  #if (LCD_328606_ROWCNT == 4)
		if (y==0)
			y=0x80;
		else if (y==1)
			y=0xc0;

		#if (LCD_328606_COLCNT == 16)
			else if (y==2)
				y=0x90;
			else
				y=0xd0;
		#endif

		#if (LCD_328606_COLCNT == 20)
			else if (y==2)
				y=0x94;
			else
				y=0xd4;
		#endif
	  #endif

		FCD_LCDDisplay0_RawSend(y+x, 0);
		Wdt_Delay_Ms(2);

}

void FCD_LCDDisplay0_PrintNumber(short Number)
{
	
		short tmp_int;
		char tmp_byte;
		if (Number < 0)
		{
			FCD_LCDDisplay0_RawSend('-', 0x10);
			Number = 0 - Number;
		}

		tmp_int = Number;
		if (Number >= 10000)
		{
			tmp_byte = tmp_int / 10000;
			FCD_LCDDisplay0_RawSend('0' + tmp_byte, 0x10);

			while (tmp_byte > 0)
			{
				tmp_int = tmp_int - 10000;
				tmp_byte--;
			}
		}
		if (Number >= 1000)
		{
			tmp_byte = tmp_int / 1000;
			FCD_LCDDisplay0_RawSend('0' + tmp_byte, 0x10);

			while (tmp_byte > 0)
			{
				tmp_int = tmp_int - 1000;
				tmp_byte--;
			}
		}
		if (Number >= 100)
		{
			tmp_byte = tmp_int / 100;
			FCD_LCDDisplay0_RawSend('0' + tmp_byte, 0x10);

			while (tmp_byte > 0)
			{
				tmp_int = tmp_int - 100;
				tmp_byte--;
			}
		}
		if (Number >= 10)
		{
			tmp_byte = tmp_int / 10;
			FCD_LCDDisplay0_RawSend('0' + tmp_byte, 0x10);

			while (tmp_byte > 0)
			{
				tmp_int = tmp_int - 10;
				tmp_byte--;
			}
		}
		FCD_LCDDisplay0_RawSend('0' + tmp_int, 0x10);

}

void FCD_LCDDisplay0_PrintString(char* String, char MSZ_String)
{
	
		char idx = 0;

		for (idx=0; idx<MSZ_String; idx++)
		{
			#ifdef _BOOSTC
				if (String[idx] == 0)
				{
					break;
				}
				FCD_LCDDisplay0_RawSend(String[idx], 0x10);
			#endif

			#ifdef HI_TECH_C
				if (*String == 0)
				{
					break;
				}
				FCD_LCDDisplay0_RawSend(*String, 0x10);
				String++;
			#endif
		}

}

void FCD_LCDDisplay0_ScrollDisplay(char Direction, char Num_Positions)
{
	
		char cmd = 0;
		char count;

		//Choose the direction
		switch (Direction)
		{
			case 0:
			case 'l':
			case 'L':

				cmd = 0x18;
				break;

			case 1:
			case 'r':
			case 'R':

				cmd = 0x1C;
				break;

			default:
				break;
		}

		//If direction accepted then scroll the specified amount
		if (cmd)
		{
			for (count = 0; count < Num_Positions; count++)
				FCD_LCDDisplay0_Command(cmd);
		}

}

void FCD_LCDDisplay0_ClearLine(char Line)
{
	
		char count;
		char rowcount;

		//Define number of columns per line
		#if (LCD_328606_ROWCNT == 1)
			rowcount=80;
		#endif

		#if (LCD_328606_ROWCNT == 2)
			rowcount=40;
		#endif

		#if (LCD_328606_ROWCNT == 4)
			#if (LCD_328606_COLCNT == 16)
				rowcount=16;
			#endif
			#if (LCD_328606_COLCNT == 20)
				rowcount=20;
			#endif
		#endif

		//Start at beginning of the line
		FCD_LCDDisplay0_Cursor (0, Line);

		//Send out spaces to clear line
		for (count = 0; count < rowcount; count++)
			FCD_LCDDisplay0_RawSend(' ', 0x10);

		//Move back to the beginning of the line.
		FCD_LCDDisplay0_Cursor (0, Line);

}

void FCD_LCDDisplay0_RAM_Write(char nIdx, char d0, char d1, char d2, char d3, char d4, char d5, char d6, char d7)
{
	   //set CGRAM address
	   FCD_LCDDisplay0_RawSend(64 + (nIdx << 3), 0);
	   delay_ms(2);

	   //write CGRAM data
	   FCD_LCDDisplay0_RawSend(d0, 0x10);
	   FCD_LCDDisplay0_RawSend(d1, 0x10);
	   FCD_LCDDisplay0_RawSend(d2, 0x10);
	   FCD_LCDDisplay0_RawSend(d3, 0x10);
	   FCD_LCDDisplay0_RawSend(d4, 0x10);
	   FCD_LCDDisplay0_RawSend(d5, 0x10);
	   FCD_LCDDisplay0_RawSend(d6, 0x10);
	   FCD_LCDDisplay0_RawSend(d7, 0x10);

	   //Clear the display
	   FCD_LCDDisplay0_RawSend(0x01, 0);
	   delay_ms(2);
	   FCD_LCDDisplay0_RawSend(0x02, 0);
	   delay_ms(2);
}



//KeyPad0: //Macro implementations


char FCD_KeyPad0_GetKeypadNumber()
{
		//get matrices for rows and columns
		#if (3 == 1)
			#define KPAD_COL_MTX {8}
			#define KPAD_COL_MASK (8)
		#endif
		#if (3 == 2)
			#define KPAD_COL_MTX {8,4}
			#define KPAD_COL_MASK (8|4)
		#endif
		#if (3 == 3)
			#define KPAD_COL_MTX {8,4,2}
			#define KPAD_COL_MASK (8|4|2)
		#endif
		#if (3 == 4)
			#define KPAD_COL_MTX {8,4,2,128}
			#define KPAD_COL_MASK (8|4|2|128)
		#endif
		#if (4 == 1)
			#define KPAD_ROW_MTX {128}
			#define KPAD_ROW_MASK (128)
		#endif
		#if (4 == 2)
			#define KPAD_ROW_MTX {128,64}
			#define KPAD_ROW_MASK (128|64)
		#endif
		#if (4 == 3)
			#define KPAD_ROW_MTX {128,64,32}
			#define KPAD_ROW_MASK (128|64|32)
		#endif
		#if (4 == 4)
			#define KPAD_ROW_MTX {128,64,32,16}
			#define KPAD_ROW_MASK (128|64|32|16)
		#endif

		#ifndef KPAD_COL_MTX
			#pragma error Keypad error: column count is not 1-4
		#endif
		#ifndef KPAD_ROW_MTX
			#pragma error Keypad error: row count is not 1-4
		#endif

		//store keys and pin connections into a constant array
		#ifdef _BOOSTC
			rom char* mtxKeysAsNumbers = {1,4,7,10,2,5,8,0,3,6,9,11};
			rom char* mtxCols = KPAD_COL_MTX;
			rom char* mtxRows = KPAD_ROW_MTX;
		#endif
		#ifdef HI_TECH_C
			const char mtxKeysAsNumbers[] = {1,4,7,10,2,5,8,0,3,6,9,11};
			const char mtxCols[] = KPAD_COL_MTX;
			const char mtxRows[] = KPAD_ROW_MTX;
		#endif

		//set up i/o of port (rows = inputs, columns = outputs)
		trisc = (trisc | KPAD_ROW_MASK);
		trisc = (trisc & ~KPAD_COL_MASK);		//BR

		char iCol;
		char iRow;
		char c_ip;
		for (iCol=0; iCol<3; iCol++)
		{
			//output the appropriate column high
			portc = mtxCols[iCol];

			//delay
			delay_us(10);

			//read the port
			c_ip = portc;

			//check for a hit
			for (iRow=0; iRow<4; iRow++)
			{
				if ((c_ip & mtxRows[iRow]) != 0)
				{
					//found it!
					goto found_key;
				}
			}
		}

		//if it gets here, it has not been found...
		return (255);

	  found_key:
		return (mtxKeysAsNumbers[(iCol*4) + iRow]);

	  #undef KPAD_COL_MTX
	  #undef KPAD_ROW_MTX
	  #undef KPAD_COL_MASK
	  #undef KPAD_ROW_MASK

}

char FCD_KeyPad0_GetKeypadAscii()
{
		//get matrices for rows and columns
		#if (3 == 1)
			#define KPAD_COL_MTX {8}
			#define KPAD_COL_MASK (8)
		#endif
		#if (3 == 2)
			#define KPAD_COL_MTX {8,4}
			#define KPAD_COL_MASK (8|4)
		#endif
		#if (3 == 3)
			#define KPAD_COL_MTX {8,4,2}
			#define KPAD_COL_MASK (8|4|2)
		#endif
		#if (3 == 4)
			#define KPAD_COL_MTX {8,4,2,128}
			#define KPAD_COL_MASK (8|4|2|128)
		#endif
		#if (4 == 1)
			#define KPAD_ROW_MTX {128}
			#define KPAD_ROW_MASK (128)
		#endif
		#if (4 == 2)
			#define KPAD_ROW_MTX {128,64}
			#define KPAD_ROW_MASK (128|64)
		#endif
		#if (4 == 3)
			#define KPAD_ROW_MTX {128,64,32}
			#define KPAD_ROW_MASK (128|64|32)
		#endif
		#if (4 == 4)
			#define KPAD_ROW_MTX {128,64,32,16}
			#define KPAD_ROW_MASK (128|64|32|16)
		#endif

		#ifndef KPAD_COL_MTX
			#pragma error Keypad error: column count is not 1-4
		#endif
		#ifndef KPAD_ROW_MTX
			#pragma error Keypad error: row count is not 1-4
		#endif

		//store keys and pin connections into a constant array
		#ifdef _BOOSTC
			rom char* mtxKeysAsChars = {'1','4','7','*','2','5','8','0','3','6','9','#'};
			rom char* mtxCols = KPAD_COL_MTX;
			rom char* mtxRows = KPAD_ROW_MTX;
		#endif
		#ifdef HI_TECH_C
			const char mtxKeysAsChars[] = {'1','4','7','*','2','5','8','0','3','6','9','#'};
			const char mtxCols[] = KPAD_COL_MTX;
			const char mtxRows[] = KPAD_ROW_MTX;
		#endif

		//set up i/o of port (rows = inputs, columns = outputs)
		trisc = (trisc | KPAD_ROW_MASK);
		trisc = (trisc & ~KPAD_COL_MASK);

		char iCol;
		char iRow;
		char c_ip;
		for (iCol=0; iCol<3; iCol++)
		{
			//output the appropriate column high
			portc = mtxCols[iCol];

			//delay
			delay_us(10);

			//read the port
			c_ip = portc;

			//check for a hit
			for (iRow=0; iRow<4; iRow++)
			{
				if ((c_ip & mtxRows[iRow]) != 0)
				{
					//found it!
					goto found_key;
				}
			}
		}

		//if it gets here, it has not been found...
		return (255);

	  found_key:
		return (mtxKeysAsChars[(iCol*4) + iRow]);

	  #undef KPAD_COL_MTX
	  #undef KPAD_ROW_MTX
	  #undef KPAD_COL_MASK
	  #undef KPAD_ROW_MASK

}



//RS2320: //Macro implementations


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

		char dMask;
		char idx;
		char count = 8;

		#ifdef RS232_328566_HARDWARE
			//wait until CTS is low
			while (( RS232_328566_CTS_PORT & (1 << RS232_328566_CTS_PIN) ) != 0);
		#endif

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

		clear_bit( RS232_328566_TX_PORT, RS232_328566_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_328566_TX_PORT, RS232_328566_SW_TX);
			else
				clear_bit( RS232_328566_TX_PORT, RS232_328566_SW_TX);

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

		set_bit( RS232_328566_TX_PORT, RS232_328566_SW_TX);			// Send Stop bit
		FCD_RS2320_RS232_Delay(0);

	#endif

	#if (RS232_328566_UART == 1)

		#ifndef MX_UART
			#pragma error "Chip does not have UART capability"
		#endif

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

		#ifdef RS232_328566_HARDWARE
			//wait until CTS is low
			while (( RS232_328566_CTS_PORT & (1 << RS232_328566_CTS_PIN) ) != 0);
		#endif

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

		txreg = nChar;

	#endif

	#if (RS232_328566_UART == 2)

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

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

		#ifdef RS232_328566_HARDWARE
			//wait until CTS is low
			while (( RS232_328566_CTS_PORT & (1 << RS232_328566_CTS_PIN) ) != 0);
		#endif

		#if( RS232_328566_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_328566_STATUS_LOOP;
		char idx;
		char count = 8;

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

		#if ( RS232_328566_UART == 1)
			#ifndef MX_UART
				#pragma error "Chip does not have UART capability"
			#endif
		#endif

		#if ( RS232_328566_UART == 2)
			#ifndef MX_UART2
				#pragma error "Chip does not have second UART capability"
			#endif
		#endif

		#ifdef RS232_328566_HARDWARE
			//ready to accept data
			clear_bit( RS232_328566_RTS_PORT, RS232_328566_RTS_PIN);
		#endif

		if (nTimeout == 255)
			bWaitForever = 1;

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

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

		while (rxStatus == RS232_328566_STATUS_LOOP)
		{
			if (bWaitForever == 0)
			{
				//don't wait forever, so do timeout thing...
				if (nTimeout == 0)
				{
					rxStatus = RS232_328566_STATUS_TIMEOUT;
				}
				else
				{
					if ( RS232_328566_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_328566_UART == 0)
				regcheck = test_bit(RS232_328566_RX_PORT, RS232_328566_SW_RX);	//Test for start bit
				if (regcheck == 0)
					rxStatus = RS232_328566_STATUS_RXBYTE;
			#endif

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

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

		if (rxStatus == RS232_328566_STATUS_RXBYTE)
		{
			#if ( RS232_328566_UART > 0)

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

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

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

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

					#ifdef RS232_328566_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_328566_LEGACY_RV == 1)
						retVal = 0x400;					//Framing Error Flag
					#endif
				}
				else
				{
					#if ( RS232_328566_UART == 1)
						regcheck = ts_bit(rcsta, OERR);
					#endif

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

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

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

						#ifdef RS232_328566_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_328566_LEGACY_RV == 1)
							retVal = 0x800;					//Overrun Error Flag
						#endif

					}
					else
					{
						#if ( RS232_328566_UART == 1)
							retVal = rcreg; 			//no error, so rx byte is valid

							#if( RS232_328566_DATASIZE == 1)
								if(ts_bit(rcsta, RX9D));
									retVal = retVal | 0x100;
							#endif

						#endif

						#if ( RS232_328566_UART == 2)
							retVal = rcreg2; 			//no error, so rx byte is valid

							#if( RS232_328566_DATASIZE == 1)
								if(ts_bit(rcsta2, RX9D));
									retVal = retVal | 0x100;
							#endif

						#endif

						#ifdef RS232_328566_ECHO
							FCD_RS2320_SendRS232Char(retVal);
						#endif
					}
				}
			#else
				#if( RS232_328566_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_328566_RX_PORT, RS232_328566_SW_RX))
							retVal = retVal | 0x100;
					}
					else
					{
						if (test_bit( RS232_328566_RX_PORT, RS232_328566_SW_RX))
							retVal = retVal | 0x80;
					}
					FCD_RS2320_RS232_Delay(1);
				}

				#ifdef RS232_328566_ECHO
					FCD_RS2320_SendRS232Char(retVal);
				#endif

			#endif
		}
		#ifdef RS232_328566_HARDWARE
			//not ready to accept data
			set_bit( RS232_328566_RTS_PORT, RS232_328566_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_328566_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_328566_SW_BAUD;

		if (mode)
			delay = delay + 1;

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

//Macro implementations

void FCM_keypad()
{
	
	//Loop
	//Loop: While KeyPressed=10
	while (1)
	{
		//Connection Point
		//Connection Point: [A]: A
FCC_keypad_A:
;


		//See which key has been pressed
		//Call Component Macro: KeyPressed=KeyPad(0)::GetKeypadNumber
		FCV_KEYPRESSED = FCD_KeyPad0_GetKeypadNumber();


		//Key #
		//Decision: KeyPressed =11?
		if (FCV_KEYPRESSED ==11)
		{
			//Clear screen
			//Call Component Macro: LCDDisplay(0)::Clear
			FCD_LCDDisplay0_Clear();


			//Calculation
			//Calculation:
			//  Count = 0
			//  Value = 0
			FCV_COUNT = 0;
			FCV_VALUE = 0;
			

			//Calculation
			//Calculation:
			//  Number[0] = 0
			//  Number[1] = 0
			//  Number[2] = 0
			//  Number[3] = 0
			FCV_NUMBER[0] = 0;
			FCV_NUMBER[1] = 0;
			FCV_NUMBER[2] = 0;
			FCV_NUMBER[3] = 0;
			

		}


		//Any key at all
		//Decision: KeyPressed <10?
		if (FCV_KEYPRESSED <10)
		{
			//Calculation
			//Calculation:
			//  Number[Count] = KeyPressed
			FCV_NUMBER[FCV_COUNT] = FCV_KEYPRESSED;
			

			//Show key (in ascii)
			//Call Component Macro: LCDDisplay(0)::PrintNumber(KeyPressed)
			FCD_LCDDisplay0_PrintNumber(FCV_KEYPRESSED);


			//Decision
			//Decision: Count=0?
			if (FCV_COUNT==0)
			{
				//Calculation
				//Calculation:
				//  Value = Number[0]
				FCV_VALUE = FCV_NUMBER[0];
				

			} else {
				//Calculation
				//Calculation:
				//  Value = (Number[0]*1000)+(Number[1]*100)+(Number[2]*10)+(Number[3]*1)
				FCV_VALUE = (FCV_NUMBER[0]*1000)+(FCV_NUMBER[1]*100)+(FCV_NUMBER[2]*10)+(FCV_NUMBER[3]*1);
				

			}


			//Calculation
			//Calculation:
			//  Count = Count+1
			FCV_COUNT = FCV_COUNT+1;
			

			//Wait for key to be released
			//Loop: While KeyPressed <>255
			while (FCV_KEYPRESSED !=255)
			{
				//
				//Call Component Macro: KeyPressed=KeyPad(0)::GetKeypadNumber
				FCV_KEYPRESSED = FCD_KeyPad0_GetKeypadNumber();


			}


		}


		//Decision
		//Decision: KeyPressed=10?
		if (FCV_KEYPRESSED==10)
		{
		} else {
			//Goto Connection Point
			//Goto Connection Point: [A]: A
			goto FCC_keypad_A;


		}


		if ((FCV_KEYPRESSED==10) != 0) break;
	}


	//Calculation
	//Calculation:
	//  count2 = count2+1
	//  KeyPressed = 0
	FCV_COUNT2 = FCV_COUNT2+1;
	FCV_KEYPRESSED = 0;
	

}

void main()
{
	
	//Initialisation
	adcon1 = 0x07;

	
	#if (RS232_328566_UART == 0)
		set_bit(RS232_328566_RX_TRIS, RS232_328566_SW_RX);		// Receive pin is a input
		clear_bit(RS232_328566_TX_TRIS, RS232_328566_SW_TX);	// Transmit pin is a output
		set_bit(RS232_328566_TX_PORT, RS232_328566_SW_TX);		// Transmit pin is default high
	#endif

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

		if(RS232_328566_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_328566_UART == 2)

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

		if(RS232_328566_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_328566_HARDWARE
		set_bit( RS232_328566_CTS_TRIS, RS232_328566_CTS_PIN);	//CTS is an input
		clear_bit( RS232_328566_RTS_TRIS, RS232_328566_RTS_PIN);	//RTS is an output
		set_bit( RS232_328566_RTS_PORT, RS232_328566_RTS_PIN); 	//not ready to accept data
	#endif


	//Interrupt initialisation code
	option_reg = 0xC0;


	//
	//Call Component Macro: LCDDisplay(0)::Start
	FCD_LCDDisplay0_Start();


	//Call Component Macro
	//Call Component Macro: LCDDisplay(0)::PrintString("Welcome Line D01 ")
	FCD_LCDDisplay0_PrintString("Welcome Line D01 ",17);


	//Calculation
	//Calculation:
	//  Value = 0
	//  Number[0] = 0
	//  Number[1] = 0
	//  Number[2] = 0
	//  Number[3] = 0
	//  count2 = 0
	//  KeyPressed = 0
	//  Count = 0
	FCV_VALUE = 0;
	FCV_NUMBER[0] = 0;
	FCV_NUMBER[1] = 0;
	FCV_NUMBER[2] = 0;
	FCV_NUMBER[3] = 0;
	FCV_COUNT2 = 0;
	FCV_KEYPRESSED = 0;
	FCV_COUNT = 0;
	

	//Loop
	//Loop: While 1
	while (1)
	{
		//Connection Point
		//Connection Point: [A]: b
FCC_Main_A:
;


		//Call Component Macro
		//Call Component Macro: RS232(0)::SendRS232String(str1)
		FCD_RS2320_SendRS232String(FCV_STR1,FCSZ_STR1);


		//Calculation
		//Calculation:
		//  Value = 0
		//  Number[0] = 0
		//  Number[1] = 0
		//  Number[2] = 0
		//  Number[3] = 0
		//  KeyPressed = 0
		//  Count = 0
		FCV_VALUE = 0;
		FCV_NUMBER[0] = 0;
		FCV_NUMBER[1] = 0;
		FCV_NUMBER[2] = 0;
		FCV_NUMBER[3] = 0;
		FCV_KEYPRESSED = 0;
		FCV_COUNT = 0;
		

		//Calculation
		//Calculation:
		//  KeyPressed = 0
		//  Value = 0
		//  Count = 0
		//  Number[0] = 0
		//  Number[1] = 0
		//  Number[2] = 0
		//  Number[3] = 0
		//  sw = 0
		FCV_KEYPRESSED = 0;
		FCV_VALUE = 0;
		FCV_COUNT = 0;
		FCV_NUMBER[0] = 0;
		FCV_NUMBER[1] = 0;
		FCV_NUMBER[2] = 0;
		FCV_NUMBER[3] = 0;
		FCV_SW = 0;
		

		//Call Macro
		//Call Macro: keypad
		FCM_keypad();


		//Switch
		//Switch: count2?
		switch (FCV_COUNT2)
		{
			case 1:
			{
				//device id
				//String Manipulation:
				//  string = "D01BD"
				FCI_CONCATENATE("D01BD",5,"",0,FCV_STRING,FCSZ_STRING);
				

				//String Manipulation
				//String Manipulation:
				//  str1 = ToString$(Value)
				//  str1 = string + str1
				FCI_TOSTRING(FCV_VALUE,FCV_STR1,FCSZ_STR1);
				FCI_CONCATENATE(FCV_STRING,FCSZ_STRING,FCV_STR1,FCSZ_STR1,FCV_STR1,FCSZ_STR1);
				

				//Delay
				//Delay: 1 ms
				delay_ms(1);


				//Call Component Macro
				//Call Component Macro: LCDDisplay(0)::Clear
				FCD_LCDDisplay0_Clear();


				//Call Component Macro
				//Call Component Macro: LCDDisplay(0)::PrintString("Data sent...................")
				FCD_LCDDisplay0_PrintString("Data sent...................",28);


				//Delay
				//Delay: 700 ms
				delay_ms(255);
				delay_ms(255);
				delay_ms(190);


				//Call Component Macro
				//Call Component Macro: LCDDisplay(0)::Clear
				FCD_LCDDisplay0_Clear();


				//Delay
				//Delay: 20 ms
				delay_ms(20);


				//Call Component Macro
				//Call Component Macro: LCDDisplay(0)::PrintString("please wait for ")
				FCD_LCDDisplay0_PrintString("please wait for ",16);


				//Delay
				//Delay: 1 ms
				delay_ms(1);


				//Call Component Macro
				//Call Component Macro: LCDDisplay(0)::Cursor(1, 1)
				FCD_LCDDisplay0_Cursor(1, 1);


				//Delay
				//Delay: 1 ms
				delay_ms(1);


				//Call Component Macro
				//Call Component Macro: LCDDisplay(0)::PrintString("   Assistence")
				FCD_LCDDisplay0_PrintString("   Assistence",13);


				//Delay
				//Delay: 20 ms
				delay_ms(20);


				break;
			}

			case 2:
			{
				//device id
				//String Manipulation:
				//  string = "D01ME"
				FCI_CONCATENATE("D01ME",5,"",0,FCV_STRING,FCSZ_STRING);
				

				//String Manipulation
				//String Manipulation:
				//  str1 = ToString$(Value)
				//  str1 = string + str1
				FCI_TOSTRING(FCV_VALUE,FCV_STR1,FCSZ_STR1);
				FCI_CONCATENATE(FCV_STRING,FCSZ_STRING,FCV_STR1,FCSZ_STR1,FCV_STR1,FCSZ_STR1);
				

				//Call Component Macro
				//Call Component Macro: LCDDisplay(0)::Clear
				FCD_LCDDisplay0_Clear();


				//Delay
				//Delay: 20 ms
				delay_ms(20);


				//Call Component Macro
				//Call Component Macro: LCDDisplay(0)::PrintString("Data sent...................")
				FCD_LCDDisplay0_PrintString("Data sent...................",28);


				//Delay
				//Delay: 700 ms
				delay_ms(255);
				delay_ms(255);
				delay_ms(190);


				//Call Component Macro
				//Call Component Macro: LCDDisplay(0)::Clear
				FCD_LCDDisplay0_Clear();


				//Call Component Macro
				//Call Component Macro: LCDDisplay(0)::PrintString("Enter reason")
				FCD_LCDDisplay0_PrintString("Enter reason",12);


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


				break;
			}

			case 3:
			{
				//device id
				//String Manipulation:
				//  string = "D01RE"
				FCI_CONCATENATE("D01RE",5,"",0,FCV_STRING,FCSZ_STRING);
				

				//String Manipulation
				//String Manipulation:
				//  str1 = ToString$(Value)
				//  str1 = string + str1
				FCI_TOSTRING(FCV_VALUE,FCV_STR1,FCSZ_STR1);
				FCI_CONCATENATE(FCV_STRING,FCSZ_STRING,FCV_STR1,FCSZ_STR1,FCV_STR1,FCSZ_STR1);
				

				break;
			}

			case 4:
			{
				//Call Component Macro
				//Call Component Macro: LCDDisplay(0)::Clear
				FCD_LCDDisplay0_Clear();


				//Delay
				//Delay: 20 ms
				delay_ms(20);


				//Call Component Macro
				//Call Component Macro: LCDDisplay(0)::PrintString("Welcome Line D01 ")
				FCD_LCDDisplay0_PrintString("Welcome Line D01 ",17);


				break;
			}

		}


		//Decision
		//Decision: count2=3?
		if (FCV_COUNT2==3)
		{
			//Call Component Macro
			//Call Component Macro: LCDDisplay(0)::Clear
			FCD_LCDDisplay0_Clear();


			//reset count2
			//Calculation:
			//  count2 = 0
			FCV_COUNT2 = 0;
			

		} else {
			//Output
			//Output: 1 -> A0
			trisa = trisa & 0xfe;
			if (1)
				porta = (porta & 0xfe) | 0x01;
			else
				porta = porta & 0xfe;


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


			//Output
			//Output: 0 -> A0
			trisa = trisa & 0xfe;
			if (0)
				porta = (porta & 0xfe) | 0x01;
			else
				porta = porta & 0xfe;


			//Goto Connection Point
			//Goto Connection Point: [A]: b
			goto FCC_Main_A;


		}


		//Call Component Macro
		//Call Component Macro: LCDDisplay(0)::PrintString("End")
		FCD_LCDDisplay0_PrintString("End",3);


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


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


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


		//Calculation
		//Calculation:
		//  Value = 0
		FCV_VALUE = 0;
		

		//Call Component Macro
		//Call Component Macro: LCDDisplay(0)::Clear
		FCD_LCDDisplay0_Clear();


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


	//Goto Connection Point
	//Goto Connection Point: [A]: b
	goto FCC_Main_A;


	mainendloop: goto mainendloop;
}

void MX_INTERRUPT_MACRO(void)
{
}



