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怎么在协议栈的发送端写外部中断呢?

在裸机程序中,测试中断完全可行,当移植到协议栈时,sampleapp.c中写外部中断根本进不去怎么办呢?

  • 请问您现在使用的是哪个中断?能否贴一下您的代码?


  • 使用的是外部中断,P0_5口下降沿触发
    进入中断count计数,send函数发送count
    #include "OSAL.h"
    #include "ZGlobals.h"
    #include "AF.h"
    #include "aps_groups.h"
    #include "ZDApp.h"

    #include "SampleApp.h"
    #include "SampleAppHw.h"

    #include "OnBoard.h"

    /* HAL */
    #include "hal_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    #include "hal_mcu.h"
    #include "MT_UART.h" //此处用于串口

    #include "LCD.h"
    #include <ioCC2530.h>
    #include "adc.h"


    #define uint unsigned int
    #define uchar unsigned char

    /*********************************************************************
    * MACROS
    */

    /*********************************************************************
    * CONSTANTS
    */

    /*********************************************************************
    * TYPEDEFS
    */

    /*********************************************************************
    * GLOBAL VARIABLES
    */

    // This list should be filled with Application specific Cluster IDs.
    const cId_t SampleApp_ClusterList[SAMPLEAPP_MAX_CLUSTERS] =
    {
    SAMPLEAPP_PERIODIC_CLUSTERID,
    SAMPLEAPP_FLASH_CLUSTERID
    };

    const SimpleDescriptionFormat_t SampleApp_SimpleDesc =
    {
    SAMPLEAPP_ENDPOINT, // int Endpoint;
    SAMPLEAPP_PROFID, // uint16 AppProfId[2];
    SAMPLEAPP_DEVICEID, // uint16 AppDeviceId[2];
    SAMPLEAPP_DEVICE_VERSION, // int AppDevVer:4;
    SAMPLEAPP_FLAGS, // int AppFlags:4;
    SAMPLEAPP_MAX_CLUSTERS, // uint8 AppNumInClusters;
    (cId_t *)SampleApp_ClusterList, // uint8 *pAppInClusterList;
    SAMPLEAPP_MAX_CLUSTERS, // uint8 AppNumInClusters;
    (cId_t *)SampleApp_ClusterList // uint8 *pAppInClusterList;
    };

    // This is the Endpoint/Interface description. It is defined here, but
    // filled-in in SampleApp_Init(). Another way to go would be to fill
    // in the structure here and make it a "const" (in code space). The
    // way it's defined in this sample app it is define in RAM.
    endPointDesc_t SampleApp_epDesc;

    /*********************************************************************
    * EXTERNAL VARIABLES
    */

    /*********************************************************************
    * EXTERNAL FUNCTIONS
    */

    /*********************************************************************
    * LOCAL VARIABLES
    */
    uint8 SampleApp_TaskID; // Task ID for internal task/event processing
    // This variable will be received when
    // SampleApp_Init() is called.
    devStates_t SampleApp_NwkState;

    uint8 SampleApp_TransID; // This is the unique message ID (counter)

    afAddrType_t SampleApp_Periodic_DstAddr;
    afAddrType_t SampleApp_Flash_DstAddr;

    afAddrType_t Point_To_Point_DstAddr;//网蜂点对点通信定义

    aps_Group_t SampleApp_Group;

    uint8 SampleAppPeriodicCounter = 0;
    uint8 SampleAppFlashCounter = 0;

    /*********************************************************************
    * LOCAL FUNCTIONS
    */
    void SampleApp_HandleKeys( uint8 shift, uint8 keys );
    void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
    void SampleApp_SendPeriodicMessage( void );
    void SampleApp_SendFlashMessage( uint16 flashTime );
    void SampleApp_SendPointToPointMessage(void );
    void Delay(uint); //延时函数声明
    void Initial(void); //初始化函数声明
    void InitKey(void); //初始化按键函数声明
    uchar KeyScan(void); //按键扫描函数声明

    uint count = 0 ; //定义变量记录按键次数
    void Delay(uint n)
    {
    uint i;
    for(i = 0;i<n;i++);
    for(i = 0;i<n;i++);
    for(i = 0;i<n;i++);
    for(i = 0;i<n;i++);
    for(i = 0;i<n;i++);
    }

    /****************************
    //中断处理函数
    *****************************/
    HAL_ISR_FUNCTION(P0_ISR,P0INT_VECTOR)
    {
    if(P0IFG>0) //中断
    {
    P0IFG = 0;
    Delay(100);
    if(P0IFG==0) //中断
    {
    Delay(100);
    count++; //每次中断发生时记录按键次数加1
    }
    }

    P0IF = 0; //清中断标志
    }

    /*********************************************************************
    * NETWORK LAYER CALLBACKS
    */

    /*********************************************************************
    * PUBLIC FUNCTIONS
    */

    /*********************************************************************
    * @fn SampleApp_Init
    *
    * @brief Initialization function for the Generic App Task.
    * This is called during initialization and should contain
    * any application specific initialization (ie. hardware
    * initialization/setup, table initialization, power up
    * notificaiton ... ).
    *
    * @param task_id - the ID assigned by OSAL. This ID should be
    * used to send messages and set timers.
    *
    * @return none
    */
    void SampleApp_Init( uint8 task_id )
    {
    SampleApp_TaskID = task_id;
    SampleApp_NwkState = DEV_INIT;
    SampleApp_TransID = 0;

    MT_UartInit();//串口初始化
    MT_UartRegisterTaskID(task_id);//登记任务号
    HalUARTWrite(0,"Hello World\n",12); //(串口0,'字符',字符个数。)

    /******光敏电阻电路初始化******/
    P0INP |= 0x20; //上拉
    P0IEN |= 0X20; //P0_4设置为中断方式
    PICTL |= 0X01; //下降沿触发
    EA = 1;
    IEN1 |= 0X20; // P0_4设置为中断方式;
    P0IFG |= 0x00; //初始化中断标志位
    P2DIR &= ~0x00;
    LCD_Init(); //oled 初始化
    LCD_CLS(); //屏全亮


    // 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().

    #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 )
    // 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)AddrBroadcast;
    SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
    SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;

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

    // 网蜂点对点通讯定义
    Point_To_Point_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;//点播
    Point_To_Point_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
    Point_To_Point_DstAddr.addr.shortAddr = 0x0000; //发给协调器


    // 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 );

    // 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 );
    aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group );

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

    /*********************************************************************
    * @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 SampleApp_ProcessEvent( uint8 task_id, uint16 events )
    {
    afIncomingMSGPacket_t *MSGpkt;
    (void)task_id; // Intentionally unreferenced parameter

    if ( events & SYS_EVENT_MSG )
    {
    MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
    while ( MSGpkt )
    {
    switch ( MSGpkt->hdr.event )
    {
    // Received when a key is pressed
    case KEY_CHANGE:
    SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
    break;

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

    // Received whenever the device changes state in the network
    case ZDO_STATE_CHANGE:
    SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
    if ( //(SampleApp_NwkState == DEV_ZB_COORD)|| //协调器不给自己点播
    (SampleApp_NwkState == DEV_ROUTER)
    || (SampleApp_NwkState == DEV_END_DEVICE) )
    {
    // Start sending the periodic message in a regular interval.
    osal_start_timerEx( SampleApp_TaskID,
    SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
    SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );
    }
    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 );
    }

    // return unprocessed events
    return (events ^ SYS_EVENT_MSG);
    }

    // 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();//周期性发送函数
    SampleApp_SendPointToPointMessage();//此处替换成点播函数

    // 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)) );

    // return unprocessed events
    return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
    }

    // Discard unknown events
    return 0;
    }

    /*********************************************************************
    * Event Generation Functions
    */
    /*********************************************************************
    * @fn SampleApp_HandleKeys
    *
    * @brief Handles all key events for this device.
    *
    * @param shift - true if in shift/alt.
    * @param keys - bit field for key events. Valid entries:
    * HAL_KEY_SW_2
    * HAL_KEY_SW_1
    *
    * @return none
    */
    void SampleApp_HandleKeys( uint8 shift, uint8 keys )
    {
    (void)shift; // Intentionally unreferenced parameter

    if ( keys & HAL_KEY_SW_1 )
    {
    /* This key sends the Flash Command is sent to Group 1.
    * This device will not receive the Flash Command from this
    * device (even if it belongs to group 1).
    */
    SampleApp_SendFlashMessage( SAMPLEAPP_FLASH_DURATION );
    }

    if ( keys & HAL_KEY_SW_2 )
    {
    /* The Flashr Command is sent to Group 1.
    * This key toggles this device in and out of group 1.
    * If this device doesn't belong to group 1, this application
    * will not receive the Flash command sent to group 1.
    */
    aps_Group_t *grp;
    grp = aps_FindGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP );
    if ( grp )
    {
    // Remove from the group
    aps_RemoveGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP );
    }
    else
    {
    // Add to the flash group
    aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group );
    }
    }
    }

    /*********************************************************************
    * LOCAL FUNCTIONS
    */

    /*********************************************************************
    * @fn SampleApp_MessageMSGCB
    *
    * @brief Data message processor callback. This function processes
    * any incoming data - probably from other devices. So, based
    * on cluster ID, perform the intended action.
    *
    * @param none
    *
    * @return none
    */
    void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
    {
    uint16 flashTime;

    switch ( pkt->clusterId )
    {
    case SAMPLEAPP_POINT_TO_POINT_CLUSTERID:

    HalUARTWrite(0,&pkt->cmd.Data[0],4); //ASCII码发给PC机
    HalUARTWrite(0,"\n",1); //ASCII码发给PC机

    LCD_Init(); //oled 初始化
    LCD_CLS(); //屏全亮
    LCD_welcome();
    LCD_P8x16Str(16*2, 2,&pkt->cmd.Data[0]);

    break;

    case SAMPLEAPP_FLASH_CLUSTERID:
    flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2] );
    HalLedBlink( HAL_LED_4, 4, 50, (flashTime / 4) );
    break;
    }
    }

    /*********************************************************************
    * @fn SampleApp_SendPeriodicMessage
    *
    * @brief Send the periodic message.
    *
    * @param none
    *
    * @return none
    */
    void SampleApp_SendPeriodicMessage( void )
    {
    uint8 data[10]={'0','1','2','3','4','5','6','7','8','9'};//自定义数据
    if ( AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc,
    SAMPLEAPP_PERIODIC_CLUSTERID,
    10,//字节数
    data,//指针头
    &SampleApp_TransID,
    AF_DISCV_ROUTE,
    AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
    {
    }
    else
    {
    // Error occurred in request to send.
    }
    }

    /*********************************************************************
    * @fn SampleApp_SendFlashMessage
    *
    * @brief Send the flash message to group 1.
    *
    * @param flashTime - in milliseconds
    *
    * @return none
    */
    void SampleApp_SendFlashMessage( uint16 flashTime )
    {
    uint8 buffer[3];
    buffer[0] = (uint8)(SampleAppFlashCounter++);
    buffer[1] = LO_UINT16( flashTime );
    buffer[2] = HI_UINT16( flashTime );

    if ( AF_DataRequest( &SampleApp_Flash_DstAddr, &SampleApp_epDesc,
    SAMPLEAPP_FLASH_CLUSTERID,
    3,
    buffer,
    &SampleApp_TransID,
    AF_DISCV_ROUTE,
    AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
    {
    }
    else
    {
    // Error occurred in request to send.
    }
    }

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

    void SampleApp_SendPointToPointMessage( void )
    {

    if(P2_0==0)
    {
    Delay(100);
    if(P2_0==0)
    {
    count=0;
    LCD_Init(); //oled 初始化
    LCD_CLS();
    LCD_P8x16Str(16, 2, "000");
    }
    }
    unsigned char adc[4]=0; //adc采样字符串
    adc[0]=count/100+'0';
    adc[1]=count%100/10+'0';
    adc[2]=count%10+'0';
    adc[3]='\0';
    LCD_Init(); //oled 初始化
    LCD_CLS();
    LCD_P8x16Str(16, 2, adc);
    if ( AF_DataRequest( &Point_To_Point_DstAddr,
    &SampleApp_epDesc,
    SAMPLEAPP_POINT_TO_POINT_CLUSTERID,
    4,
    adc,
    &SampleApp_TransID,
    AF_DISCV_ROUTE,
    AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )

    {
    }
    else
    {
    // Error occurred in request to send.
    }


    }