//************************************************************************************
//**  
//**  File name:     H:\05 - Projects - Complete & Working 8 Bit\Zigbee\Simple RX-TX\End Device.c
//**  Generated by:  Flowcode v4.5.18.74
//**  Date:          Wednesday, December 28, 2011 10:24:33
//**  Licence:       Professional
//**  Registered to: Mr John Crow
//**  
//**  
//**  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 19660800
#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 (x86)\Matrix Multimedia\Flowcode V4\FCD\internals.h"

//Macro function declarations


//Variable declarations
char FCV_RETVAL;
char FCV_CHAR;




//LCDDisplay(0): //Defines:

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

	//component connections
	#define LCD_1509084_PORT0    portb
	#define LCD_1509084_TRIS0    trisb
	#define LCD_1509084_PORT1    portb
	#define LCD_1509084_TRIS1    trisb
	#define LCD_1509084_PORT2    portb
	#define LCD_1509084_TRIS2    trisb
	#define LCD_1509084_PORT3    portb
	#define LCD_1509084_TRIS3    trisb
	#define LCD_1509084_PORT4    portb
	#define LCD_1509084_TRIS4    trisb
	#define LCD_1509084_PORT5    portb
	#define LCD_1509084_TRIS5    trisb
	#define LCD_1509084_BIT0    	0
	#define LCD_1509084_BIT1    	1
	#define LCD_1509084_BIT2    	2
	#define LCD_1509084_BIT3    	3
	#define LCD_1509084_RS      	4
	#define LCD_1509084_E       	5
	#define LCD_1509084_ROWCNT	2
	#define LCD_1509084_COLCNT	16

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

	#define ModIDLCD_1509084LCD_1509084 (((trisb << 5) + portb) << 9)




//LCDDisplay(0): //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);


//Zigbee(0): //Defines:

/**** Macro Substitutions ****
End = Device Name
2 = Function
0234 = Pan ID
1 = Join Notification
FF = Node Join Time
4 = Max Hops
1 = Verbose Mode Enable
8000 = Scan Code
3 = Scan Duration
7D0 = Sleep Node Time
8 = Length Of Device Name
1 = Hardware Flowcontrol Enable
0 = Request To Send Pin
4 = Clear To Send Pin
3 = Sleep Enable Pin
4 = UART TXSTA Initialization Value
127 = UART SPBRG Initialization Value
portc = Zigbee Port Register
******************************/

	//common defines
	#define MX_ZIG_DEVICENAME	"ATNI End"		//Device name
	#define MX_ZIG_MODE			2				//0 = Coordinator / 1 = Router / 2 = End Device
	#define MX_ZIG_PANID		"ATID 0234"		//0xFFFF = Random (All Present networks) / 0x00 - 0x3FFF = Preset
	#define MX_ZIG_JNOT			"ATJN 1"		//Zigbee Join Notification
	#define MX_ZIG_JTIME		"ATNJ FF"		//Time to allow nodes to join to device
	#define MX_ZIG_HOPS			"ATBH 4"		//Zigbee Maximum Hops
	#define MX_ZIG_LCD			1				//LCD Present for displaying messages.
	#define MX_ZIG_SCAN			"ATSC 8000"		//Channels to Scan
	#define MX_ZIG_CHAN			"ATSD 3"		//Channel Scan Duration
	#define MX_ZIG_SLEEP		"ATSP 7D0"		//Time to buffer packets for sleeping nodes x 10ms - range 0x020 - 0xAF0
	#define MX_ZIG_NLEN			8	 			//Device name length + 5 for "ATNI "

	#define MX_ZIG_HWFC			1		//Zigbee Hardware Flow Control

#ifndef MX_MIAC_SYSTEM
	#define MX_ZIG_RTSP			0	 	//Zigbee RTS Pin
	#define MX_ZIG_CTSP			4	 	//Zigbee CTS Pin
	#define MX_ZIG_SLEP			3		//Zigbee Sleep Pin
	#define MX_ZIG_TXSTA    	4	 	//RS232 9600-Baud Constants @ 19660800Hz
	#define MX_ZIG_SPBRG    	127		//RS232 9600-Baud Constants @ 19660800Hz
	#define MX_ZIG_PORT			portc		//Zigbee port for pins
	#define MX_ZIG_TRIS			trisc		//Zigbee port for pins
#endif

	//global variables
	char In_Buff[16];	//Buffer to store incoming AT Responses
	char Num_Str[4];	//Buffer to store outgoing number strings
	char ID_name[64];	//Buffer to store name library or low bytes of address library
	char ID_AddH[64];	//Buffer to store the high bytes of address library
	char LCD_VERB_ZIG = 0;

	//static defines
	#define MX_ZIG_MAX_DEV 			8	//Max number of devices in buffer
	#define MX_ZIG_CARRAGE_RETURN 	13	//Carrage Return constant

#ifdef MX_MIAC
	#define MX_MIAC_ZIGBEE_ID MX_MIAC_ZIGBEEportc_ID
#else
	#ifdef MX_UART_A
		#define MX_ZIG_HAS_UART
	#endif
	#ifdef MX_UART_B
		#define MX_ZIG_HAS_UART
	#endif
	#ifdef MX_UART_C
		#define MX_ZIG_HAS_UART
	#endif

	#ifndef MX_ZIG_HAS_UART
		#error "The target microcontroller does not have a UART"
	#endif
#endif

	#if MX_ZIG_LCD == 1
		#define MX_ZIG_LCDDEL	250		//Time to display AT commands and responces on the LCD.
	#else
		#define MX_ZIG_LCDDEL	0
	#endif





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

void FCD_Zigbee0_delms();
void FCD_Zigbee0_ToHexString(short Number);
void FCD_Zigbee0_Init_Network();
void FCD_Zigbee0_Restart_Network();
char FCD_Zigbee0_Scan_Network(char mode);
char FCD_Zigbee0_Connect_To_Address(char* Address, char MSZ_Address);
char FCD_Zigbee0_Connect_From_Library(char mode, char device);
char FCD_Zigbee0_Connect_To_Coordinator();
char FCD_Zigbee0_Connect_To_All();
char FCD_Zigbee0_Connect_To_Name(char* String, char MSZ_String);
void FCD_Zigbee0_Send_Char(char nChar);
char FCD_Zigbee0_Receive_Char(char nTimeout);
void FCD_Zigbee0_Node_Configure_Sleep(char Mode, short Timeout);
void FCD_Zigbee0_Node_Wake();
char FCD_Zigbee0_Get_Signal_Level();
void FCD_Zigbee0_Enter_Command_Mode();
void FCD_Zigbee0_StoreParams();
void FCD_Zigbee0_Exit_Command_Mode();
void FCD_Zigbee0_Send_AT_Command(char* String, char MSZ_String);
char FCD_Zigbee0_Get_AT_Response(char logging);
char FCD_Zigbee0_Read_AT_Response(char idx);
char FCD_Zigbee0_Read_Address_From_Buffer(char device, char idx);
char FCD_Zigbee0_Read_Name_From_Buffer(char device, char idx);
char FCD_Zigbee0_Scan_Buffer(char* String, char MSZ_String, char length);



//LCDDisplay(0): //Macro implementations


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

		clear_bit(LCD_1509084_PORT0, LCD_1509084_BIT0);
		clear_bit(LCD_1509084_PORT1, LCD_1509084_BIT1);
		clear_bit(LCD_1509084_PORT2, LCD_1509084_BIT2);
		clear_bit(LCD_1509084_PORT3, LCD_1509084_BIT3);
		clear_bit(LCD_1509084_PORT4, LCD_1509084_RS);
		clear_bit(LCD_1509084_PORT5, LCD_1509084_E);
		pt = ((in >> 4) & 0x0f);
		if (pt & 0x01)
		    set_bit(LCD_1509084_PORT0, LCD_1509084_BIT0);
		if (pt & 0x02)
		    set_bit(LCD_1509084_PORT1, LCD_1509084_BIT1);
		if (pt & 0x04)
		    set_bit(LCD_1509084_PORT2, LCD_1509084_BIT2);
		if (pt & 0x08)
		    set_bit(LCD_1509084_PORT3, LCD_1509084_BIT3);
		if (mask)
		    set_bit(LCD_1509084_PORT4, LCD_1509084_RS);
		LCD_1509084_DELAY;
		set_bit (LCD_1509084_PORT5, LCD_1509084_E);
		LCD_1509084_DELAY;
		clear_bit (LCD_1509084_PORT5, LCD_1509084_E);
		pt = (in & 0x0f);
		LCD_1509084_DELAY;
		clear_bit(LCD_1509084_PORT0, LCD_1509084_BIT0);
		clear_bit(LCD_1509084_PORT1, LCD_1509084_BIT1);
		clear_bit(LCD_1509084_PORT2, LCD_1509084_BIT2);
		clear_bit(LCD_1509084_PORT3, LCD_1509084_BIT3);
		clear_bit(LCD_1509084_PORT4, LCD_1509084_RS);
		clear_bit(LCD_1509084_PORT5, LCD_1509084_E);
		if (pt & 0x01)
		    set_bit(LCD_1509084_PORT0, LCD_1509084_BIT0);
		if (pt & 0x02)
		    set_bit(LCD_1509084_PORT1, LCD_1509084_BIT1);
		if (pt & 0x04)
		    set_bit(LCD_1509084_PORT2, LCD_1509084_BIT2);
		if (pt & 0x08)
		    set_bit(LCD_1509084_PORT3, LCD_1509084_BIT3);
		if (mask)
		    set_bit(LCD_1509084_PORT4, LCD_1509084_RS);
		LCD_1509084_DELAY;
		set_bit (LCD_1509084_PORT5, LCD_1509084_E);
		LCD_1509084_DELAY;
		clear_bit (LCD_1509084_PORT5, LCD_1509084_E);
		LCD_1509084_DELAY;

	#endif

}

void FCD_LCDDisplay0_Start()
{
	
	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)

		MIAC_CAN_TX[0] = 0;
		MIAC_CAN_TX[1] = 1;
		MIAC_CAN_TX[2] = 2;
		MIAC_CAN_TX[3] = 3;
		MIAC_CAN_TX[4] = 4;
		MIAC_CAN_TX[5] = 5;

		MIAC_Module_WR(ModIDLCD_1509084, 608, 6,1,1);
		MIAC_Module_GetAck(ModIDLCD_1509084 + 32, 608, 20);

		MIAC_CAN_TX[0] = 2;
		MIAC_CAN_TX[1] = 16;
		MIAC_Module_WR(ModIDLCD_1509084, 608, 2,0,0);
		MIAC_Module_GetAck(ModIDLCD_1509084 + 32, 608, 255);

	  #endif
	#else

		clear_bit(LCD_1509084_TRIS0, LCD_1509084_BIT0);
		clear_bit(LCD_1509084_TRIS1, LCD_1509084_BIT1);
		clear_bit(LCD_1509084_TRIS2, LCD_1509084_BIT2);
		clear_bit(LCD_1509084_TRIS3, LCD_1509084_BIT3);
		clear_bit(LCD_1509084_TRIS4, LCD_1509084_RS);
		clear_bit(LCD_1509084_TRIS5, LCD_1509084_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);
	#endif

}

void FCD_LCDDisplay0_Clear()
{
	
	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)

		MIAC_Module_WR(ModIDLCD_1509084, 609, 0,1,0);
		MIAC_Module_GetAck(ModIDLCD_1509084 + 32, 609, 200);
	  #endif
	#else
		FCD_LCDDisplay0_RawSend(0x01, 0);
		Wdt_Delay_Ms(2);
		FCD_LCDDisplay0_RawSend(0x02, 0);
		Wdt_Delay_Ms(2);
	#endif

}

void FCD_LCDDisplay0_PrintASCII(char Character)
{
	
	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)



		MIAC_CAN_TX[0] = Character;

		MIAC_Module_WR(ModIDLCD_1509084, 610, 1,1,0);
		MIAC_Module_GetAck(ModIDLCD_1509084 + 32, 610, 20);
	  #endif
	#else
		FCD_LCDDisplay0_RawSend(Character, 0x10);
	#endif

}

void FCD_LCDDisplay0_Command(char in)
{
	
	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)

		MIAC_CAN_TX[0] = in;

		MIAC_Module_WR(ModIDLCD_1509084, 611, 1,1,0);
		MIAC_Module_GetAck(ModIDLCD_1509084 + 32, 611, 200);
	  #endif
	#else
		FCD_LCDDisplay0_RawSend(in, 0);
		Wdt_Delay_Ms(2);
	#endif

}

void FCD_LCDDisplay0_Cursor(char x, char y)
{
	
	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)

		MIAC_CAN_TX[0] = x;
		MIAC_CAN_TX[1] = y;

		MIAC_Module_WR(ModIDLCD_1509084, 612, 2,1,0);
		MIAC_Module_GetAck(ModIDLCD_1509084 + 32, 612, 20);
	  #endif

	#else

	  #if (LCD_1509084_ROWCNT == 1)
	    y=0x80;
	  #endif

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

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

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

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

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

}

void FCD_LCDDisplay0_PrintNumber(short Number)
{
	
	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)

		MIAC_CAN_TX[1] = Number;
		MIAC_CAN_TX[0] = Number >> 8;

		MIAC_Module_WR(ModIDLCD_1509084, 613, 4,1,0);
		MIAC_Module_GetAck(ModIDLCD_1509084 + 32, 613, 200);
	  #endif

	#else

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

}

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

	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)

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

		// 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
			}

			MIAC_Module_WR(ModIDLCD_1509084, 614, msg_count,cmd_start,more_data);		// TX data
			MIAC_Module_GetAck(ModIDLCD_1509084 + 32, 614, 200);

			cmd_start = 0;
		}
		while (more_data);
	  #endif
	#else
		for (idx=0; idx<MSZ_String; idx++)
		{
			if (String[idx] == 0)
			{
				break;
			}
			FCD_LCDDisplay0_RawSend(String[idx], 0x10);
		}
	#endif

}

void FCD_LCDDisplay0_ScrollDisplay(char Direction, char Num_Positions)
{
	
	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)

		MIAC_CAN_TX[0] = Direction;
		MIAC_CAN_TX[1] = Num_Positions;

		MIAC_Module_WR(ModIDLCD_1509084, 615, 2,1,0);
		MIAC_Module_GetAck(ModIDLCD_1509084 + 32, 615, 20);
	  #endif
	#else

		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);
		}
	#endif

}

void FCD_LCDDisplay0_ClearLine(char Line)
{
	
	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)

		MIAC_CAN_TX[0] = Line;

		MIAC_Module_WR(ModIDLCD_1509084, 616, 1,1,0);
		MIAC_Module_GetAck(ModIDLCD_1509084 + 32, 616, 200);
	  #endif
	#else
		char count;
		char rowcount;

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

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

		#if (LCD_1509084_ROWCNT == 4)
			#if (LCD_1509084_COLCNT == 16)
				rowcount=16;
			#endif
			#if (LCD_1509084_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);
	#endif

}

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

	#ifdef MX_MIAC_SYSTEM
	  #if (portb != MX_MIAC_MM_MIAC_BASE)

		MIAC_CAN_TX[0] = nIdx;
		MIAC_CAN_TX[1] = d0;
		MIAC_CAN_TX[2] = d1;
		MIAC_CAN_TX[3] = d2;
		MIAC_CAN_TX[4] = d3;
		MIAC_CAN_TX[5] = d4;

		MIAC_Module_WR(ModIDLCD_1509084, 617, 6,1,1);
		MIAC_Module_GetAck(ModIDLCD_1509084 + 32, 617, 20);

		MIAC_CAN_TX[0] = d5;
		MIAC_CAN_TX[1] = d6;
		MIAC_CAN_TX[2] = d7;

		MIAC_Module_WR(ModIDLCD_1509084, 617, 3,0,0);
		MIAC_Module_GetAck(ModIDLCD_1509084 + 32, 617, 200);

	  #endif
	#else

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

}



//Zigbee(0): //Macro implementations


void FCD_Zigbee0_delms()
{
		if (LCD_VERB_ZIG == 1)
		{
			short count = MX_ZIG_LCDDEL;

			while (count > 255)
			{
				Wdt_Delay_Ms(255);
				count = count - 255;
			}
			Wdt_Delay_Ms(count);
		}
}

void FCD_Zigbee0_ToHexString(short Number)
{
		unsigned short tmp_int;
		char tmp_byte;

		Num_Str[0] = ' ';
		Num_Str[1] = ' ';
		Num_Str[2] = ' ';
		Num_Str[3] = ' ';

		tmp_int = Number;
		if (Number >= 4096)
		{
			tmp_byte = tmp_int / 4096;

			if (tmp_byte > 9)
			{
				Num_Str[0] = 'A' + (tmp_byte - 10);
			}
			else
				Num_Str[0] = '0' + tmp_byte;

			while (tmp_byte > 0)
			{
				tmp_int = tmp_int - 4096;
				tmp_byte--;
			}
		}
		if (Number >= 256)
		{
			tmp_byte = tmp_int / 256;

			if (tmp_byte > 9)
			{
				Num_Str[1] = 'A' + (tmp_byte - 10);
			}
			else
				Num_Str[1] = '0' + tmp_byte;

			while (tmp_byte > 0)
			{
				tmp_int = tmp_int - 256;
				tmp_byte--;
			}
		}
		if (Number >= 16)
		{
			tmp_byte = tmp_int / 16;

			if (tmp_byte > 9)
			{
				Num_Str[2] = 'A' + (tmp_byte - 10);
			}
			else
				Num_Str[2] = '0' + tmp_byte;

			while (tmp_byte > 0)
			{
				tmp_int = tmp_int - 16;
				tmp_byte--;
			}
		}

		if (tmp_int > 9)
		{
			Num_Str[3] = 'A' + (tmp_int - 10);
		}
		else
			Num_Str[3] = '0' + tmp_int;
}

void FCD_Zigbee0_Init_Network()
{
	
		//Initiate Low Speed Command Mode
		FCD_Zigbee0_Get_AT_Response(0);					//Clear UART Buffer
		Wdt_Delay_S(1);						//Fixed delay before sending command
		FCD_Zigbee0_Send_Char('+');						//Send characters to enter command mode
		FCD_Zigbee0_Send_Char('+');
		FCD_Zigbee0_Send_Char('+');
		Wdt_Delay_Ms(50);					//Fixed delay before sending command
		FCD_Zigbee0_Get_AT_Response(0);					//Collect Response

		FCD_Zigbee0_Send_AT_Command("ATGT 28",7);		//Set to guard time to 10ms
		FCD_Zigbee0_Get_AT_Response(0);					//Collect Response
		FCD_Zigbee0_delms ();

		//These Lines were added to allow the XBEE module to rescan for a network
		//Comment out these lines to allow for a fast recovery startup.
		FCD_Zigbee0_Send_AT_Command("ATDH 0",6);
		FCD_Zigbee0_Get_AT_Response(0);					//Collect Response
		FCD_Zigbee0_delms ();

		FCD_Zigbee0_Send_AT_Command("ATDL 0",6);
		FCD_Zigbee0_Get_AT_Response(0);					//Collect Response
		FCD_Zigbee0_delms ();
		//End of Added


		#if MX_ZIG_HWFC == 1				//Flow control enabled
			FCD_Zigbee0_Send_AT_Command("ATD6 1",6);	//Enable RTS Pin
			FCD_Zigbee0_Get_AT_Response(0);				//Collect Response
			FCD_Zigbee0_delms ();
			FCD_Zigbee0_Send_AT_Command("ATD7 1",6);	//Enable CTS Pin
			FCD_Zigbee0_Get_AT_Response(0);				//Collect Response
			FCD_Zigbee0_delms ();
		#else								//Flow control disabled
			FCD_Zigbee0_Send_AT_Command("ATD6 0",6);	//Disable RTS Pin
			FCD_Zigbee0_Get_AT_Response(0);				//Collect Response
			FCD_Zigbee0_delms ();
			FCD_Zigbee0_Send_AT_Command("ATD7 0",6);	//Disable CTS Pin
			FCD_Zigbee0_Get_AT_Response(0);				//Collect Response
			FCD_Zigbee0_delms ();
		#endif

		FCD_Zigbee0_Send_AT_Command("ATRO 0",6);		//Set packetisation timeout to 0 bytes - Instant send
		FCD_Zigbee0_Get_AT_Response(0);					//Collect Response
		FCD_Zigbee0_delms ();

		FCD_Zigbee0_Send_AT_Command("ATEE 0",6);		//Disable encryption
		FCD_Zigbee0_Get_AT_Response(0);					//Collect Response
		FCD_Zigbee0_delms ();

		FCD_Zigbee0_Send_AT_Command(MX_ZIG_PANID,9);	//Set PAN ID
		FCD_Zigbee0_Get_AT_Response(0);					//Collect Response
		FCD_Zigbee0_delms ();

		FCD_Zigbee0_Send_AT_Command(MX_ZIG_DEVICENAME, MX_ZIG_NLEN);	//Set Device Name
		FCD_Zigbee0_Get_AT_Response(0);					//Collect Response
		FCD_Zigbee0_delms ();

		FCD_Zigbee0_Send_AT_Command(MX_ZIG_SLEEP,8);	//Set cyclic sleep period
		FCD_Zigbee0_Get_AT_Response(0);					//Collect Response
		FCD_Zigbee0_delms ();

		FCD_Zigbee0_Send_AT_Command(MX_ZIG_CHAN,6);		//Channels to scan
		FCD_Zigbee0_Get_AT_Response(0);					//Collect Response
		FCD_Zigbee0_delms ();

		FCD_Zigbee0_Send_AT_Command(MX_ZIG_SCAN,9);		//Channel Scan Duration
		FCD_Zigbee0_Get_AT_Response(0);					//Collect Response
		FCD_Zigbee0_delms ();

		//Coordinator Mode
		#if MX_ZIG_MODE == 0
		{
			FCD_Zigbee0_Send_AT_Command(MX_ZIG_JTIME,7);//Set device join time
			FCD_Zigbee0_Get_AT_Response(0);				//Collect Response
			FCD_Zigbee0_delms ();

			FCD_Zigbee0_Send_AT_Command(MX_ZIG_HOPS,6);	//Set maximum hops
			FCD_Zigbee0_Get_AT_Response(0);				//Collect Response
			FCD_Zigbee0_delms ();
		}

		//Router Mode or End Device Mode Defines
		#else
		{
			FCD_Zigbee0_Send_AT_Command("ATSM 0",6);	//Set sleep mode to off
			FCD_Zigbee0_Get_AT_Response(0);				//Collect Response
			FCD_Zigbee0_delms ();

			FCD_Zigbee0_Send_AT_Command(MX_ZIG_JNOT,6);	//Set join notification
			FCD_Zigbee0_Get_AT_Response(0);				//Collect Response
			FCD_Zigbee0_delms ();

			//Router Mode Defines
			#if MX_ZIG_MODE == 1
			{
				FCD_Zigbee0_Send_AT_Command(MX_ZIG_JTIME,7);	//Set device join time
				FCD_Zigbee0_Get_AT_Response(0);					//Collect Response
				FCD_Zigbee0_delms ();
			}
			#endif
		}
		#endif

		FCD_Zigbee0_StoreParams();
		FCD_Zigbee0_Exit_Command_Mode();
		FCD_Zigbee0_Restart_Network();
}

void FCD_Zigbee0_Restart_Network()
{
		FCD_Zigbee0_Enter_Command_Mode();			//Initiate Command Mode
		FCD_Zigbee0_Send_AT_Command("ATFR",4);		//Resets the Zigbee module
		FCD_Zigbee0_Get_AT_Response(1);				//Collect Response
		FCD_Zigbee0_delms ();
		Wdt_Delay_S(2);					//Wait for reset.
}

char FCD_Zigbee0_Scan_Network(char mode)
{
	
	//mode = 0 - Store Names
	//mode = 1 - Store Addresses

		char num_devices = 0;
		char i, j;
		char length;

		for(i=0;i<64;i++)	//Initialise Buffer
		{
			ID_AddH[i] = 0;	//Store High Bytes of Address
			ID_name[i] = 0;	//Store Low Bytes of address
		}

		FCD_Zigbee0_Enter_Command_Mode();			//Initiate Command Mode
		FCD_Zigbee0_Send_AT_Command("ATND",4);		//Performs node discovery
		Wdt_Delay_Ms(200);

		for(i=0;i<MX_ZIG_MAX_DEV;i++)
		{
			length = FCD_Zigbee0_Get_AT_Response(0);			//Collect 16 bit network address

			if (length <= 2 || length == 255)		//If length <= 2 then device listing has finished, 255 then no data being received.
				break;

			if (mode)
			{
				length = FCD_Zigbee0_Get_AT_Response(1);		//Collect 64 bit device address
				for (j=0;j<8;j++)
				{
					ID_AddH[j+(num_devices*8)] = In_Buff[j];	//Store High Bytes of Address
				}

				length = FCD_Zigbee0_Get_AT_Response(1);		//Collect 64 bit device address
				for (j=0;j<8;j++)
				{
					ID_name[j+(num_devices*8)] = In_Buff[j];	//Store Low Bytes of address
				}

				FCD_Zigbee0_Get_AT_Response(0);					//Collect device identifier name
			}
			else
			{
				FCD_Zigbee0_Get_AT_Response(0);					//Collect 1st half of 64 bit device address
				FCD_Zigbee0_Get_AT_Response(0);					//Collect 2nd half of 64 bit device address
				length = FCD_Zigbee0_Get_AT_Response(1);		//Collect device identifier name
				for (j=0;j<8;j++)
				{
					if (j > length - 1)
						ID_name[j+(num_devices*8)] = 0;			//Terminate the end of the name string
					else
						ID_name[j+(num_devices*8)] = In_Buff[j];//Collect the name string
				}
			}

			FCD_Zigbee0_Get_AT_Response(0);						//Collect FFFF, FFFE
			FCD_Zigbee0_Get_AT_Response(0);						//Collect 00, 01
			FCD_Zigbee0_Get_AT_Response(0);						//Collect 00, 00
			FCD_Zigbee0_Get_AT_Response(0);						//Collect C105
			FCD_Zigbee0_Get_AT_Response(0);						//Collect 101E
			//Get_AT_Response(0);					//Collect spacer carrage return
			FCD_Zigbee0_Receive_Char(200);						//Receive Character
			num_devices = num_devices + 1;			//Increment number of devices
		}

		FCD_Zigbee0_Exit_Command_Mode();

		return (num_devices);
}

char FCD_Zigbee0_Connect_To_Address(char* Address, char MSZ_Address)
{
		char HiChar[13] = "ATDH        ";			//Initialise Address Commands
		char LoChar[13] = "ATDL ";
		char i, j;
		char Retval;

		HiChar[12] = ' ';							//Remove string terminator
		i = 0;

		for (j=0; j<MSZ_Address - 8; j++)			//Pull the high 32 bits out of the string
		{
			HiChar[j+5] = Address[i];				//Add to Address High Command
			i = i + 1;
		}

		i = MSZ_Address - 8;
		for (j=0; j<8; j++)							//Pull the low 32 bits out of the string
		{
			LoChar[j+5] = Address[i];				//Add to Address Low Command
			i = i + 1;
		}

		FCD_Zigbee0_Enter_Command_Mode();							//Initiate Command Mode

		FCD_Zigbee0_Send_AT_Command(HiChar,13);		//Stores the high byte destinating address value into memory
		FCD_Zigbee0_Get_AT_Response(0);				//Collect and discard Response
		FCD_Zigbee0_delms ();

		FCD_Zigbee0_Send_AT_Command(LoChar,13);		//Stores the low byte destinating address value into memory
		FCD_Zigbee0_Get_AT_Response(1);				//Collect and store Response
		Retval = FCD_Zigbee0_Read_AT_Response(0);
		FCD_Zigbee0_delms ();

		FCD_Zigbee0_Exit_Command_Mode();

		if (Retval == 'E')
			return(1);
		return (0);

}

char FCD_Zigbee0_Connect_From_Library(char mode, char device)
{
	
	//mode = 0 - Use Names
	//mode = 1 - Use Addresses

		char i;
		char temp;

		if (mode)
		{
			char buff_addr[16];

			for(i=0;i<8;i++)
			{

				buff_addr[i]   = ID_name[i+(device*8)];
				buff_addr[i+8] = ID_AddH[i+(device*8)];
			}
			temp = FCD_Zigbee0_Connect_To_Address(buff_addr,16);
		}
		else
		{
			char buff_name[8];

			for(i=0;i<8;i++)
			{
				if (ID_name[i+(device*8)] == ' ')
					break;
				else
					buff_name[i] = ID_name[i+(device*8)];
			}
			temp = FCD_Zigbee0_Connect_To_Name(buff_name,i);
		}
		return temp;
}

char FCD_Zigbee0_Connect_To_Coordinator()
{
		char Retval;

		FCD_Zigbee0_Enter_Command_Mode();					//Initiate Command Mode

		FCD_Zigbee0_Send_AT_Command("ATDL 0",6);
		FCD_Zigbee0_Get_AT_Response(0);						//Collect and discard response
		FCD_Zigbee0_delms ();

		FCD_Zigbee0_Send_AT_Command("ATDH 0",6);
		FCD_Zigbee0_Get_AT_Response(1);						//Collect and log Response
		Retval = FCD_Zigbee0_Read_AT_Response(0);
		FCD_Zigbee0_delms ();

		FCD_Zigbee0_StoreParams();							//Store the settings to non volitile memory
		FCD_Zigbee0_Exit_Command_Mode();

		if (Retval == 'E')
			return(1);
		return (0);
}

char FCD_Zigbee0_Connect_To_All()
{
		char Retval;

		FCD_Zigbee0_Enter_Command_Mode();					//Initiate Command Mode

		FCD_Zigbee0_Send_AT_Command("ATDL FFFF",9);
		FCD_Zigbee0_Get_AT_Response(0);						//Collect and discard response
		FCD_Zigbee0_delms ();

		FCD_Zigbee0_Send_AT_Command("ATDH 0",6);
		FCD_Zigbee0_Get_AT_Response(1);						//Collect and log Response
		Retval = FCD_Zigbee0_Read_AT_Response(0);
		FCD_Zigbee0_delms ();

		FCD_Zigbee0_StoreParams();							//Store the settings to non volitile memory
		FCD_Zigbee0_Exit_Command_Mode();

		if (Retval == 'E')
			return(1);
		return (0);
}

char FCD_Zigbee0_Connect_To_Name(char* String, char MSZ_String)
{
		char devname[25] = "ATDN ";
		char i=0;
		char Retval;

		while(i < MSZ_String)
		{
			devname[i+5] = String[i];
			i = i + 1;
		}
		FCD_Zigbee0_Enter_Command_Mode();					//Initiate Command Mode

		FCD_Zigbee0_Send_AT_Command(devname, MSZ_String+5);	//Sets the destination name
		FCD_Zigbee0_Get_AT_Response(1);						//Collect and log Response
		Retval = FCD_Zigbee0_Read_AT_Response(0);
		FCD_Zigbee0_delms ();

		FCD_Zigbee0_Exit_Command_Mode();

		if (Retval == 'O')
			return(0);
		return (1);
}

void FCD_Zigbee0_Send_Char(char nChar)
{
	
	#ifdef MX_MIAC_SYSTEM
	  #if (portc != MX_MIAC_MM_MIAC_BASE)

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

		MIAC_CAN_TX[0] = nChar;
		MIAC_Module_WR(ModID, 481, 1,1,0);
		  #if MX_ZIG_HWFC == 1
			MIAC_Module_GetAck(ModID + 32, 481, 255);
		  #else
			MIAC_Module_GetAck(ModID + 32, 481, 250);
		  #endif
	  #endif
	#else
		st_bit(txsta, TXEN);
		while (ts_bit(pir1, TXIF) == 0);
		#if MX_ZIG_HWFC == 1
			while ((MX_ZIG_PORT & (1 << MX_ZIG_CTSP) ) != 0); 	//wait until CTS is low
		#endif
		txreg = nChar;											//Send Character
	#endif

}

char FCD_Zigbee0_Receive_Char(char nTimeout)
{
	
	#ifdef MX_MIAC_SYSTEM
	  #if (portc != MX_MIAC_MM_MIAC_BASE)

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

		MIAC_CAN_TX[0] = nTimeout;
		MIAC_Module_WR(ModID, 482, 1,1,0);
		  #if MX_ZIG_HWFC == 1
			MIAC_Module_GetAck(ModID + 32, 482, 255);
		  #else
			MIAC_Module_GetAck(ModID + 32, 482, 250);
		  #endif
		return (MIAC_CAN_RX[0]);
	  #endif
	#else
		char delay1 = 0;
		char delay2 = 0;
		char dummy = 0;
		char retVal = 255;
		char bWaitForever = 0;
		char rxStatus = 0;

		#if MX_ZIG_HWFC == 1
			clear_bit(MX_ZIG_PORT, MX_ZIG_RTSP);	//ready to accept data
		#endif

		if (nTimeout == 255)
		{
			bWaitForever = 1;
		}

		st_bit(rcsta, CREN);

		while (rxStatus == 0)
		{
			if (ts_bit(pir1, RCIF) != 0)
			{
				//received a Char
				rxStatus = 2;
			}
			else
			{
				if (bWaitForever == 0)
				{
					//don't wait forever, so do timeout thing...
					if (nTimeout == 0)
					{
						rxStatus = 1;
					}
					else
					{
						//decrement timeout
						delay1--;
						if (delay1 == 0)
						{
							nTimeout--;
		}	}	}	}	}

		if (rxStatus == 2)
		{
			if (ts_bit(rcsta, FERR) != 0)
			{
				dummy = rcreg;      //need to read the rcreg to clear FERR
			}
			else
			{
				if (ts_bit(rcsta, OERR) != 0)
				{
					//need to read the rcreg to clear error
					cr_bit(rcsta, CREN);
					st_bit(rcsta, CREN);
				}
				else
				{
					retVal = rcreg; //no error, so rx byte is valid
		}	}	}

		#if MX_ZIG_HWFC == 1
			set_bit(MX_ZIG_PORT, MX_ZIG_RTSP);  //not ready to accept data
		#endif

		return (retVal);
	#endif

}

void FCD_Zigbee0_Node_Configure_Sleep(char Mode, short Timeout)
{
	
	//ATSM 1 - PIN Hibernate 	- 10uA
	//ATSM 2 - PIN Doze			- 50uA
	//ATSM 4 - Cyclic Sleep		Sample time us - ms
	//ATSM 5 - Cyclic Doze		Sample time or pin wake up.
	//ATST - Timeout - x 1ms - Longest = 65.535 seconds or 0xFFFF before sleep

		#if MX_ZIG_MODE == 2
			char ModeRep[6] = "ATSM ";

			FCD_Zigbee0_Enter_Command_Mode();							//Initiate Command Mode

			//wrong command sent as hex not numbers
			if (Mode > 3 && Mode < 6)
			{
				char ToutRep[9] = "ATST ";

				FCD_Zigbee0_ToHexString(Timeout);
				ToutRep[5] = Num_Str[0];
				ToutRep[6] = Num_Str[1];
				ToutRep[7] = Num_Str[2];
				ToutRep[8] = Num_Str[3];

				FCD_Zigbee0_Send_AT_Command(ToutRep,9);		//Sends period of time with inactivity to wait before sleeping
				FCD_Zigbee0_Get_AT_Response(0);				//Collect and discard Response
				FCD_Zigbee0_delms ();
			}

			FCD_Zigbee0_ToHexString(Mode);
			ModeRep[5] = Num_Str[3];

			FCD_Zigbee0_Send_AT_Command(ModeRep,6);		//Sends sleep command
			FCD_Zigbee0_Get_AT_Response(0);				//Collect and discard Response
			FCD_Zigbee0_delms ();

			FCD_Zigbee0_Exit_Command_Mode();

		  #ifdef MX_MIAC_SYSTEM
		    #if (portc != MX_MIAC_MM_MIAC_BASE)

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

			  MIAC_Module_WR(ModID, 484, 0,1,0);
			  MIAC_Module_GetAck(ModID + 32, 484, 20);
			#endif
		  #else
			set_bit(MX_ZIG_PORT, MX_ZIG_SLEP);			//Sleep is enabled
		  #endif

		  delay_us(50);

		#endif
}

void FCD_Zigbee0_Node_Wake()
{
		#if MX_ZIG_MODE == 2

		  #ifdef MX_MIAC_SYSTEM
		    #if (portc != MX_MIAC_MM_MIAC_BASE)

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

			  MIAC_Module_WR(ModID, 485, 0,1,0);
			  MIAC_Module_GetAck(ModID + 32, 485, 20);
			#endif
		  #else

			clear_bit(MX_ZIG_PORT, MX_ZIG_SLEP);		//Sleep is disabled

		  #endif

			Wdt_Delay_Ms(15);

			FCD_Zigbee0_Enter_Command_Mode();						//Initiate Command Mode

			FCD_Zigbee0_Send_AT_Command("ATFP",4);					//Sends poll data command
			FCD_Zigbee0_Get_AT_Response(1);							//Collect Response
			FCD_Zigbee0_delms ();

			FCD_Zigbee0_Exit_Command_Mode();

			delay_us(50);
		#endif
}

char FCD_Zigbee0_Get_Signal_Level()
{
		char calc;
		char temp;

		FCD_Zigbee0_Enter_Command_Mode();							//Initiate Command Mode

		FCD_Zigbee0_Send_AT_Command("ATDB",4);		//Collects the last received signal strength in dBm
		FCD_Zigbee0_Get_AT_Response(1);	//Collect and log Response
		FCD_Zigbee0_delms ();

		FCD_Zigbee0_Exit_Command_Mode();

		temp = In_Buff[0];

		if (temp <= '9' && temp >= '0')
		{
			calc = temp - '0';
			calc = calc << 4;
		}
		else
		{
			calc = temp - 'A';
			calc = calc + 0x0A;
			calc = calc << 4;
		}

		temp = In_Buff[1];

		if (temp <= '9' && temp >= '0')
		{
			calc = calc + (temp - '0');
		}
		else
		{
			calc = calc + (temp - 'A');
			calc = calc + 0x0A;
		}

		return (calc);
}

void FCD_Zigbee0_Enter_Command_Mode()
{
		Wdt_Delay_S(1);					//Fixed delay before sending command
		FCD_Zigbee0_Send_Char('+');						//Send characters to enter command mode
		FCD_Zigbee0_Send_Char('+');
		FCD_Zigbee0_Send_Char('+');
		Wdt_Delay_Ms(50);					//Fixed delay before sending command
		FCD_Zigbee0_Get_AT_Response(0);					//Collect Response
}

void FCD_Zigbee0_StoreParams()
{
		FCD_Zigbee0_Send_AT_Command("ATWR",4);		//Stores the value into memory
		FCD_Zigbee0_Get_AT_Response(0);
		FCD_Zigbee0_delms ();
}

void FCD_Zigbee0_Exit_Command_Mode()
{
		FCD_Zigbee0_Send_AT_Command("ATCN",4);		//Exits command mode
		FCD_Zigbee0_Get_AT_Response(0);				//Collect and discard Response
		FCD_Zigbee0_delms ();
}

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

	#ifdef MX_MIAC_SYSTEM
	  #if (portc != MX_MIAC_MM_MIAC_BASE)

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

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

	  #if MX_ZIG_LCD == 1
		FCD_MIAC0_DisplayClear();
		FCD_MIAC0_PrintString(String, 10);
	  #endif

		// Split string into 6-byte packets
		MSZ_String = MSZ_String + 1;

		do
		{
			loop_exit = 0;
			do
			{
				if (idx == MSZ_String)
				{
					MIAC_CAN_TX[msg_count] = MX_ZIG_CARRAGE_RETURN;
					loop_exit = 1;
					more_data = 0;
				}else{
					ch = String[idx];
					if(ch == 0)
					{
						ch = MX_ZIG_CARRAGE_RETURN;
						loop_exit = 1;
						more_data = 0;
					}
					MIAC_CAN_TX[msg_count] = ch;
					idx = idx + 1;
				}
				msg_count = msg_count + 1;
			}
			while ((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;
			}

			MIAC_Module_WR(ModID, 483, msg_count,cmd_start,more_data);	// TX data

			if(more_data)
				MIAC_Module_GetAck(ModID + 32, 483, 20);				// Check for ACK
			else
			{
			  #if MX_ZIG_HWFC == 1
				MIAC_Module_GetAck(ModID + 32, 483, 255);				// Check for ACK
			  #else
				MIAC_Module_GetAck(ModID + 32, 483, 250);				// Check for ACK
			  #endif
			}

			cmd_start = 0;
			msg_count = 0;
		}
		while (more_data);
	  #endif

	#else

		if (LCD_VERB_ZIG == 1)
		{
			#if MX_ZIG_LCD == 1
				#ifdef MX_GFXLCD_PORT
					FCD_gLCD0_Clear();
					FCD_gLCD0_Print(String, 10, 2, 2, 2, 0);
				#else
					FCD_LCDDisplay0_Clear();
					FCD_LCDDisplay0_PrintString(String, 10);
				#endif
			#endif
		}

		while (idx < MSZ_String)		//While There is still characters to send
		{
			FCD_Zigbee0_Send_Char(String[idx]);	//Output Character
			idx++;
		}
		FCD_Zigbee0_Send_Char(MX_ZIG_CARRAGE_RETURN);	//Output Carrage Return
	#endif

}

char FCD_Zigbee0_Get_AT_Response(char logging)
{
		char idx = 0;
		char end = 1;
		char temp = 255;
		char String[2] = "0";
		char count = 0;

		//While not received anything
		while (temp == 255 && count < 255)
		{
			temp = FCD_Zigbee0_Receive_Char(50);		//Receive Character
			count = count + 1;
		}

		if (count == 255) return count;

		String[0] = temp;

		if (LCD_VERB_ZIG == 1)
		{
			#if MX_ZIG_LCD == 1
				#ifdef MX_GFXLCD_PORT
					FCD_gLCD0_Print(String, 10, 2+(idx * 12), 20, 2, 0);
				#else
			  	  #ifdef MX_MIAC
					FCD_MIAC0_PrintASCII(temp);
				  #else
					FCD_LCDDisplay0_PrintASCII(temp);			//Output to LCD
				  #endif
				#endif
			#endif
		}

		if (logging)
			In_Buff[idx] = temp;						//Store into array

		idx++;											//Increment index

		//While not received carrage return or timeout and buffer not full
		while (end)
		{
			temp = FCD_Zigbee0_Receive_Char(20);		//Receive Character
			String[0] = temp;

			//Check for carrage return or timeout or full buffer
			if (temp == MX_ZIG_CARRAGE_RETURN || (temp == 255 || idx == 16))
				end = 0;

			//else store and move to next character
			else
			{
				if (LCD_VERB_ZIG == 1)
				{
					#if MX_ZIG_LCD == 1
						#ifdef MX_GFXLCD_PORT
							FCD_gLCD0_Print(String, 10, 2+(idx * 12), 20, 2, 0);
						#else
					  	  #ifdef MX_MIAC
							FCD_MIAC0_PrintASCII(temp);
						  #else
							FCD_LCDDisplay0_PrintASCII(temp);			//Output to LCD
						  #endif
						#endif
					#endif
				}

				if (logging)
					In_Buff[idx] = temp;

				idx++;
			}
		}
		return (idx);			//Return the length of the response
}

char FCD_Zigbee0_Read_AT_Response(char idx)
{
		return (In_Buff[idx]);
}

char FCD_Zigbee0_Read_Address_From_Buffer(char device, char idx)
{
	
		//returns a byte from the 16-byte address

		if (device > 7) return 255; 			//Max 8 devices

		device = device * 8;
		device = device + idx;

		if (idx > 7)
		{
			device = device - 8;
			return ID_AddH[device];
		}

		else
		{
			return ID_name[device];
		}

}

char FCD_Zigbee0_Read_Name_From_Buffer(char device, char idx)
{
	
		//returns a byte from the 8-byte name

		if (device > 7) return 255; 			//Max 8 devices

		device = device * 8;
		device = device + idx;

		return ID_name[device];
}

char FCD_Zigbee0_Scan_Buffer(char* String, char MSZ_String, char length)
{
	
		//returns a binary list of matched identifier names
		char i, j;
		char retval = 0;
		char comparison;
		char temp[8];

		for (i=0;i<8;i++)			//Step Through Buffered Devices
		{
			retval = retval >> 1;

			for (j=0;j<8;j++)  		//Step through Device Identifier Names
			{
				temp[j] = FCD_Zigbee0_Read_Name_From_Buffer(i, j);
			}

			FCI_LEFTSTRING(temp,8,temp,8,length);				//Chop identifier string down to size
			FCI_LEFTSTRING(String,8,String,8,length);				//Chop comparison string down to size
			comparison = FCI_COMPARE(temp, length, String, length, 0);

			if( comparison == 0 )				//Case insensitive compare
			{
				retval = retval | 0x80;
			}
		}

		return retval;
}

//Macro implementations

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

	
	#ifdef MX_MIAC_SYSTEM
	  #if (portc != MX_MIAC_MM_MIAC_BASE)

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

		MIAC_CAN_TX[0] = MX_ZIG_HWFC;
		MIAC_Module_WR(ModID, 480, 1,1,0);
		MIAC_Module_GetAck(ModID + 32, 480, 20);
	  #endif
	#else
		clear_bit (MX_ZIG_TRIS, MX_UART_TX);		// TX pin is an output
		set_bit (MX_ZIG_TRIS, MX_UART_RX);			// RX pin is an input
		txsta = MX_ZIG_TXSTA;   					// 8-bit, async, low speed, off
		spbrg = MX_ZIG_SPBRG;   					// set the baud rate 9600
		rcsta = 0;                    				// 8-bit, disabled
		st_bit(rcsta, SPEN);         				// turn on serial interface

		#ifdef MX_ZIG_HWFC == 1
			set_bit(MX_ZIG_TRIS, MX_ZIG_CTSP);		// CTS is an input
			clear_bit(MX_ZIG_TRIS, MX_ZIG_RTSP);	// RTS is an output
		#endif

		clear_bit(MX_ZIG_TRIS, MX_ZIG_SLEP);		// Sleep is an output
		clear_bit(MX_ZIG_PORT, MX_ZIG_SLEP);		// Sleep is disabled
	#endif

		#if MX_ZIG_LCD == 1
			LCD_VERB_ZIG = 1;
		#endif


	//Interrupt initialisation code
	option_reg = 0xC0;


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


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


	//Setup Zigbee End Node
	//Call Component Macro: Zigbee(0)::Init_Network
	FCD_Zigbee0_Init_Network();


	//Connect To Co-Ordinator
	//Call Component Macro: retval=Zigbee(0)::Connect_To_Coordinator
	FCV_RETVAL = FCD_Zigbee0_Connect_To_Coordinator();


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


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


	//Display Transmitting
	//Call Component Macro: LCDDisplay(0)::PrintString("Transmitting...")
	FCD_LCDDisplay0_PrintString("Transmitting...",15);


	//Char = 0
	//Calculation:
	//  char = 0
	FCV_CHAR = 0;
	

	//Loop
	//Loop: While 1
	while (1)
	{
		//
		//Call Component Macro: LCDDisplay(0)::Cursor(0, 1)
		FCD_LCDDisplay0_Cursor(0, 1);


		//Display Transmitted Number
		//Call Component Macro: LCDDisplay(0)::PrintNumber(char)
		FCD_LCDDisplay0_PrintNumber(FCV_CHAR);


		//Transmit Number
		//Call Component Macro: Zigbee(0)::Send_Char(char)
		FCD_Zigbee0_Send_Char(FCV_CHAR);


		//Increase Number
		//Calculation:
		//  char = char + 1
		FCV_CHAR = FCV_CHAR + 1;
		

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


	}


	mainendloop: goto mainendloop;
}

void MX_INTERRUPT_MACRO(void)
{
}



