⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demozcoordapp.c

📁 zbee potocol 無線通訊測試.平台:microchip 18f452 與 cc2420 zbee 晶片.含coord端與rfd端
💻 C
📖 第 1 页 / 共 3 页
字号:
            // First bind #1
            customBindInfo.Flags.bits.bDestBindInfoReceived = TRUE;
            customBindInfo.Flags.bits.bSourceBindInfoReceived = TRUE;
            customBindInfo.srcKey = remoteNodeKey;
            sourceBindInfo.ep = EP_S2;
            sourceBindInfo.clusterID = PICDEMZ_DEMO_CLUSTER_ID;
            destBindInfo.ep = EP_D1;
            customBindInfo.destKey = 0;

            PrepareBindEntry();

            // Bind #3
            customBindInfo.Flags.bits.bDestBindInfoReceived = TRUE;
            customBindInfo.Flags.bits.bSourceBindInfoReceived = TRUE;
            customBindInfo.srcKey = 0;
            sourceBindInfo.ep = EP_S2;
            sourceBindInfo.clusterID = PICDEMZ_DEMO_CLUSTER_ID;
            destBindInfo.ep = EP_D1;
            customBindInfo.destKey = remoteNodeKey;

            PrepareBindEntry();

            ConsolePutROMString(demoBindSuccessMsg);
        }
    }

    else if ( bindInfo.cmd == CUSTOM_BIND_DEST)
    {
        // Make sure that this is destination node is in our neighbor table
        customBindInfo.destKey = LookupNodeByLongAddr(&bindInfo.nodeAddr);

        if ( customBindInfo.destKey == NEIGHBOR_KEY_INVALID )
        {
            ConsolePutROMString(unknownDestNodeMsg);
        }
        else
        {
            ConsolePutROMString(validDestInfoMsg);

            // Save dest EP #
            //destBindInfo.ep = bindInfo.ep;
            destBindInfo.ep = EP_LED_S;
            // Remember that we have received destination information.
            customBindInfo.Flags.bits.bDestBindInfoReceived = TRUE;
            
            //=======================================
            // test
            sourceBindInfo.clusterID = bindInfo.clusterID;
            customBindInfo.Flags.bits.bSourceBindInfoReceived = TRUE;
            sourceBindInfo.ep = EP_LED_D;
            customBindInfo.srcKey = 0;

        }
    }


    PrepareBindEntry();

    // As part of our custom binding procedure, we are not sending any
    // confirmation back. When individual in/out binding request was received
    // our MAC would already have sent ACK to confirm reception.
    // For simplicity, we will use that mechanism to ensure good delivery assurance.
    return FALSE;
}


static void PrepareBindEntry(void)
{

    // If we have received both source and dest binding info, create a
    // binding entry.
    // Keep LED solid ON for few moments to indicate that we have created
    // one binding entry.
    if ( customBindInfo.Flags.bits.bDestBindInfoReceived &&
         customBindInfo.Flags.bits.bSourceBindInfoReceived )
    {
        if ( AddBindingInfo(customBindInfo.srcKey,
                            sourceBindInfo.ep,
                            sourceBindInfo.clusterID,
                            destBindInfo.ep,
                            customBindInfo.destKey) )
        {
            ConsolePutROMString(bindingSuccessMsg);
        }
        else
        {
            ConsolePutROMString(bindingFailedMsg);
        }

        customBindInfo.Flags.Val = 0x00;
    }

}


static void InitializeBoard(void)
{
    // This is a RS232 console - you may replace it with any console you like.
    ConsoleInit();
    //RCIP = 1;

    // Switches S2 and S3 are on RB5 and RB4 respectively. We want interrupt-on-change
    INTCON = 0x00;

    // There is no external pull-up resistors on S2 and S3. We will use internal pull-ups.
    // The CC2420 provides SFD (Start of Frame Detect) signal on RB2. We are using
    // the falling edge of that signal to generate INT2
    // Enable PORTB internal pullups, INT2 on falling edge
    INTCON2 = 0x00;

    // Enable INT2 interrupt - SFD from CC2420
    INTCON3 = 0xD0;

    // CC2420 I/O assignments with respect to PIC:
    // RB0 <- FIFO      (Input)
    // RB1 <- CCA       (Input - Not used in this version of stack)
    // RB2 <- SFD       (Input - Generates interrupt on falling edge)
    // RB3 <- FIFOP     (Input - Used to detect overflow)
    // RC0 -> CSn       (Output - to select CC2420 SPI slave)
    // RC1 -> VREG_EN   (Output - to enable CC2420 voltage regulator)
    // RC2 -> RESETn    (Output - to reset CC2420)
    // RC3 - > SCK      (Output - SPI Clock to CC2420)
    // RC4 <- SO        (Input - SPI data from CC2420)
    // RC5 -> SI        (Output - SPI data to CC2420)

    // Make PORTB as input - this is the RESET default
    TRISB = 0xff;

    // Set PORTC control signal direction and initial states
    // Start with CC2420 disabled and not selected
    LATC = 0xfd;

    // Set the SPI module for use by Stack
    TRISC = 0xD0;

    // Set the SPI module
    SSPSTAT = 0xC0;
    SSPCON1 = 0x20;


    // D1 and D2 are on RA0 and RA1 respectively, and CS of TC77 is on RA2.
    // Make PORTA as digital I/O.
    // The TC77 temp sensor CS is on RA2.
    ADCON1 = 0x0F;

    // Deselect TC77 (RA2)
    LATA = 0x04;

    // Make RA0, RA1, RA2 and RA4 as outputs.
    TRISA = 0xE0;


    // Enable global interrupts.
    GIEL = 1;
    GIEH = 1;
}


#pragma interruptlow LowISR
void LowISR(void)
{
    return;
}


// NOTE: Several PICs, including the PIC18F4620 revision A3 have a RETFIE FAST/MOVFF bug
// The interruptlow keyword is used to work around the bug when using C18
#if defined(MCHP_C18)
    #pragma interruptlow HighISR
    void HighISR(void)
#elif defined(HITECH_C18)
    void interrupt HighISR(void)
#else
    void HighISR(void)
#endif
{
    // This is a quick way to display MAC TX/RX status on RD1 LED.
    // TMR0IF is also used by TickUpdate, so we will let it clear the flag.
    if ( !appFlags.bits.bInConfigMode )
    {
       if ( TMR0IF )
            D2 = 0;
            
       if(ConsoleIsGetReady())
       {
    	    if(sendCount < 19)
    	    {
    		sendBuf[sendCount] = ConsoleGet();
    		ConsolePutString(sendBuf);
    		//ConsolePut(sendBuf[sendCount]);
    		sendCount++;
    		getCmd = 1;	
    	    }
       }
    }

    // Must call this so that MAC module can determine if a transmission is in progress or not.
    MACISR();

    // Must call this so that tick manager can update its tick count.
    TickUpdate();

    // Is this a interrupt-on-change interrupt?
    if ( RBIF == 1 )
    {
        // Clear mismatch condition and flag
        if ( S2 == 0 )
            appFlags.bits.S2Toggled = TRUE;

        // Clear mis-match condition and reset the interrupt flag
        LATB = PORTB;
        RBIF = 0;

    }

}

#if defined(MCHP_C18)
#pragma code highVector=0x08
void HighVector (void)
{
    _asm goto HighISR _endasm
}
#pragma code /* return to default code section */
#endif

#if defined(MCHP_C18)
#pragma code lowhVector=0x18
void LowVector (void)
{
    _asm goto LowISR _endasm
}
#pragma code /* return to default code section */
#endif



/*
 * ConfigTask is a blocking task that is executed when micro is reset with S3 pushed down.
 * To indicate that config mode is entered, RD0 LED is turned ON.
 *
 * Once in config mode, you would see config menu on RS232 terminal. Use 19200, 8-N-1.
 * When micro is first programmed, you must set its node id. Without valid node id,
 * board will automatically enter into config mode. Once a node id is set, board will
 * only enter into config mode if S3 was pushed down on startup.
 *
 */
static void ConfigTask(void)
{
    MENU_CMD c;

    ConsolePutROMString(menu);

    // Turn D1 & D2 ON to indicate that we are in config mode.
    D1 = 1;
    D2 = 1;

    // Wait for S3 to get released
    while( S3 == 0 );

    while( 1)
    {
        // Check to see if there was any key press in terminal.
        if ( ConsoleIsGetReady() )
        {
            // Get the key value.
            c = ConsoleGet();

            // If this is a valid command, execute it.
            if ( ExecuteMenuChoice(c) )
                // After completion of every menu command or invalid command
                // redisplay the menu message.
                ConsolePutROMString(menu);

            else // Quit command is executed
            {
                // Display message and quit
                ConsolePutROMString(exitMsg);
                break;
            }

        }
    }


    // Turn OFF D1 & D2 to indicate that we are leaving config mode.
    D1 = 0;
    D2 = 0;

}



static BOOL ExecuteMenuChoice(MENU_CMD choice)
{
    LONG_ADDR longAddr;
    BYTE *p;
    char idString[5];
    WORD_VAL idValue;

    switch(choice)
    {
    case MENU_CMD_SET_ID:
        // Node id is combined with Microchip OUI to create 8-byte MAC address.
        p = &longAddr.v[7];

        // Preset the OUI and three other bytes.
        *p-- = MAC_LONG_ADDR_BYTE7;
        *p-- = MAC_LONG_ADDR_BYTE6;
        *p-- = MAC_LONG_ADDR_BYTE5;
        *p-- = 0x00;
        *p-- = 0x00;
        *p-- = 0x00;

        // Prompt the user.
        ConsolePutROMString(idEntryMsg);
        if ( ConsoleGetString(idString, sizeof(idString)-1) )
        {
            // Convert string to 16-bit integer
            idValue.Val = (WORD)atoi(idString);
            // Make sure that it is a valid number.
            if ( idValue.Val > 0xfffe )
            {
                ConsolePutROMString(invalidValueMsg);

                // Wait for user to acknowledge the message.
                while ( !ConsoleIsGetReady() );
            }
            else
            {

                // Save it locally
                *p-- = idValue.byte.MSB;
                *p = idValue.byte.LSB;

                // Now that we have complete board id, save the info into nonvolatile memory.
                SaveMACLongAddr(&longAddr);

            }
        }

        else
        {
            ConsolePutROMString(invalidValueMsg);
        }

        break;


    case MENU_CMD_CLEAR_NEIGHBOR_TABLE:
        ClearNeighborTable();
        break;

    case MENU_CMD_CLEAR_BINDING_TABLE:
        ClearBindingTable();
        break;

    case MENU_CMD_NEXT_CHANNEL:
        MACSetNextChannel();
        break;

    case MENU_CMD_UNMODULATED_OP:
        MACTxUnmodulatedCarrier();
        ConsolePutROMString(testTxMsg);

        // Must reset to recover from this command
        while(1);
        break;

    case MENU_CMD_RANDOM_OP:
        MACTxTestPattern();
        ConsolePutROMString(testTxMsg);

        // Must reset to recover from this command
        while(1);
        break;

    case MENU_CMD_QUIT:
        return FALSE;

    }

    return TRUE;
}

// TestTC77 is designed to do a power on test to ensure that a Microchip TC77
// temperature sensor is installed correctly and working on the PICDEM Z board.
// An actual application may wish to remove this function to save code space.
static void TestTC77(void)
{
#if !defined(WIN32)
    BYTE v;

    ConsolePutROMString(tc77TestingMsg);

    // Select TC77
    RA2 = 0;

    // To read data from TC77, we need to write dummy data
    SSPBUF = 0x00;

    // Now wait for response from TC77.
    while( !SSPBF );

    // Deselect TC77
    RA2 = 1;

    // Now read the same register to read the response
    v = SSPBUF;

    // For our test purpose, make sure that we read something other than 0x00
    if ( v == 0x00 || v == 0xff )
        ConsolePutROMString(tc77ProblemMsg);

    else
        ConsolePutROMString(tc77TestSuccessMsg);
#endif
}









⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -