コード例 #1
0
ファイル: MainDemo.c プロジェクト: Athuli7/Microchip
int main(void)
#endif
{
    BYTE i;
    static DWORD t = 0;
    static DWORD dwLastIP = 0;
    static UINT8 updateDisplay = 0;

#if defined (EZ_CONFIG_STORE)
    static DWORD ButtonPushStart = 0;
#endif

#if (MY_DEFAULT_NETWORK_TYPE == WF_SOFT_AP)
    UINT8 channelList[] = MY_DEFAULT_CHANNEL_LIST_PRESCAN; // WF_PRESCAN
    tWFScanResult bssDesc;
#endif

    // Initialize application specific hardware
    InitializeBoard();

    // Initiates board setup process if button is depressed
    // on startup
    if (BUTTON1_IO == 0u) {
        while (BUTTON1_IO == 0);
        SelfTestMode();
    }

    //#if defined(USE_LCD)



    /*******************************************************************/
    // Initialize the LCD
    /*******************************************************************/

    ConfigureLCD_SPI();

    LCDInit();

    /*******************************************************************/
    // Display Start-up Splash Screen
    /*******************************************************************/
    LCDBacklightON();
    LEDS_ON();
    LCDErase();
    sprintf((char *) LCDText, (char*) "  MiWi - WiFi  ");
    sprintf((char *) &(LCDText[16]), (char*) " Gateway  Demo");
    LCDUpdate();





    /*******************************************************************/
    // Initialize the MiWi Protocol Stack. The only input parameter indicates
    // if previous network configuration should be restored.
    /*******************************************************************/
    MiApp_ProtocolInit(FALSE);

    /*******************************************************************/
    // Set Device Communication Channel
    /*******************************************************************/
    if (MiApp_SetChannel(myChannel) == FALSE) {
        LCDDisplay((char *) "ERROR: Unable to Set Channel..", 0, TRUE);
        while (1);
    }

    /*******************************************************************/
    //  Set the connection mode. The possible connection modes are:
    //      ENABLE_ALL_CONN:    Enable all kinds of connection
    //      ENABLE_PREV_CONN:   Only allow connection already exists in
    //                          connection table
    //      ENABL_ACTIVE_SCAN_RSP:  Allow response to Active scan
    //      DISABLE_ALL_CONN:   Disable all connections.
    /*******************************************************************/
    MiApp_ConnectionMode(ENABLE_ALL_CONN);

    /*******************************************************************/
    // Function MiApp_EstablishConnection try to establish a new
    // connection with peer device.
    // The first parameter is the index to the active scan result,
    //      which is acquired by discovery process (active scan). If
    //      the value of the index is 0xFF, try to establish a
    //      connection with any peer.
    // The second parameter is the mode to establish connection,
    //      either direct or indirect. Direct mode means connection
    //      within the radio range; indirect mode means connection
    //      may or may not in the radio range.
    /*******************************************************************/
    i = MiApp_EstablishConnection(0xFF, CONN_MODE_DIRECT);

    /*******************************************************************/
    // Display current opertion on LCD of demo board, if applicable
    /*******************************************************************/
    if (i != 0xFF) {
        ; // Connected Peer on Channel
    } else {
        /*******************************************************************/
        // If no network can be found and join, we need to start a new
        // network by calling function MiApp_StartConnection
        //
        // The first parameter is the mode of start connection. There are
        // two valid connection modes:
        //   - START_CONN_DIRECT        start the connection on current
        //                              channel
        //   - START_CONN_ENERGY_SCN    perform an energy scan first,
        //                              before starting the connection on
        //                              the channel with least noise
        //   - START_CONN_CS_SCN        perform a carrier sense scan
        //                              first, before starting the
        //                              connection on the channel with
        //                              least carrier sense noise. Not
        //                              supported for current radios
        //
        // The second parameter is the scan duration, which has the same
        //     definition in Energy Scan. 10 is roughly 1 second. 9 is a
        //     half second and 11 is 2 seconds. Maximum scan duration is
        //     14, or roughly 16 seconds.
        //
        // The third parameter is the channel map. Bit 0 of the
        //     double word parameter represents channel 0. For the 2.4GHz
        //     frequency band, all possible channels are channel 11 to
        //     channel 26. As the result, the bit map is 0x07FFF800. Stack
        //     will filter out all invalid channels, so the application
        //     only needs to pay attention to the channels that are not
        //     preferred.
        /*******************************************************************/
        MiApp_StartConnection(START_CONN_DIRECT, 10, 0);
    }

    // Turn OFF LCD after setting up MiWi Connection
    LCDBacklightOFF();

    // Initialize stack-related hardware components that may be
    // required by the UART configuration routines
    TickInit();
#if defined(STACK_USE_MPFS2)
    MPFSInit();
#endif

    // Initialize Stack and application related NV variables into AppConfig.
    InitAppConfig();
    dwLastIP = AppConfig.MyIPAddr.Val;
    // Initialize core stack layers (MAC, ARP, TCP, UDP) and
    // application modules (HTTP, SNMP, etc.)
    StackInit();

#if defined ( EZ_CONFIG_SCAN )
    WFInitScan();
#endif

#if (MY_DEFAULT_NETWORK_TYPE == WF_SOFT_AP)
    // WF_PRESCAN: Pre-scan before starting up as SoftAP mode  
    WF_CASetScanType(MY_DEFAULT_SCAN_TYPE);
    WF_CASetChannelList(channelList, sizeof (channelList));

    if (WFStartScan() == WF_SUCCESS) {
        SCAN_SET_DISPLAY(SCANCXT.scanState);
        SCANCXT.displayIdx = 0;
        //putsUART("main: Prescan WFStartScan() success ................. \r\n");
    }

    // Needed to trigger g_scan_done		
    WFRetrieveScanResult(0, &bssDesc);
#else

#if defined(WF_CS_TRIS)
    WF_Connect();
#endif // defined(WF_CS_TRIS)

#endif // (MY_DEFAULT_NETWORK_TYPE == WF_SOFT_AP)

    // Initialize any application-specific modules or functions/
    // For this demo application, this only includes the
    // UART 2 TCP Bridge
#if defined(STACK_USE_UART2TCP_BRIDGE)
    UART2TCPBridgeInit();
#endif

#if defined(STACK_USE_ZEROCONF_LINK_LOCAL)
    ZeroconfLLInitialize();
#endif

#if defined(STACK_USE_ZEROCONF_MDNS_SD)
    mDNSInitialize(MY_DEFAULT_HOST_NAME);
    mDNSServiceRegister(
            (const char *) AppConfig.NetBIOSName, // base name of the service
            "_http._tcp.local", // type of the service
            80, // TCP or UDP port, at which this service is available
            ((const BYTE *) "path=/index.htm"), // TXT info
            1, // auto rename the service when if needed
            NULL, // no callback function
            NULL // no application context
            );

    mDNSMulticastFilterRegister();
#endif

#if defined(WF_CONSOLE)
    WFConsoleInit();
#endif

    // Now that all items are initialized, begin the co-operative
    // multitasking loop.  This infinite loop will continuously
    // execute all stack-related tasks, as well as your own
    // application's functions.  Custom functions should be added
    // at the end of this loop.
    // Note that this is a "co-operative mult-tasking" mechanism
    // where every task performs its tasks (whether all in one shot
    // or part of it) and returns so that other tasks can do their
    // job.
    // If a task needs very long time to do its job, it must be broken
    // down into smaller pieces so that other tasks can have CPU time.

    LEDS_OFF();

    while (1) {

        /*******************************************************************/
        // Check Button Events
        /*******************************************************************/
        if (BUTTON1_IO == 0u) {
            while (BUTTON1_IO == 0);
            LCDErase();
            sprintf((char *) LCDText, (char*) "Erase Room Info?");
            sprintf((char *) &(LCDText[16]), (char*) "SW0:Yes  SW2:No");
            LCDUpdate();

            while (1) {
                if (BUTTON1_IO == 0u) {
                    while (BUTTON1_IO == 0);
                    LCDDisplay((char *) "STATUS: Erasing...", 0, TRUE);
                    EraseRoomInfo();
                    DisplaySSID();
                    break;
                } else if (BUTTON2_IO == 0u) {
                    while (BUTTON2_IO == 0);
                    DisplaySSID();
                    break;
                }


            }
        }

        // Blink LED0 twice per sec when unconfigured, once per sec after config
        if ((TickGet() - t >= TICK_SECOND / (4ul - (CFGCXT.isWifiDoneConfigure * 2ul)))) {
            t = TickGet();
            LED0_INV();
        }

        if(CFGCXT.isWifiNeedToConfigure) updateDisplay = 1;
        

        #if (MY_DEFAULT_NETWORK_TYPE == WF_SOFT_AP)
                if (g_scan_done) {
                    if (g_prescan_waiting) {
                        SCANCXT.displayIdx = 0;
                        while (IS_SCAN_STATE_DISPLAY(SCANCXT.scanState)) {
                            WFDisplayScanMgr();
                        }


        #if defined(WF_CS_TRIS)
                        WF_Connect();
        #endif
                        DisplaySSID();
                        g_scan_done = 0;
                        g_prescan_waiting = 0;
                    }
                }
        #endif // (MY_DEFAULT_NETWORK_TYPE == WF_SOFT_AP)


        // This task performs normal stack task including checking
        // for incoming packet, type of packet and calling
        // appropriate stack entity to process it.
        StackTask();

        WiFiTask();
        // This tasks invokes each of the core stack application tasks
        StackApplications();

        #if defined(STACK_USE_ZEROCONF_LINK_LOCAL)
                ZeroconfLLProcess();
        #endif

        #if defined(STACK_USE_ZEROCONF_MDNS_SD)
                mDNSProcess();
                // Use this function to exercise service update function
                // HTTPUpdateRecord();
        #endif

        // Process application specific tasks here.
        // For this demo app, this will include the Generic TCP
        // client and servers, and the SNMP, Ping, and SNMP Trap
        // demos.  Following that, we will process any IO from
        // the inputs on the board itself.
        // Any custom modules or processing you need to do should
        // go here.

        #if defined(WF_CONSOLE)
                //WFConsoleProcess();
                //    #if !defined(STACK_USE_EZ_CONFIG)
                //	IperfAppCall();
                //      #endif
                //WFConsoleProcessEpilogue();
        wait_console_input:
        #endif

        #if defined(STACK_USE_GENERIC_TCP_CLIENT_EXAMPLE)
                GenericTCPClient();
        #endif

        #if defined(STACK_USE_GENERIC_TCP_SERVER_EXAMPLE)
                GenericTCPServer();
        #endif

        #if defined(STACK_USE_SMTP_CLIENT)
                SMTPDemo();
        #endif

        #if defined(STACK_USE_ICMP_CLIENT)
                PingDemo();
                //PingConsole();
        #endif

        #if defined(STACK_USE_SNMP_SERVER) && !defined(SNMP_TRAP_DISABLED)
                //User should use one of the following SNMP demo
                // This routine demonstrates V1 or V2 trap formats with one variable binding.
                SNMPTrapDemo();

        #if defined(SNMP_STACK_USE_V2_TRAP) || defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
                //This routine provides V2 format notifications with multiple (3) variable bindings
                //User should modify this routine to send v2 trap format notifications with the required varbinds.
                //SNMPV2TrapDemo();
        #endif
                if (gSendTrapFlag)
                    SNMPSendTrap();
        #endif

        #if defined ( WF_CONSOLE ) && defined ( EZ_CONFIG_SCAN )
                WFDisplayScanMgr();
        #endif

        #if defined(STACK_USE_BERKELEY_API)
                BerkeleyTCPClientDemo();
                BerkeleyTCPServerDemo();
                BerkeleyUDPClientDemo();
        #endif

        if((updateDisplay && CFGCXT.isWifiDoneConfigure) || (dwLastIP != AppConfig.MyIPAddr.Val))
        {
            if(dwLastIP != AppConfig.MyIPAddr.Val)
                dwLastIP = AppConfig.MyIPAddr.Val;
            if(updateDisplay && CFGCXT.isWifiDoneConfigure)
                updateDisplay = 0;
            
            #if defined(STACK_USE_ANNOUNCE)
                AnnounceIP();
            #endif
            #if defined(STACK_USE_ZEROCONF_MDNS_SD)
                mDNSFillHostRecord();
            #endif
            DisplaySSID();


        }

    }
}
コード例 #2
0
ファイル: Demo.c プロジェクト: Athuli7/Microchip
/*********************************************************************
* Function:         void main(void)
*
* PreCondition:     none
*
* Input:		    none
*
* Output:		    none
*
* Side Effects:	    none
*
* Overview:		    This is the main function that runs the simple 
*                   example demo. The purpose of this example is to
*                   demonstrate the simple application programming
*                   interface for the MiWi(TM) Development 
*                   Environment. By virtually total of less than 30 
*                   lines of code, we can develop a complete 
*                   application using MiApp interface. The 
*                   application will first try to establish a P2P 
*                   link with another device and then process the 
*                   received information as well as transmit its own 
*                   information.
*                   MiWi(TM) DE also support a set of rich 
*                   features. Example code FeatureExample will
*                   demonstrate how to implement the rich features 
*                   through MiApp programming interfaces.
*
* Note:			    
**********************************************************************/
void main (void)
{
    BYTE    i;
    BYTE    TxSynCount = 0;
    BYTE    TxSynCount2 = 0;
    BoardInit();            //Has LCDInit() also covered in this, need to make this separate to ensure that if there was a problem with I2C the board hangs up
    ConsoleInit();

    // Initialize the system

    /*******************************************************************/
    
    LCDBacklightON();
    LCDDisplay((char *)"8-Bit Wireless  Development Kit ", 0, TRUE);
    DelayMs(5000);
    LCDBacklightOFF();


    Printf("\r\nInput Configuration:");
    Printf("\r\n           Button 1: RB0");
    Printf("\r\n           Button 2: RB2");
    Printf("\r\nOutput Configuration:");
    Printf("\r\n              LED 1: RA2");
    Printf("\r\n              LED 2: RA3");
    Printf("\r\n              LED 3: RB1");
            #if defined(MRF24J40)
    Printf("\r\n     RF Transceiver: MRF24J40");
            #elif defined(MRF49XA)
    Printf("\r\n     RF Transceiver: MRF49XA");
            #elif defined(MRF89XA)
    Printf("\r\n     RF Transceiver: MRF89XA");
            #endif
    Printf("\r\n   Demo Instruction:");
    Printf("\r\n                     Power on the board until LED 1 lights up  ");
    Printf("\r\n                     to indicate connecting with peer.         ");
    Printf("\r\n                     Ping Pong Results will be displayed on LCD");
    Printf("\r\n                     Use MCLR + RB2 to switch to Self Test Mode. ");
    Printf("\r\n\r\n");
    

    LED_1 = 0;
    LED_2 = 0;

    MiApp_ProtocolInit(FALSE);

    // Set default channel
    if(MiApp_SetChannel(myChannel) == FALSE)
    {
        #if defined(__18CXX)
        return;
        #else
        return (0);
        #endif
    }

    /*******************************************************************/

    // Function MiApp_ConnectionMode defines the connection mode. The
    // possible connection modes are:
    //  ENABLE_ALL_CONN:    Enable all kinds of connection
    //  ENABLE_PREV_CONN:   Only allow connection already exists in
    //                      connection table
    //  ENABL_ACTIVE_SCAN_RSP:  Allow response to Active scan
    //  DISABLE_ALL_CONN:   Disable all connections.

    /*******************************************************************/
    MiApp_ConnectionMode(ENABLE_ALL_CONN);

    /*******************************************************************/

    // Display current opertion on LCD of demo board, if applicable

    /*******************************************************************/
    LCDDisplay((char *)"Connecting Peer  on Channel %d ", myChannel, TRUE);

    /*******************************************************************/

    // Function MiApp_EstablishConnection try to establish a new
    // connection with peer device.
    // The first parameter is the index to the active scan result,
    //      which is acquired by discovery process (active scan). If
    //      the value of the index is 0xFF, try to establish a
    //      connection with any peer.
    // The second parameter is the mode to establish connection,
    //      either direct or indirect. Direct mode means connection
    //      within the radio range; indirect mode means connection
    //      may or may not in the radio range.

    /*******************************************************************/
    #ifdef ENABLE_HAND_SHAKE
    i = MiApp_EstablishConnection(0xFF, CONN_MODE_DIRECT);
    #endif

    /*******************************************************************/

    // Display current opertion on LCD of demo board, if applicable

    /*******************************************************************/
    if(i != 0xFF)
    {
        LCDDisplay((char *)"Joined  Network Successfully..", 0, TRUE);
    }
    else
    {
        MiApp_StartConnection(START_CONN_DIRECT, 10, 0);
    }

    /*******************************************************************/

    // Function DumpConnection is used to print out the content of the
    //  Connection Entry on the hyperterminal. It may be useful in
    //  the debugging phase.
    // The only parameter of this function is the index of the
    //  Connection Entry. The value of 0xFF means to print out all
    //  valid Connection Entry; otherwise, the Connection Entry
    //  of the input index will be printed out.

    /*******************************************************************/
    #ifdef ENABLE_DUMP
    DumpConnection(0xFF);
    #endif
    #ifndef ENABLE_POWERSAVE

    // Turn on LED 1 to indicate P2P connection established
    LED_1 = 1;
    #endif
    DelayMs(20);
    LCDDisplay((char *)"Ping Pong Demo  RB0(TX) RB2(RX)", 0, TRUE);

    /*******************************************************************/

    // Following block display demo instructions on LCD based on the
    // demo board used.

    /*******************************************************************/
    /*Configure the device in transmit or Receive Mode*/
   
        
        ReadButtonPress();   

        while(1)
        {
            
            PingPongStateMachine();
            
        }                   //end of while(1)
    
}                           //end of main
コード例 #3
0
ファイル: RangeDemo.c プロジェクト: Athuli7/Microchip
/*********************************************************************
* Function:         void RangeDemo(void)
*
* PreCondition:     none
*
* Input:		    none
*
* Output:		    none
*
* Side Effects:	    none
*
* Overview:		    Following routine 
*                    
*
* Note:			    
**********************************************************************/                                
void RangeDemo(void)
{
    BOOL Run_Demo = TRUE;
    BOOL Tx_Packet = TRUE;
    BYTE rssi = 0;
	BYTE Pkt_Loss_Cnt = 0;
	MIWI_TICK tick1, tick2;
    BYTE switch_val;
    

	/*******************************************************************/
    // Dispaly Range Demo Splach Screen
    /*******************************************************************/	
    LCDBacklightON();
    LCDDisplay((char *)"   Microchip       Range Demo  ", 0, TRUE);
    LCDBacklightOFF();
        
	/*******************************************************************/
    // Read Start tickcount
    /*******************************************************************/	
	tick1 = MiWi_TickGet();
    
    while(Run_Demo)
    {	
        /*******************************************************************/
        // Read current tickcount
        /*******************************************************************/
    	tick2 = MiWi_TickGet();
        
    	// Send a Message
        if((MiWi_TickGetDiff(tick2,tick1) > (ONE_SECOND * TX_PKT_INTERVAL)))
    	{
        	LCDErase();
        	
        	if(Tx_Packet)
        	{	
            	LCDDisplay((char *)"Checking Signal Strength...", 0, TRUE);
            		
        	    MiApp_FlushTx();
        	    
        	    MiApp_WriteData(RANGE_PKT);
        	    MiApp_WriteData(0x4D);
        	    MiApp_WriteData(0x69);
        	    MiApp_WriteData(0x57);
        	    MiApp_WriteData(0x69);
        	    MiApp_WriteData(0x20);
        	    MiApp_WriteData(0x52);
        	    MiApp_WriteData(0x6F);
        	    MiApp_WriteData(0x63);
        	    MiApp_WriteData(0x6B);
        	    MiApp_WriteData(0x73);
        	    MiApp_WriteData(0x21);
        	    	    
                if( MiApp_UnicastConnection(0, FALSE) == FALSE )
                    Pkt_Loss_Cnt++;
        	    else
        	        Pkt_Loss_Cnt = 0;
        	        
        	    Tx_Packet = FALSE;   		
    		}
    		else
    		{	      	
            	if(Pkt_Loss_Cnt < 1)
            	{            		
            		if(rssi > 120)
            		{
                		sprintf((char *)&LCDText, (far rom char*)"Strength: High ");
                        LED0 = 1;
                        LED1 = 0;
                        LED2 = 0;
                    }  		
                	else if(rssi < 121 && rssi > 60)
            		{
                		sprintf((char *)&LCDText, (far rom char*)"Strength: Medium");
                        LED0 = 1;
                        LED1 = 1;
                        LED2 = 0;
                    } 
                  	else if(rssi < 61)
            		{
                		sprintf((char *)&LCDText, (far rom char*)"Strength: Low");
                        LED0 = 0;
                        LED1 = 1;
                        LED2 = 0;
                    }
                    
                    // Convert to dB
        		    //rssi = RSSIlookupTable[rssi];
                  	sprintf((char *)&(LCDText[16]), (far rom char*)"Rcv RSSI: %03d", rssi);                  	                  
                }       
                else
                {
                        LCDDisplay((char *)"No Device Found or Out of Range ", 0, TRUE);
                        LED0 = 0;
                        LED1 = 0;
                        LED2 = 1;
                }

                LCDUpdate();
                Tx_Packet = TRUE;
                        		
            } 	     
    	    /*******************************************************************/
            // Read New Start tickcount
            /*******************************************************************/
      		tick1 = MiWi_TickGet(); 
      		  
        }
        
    	// Check if Message Available
    	if(MiApp_MessageAvailable())
    	{
        	if(rxMessage.Payload[0] == EXIT_PKT)
        	{
            	MiApp_DiscardMessage();
            	MiApp_FlushTx();
    	        MiApp_WriteData(ACK_PKT);
    	        MiApp_UnicastConnection(0, FALSE);
            	Run_Demo = FALSE;
            	LCDBacklightON();
                LCDDisplay((char *)"   Exiting....     Range Demo  ", 0, TRUE);
                LCDBacklightOFF();
            }
            else if(rxMessage.Payload[0] == RANGE_PKT)
            {
            	// Get RSSI value from Recieved Packet
            	rssi = rxMessage.PacketRSSI;
        	    
        	    // Disguard the Packet so can recieve next
        	    MiApp_DiscardMessage();
        	}
        	else
            	MiApp_DiscardMessage(); 
    	}
    	
    	// Check if  Switch Pressed
    	switch_val = ButtonPressed();
    	
        if((switch_val == SW0) || (switch_val == SW1))
        {
            /*******************************************************************/
        	// Send Exit Demo Request Packet and exit Range Demo
        	/*******************************************************************/ 
            MiApp_FlushTx();    
            MiApp_WriteData(EXIT_PKT);
            MiApp_UnicastConnection(0, FALSE);
   
            LCDBacklightON();
            LCDDisplay((char *)"   Exiting....     Range Demo  ", 0, TRUE);
            LCDBacklightOFF();
            
            tick1 = MiWi_TickGet();
            // Wait for ACK Packet
            while(Run_Demo)
            {
                if(MiApp_MessageAvailable())
                {
                    if(rxMessage.Payload[0] == ACK_PKT)          
                        Run_Demo = FALSE;
                        
                    MiApp_DiscardMessage();
                }
                if ((MiWi_TickGetDiff(tick2,tick1) > (ONE_SECOND * EXIT_DEMO_TIMEOUT)))
                    Run_Demo = FALSE;
                    
                tick2 = MiWi_TickGet();
            }    
        }    
    }
}
コード例 #4
0
ファイル: main.c プロジェクト: kerikun11/MPLABXProjects
void main (void)
{
    uint8_t    i;
    
    
    SYSTEM_Initialize();
    CONSOLE_Initialize();

    /*******************************************************************/

    // Initialize the system

    /*******************************************************************/

    /*******************************************************************/

    // Following block display demo information on LCD of Explore 16 or
    // PIC18 Explorer demo board.

    /*******************************************************************/
    LCDDisplay((char *)"Chat Demo", 0, true);

    // Clear the screen (VT100)
    Printf("\x1b[2J");
    // Send the cursor home (VT100)
    Printf("\x1b[H");
    Printf("\r\nChat Demo");
            #if defined(MRF24J40)
    Printf("\r\nRF Transceiver: MRF24J40");
            #elif defined(MRF49XA)
    Printf("\r\nRF Transceiver: MRF49XA");
            #elif defined(MRF89XA)
    Printf("\r\nRF Transceiver: MRF89XA");
            #endif
    Printf("\r\n\r\nDemo Instruction:");
    Printf("\r\nUse Console to Chat with the Peer Device");
    Printf("\r\n");
    LED_1 = 0;
    LED_2 = 0;

   /******************************************************************/

    // Read the MAC address from the MAC EEPROM on the PICTail Card

    ReadMacAddress();

    // ..and display on terminal

	Printf("\r\n\r\nMy MAC Address: 0x");
    for(i = 0; i < MY_ADDRESS_LENGTH; i++)
    {
        CONSOLE_PrintHex(myLongAddress[MY_ADDRESS_LENGTH-1-i]);
    }
    /*******************************************************************/

    // Initialize Microchip proprietary protocol. Which protocol to use
    // depends on the configuration in ConfigApp.h

    /*******************************************************************/

    /*******************************************************************/

    // Function MiApp_ProtocolInit initialize the protocol stack. The
    // only input parameter indicates if previous network configuration
    // should be restored. In this simple example, we assume that the
    // network starts from scratch.

    /*******************************************************************/
    MiApp_ProtocolInit(false);

    // Set default channel
    if(MiApp_SetChannel(myChannel) == false)
    {
        Printf("\r\nERROR: Unable to program the channel\r\n");
        Printf("\r\nPress MCLR to start again\r\n");
        LCDDisplay((char *)"Error: Unable to Program Channel ", 0, TRUE);
        while(1);
        //Display error message on LCD and Console
    }

    /*******************************************************************/

    // Function MiApp_ConnectionMode defines the connection mode. The
    // possible connection modes are:
    //  ENABLE_ALL_CONN:    Enable all kinds of connection
    //  ENABLE_PREV_CONN:   Only allow connection already exists in
    //                      connection table
    //  ENABL_ACTIVE_SCAN_RSP:  Allow response to Active scan
    //  DISABLE_ALL_CONN:   Disable all connections.

    /*******************************************************************/
    MiApp_ConnectionMode(ENABLE_ALL_CONN);

    /*******************************************************************/

    // Display current opertion on LCD of demo board, if applicable

    /*******************************************************************/
    LCDDisplay((char *)"Connecting Peer  on Channel %d ", myChannel, true);
    Printf("\r\n\r\nConnecting to Peer...\r\n");

    /*******************************************************************/

    // Function MiApp_StartConnection will enable a node to start operating
    // in a variety of ways. Usually, this fucntion is called by the
    // PAN Coordinator who is the first in the PAN.
    //
    // The first parameter defines the mode to start the PAN in
    //
    // The second parameter defines the scan duration (if energy/carrier
    // sense scan is enabled). 0 if START_CONN_DIRECT used.
    //
    // The third parameter is a bit map of of the channels to perform the
    // noise scan on. 0 if START_CONN_DIRECT used.

    /*******************************************************************/

    MiApp_StartConnection(START_CONN_DIRECT, 0, 0) ;
    /*******************************************************************/

    // Function MiApp_EstablishConnection try to establish a new
    // connection with peer device.
    // The first parameter is the index to the active scan result,
    //      which is acquired by discovery process (active scan). If
    //      the value of the index is 0xFF, try to establish a
    //      connection with any peer.
    // The second parameter is the mode to establish connection,
    //      either direct or indirect. Direct mode means connection
    //      within the radio range; indirect mode means connection
    //      may or may not in the radio range.

    /*******************************************************************/
    #ifdef ENABLE_HAND_SHAKE
    i = 0xFF;
    while(i == 0xFF)
    {
        i = MiApp_EstablishConnection(0xFF, CONN_MODE_DIRECT);
    }
    #endif

    /*******************************************************************/

    // Display current opertion on LCD of demo board, if applicable

    /*******************************************************************/

    LCDDisplay((char *)"Joined  Network Successfully..", 0, true);

    /*******************************************************************/

    // Function DumpConnection is used to print out the content of the
    //  Connection Entry on the hyperterminal. It may be useful in
    //  the debugging phase.
    // The only parameter of this function is the index of the
    //  Connection Entry. The value of 0xFF means to print out all
    //  valid Connection Entry; otherwise, the Connection Entry
    //  of the input index will be printed out.

    /*******************************************************************/
    #ifdef ENABLE_DUMP
    DumpConnection(0xFF);
    #endif

    #ifndef ENABLE_POWERSAVE
    // Turn on LED 1 to indicate P2P connection established
    LED_1 = 1;
    #endif
    DELAY_ms(100);

    LCDBacklightON();
    LCD_Erase();
    sprintf((char *)LCDText, (char *)"MyAddr:   %02x%02x%02x", myLongAddress[2],
             myLongAddress[1], myLongAddress[0]);
    sprintf((char *) &(LCDText[16]), (char *)"PeerAddr: %02x%02x%02x", ConnectionTable[i].Address[2],
             ConnectionTable[i].Address[1], ConnectionTable[i].Address[0]);
    LCD_Update();

    /*******************************************************************/

    // Following block display demo instructions on LCD based on the
    // demo board used.

    /*******************************************************************/
    Printf("-------------------------------------------------------\r\n");
    Printf("Chat Window: \r\n");
    Printf("-------------------------------------------------------\r\n");
    Printf("$$");

    while(1)
    {
        if(MiApp_MessageAvailable())
        {  
            ProcessRxMessage();      
        }

        if(CONSOLE_IsGetReady())
        {
            FormatTxMessage();
            
        }

        if(messagePending)
        {
            tickCurrent = MiWi_TickGet();
            if
            (
                (MiWi_TickGetDiff(tickCurrent, tickPrevious) > (ONE_SECOND * 30)) ||
                (TxMessageSize >= MAX_MESSAGE_LEN) ||
                (transmitPending == true)
            )
            {
                
                TransmitMessage();


            }
        }

        // Display connection table if RB0 is pressed
        if(PUSH_BUTTON_1 == 0)
    	{
        	while(PUSH_BUTTON_1 == 0);
        	Printf("\r\n\r\nDumping Connection Table...\r\n");
        	DumpConnection(0xFF);
        	Printf("-------------------------------------------------------\r\n");
    		Printf("Chat Window: \r\n");
    		Printf("-------------------------------------------------------\r\n");
    		Printf("$$ ");
    	}


    }

    //Enable device to foward the received packet information to the console
}                           //end of main