This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2530: zigbee

Part Number: CC2530

现在有一个变量 h,我想计算每分钟 h 增加的数量,用什么可以实现每分钟使 h 清零重新计数?有简单的方法吗?

  • 用osal_startTimeEx啟動一個每分钟的event去使 h 清零重新计数就可以

  • 如Yikai所说,你可以开启一个定期的event来实现定期清零计数

  • 我加上去并没有实现,不知道错在哪,我需要把变量n定时清零,重新计数(黄色背景部分)。

    uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )
    {
         uint8 Display_Buf[6] = {0};
         uint16 shortAddr  = 0;
          uint16 shortAddrNew  = 0; 
          uint16 PANID=0;
          uint16 parentshortAddr  = 0;
          uint16 parentshortAddrNew  = 0;
          

    //      uint16 h;
          

      afIncomingMSGPacket_t *MSGpkt;
      
      (void)task_id;  // Intentionally unreferenced parameter
      
     

      if ( events & SYS_EVENT_MSG )    /*系统消息事件*/
      {
        
        MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
        while ( MSGpkt )  /*调用数据接收函数接收数据.2*/
        {

          
          switch ( MSGpkt->hdr.event )    /*判断具体事件*/
          {        
            // Received when a key is pressed
            case KEY_CHANGE:
              SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
              break;   /*如果为按键事件,调用案件处理函数.3*/

            // Received when a messages is received (OTA) for this endpoint
            case AF_INCOMING_MSG_CMD:
              HAL_TOGGLE_LED1();   /*?*/
              SampleApp_MessageMSGCB( MSGpkt );
              
              n=n+1;
               m=n;
               osal_start_timerEx(SampleApp_TaskID,CLEAR_EVT,CLEAR_EVENT_TIMEOUT);
               
               num_to_str(m,Display_Buf, 4 , 16); 
               Gui_DrawFont_GBK16(48,96,BLACK,YELLOW,Display_Buf);
               
               if (m>0x20)
                {
                  void leaveNetBySelf(void);
                   {
                     NLME_LeaveReq_t leaveReq;
                     osal_memset((uint8 *)&leaveReq,0,sizeof(NLME_LeaveReq_t));
                     osal_memcpy(leaveReq.extAddr,NLME_GetExtAddr(),Z_EXTADDR_LEN);
    //               zgWriteStartupOptions(ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE);
    //               SystemReset();
                     leaveReq.removeChildren = 1;
                     leaveReq.rejoin = 0;
                     leaveReq.silent = 0;
                     Delay_3000us();
                     SystemReset();
                     
    //                 osal_start_timerEx(SampleApp_TaskID,SAMPLEAPP_SEND_PERIODIC_MSG_EVT,15000);  //使m定时清零
    //                 m=0;
    //               HalLedBlink (HAL_LED_1, 0, 50, 2000 ); 
    //               if( NLME_LeaveReq(&leaveReq)!=afStatus_SUCCESS )
    //               {
    //               ZDApp_LeaveReset(false);
    //               }
                   }
                 }
               break;    /*如果为数据接收事件,调用数据接收函数.3*/

     if ( events & CLEAR_EVT )
      {  n=0;
         osal_start_timerEx(SampleApp_TaskID,CLEAR_EVT,CLEAR_EVENT_TIMEOUT);
         return (events & CLEAR_EVT);
      }

  • 你在每次收到AF_INCOMING_MSG_CMD 就調用osal_start_timerEx去啟動CLEAR_EVENT_TIMEOUT應該不大對吧,不是應該在init的時候調用?

  • 我加入到这里,发现清零后计数到1,就一直不变了,这是哪里的问题,(相关代码黄色背景部分)谢前辈解答

    void SampleApp_Init( uint8 task_id )
    {


    //点对点通信
    Point_To_Point_DstAddr.addrMode=(afAddrMode_t)Addr16Bit;
    Point_To_Point_DstAddr.endPoint=SAMPLEAPP_ENDPOINT;
    Point_To_Point_DstAddr.addr.shortAddr=0x0000;




    SampleApp_TaskID = task_id; /*任务ID*/
    SampleApp_NwkState = DEV_INIT; /*网络状态*/
    SampleApp_TransID = 0; /*传输序列号*/

    uint8 Display_Buf[6] = {0};
    halUARTCfg_t uartConfig;
    // MT_UartInit(); //串口初始化
    uartConfig.configured = TRUE; // 2x30 don't care - see uart driver.
    uartConfig.baudRate = HAL_UART_BR_38400;
    uartConfig.flowControl = FALSE;
    uartConfig.flowControlThreshold = 48; // 2x30 don't care - see uart driver.
    uartConfig.rx.maxBufSize = 128; // 2x30 don't care - see uart driver.
    uartConfig.tx.maxBufSize = 128; // 2x30 don't care - see uart driver.
    uartConfig.idleTimeout = 6; // 2x30 don't care - see uart driver.
    uartConfig.intEnable = TRUE; // 2x30 don't care - see uart driver.
    uartConfig.callBackFunc = uartRxCB;
    HalUARTOpen (0, &uartConfig);
    MT_UartRegisterTaskID(task_id); //注册串口任务
    // HalUARTWrite(0,"UartInit OK\n", sizeof("UartInit OK\n"));//提示信息

    // Device hardware initialization can be added here or in main() (Zmain.c).
    // If the hardware is application specific - add it here.
    // If the hardware is other parts of the device add it in main().

    dsp_single_colour(YELLOW);
    Line_Dsp_single_colour(0,0,128,32,CYAN);

    //向lcd输出相关信息
    Gui_DrawFont_GBK16(0,0,RED,CYAN, " ZigBee topology ");
    Gui_DrawFont_GBK16(0,32,BLACK,YELLOW,"Panid: ");

    #if defined ( BUILD_ALL_DEVICES )
    // The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START
    // We are looking at a jumper (defined in SampleAppHw.c) to be jumpered
    // together - if they are - we will start up a coordinator. Otherwise,
    // the device will start as a router.
    if ( readCoordinatorJumper() )
    zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR;
    else
    zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER;
    #endif // BUILD_ALL_DEVICES

    #if defined ( HOLD_AUTO_START ) /*ZDO初始化*/
    HOLD_AUTO_START is a compile option that will surpress ZDApp
    from starting the device and wait for the application to
    start the device.
    ZDOInitDevice(0);
    #endif

    // Setup for the periodic message's destination address
    // Broadcast to everyone
    SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
    SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
    SampleApp_Periodic_DstAddr.addr.shortAddr = 0x0000; /*以上三行为数据广播地址初始化*/

    // Setup for the flash command's destination address - Group 1
    SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup;
    SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
    SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP; /*以上三行为组播地址初始化*/

    // Fill out the endpoint description.
    SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT;
    SampleApp_epDesc.task_id = &SampleApp_TaskID;
    SampleApp_epDesc.simpleDesc
    = (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc;
    SampleApp_epDesc.latencyReq = noLatencyReqs; /*以上为端点信息初始化*/

    // Register the endpoint description with the AF
    afRegister( &SampleApp_epDesc ); /*注册端点至AF层*/

    // Register for all key events - This app will handle all key events
    RegisterForKeys( SampleApp_TaskID ); /*注册按键*/

    // By default, all devices start out in Group 1
    SampleApp_Group.ID = 0x0001;
    osal_memcpy( SampleApp_Group.name, "Group 1", 7 ); /*组名设置为group 1*/
    aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group ); /*将组信息添加至aps层*/ /*组信息初始化.3*/

    osal_start_timerEx(SampleApp_TaskID,CLEAR_EVT,CLEAR_EVENT_TIMEOUT);

    #if defined ( LCD_SUPPORTED )
    HalLcdWriteString( "SampleApp", HAL_LCD_LINE_1 );
    #endif
    }

    void Delay_3000us(void) //10 us延时
    {
    MicroWait(3000);
    }

    /*如果定义了LCD,LCD显示.4*/

    /*********************************************************************
    * @fn SampleApp_ProcessEvent
    *
    * @brief Generic Application Task event processor. This function
    * is called to process all events for the task. Events
    * include timers, messages and any other user defined events.
    *
    * @param task_id - The OSAL assigned task ID.
    * @param events - events to process. This is a bit map and can
    * contain more than one event.
    *
    * @return none
    */

    uint16 i = 0;
    uint16 h;
    uint16 j = 0;
    uint16 s;
    uint16 n = 0;
    uint16 m;

    uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )
    {
    uint8 Display_Buf[6] = {0};
    uint16 shortAddr = 0;
    uint16 shortAddrNew = 0;
    uint16 PANID=0;
    uint16 parentshortAddr = 0;
    uint16 parentshortAddrNew = 0;

    // uint16 h;

    afIncomingMSGPacket_t *MSGpkt;

    (void)task_id; // Intentionally unreferenced parameter

    if ( events & SYS_EVENT_MSG ) /*系统消息事件*/
    {

    MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
    while ( MSGpkt ) /*调用数据接收函数接收数据.2*/
    {


    switch ( MSGpkt->hdr.event ) /*判断具体事件*/
    {
    // Received when a key is pressed
    case KEY_CHANGE:
    SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
    break; /*如果为按键事件,调用案件处理函数.3*/

    // Received when a messages is received (OTA) for this endpoint
    case AF_INCOMING_MSG_CMD:
    HAL_TOGGLE_LED1(); /*?*/
    SampleApp_MessageMSGCB( MSGpkt );

    n=n+1;
    m=n;

    num_to_str(m,Display_Buf, 4 , 16);
    Gui_DrawFont_GBK16(48,96,BLACK,YELLOW,Display_Buf);

    if (m>0x20)
    {
    void leaveNetBySelf(void);
    {
    NLME_LeaveReq_t leaveReq;
    osal_memset((uint8 *)&leaveReq,0,sizeof(NLME_LeaveReq_t));
    osal_memcpy(leaveReq.extAddr,NLME_GetExtAddr(),Z_EXTADDR_LEN);
    // zgWriteStartupOptions(ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE);
    // SystemReset();
    leaveReq.removeChildren = 1;
    leaveReq.rejoin = 0;
    leaveReq.silent = 0;
    Delay_3000us();
    SystemReset();
    }
    }


    break; /*如果为数据接收事件,调用数据接收函数.3*/

    // Received whenever the device changes state in the network
    case ZDO_STATE_CHANGE:

    if(SampleApp_NwkState == DEV_NWK_ORPHAN)
    {
    void leaveNetBySelf(void);
    {

    //主动离开参考
    NLME_LeaveReq_t leaveReq;
    osal_memset((uint8 *)&leaveReq,0,sizeof(NLME_LeaveReq_t));
    osal_memcpy(leaveReq.extAddr,NLME_GetExtAddr(),Z_EXTADDR_LEN);
    // zgWriteStartupOptions(ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE);
    // SystemReset();
    leaveReq.removeChildren = 1;
    leaveReq.rejoin = 0;
    leaveReq.silent = 0;
    SystemReset();
    // HalLedBlink (HAL_LED_1, 0, 50, 2000 );
    // if( NLME_LeaveReq(&leaveReq)!=afStatus_SUCCESS )
    // {
    // ZDApp_LeaveReset(false);
    // }
    }

    }

    SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);

    if ( (SampleApp_NwkState == DEV_ZB_COORD)
    ||(SampleApp_NwkState == DEV_ROUTER)
    || (SampleApp_NwkState == DEV_END_DEVICE) ) /*如果为网络状态改变事件,则判断网络类型.5*/
    {
    // osal_start_timerEx(SampleApp_TaskID,CLEAR_EVT,15000);

    if(SampleApp_NwkState == DEV_END_DEVICE)
    {
    Gui_DrawFont_GBK16(0,16,RED,CYAN," EndDevice ");
    Gui_DrawFont_GBK16(0,48,BLACK,YELLOW,"ShortAddr: ");
    Gui_DrawFont_GBK16(0,64,BLACK,YELLOW, "Temp:");
    Gui_DrawFont_GBK16(0,80,BLACK,YELLOW, "parentSA:");


    }
    else if(SampleApp_NwkState == DEV_ZB_COORD)
    {
    Gui_DrawFont_GBK16(0,16,RED,CYAN," CooDevice ");
    }
    else
    {
    Gui_DrawFont_GBK16(0,16,RED,CYAN," RouterDevice ");
    Gui_DrawFont_GBK16(0,48,BLACK,YELLOW,"ShortAddr: ");
    Gui_DrawFont_GBK16(0,64,BLACK,YELLOW, "parentSA:");
    }


    if ( (SampleApp_NwkState == DEV_ROUTER)
    || (SampleApp_NwkState == DEV_END_DEVICE) )
    {

    //显示设备地址shortAddr
    zb_GetDeviceInfo(ZB_INFO_SHORT_ADDR, &shortAddr);
    shortAddrNew = LO_UINT16(shortAddr) << 8;
    shortAddrNew = HI_UINT16(shortAddr) |shortAddrNew;
    num_to_str(shortAddrNew, Display_Buf, 4 , 16);
    Gui_DrawFont_GBK16(94,48,BLACK,YELLOW,Display_Buf);


    //显示父节点
    zb_GetDeviceInfo(ZB_INFO_PARENT_SHORT_ADDR, &parentshortAddr);
    parentshortAddrNew = LO_UINT16(parentshortAddr) << 8;
    parentshortAddrNew = HI_UINT16(parentshortAddr) |parentshortAddrNew;
    num_to_str(parentshortAddrNew, Display_Buf, 4 , 16);
    Gui_DrawFont_GBK16(94,80,BLACK,YELLOW,Display_Buf);



    // Start sending the periodic message in a regular interval.
    osal_start_timerEx( SampleApp_TaskID,
    SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
    SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT ); /*调用定时函数,进行定时事件处理.3*/

    }
    //显示PANID
    zb_GetDeviceInfo(ZB_INFO_PAN_ID, &PANID);
    num_to_str(PANID, Display_Buf, 4 , 16);
    Gui_DrawFont_GBK16(94,32,BLACK,YELLOW,Display_Buf);


    }
    else
    {
    // Device is no longer in the network
    }

    break;



    default:
    break;
    }

    // Release the memory
    osal_msg_deallocate( (uint8 *)MSGpkt ); /*释放空间*/

    // Next - if one is available
    MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID ); /*调用数据接收函数接收数据*/
    //以下勿删
    // h=i;
    // num_to_str(h,Display_Buf, 4 , 16);
    // Gui_DrawFont_GBK16(0,96,BLACK,YELLOW,Display_Buf);
    // i++;
    // void leaveNetBySelf(void);
    // {
    // if ( h>0x010)
    // {
    // //主动离开参考
    // NLME_LeaveReq_t leaveReq;
    // osal_memset((uint8 *)&leaveReq,0,sizeof(NLME_LeaveReq_t));
    // osal_memcpy(leaveReq.extAddr,NLME_GetExtAddr(),Z_EXTADDR_LEN);
    //// zgWriteStartupOptions(ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE);
    //// SystemReset();
    // leaveReq.removeChildren = 1;
    // leaveReq.rejoin = 0;
    // leaveReq.silent = 0;
    // SystemReset();
    //// HalLedBlink (HAL_LED_1, 0, 50, 2000 );
    // if( NLME_LeaveReq(&leaveReq)!=afStatus_SUCCESS )
    // {
    //
    //
    // ZDApp_LeaveReset(false);
    // }
    // }
    // }



    }

    // return unprocessed events返回未处理的事件
    return (events ^ SYS_EVENT_MSG);
    }

    if ( events & CLEAR_EVT )
    { n=0;
    osal_start_timerEx(SampleApp_TaskID,CLEAR_EVT,CLEAR_EVENT_TIMEOUT);
    return (events & CLEAR_EVT);
    }

    // Send a message out - This event is generated by a timer发送消息-此事件由计时器生成
    // (setup in SampleApp_Init()).
    if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT ) /*如果为定时事件,进行定时事件处理*/
    {
    // Send the periodic message
    // SampleApp_SendPeriodicMessage();
    // HalUARTWrite(0,"UartInit OK\n", sizeof("UartInit OK\n"));//提示信息



    if(SampleApp_NwkState == DEV_ROUTER)
    {

    //sendDummyReport();
    sendReport();
    osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
    (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) ); /*循环调用定时函数.2*/
    }
    else{
    DHT11_TEST();
    sendReport();

    num_to_str(ucharT_data_H, Display_Buf, 4 , 10 );
    Gui_DrawFont_GBK16(94,64,BLACK,YELLOW, Display_Buf);
    // Setup to send message again in normal period (+ a little jitter)
    osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
    (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) ); /*循环调用定时函数.2*/
    // HAL_TOGGLE_LED1();
    // HAL_TOGGLE_LED2();
    }
    // return unprocessed events
    return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
    }

    // Discard unknown events
    return 0;
    }

  • return (events & CLEAR_EVT);應該要改成return (events ^ CLEAR_EVT);

  • 改完还是这个现象,清零以后,计数数值停留在1

  • 你應該調試看看是不是沒有AF_INCOMING_MSG_CMD導致沒有繼續累加,另外你的CLEAR_EVENT_TIMEOUT是多大?

  • CLEAR_EVENT_TIMEOUT是50秒

  • CLEAR_EVENT_TIMEOUT=50?

  • CLEAR_EVENT_TIMEOUT=50000

  • 1. 調試看看是不是沒有AF_INCOMING_MSG_CMD導致沒有繼續累加

    2. 調試看看CLEAR_EVENT是不是50秒被觸發一次,還是一直被觸發,另外,你CLEAR_EVENT的定義是?

  • 调试我不太会,我再学学,好像一直在循环CLEAR_EVENT,这个该怎么定义?除了.C文件中的那些代码,还需要其他的定义吗?

    #define CLEAR_EVT       0x0002   

  • 一直在循环CLEAR_EVENT你可能要查一下是不是有其他的事件也定義為0x0002了

  • 1、我经过调试发现,运行到 n=0,会在此一直循环运行这一句,有时会跳出来运行一次 n+1,但下一步又会回到 n=0 .

    2、事件号我也改过,还是不行。

  • 解决了解决了,我是代码有些错误,我太马虎了,多谢大佬指点,您是我的榜样!!!