ZigBee wireless lighting example

  light_switch.c code explanation: no matter which experimental code you look at, the first thing to look for is the main function:

void main ( void ) {
    uint8 appMode = NONE; /* The mode of the module is not set */
    /* Config basicRF */
    basicRfConfig.panId = PAN_ID;
    basicRfConfig.channel = RF_CHANNEL;
    basicRfConfig.ackRequest = TRUE;
#ifdef SECURITY_CCM / * key secure communication, this routine does not encrypt*/
    basicRfConfig.securityKey = key;
#endif
    /* Initalise board peripherals Initialize peripherals */
    halBoardInit();
    halJoystickInit();
    /* Initalise hal_rf Initialize the rf of the hardware abstraction layer */
    if ( halRfInit() = = FAILED ) {
        HAL_ASSERT ( FALSE );
    }
    /*-----------Learn the backplane configuration according to WeBee----------*/
    halLedClear ( 2 ); /* Off LED2 */
    halLedClear ( 1 ); /* Off LED1 */
    /*-----Selective download program, sending module and receiving module-----*/
    appSwitch(); /* The node is key S1 and the pin is P0_0 */
    appLight(); /* The node is the indicator LED1 and the pin is P1_0 */
    /* Role is undefined. This code should not be reached */
    HAL_ASSERT ( FALSE );
}
  • Lines 18 to 19: close LED2 and LED1 of the WeBee backplane. halLedSet(x) is to turn the lamp on and halLedClear(x) is to turn the lamp off.
  • Lines 21 to 22: select one line and block the other. One is to realize the function of transmitting key information, the other is to receive key information and change LED status, which are Basic RF transmission and reception respectively. Different modules choose different functions when burning programs.

Note that the program will loop or wait in the appSwitch function or appLight function and will not execute to line 26.
  next, look at the appSwitch function:

static void appSwitch() {
#ifdef ASSY_EXP4618_CC2420
    halLcdClearLine ( 1 );
    halLcdWriteSymbol ( HAL_LCD_SYMBOL_TX, 1 );
#endif
    /* Initialize BasicRF */
    basicRfConfig.myAddr = SWITCH_ADDR;

    if ( basicRfInit ( &basicRfConfig ) == FAILED ) {
        HAL_ASSERT ( FALSE );
    }

    pTxData[0] = LIGHT_TOGGLE_CMD;
    /* Keep Receiver off when not needed to save power */
    basicRfReceiveOff();

    /* Main loop */
    while ( TRUE ) { /* The program enters an endless loop */
        if ( halButtonPushed() == HAL_BUTTON_1 ) { /* Key S1 is pressed */
            basicRfSendPacket ( LIGHT_ADDR, pTxData, APP_PAYLOAD_LENGTH );
            /* Put MCU to sleep. It will wake up on joystick interrupt */
            halIntOff();
            halMcuSetLowPowerMode ( HAL_MCU_LPM_3 ); /* Will turn on global */
            /* interrupt enable */
            halIntOn();
        }
    }
}
  • Lines 2 to 5: we don't care about the definition of the LCD module on the TI learning board.
  • Lines 7 to 11: initialization in Basic RF startup is the third step of Basic RF startup mentioned above.
  • Line 13: Basic RF transmission step 1: put the data or command to be transmitted into a data buffer, where the command to change the lamp state is light_ TOGGLE_ Put CMD into pTxData.
  • Line 15: since the module only needs to transmit, the reception is shielded to reduce power consumption.
  • Line 19: if(halButtonPushed() == HAL_BUTTON_1) judge whether the key S1 is pressed. The halButtonPushed function is in halButton.c. its function is: when the key S1 is pressed, it returns true, and then enters basicrfsendpacket (light_add, ptxdata, app_payload_length);.
  • Line 20: the second step of Basic RF transmission is also the most critical step of transmitting data. The function has been described above. basicRfSendPacket(LIGHT_ADDR, pTxData, APP_PAYLOAD_LENGTH) will light_ ADDR,pTxData,APP_ PAYLOAD_ The argument of length is written as basicRfSendPacket(0xBEEF, pTxData[0], 1). In fact, it sends the command with byte length of 1 to address 0xBEEF.
  • Lines 22 to 23: there is no joystick (multi-directional key) on the WeBee development board, so ignore it.
  • Line 25: enable interrupt.

   after explaining the sent appSwitch, go to the receiving function appLight:

static void appLight() {
    /*-----------------------------------------
    halLcdWriteLine(HAL_LCD_LINE_1, "Light");
    halLcdWriteLine(HAL_LCD_LINE_2, "Ready");
    -----------------------------------------*/
#ifdef ASSY_EXP4618_CC2420
    halLcdClearLine ( 1 );
    halLcdWriteSymbol ( HAL_LCD_SYMBOL_RX, 1 );
#endif
    /* Initialize BasicRF */
    basicRfConfig.myAddr = LIGHT_ADDR;
    if ( basicRfInit ( &basicRfConfig ) == FAILED ) {
        HAL_ASSERT ( FALSE );
    }
    basicRfReceiveOn();
    /* Main loop */
    while ( TRUE ) {
        while ( !basicRfPacketIsReady() );
        if ( basicRfReceive ( pRxData, APP_PAYLOAD_LENGTH, NULL ) > 0 ) {
            if ( pRxData[0] == LIGHT_TOGGLE_CMD ) {
                halLedToggle ( 1 );
            }
        }
    }
}
  • Lines 7 to 9: This is the content displayed by LCD. Ignore it for the time being.
  • Lines 11 to 15: initialization in Basic RF startup, step 3 of Basic RF startup above.
  • Line 17: the function basicfreceiveon turns on the wireless reception function. After calling this function, the module will always receive unless basicfreceiveoff is called again to turn off the reception.
  • Line 20: the program starts a cycle of continuous scanning.
  • Line 21: Step 1 of Basic RF reception, while (! Basicrfpacketisread()) check whether the upper layer data is received.
  • Line 23: Step 2 of Basic RF reception, if (basicfreceive (prxdata, app_payload_length, null) > 0) judge whether valid data is received.
  • Line 24: if(pRxData[0] == LIGHT_TOGGLE_CMD) judge whether the received data is the light in the sending function_ TOGGLE_ CMD. If so, proceed to line 25.
  • Line 25: halLedToggle(1) changes the state of Led1.

   after burning, power on and press the S1 key of the transmitting module to see that the LED1 of the receiving module is lit.

Keywords: zigbee

Added by JoeZ on Sun, 26 Sep 2021 08:18:44 +0300