//************************************************************************************
//**  
//**  File name:     Z:\El-Service\Marcel\Roy\COPD-project\Circular-buffer.c
//**  Generated by:  Flowcode v4.5.18.74
//**  Date:          Thursday, August 30, 2012 14:41:01
//**  Licence:       Professional
//**  Registered to: Marcel van Leth
//**  
//**  
//**  http://www.matrixmultimedia.com
//************************************************************************************


#define MX_PIC

//Defines voor de microcontroller
#define P18F6722
#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_UART2
#define MX_UART2_G
#define MX_UART2_TX 1
#define MX_UART2_RX 2
#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
#define MX_PWM_TRIS2a trise
#define MX_PWM_2a 7
#define MX_PWM_TRIS3 trisg
#define MX_PWM_3 0
#define MX_PWM_TRIS4 trisg
#define MX_PWM_4 3
#define MX_PWM_TRIS5 trisg
#define MX_PWM_5 4

//Functies
#define MX_CLK_SPEED 20000000
#ifdef _BOOSTC
#include <system.h>
#endif
#ifdef HI_TECH_C
#include <pic18.h>
#endif

//Configuratiegegevens
#ifdef _BOOSTC
#pragma DATA 0x300000, 0xff
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300001, 0xc2
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300002, 0x1f
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300003, 0x1e
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300004, 0x3
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300005, 0x85
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300006, 0xa1
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300007, 0xff
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300008, 0xff
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x300009, 0xc0
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000a, 0xff
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000b, 0xe0
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000c, 0xff
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif
#ifdef _BOOSTC
#pragma DATA 0x30000d, 0x40
#endif
#ifdef _HI_TECH_C
%C__CONFIG(%A, %V);
#endif

//Interne functies
#include "C:\Program Files (x86)\Matrix Multimedia\Flowcode V4\FCD\internals.h"

//Macro functie declaraties
void FCM_RB1();
void FCM_rx();
void FCM_rx_uart();


//Variable declaraties
#define FCSZ_ADC_STRING 4
char FCV_STOP;
char FCV_ADC_STRING[FCSZ_ADC_STRING];
char FCV_RX_START;
char FCV_TEST;
char FCV_COUNTER;
char FCV_RX_END;
char FCV_IDX;
char FCV_RX_DATA[100];
short FCV_INCOM;
char FCV_BRAKE;
char FCV_DATA[13];
short FCV_ADC;
char FCV_MAX_SIZE;
char FCV_TELLER;
char FCV_START;




//RS232(0): //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_920236 = Unique ID
Void = Bitbanged Receive Port Register
Void = Bitbanged Receive Data Direction Register
Void = Bitbanged Receive Pin
Void = Bitbanged Transmit Pin
104 = 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)
Void = Bitbanged Transmit Port Register
Void = Bitbanged Transmit Data Direction Register
******************************/

#define RS232_920236_UART				1

#ifdef MX_MIAC_SYSTEM
	#define RS232_920236_SW_BAUD			0
#else
	#define RS232_920236_RTS_PORT			portc
	#define RS232_920236_RTS_TRIS			trisc
	#define RS232_920236_CTS_PORT			portc
	#define RS232_920236_CTS_TRIS			trisc
	#define RS232_920236_RTS_PIN			0
	#define RS232_920236_CTS_PIN			4

	#define RS232_920236_TOUT  			0
	#define RS232_920236_DATASIZE			0
	#define RS232_920236_PARITY			0
	#define RS232_920236_LEGACY_RV		0

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

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

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

	#if (RS232_920236_UART == 0)
		#define RS232_920236_RX_PORT		Void
		#define RS232_920236_RX_TRIS		Void
		#define RS232_920236_SW_RX		Void
		#define RS232_920236_SW_TX		Void
		#define RS232_920236_TX_PORT		Void
		#define RS232_920236_TX_TRIS		Void

		#if MX_CLK_SPEED < 20000000						//Clock Less then 20MHz?
		  #if MX_CLK_SPEED < 10000000					//Clock Less then 10MHz?
		    #if MX_CLK_SPEED < 6000000					//Clock Less then 6MHz?
		      #define RS232_920236_SW_BAUD   104 - 54				//19200 Baud Not Supported at under 6MHz
		    #else
		      #define RS232_920236_SW_BAUD   104 - 26				//Under 10MHz
		    #endif
		  #else
		    #define RS232_920236_SW_BAUD      104 - 9				//Under 20MHz
		  #endif
		#else
		  #define RS232_920236_SW_BAUD         104 - 4				//20MHz or more
		#endif

	#else
		#define RS232_920236_TXSTA_VAL  	4
		#define RS232_920236_SPBRG_VAL  	129
		#define RS232_920236_SW_BAUD		0
	#endif

	#if (RS232_920236_LEGACY_RV == 0)
		#if (RS232_920236_DATASIZE == 1)
			#error "Chip does not have second UART capability"
		#endif
	#endif

	#define RS232_920236_STATUS_LOOP		0
	#define RS232_920236_STATUS_TIMEOUT	1
	#define RS232_920236_STATUS_RXBYTE	2
#endif




//RS232(0): //Macro functie declaraties

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


//RS232(1): //Defines:

/**** Macro Substitutions ****
portc = RTS Port
trisc = RTS Data Direction
portc = CTS Port
trisc = CTS Data Direction
0 = RTS Pin
4 = CTS Pin
2 = 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_656538 = Unique ID
Void = Bitbanged Receive Port Register
Void = Bitbanged Receive Data Direction Register
Void = Bitbanged Receive Pin
Void = Bitbanged Transmit Pin
104 = 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)
Void = Bitbanged Transmit Port Register
Void = Bitbanged Transmit Data Direction Register
******************************/

#define RS232_656538_UART				2

#ifdef MX_MIAC_SYSTEM
	#define RS232_656538_SW_BAUD			0
#else
	#define RS232_656538_RTS_PORT			portc
	#define RS232_656538_RTS_TRIS			trisc
	#define RS232_656538_CTS_PORT			portc
	#define RS232_656538_CTS_TRIS			trisc
	#define RS232_656538_RTS_PIN			0
	#define RS232_656538_CTS_PIN			4

	#define RS232_656538_TOUT  			0
	#define RS232_656538_DATASIZE			0
	#define RS232_656538_PARITY			0
	#define RS232_656538_LEGACY_RV		0

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

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

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

	#if (RS232_656538_UART == 0)
		#define RS232_656538_RX_PORT		Void
		#define RS232_656538_RX_TRIS		Void
		#define RS232_656538_SW_RX		Void
		#define RS232_656538_SW_TX		Void
		#define RS232_656538_TX_PORT		Void
		#define RS232_656538_TX_TRIS		Void

		#if MX_CLK_SPEED < 20000000						//Clock Less then 20MHz?
		  #if MX_CLK_SPEED < 10000000					//Clock Less then 10MHz?
		    #if MX_CLK_SPEED < 6000000					//Clock Less then 6MHz?
		      #define RS232_656538_SW_BAUD   104 - 54				//19200 Baud Not Supported at under 6MHz
		    #else
		      #define RS232_656538_SW_BAUD   104 - 26				//Under 10MHz
		    #endif
		  #else
		    #define RS232_656538_SW_BAUD      104 - 9				//Under 20MHz
		  #endif
		#else
		  #define RS232_656538_SW_BAUD         104 - 4				//20MHz or more
		#endif

	#else
		#define RS232_656538_TXSTA_VAL  	4
		#define RS232_656538_SPBRG_VAL  	129
		#define RS232_656538_SW_BAUD		0
	#endif

	#if (RS232_656538_LEGACY_RV == 0)
		#if (RS232_656538_DATASIZE == 1)
			#error "Chip does not have second UART capability"
		#endif
	#endif

	#define RS232_656538_STATUS_LOOP		0
	#define RS232_656538_STATUS_TIMEOUT	1
	#define RS232_656538_STATUS_RXBYTE	2
#endif




//RS232(1): //Macro functie declaraties

void FCD_RS2321_SendRS232Char(short nChar);
void FCD_RS2321_SendRS232String(char* String, char MSZ_String);
short FCD_RS2321_ReceiveRS232Char(short nTimeout);
void FCD_RS2321_ReceiveRS232String(char* FCR_RETVAL, char FCR_RETVAL_SIZE, char nTimeout, char NumBytes);
void FCD_RS2321_RS232_Delay();



//RS232(0): //Macro implementaties


void FCD_RS2320_SendRS232Char(short nChar)
{
	
	#ifdef MX_MIAC_SYSTEM

		unsigned int ModID = ((trisc << 5) + portc) << 9;

		MIAC_CAN_TX[0] = nChar;

	  #if (RS232_920236_UART == 1)
		MIAC_Module_WR(ModID, 417, 1,1,0);
		MIAC_Module_GetAck(ModID + 32, 417, 20);
	  #endif

	  #if (RS232_920236_UART == 2)
		MIAC_Module_WR(ModID, 449, 1,1,0);
		MIAC_Module_GetAck(ModID + 32, 449, 20);
	  #endif

	#else

		#if (RS232_920236_UART == 0)

			char dMask;
			char idx;
			char count = 8;

			#ifdef RS232_920236_HARDWARE
				//wait until CTS is low
				while (( RS232_920236_CTS_PORT & (1 << RS232_920236_CTS_PIN) ) != 0);
			#endif

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

			clear_bit( RS232_920236_TX_PORT, RS232_920236_SW_TX);			// Send Start bit
		FCD_RS2320_RS232_Delay();

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

				if (dMask)
					set_bit( RS232_920236_TX_PORT, RS232_920236_SW_TX);
				else
					clear_bit( RS232_920236_TX_PORT, RS232_920236_SW_TX);

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

			set_bit( RS232_920236_TX_PORT, RS232_920236_SW_TX);				// Send Stop bit
		FCD_RS2320_RS232_Delay();

		#endif

		#if (RS232_920236_UART == 1)

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

			while (ts_bit(pir1, TXIF) == 0);

			#ifdef RS232_920236_HARDWARE
				//wait until CTS is low
				while (( RS232_920236_CTS_PORT & (1 << RS232_920236_CTS_PIN) ) != 0);
			#endif

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

			txreg = nChar;

		#endif

		#if (RS232_920236_UART == 2)

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

			while (ts_bit(pir3, TX2IF) == 0);

			#ifdef RS232_920236_HARDWARE
				//wait until CTS is low
				while (( RS232_920236_CTS_PORT & (1 << RS232_920236_CTS_PIN) ) != 0);
			#endif

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

			txreg2 = nChar;

		#endif
	#endif

}

void FCD_RS2320_SendRS232String(char* String, char MSZ_String)
{
		char idx;

	#ifdef MX_MIAC_SYSTEM

		unsigned int ModID = ((trisc << 5) + portc) << 9;

		char msg_count, ch;
		char cmd_start = 1;
		char more_data = 1;

		idx = 0;

		// Split string into 6-byte packets
		do
		{
			msg_count = 0;
			do
			{
				ch = String[idx];
				if (ch)
				{
					MIAC_CAN_TX[msg_count] = ch;
					idx = idx + 1;
					msg_count = msg_count + 1;
				}
			}
			while (ch && (msg_count < 6) && (idx < MSZ_String));			// check for 2 x End Of String conditions and packet full

			if (idx == MSZ_String)											// end of string
			{
				more_data = 0;
			}else{
				if (String[idx] == 0) more_data = 0;						// next ch = null
			}

		  #if (RS232_920236_UART == 1)
			MIAC_Module_WR(ModID, 418, msg_count,cmd_start,more_data);		// TX data
			MIAC_Module_GetAck(ModID + 32, 418, 20);						// Check for ACK
		  #endif

		  #if (RS232_920236_UART == 2)
			MIAC_Module_WR(ModID, 450, msg_count,cmd_start,more_data);		// TX data
			MIAC_Module_GetAck(ModID + 32, 450, 20);						// Check for ACK
		  #endif

			cmd_start = 0;
		}
		while (more_data);

	#else

		for(idx = 0; idx < MSZ_String; idx++)
		{
			if (String[idx] == 0)
				break;
			else FCD_RS2320_SendRS232Char(String[idx]);
		}

	#endif

}

short FCD_RS2320_ReceiveRS232Char(short nTimeout)
{
	
		short retVal = 512;

	#ifdef MX_MIAC_SYSTEM

		unsigned int ModID = ((trisc << 5) + portc) << 9;

		MIAC_CAN_TX[0] = nTimeout;

	  #if (RS232_920236_UART == 1)
		MIAC_Module_WR(ModID, 419, 1,1,0);
	  #endif

	  #if (RS232_920236_UART == 2)
		MIAC_Module_WR(ModID, 451, 1,1,0);
	  #endif

		if (nTimeout = 255)
		{
		  #if (RS232_920236_UART == 1)
			retVal = MIAC_Module_GetAck(ModID + 32, 419, 255);
		  #endif

		  #if (RS232_920236_UART == 2)
			retVal = MIAC_Module_GetAck(ModID + 32, 451, 255);
		  #endif
		}else{
			do
			{
			  #if (RS232_920236_UART == 1)
				retVal = MIAC_Module_GetAck(ModID + 32, 419, 10);
			  #endif

			  #if (RS232_920236_UART == 2)
				retVal = MIAC_Module_GetAck(ModID + 32, 451, 10);
			  #endif

				nTimeout = nTimeout - 1;
			}
			while((retVal & 0xe0) && (nTimeout > 0));
		}

		if (retVal & 0xe0)
			retVal = 0x2f;
		else
			retVal = (MIAC_CAN_RX[0] << 8) | MIAC_CAN_RX[1];

	#else

		char delay1 = 0;
		char regcheck = 0;
		char dummy = 0;

		char bWaitForever = 0;
		char rxStatus = RS232_920236_STATUS_LOOP;
		char idx;
		char count = 8;

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

		#if ( RS232_920236_UART == 1)
			#ifndef MX_UART
				#error "Chip does not have UART1 capability"
			#endif
		#endif

		#if ( RS232_920236_UART == 2)
			#ifndef MX_UART2
				#error "Chip does not have UART2 capability"
			#endif
		#endif

		#ifdef RS232_920236_HARDWARE
			//ready to accept data
			clear_bit( RS232_920236_RTS_PORT, RS232_920236_RTS_PIN);
		#endif

		if (nTimeout == 255)
			bWaitForever = 1;

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

			#if ( RS232_920236_UART == 0)
				regcheck = test_bit(RS232_920236_RX_PORT, RS232_920236_SW_RX);	//Test for start bit
				if (regcheck == 0)
					rxStatus = RS232_920236_STATUS_RXBYTE;
			#endif

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

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

		if (rxStatus == RS232_920236_STATUS_RXBYTE)
		{
			#if ( RS232_920236_UART > 0)

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

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

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

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

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

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

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

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

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

					}
					else
					{
						retVal = 0;

						#if ( RS232_920236_UART == 1)

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

						#endif

						#if ( RS232_920236_UART == 2)

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

						#endif

						#ifdef RS232_920236_ECHO
							FCD_RS2320_SendRS232Char(retVal);
						#endif
					}
				}
			#else
				#if( RS232_920236_DATASIZE == 1)
					count = 9;
				#endif

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

					if(count == 9)
					{
						if (test_bit( RS232_920236_RX_PORT, RS232_920236_SW_RX))
							retVal = retVal | 0x100;
					}
					else
					{
						if (test_bit( RS232_920236_RX_PORT, RS232_920236_SW_RX))
							retVal = retVal | 0x80;
					}
					FCD_RS2320_RS232_Delay();
				}

				#ifdef RS232_920236_ECHO
					FCD_RS2320_SendRS232Char(retVal);
				#endif

			#endif
		}
		#ifdef RS232_920236_HARDWARE
			//not ready to accept data
			set_bit( RS232_920236_RTS_PORT, RS232_920236_RTS_PIN);
		#endif
	#endif

		return (retVal);
}

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

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

		if (NumBytes > FCR_RETVAL_SIZE)
			NumBytes = FCR_RETVAL_SIZE;

	#ifdef MX_MIAC

		unsigned int ModID = ((trisc << 5) + portc) << 9;

		char msg_count, data_count, ch;
		char cmd_start = 1;
		char tx_count = 2;
		char more_data;
		char ack_val;

		// Split string into 6-byte packets

		msg_count = 0;

		MIAC_CAN_TX[0] = nTimeout;
		MIAC_CAN_TX[1] = NumBytes;

		do
		{
		  #if (RS232_920236_UART == 1)
			MIAC_Module_WR(ModID, 420, tx_count, cmd_start, 0);		// TX data
		  #endif

		  #if (RS232_920236_UART == 2)
			MIAC_Module_WR(ModID, 452, tx_count, cmd_start, 0);		// TX data
		  #endif


			do
			{
			  #if (RS232_920236_UART == 1)
				ack_val = MIAC_Module_GetAck(ModID + 32, 420, 10);
			  #endif

			  #if (RS232_920236_UART == 2)
				ack_val = MIAC_Module_GetAck(ModID + 32, 452, 10);
			  #endif

				nTimeout = nTimeout - 1;
			}
			while((ack_val & 0xe0) && (nTimeout > 0));

			tx_count = 0;
			cmd_start = 0;

			if(ack_val & 0xe0)
			{
				more_data = 0;
			}else{
				data_count = ack_val & 0x07;
				more_data = ack_val & 0x10;
				if(data_count)
				{
					for(idx = 0; idx < data_count; idx++)
					{
						if(msg_count < NumBytes)
						{
							ch = MIAC_CAN_RX[idx];
							FCR_RETVAL[msg_count] = ch;
							msg_count = msg_count + 1;
						}else{
							break;
						}
					}
				}
			}
		}
		while(more_data && (msg_count < NumBytes));

		if(msg_count < FCR_RETVAL_SIZE)
			FCR_RETVAL[msg_count] = 0;
	#else

		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;

	#endif

		#undef RS232_TO
}

void FCD_RS2320_RS232_Delay()
{
	
		unsigned short delay = RS232_920236_SW_BAUD;
		while (delay > 255)
		{
			delay_us(255);
			delay = delay - 255;
			MX_CLEAR_WATCHDOG;
		}
		delay_us(delay);
		MX_CLEAR_WATCHDOG;

}



//RS232(1): //Macro implementaties


void FCD_RS2321_SendRS232Char(short nChar)
{
	
	#ifdef MX_MIAC_SYSTEM

		unsigned int ModID = ((trisc << 5) + portc) << 9;

		MIAC_CAN_TX[0] = nChar;

	  #if (RS232_656538_UART == 1)
		MIAC_Module_WR(ModID, 417, 1,1,0);
		MIAC_Module_GetAck(ModID + 32, 417, 20);
	  #endif

	  #if (RS232_656538_UART == 2)
		MIAC_Module_WR(ModID, 449, 1,1,0);
		MIAC_Module_GetAck(ModID + 32, 449, 20);
	  #endif

	#else

		#if (RS232_656538_UART == 0)

			char dMask;
			char idx;
			char count = 8;

			#ifdef RS232_656538_HARDWARE
				//wait until CTS is low
				while (( RS232_656538_CTS_PORT & (1 << RS232_656538_CTS_PIN) ) != 0);
			#endif

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

			clear_bit( RS232_656538_TX_PORT, RS232_656538_SW_TX);			// Send Start bit
		FCD_RS2321_RS232_Delay();

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

				if (dMask)
					set_bit( RS232_656538_TX_PORT, RS232_656538_SW_TX);
				else
					clear_bit( RS232_656538_TX_PORT, RS232_656538_SW_TX);

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

			set_bit( RS232_656538_TX_PORT, RS232_656538_SW_TX);				// Send Stop bit
		FCD_RS2321_RS232_Delay();

		#endif

		#if (RS232_656538_UART == 1)

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

			while (ts_bit(pir1, TXIF) == 0);

			#ifdef RS232_656538_HARDWARE
				//wait until CTS is low
				while (( RS232_656538_CTS_PORT & (1 << RS232_656538_CTS_PIN) ) != 0);
			#endif

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

			txreg = nChar;

		#endif

		#if (RS232_656538_UART == 2)

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

			while (ts_bit(pir3, TX2IF) == 0);

			#ifdef RS232_656538_HARDWARE
				//wait until CTS is low
				while (( RS232_656538_CTS_PORT & (1 << RS232_656538_CTS_PIN) ) != 0);
			#endif

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

			txreg2 = nChar;

		#endif
	#endif

}

void FCD_RS2321_SendRS232String(char* String, char MSZ_String)
{
		char idx;

	#ifdef MX_MIAC_SYSTEM

		unsigned int ModID = ((trisc << 5) + portc) << 9;

		char msg_count, ch;
		char cmd_start = 1;
		char more_data = 1;

		idx = 0;

		// Split string into 6-byte packets
		do
		{
			msg_count = 0;
			do
			{
				ch = String[idx];
				if (ch)
				{
					MIAC_CAN_TX[msg_count] = ch;
					idx = idx + 1;
					msg_count = msg_count + 1;
				}
			}
			while (ch && (msg_count < 6) && (idx < MSZ_String));			// check for 2 x End Of String conditions and packet full

			if (idx == MSZ_String)											// end of string
			{
				more_data = 0;
			}else{
				if (String[idx] == 0) more_data = 0;						// next ch = null
			}

		  #if (RS232_656538_UART == 1)
			MIAC_Module_WR(ModID, 418, msg_count,cmd_start,more_data);		// TX data
			MIAC_Module_GetAck(ModID + 32, 418, 20);						// Check for ACK
		  #endif

		  #if (RS232_656538_UART == 2)
			MIAC_Module_WR(ModID, 450, msg_count,cmd_start,more_data);		// TX data
			MIAC_Module_GetAck(ModID + 32, 450, 20);						// Check for ACK
		  #endif

			cmd_start = 0;
		}
		while (more_data);

	#else

		for(idx = 0; idx < MSZ_String; idx++)
		{
			if (String[idx] == 0)
				break;
			else FCD_RS2321_SendRS232Char(String[idx]);
		}

	#endif

}

short FCD_RS2321_ReceiveRS232Char(short nTimeout)
{
	
		short retVal = 512;

	#ifdef MX_MIAC_SYSTEM

		unsigned int ModID = ((trisc << 5) + portc) << 9;

		MIAC_CAN_TX[0] = nTimeout;

	  #if (RS232_656538_UART == 1)
		MIAC_Module_WR(ModID, 419, 1,1,0);
	  #endif

	  #if (RS232_656538_UART == 2)
		MIAC_Module_WR(ModID, 451, 1,1,0);
	  #endif

		if (nTimeout = 255)
		{
		  #if (RS232_656538_UART == 1)
			retVal = MIAC_Module_GetAck(ModID + 32, 419, 255);
		  #endif

		  #if (RS232_656538_UART == 2)
			retVal = MIAC_Module_GetAck(ModID + 32, 451, 255);
		  #endif
		}else{
			do
			{
			  #if (RS232_656538_UART == 1)
				retVal = MIAC_Module_GetAck(ModID + 32, 419, 10);
			  #endif

			  #if (RS232_656538_UART == 2)
				retVal = MIAC_Module_GetAck(ModID + 32, 451, 10);
			  #endif

				nTimeout = nTimeout - 1;
			}
			while((retVal & 0xe0) && (nTimeout > 0));
		}

		if (retVal & 0xe0)
			retVal = 0x2f;
		else
			retVal = (MIAC_CAN_RX[0] << 8) | MIAC_CAN_RX[1];

	#else

		char delay1 = 0;
		char regcheck = 0;
		char dummy = 0;

		char bWaitForever = 0;
		char rxStatus = RS232_656538_STATUS_LOOP;
		char idx;
		char count = 8;

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

		#if ( RS232_656538_UART == 1)
			#ifndef MX_UART
				#error "Chip does not have UART1 capability"
			#endif
		#endif

		#if ( RS232_656538_UART == 2)
			#ifndef MX_UART2
				#error "Chip does not have UART2 capability"
			#endif
		#endif

		#ifdef RS232_656538_HARDWARE
			//ready to accept data
			clear_bit( RS232_656538_RTS_PORT, RS232_656538_RTS_PIN);
		#endif

		if (nTimeout == 255)
			bWaitForever = 1;

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

			#if ( RS232_656538_UART == 0)
				regcheck = test_bit(RS232_656538_RX_PORT, RS232_656538_SW_RX);	//Test for start bit
				if (regcheck == 0)
					rxStatus = RS232_656538_STATUS_RXBYTE;
			#endif

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

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

		if (rxStatus == RS232_656538_STATUS_RXBYTE)
		{
			#if ( RS232_656538_UART > 0)

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

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

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

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

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

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

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

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

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

					}
					else
					{
						retVal = 0;

						#if ( RS232_656538_UART == 1)

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

						#endif

						#if ( RS232_656538_UART == 2)

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

						#endif

						#ifdef RS232_656538_ECHO
							FCD_RS2321_SendRS232Char(retVal);
						#endif
					}
				}
			#else
				#if( RS232_656538_DATASIZE == 1)
					count = 9;
				#endif

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

					if(count == 9)
					{
						if (test_bit( RS232_656538_RX_PORT, RS232_656538_SW_RX))
							retVal = retVal | 0x100;
					}
					else
					{
						if (test_bit( RS232_656538_RX_PORT, RS232_656538_SW_RX))
							retVal = retVal | 0x80;
					}
					FCD_RS2321_RS232_Delay();
				}

				#ifdef RS232_656538_ECHO
					FCD_RS2321_SendRS232Char(retVal);
				#endif

			#endif
		}
		#ifdef RS232_656538_HARDWARE
			//not ready to accept data
			set_bit( RS232_656538_RTS_PORT, RS232_656538_RTS_PIN);
		#endif
	#endif

		return (retVal);
}

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

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

		if (NumBytes > FCR_RETVAL_SIZE)
			NumBytes = FCR_RETVAL_SIZE;

	#ifdef MX_MIAC

		unsigned int ModID = ((trisc << 5) + portc) << 9;

		char msg_count, data_count, ch;
		char cmd_start = 1;
		char tx_count = 2;
		char more_data;
		char ack_val;

		// Split string into 6-byte packets

		msg_count = 0;

		MIAC_CAN_TX[0] = nTimeout;
		MIAC_CAN_TX[1] = NumBytes;

		do
		{
		  #if (RS232_656538_UART == 1)
			MIAC_Module_WR(ModID, 420, tx_count, cmd_start, 0);		// TX data
		  #endif

		  #if (RS232_656538_UART == 2)
			MIAC_Module_WR(ModID, 452, tx_count, cmd_start, 0);		// TX data
		  #endif


			do
			{
			  #if (RS232_656538_UART == 1)
				ack_val = MIAC_Module_GetAck(ModID + 32, 420, 10);
			  #endif

			  #if (RS232_656538_UART == 2)
				ack_val = MIAC_Module_GetAck(ModID + 32, 452, 10);
			  #endif

				nTimeout = nTimeout - 1;
			}
			while((ack_val & 0xe0) && (nTimeout > 0));

			tx_count = 0;
			cmd_start = 0;

			if(ack_val & 0xe0)
			{
				more_data = 0;
			}else{
				data_count = ack_val & 0x07;
				more_data = ack_val & 0x10;
				if(data_count)
				{
					for(idx = 0; idx < data_count; idx++)
					{
						if(msg_count < NumBytes)
						{
							ch = MIAC_CAN_RX[idx];
							FCR_RETVAL[msg_count] = ch;
							msg_count = msg_count + 1;
						}else{
							break;
						}
					}
				}
			}
		}
		while(more_data && (msg_count < NumBytes));

		if(msg_count < FCR_RETVAL_SIZE)
			FCR_RETVAL[msg_count] = 0;
	#else

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

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

	#endif

		#undef RS232_TO
}

void FCD_RS2321_RS232_Delay()
{
	
		unsigned short delay = RS232_656538_SW_BAUD;
		while (delay > 255)
		{
			delay_us(255);
			delay = delay - 255;
			MX_CLEAR_WATCHDOG;
		}
		delay_us(delay);
		MX_CLEAR_WATCHDOG;

}

//Macro implementaties

void FCM_RB1()
{
	
	//Calculation
	//Calculation:
	//  STOP = 1
	//  BRAKE = 1
	FCV_STOP = 1;
	FCV_BRAKE = 1;
	

}

void FCM_rx()
{
	
}

void FCM_rx_uart()
{
	
	//Calculation
	//Calculation:
	//  test = (rx_end + 1) MOD max_size
	FCV_TEST = (FCV_RX_END + 1) % FCV_MAX_SIZE;
	

	//Decision
	//Decision: test != rx_start?
	if (FCV_TEST != FCV_RX_START)
	{
		//Component Macro Aanroepen
		//Component Macro Aanroepen: rx_data[rx_end]=RS232(1)::ReceiveRS232Char(10)
		FCV_RX_DATA[FCV_RX_END] = FCD_RS2321_ReceiveRS232Char(10);


		//Calculation
		//Calculation:
		//  rx_end = test
		FCV_RX_END = FCV_TEST;
		

	}


}

void main()
{
	
	//Initialisatie
	adcon1 = 0x0F;

	
	#ifdef MX_MIAC_SYSTEM

	  #define MOD_ID	((trisc << 5) + portc) << 9

		MIAC_CAN_TX[0] = 4;				//TXSTA
		MIAC_CAN_TX[1] = 129;				//SPBRG
		MIAC_CAN_TX[2] = 0;				//Data size
		MIAC_CAN_TX[3] = 0 + (0 << 1) + (0 << 2) + (0 << 3) + (0 << 4);
		MIAC_CAN_TX[4] = 0;
		MIAC_CAN_TX[5] = 4;

	  #if (RS232_920236_UART == 1)
		MIAC_Module_WR(MOD_ID, 416, 6,1,0);
		MIAC_Module_GetAck(MOD_ID + 32, 416, 20);
	  #endif

	  #if (RS232_920236_UART == 2)
		MIAC_Module_WR(MOD_ID, 448, 6,1,0);
		MIAC_Module_GetAck(MOD_ID + 32, 448, 20);
	  #endif

	  #undef MOD_ID

	#else
		#if (RS232_920236_UART == 0)
			set_bit(RS232_920236_RX_TRIS, RS232_920236_SW_RX);				// Receive pin is a input
			clear_bit(RS232_920236_TX_TRIS, RS232_920236_SW_TX);			// Transmit pin is a output
			set_bit(RS232_920236_TX_PORT, RS232_920236_SW_TX);				// Transmit pin is default high
		#endif

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

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

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

		#if (RS232_920236_UART == 2)
			txsta2 = RS232_920236_TXSTA_VAL;   					// 8-bit, async, low speed, off
			spbrg2 = RS232_920236_SPBRG_VAL;   					// set the baud rate
			rcsta2 = 0;                    				// 8-bit, disabled

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

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

		#ifdef RS232_920236_HARDWARE
			set_bit( RS232_920236_CTS_TRIS, RS232_920236_CTS_PIN);			//CTS is an input
			clear_bit( RS232_920236_RTS_TRIS, RS232_920236_RTS_PIN);		//RTS is an output
			set_bit( RS232_920236_RTS_PORT, RS232_920236_RTS_PIN); 			//not ready to accept data
		#endif
	#endif

	
	#ifdef MX_MIAC_SYSTEM

	  #define MOD_ID	((trisc << 5) + portc) << 9

		MIAC_CAN_TX[0] = 4;				//TXSTA
		MIAC_CAN_TX[1] = 129;				//SPBRG
		MIAC_CAN_TX[2] = 0;				//Data size
		MIAC_CAN_TX[3] = 0 + (0 << 1) + (0 << 2) + (0 << 3) + (0 << 4);
		MIAC_CAN_TX[4] = 0;
		MIAC_CAN_TX[5] = 4;

	  #if (RS232_656538_UART == 1)
		MIAC_Module_WR(MOD_ID, 416, 6,1,0);
		MIAC_Module_GetAck(MOD_ID + 32, 416, 20);
	  #endif

	  #if (RS232_656538_UART == 2)
		MIAC_Module_WR(MOD_ID, 448, 6,1,0);
		MIAC_Module_GetAck(MOD_ID + 32, 448, 20);
	  #endif

	  #undef MOD_ID

	#else
		#if (RS232_656538_UART == 0)
			set_bit(RS232_656538_RX_TRIS, RS232_656538_SW_RX);				// Receive pin is a input
			clear_bit(RS232_656538_TX_TRIS, RS232_656538_SW_TX);			// Transmit pin is a output
			set_bit(RS232_656538_TX_PORT, RS232_656538_SW_TX);				// Transmit pin is default high
		#endif

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

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

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

		#if (RS232_656538_UART == 2)
			txsta2 = RS232_656538_TXSTA_VAL;   					// 8-bit, async, low speed, off
			spbrg2 = RS232_656538_SPBRG_VAL;   					// set the baud rate
			rcsta2 = 0;                    				// 8-bit, disabled

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

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

		#ifdef RS232_656538_HARDWARE
			set_bit( RS232_656538_CTS_TRIS, RS232_656538_CTS_PIN);			//CTS is an input
			clear_bit( RS232_656538_RTS_TRIS, RS232_656538_RTS_PIN);		//RTS is an output
			set_bit( RS232_656538_RTS_PORT, RS232_656538_RTS_PIN); 			//not ready to accept data
		#endif
	#endif


	//Interrupt initialisatie code
	


	//Output
	//Output: 1 -> B0
	trisb = trisb & 0xfe;
	if (1)
		portb = (portb & 0xfe) | 0x01;
	else
		portb = portb & 0xfe;


	//Output
	//Output: 1 -> B2
	trisb = trisb & 0xfb;
	if (1)
		portb = (portb & 0xfb) | 0x04;
	else
		portb = portb & 0xfb;


	//Output
	//Output: 1 -> B3
	trisb = trisb & 0xf7;
	if (1)
		portb = (portb & 0xf7) | 0x08;
	else
		portb = portb & 0xf7;


	//Output
	//Output: 1 -> B4
	trisb = trisb & 0xef;
	if (1)
		portb = (portb & 0xef) | 0x10;
	else
		portb = portb & 0xef;


	//Calculation
	//Calculation:
	//  rx_start = 0
	//  rx_end = 0
	//  max_size = 100
	FCV_RX_START = 0;
	FCV_RX_END = 0;
	FCV_MAX_SIZE = 100;
	

	//Interrupt
	//Interrupt: Enable RXINT2
	st_bit(intcon, GIE);
	st_bit(pie3, RC2IE);


	//Loop
	//Loop: While 1
	while (1)
	{
		//Component Macro Aanroepen
		//Component Macro Aanroepen: start=RS232(0)::ReceiveRS232Char(10)
		FCV_START = FCD_RS2320_ReceiveRS232Char(10);


		//Switch
		//Switch: start?
		switch (FCV_START)
		{
			case 'p':
			{
				//Loop
				//Loop: While 1
				while (1)
				{
					//Loop
					//Loop: While rx_start != rx_end
					while (FCV_RX_START != FCV_RX_END)
					{
						//Component Macro Aanroepen
						//Component Macro Aanroepen: RS232(0)::SendRS232Char(rx_data[rx_start])
						FCD_RS2320_SendRS232Char(FCV_RX_DATA[FCV_RX_START]);


						//Calculation
						//Calculation:
						//  rx_start = (rx_start + 1) MOD max_size
						FCV_RX_START = (FCV_RX_START + 1) % FCV_MAX_SIZE;
						

					}


				}


				break;
			}

		}


	}


	mainendloop: goto mainendloop;
}

void MX_INTERRUPT_MACRO(void)
{
	//Handler code for [RXINT2]
	#ifndef MX_INTHANDLER_pir3_RC2IF
	#define MX_INTHANDLER_pir3_RC2IF
	if (ts_bit(pir3, RC2IF) && ts_bit(pie3, RC2IE))
	{
		#ifdef USE_FLOWCODE_ICD
			extern char ICD_Interrupt_Enable = 1;
		#endif
		FCM_rx_uart();
		cr_bit(pir3, RC2IF);
		#ifdef USE_FLOWCODE_ICD
			extern char ICD_Interrupt_Enable = 0;
		#endif	
	}
	#else
	#warning "This interrupt has previously been enabled, so the macro <rx_uart> may never get called."
	#endif

}



