qq_35007354 2016-06-02 13:08 采纳率: 100%
浏览 1779

跪求以下zigbee程序协调器S1在哪部分控制电机转动?急急急急急急

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

  • INCLUDES */ #include "OSAL.h" #include "AF.h" #include "ZDApp.h" #include "ZDObject.h" #include "ZDProfile.h"

#include "GenericApp.h"
#include "DebugTrace.h"

#if !defined( WIN32 )
#include "OnBoard.h"
#endif

/* HAL /
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"
/
RTOS /
#if defined( IAR_ARMCM3_LM )
#include "RTOS_App.h"
#endif

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

  • MACROS */ #define LIGHT P0_5 //定义P0.5口为光敏控制端 #define SMOKE P0_6 //定义P0.6口为烟雾控制端 #define LED P1_0 #define A1 P1_4 //定义步进电机连接端口 #define B1 P1_5 #define C1 P1_6 #define D1 P1_7

#define uchar unsigned char
#define uint unsigned int
uint8 motorstatus,CS,CCS,Cmotorstatus;
uchar phasecw[4] ={0x80,0x40,0x20,0x10};//正转 电机导通相序 D-C-B-A
uchar phaseccw[4]={0x10,0x20,0x40,0x80};//反转 电机导通相序 A-B-C-D
static void MotorData(uchar data);
static void MotorCW(void);
static void MotorCCW(void);
static void MotorStop(void);
static void InitIO(void);
static void Delay_MS(uint Time);// ms延时
uint8 AppTitle[] = "ALD2530 Motor"; //应用程序名称
uint8 LedState = 0;
uint8 ucEdDir = 0; //终端1为正转 2为反转
uint8 ucDirection = 1; //1为正转 2为反转
uint8 ucSpeed = 2; //速度2-10之间
uint8 DataBuf[3];
uint8 count,Ccount;
/*********************************************************************

  • MACROS */

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

  • CONSTANTS */

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

  • TYPEDEFS */

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

  • GLOBAL VARIABLES */ // This list should be filled with Application specific Cluster IDs. const cId_t GenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS] = { GENERICAPP_zigbee1 };

const SimpleDescriptionFormat_t GenericApp_SimpleDesc =
{
GENERICAPP_ENDPOINT, // int Endpoint;
GENERICAPP_PROFID, // uint16 AppProfId[2];
GENERICAPP_DEVICEID, // uint16 AppDeviceId[2];
GENERICAPP_DEVICE_VERSION, // int AppDevVer:4;
GENERICAPP_FLAGS, // int AppFlags:4;
GENERICAPP_MAX_CLUSTERS, // byte AppNumInClusters;
(cId_t *)GenericApp_ClusterList, // byte *pAppInClusterList;
GENERICAPP_MAX_CLUSTERS, // byte AppNumInClusters;
(cId_t *)GenericApp_ClusterList // byte *pAppInClusterList;
};

// This is the Endpoint/Interface description. It is defined here, but
// filled-in in GenericApp_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 GenericApp_epDesc;

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

  • EXTERNAL VARIABLES */

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

  • EXTERNAL FUNCTIONS */

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

  • LOCAL VARIABLES */ byte GenericApp_TaskID; // Task ID for internal task/event processing // This variable will be received when // GenericApp_Init() is called. devStates_t GenericApp_NwkState;

byte GenericApp_TransID; // This is the unique message ID (counter)

afAddrType_t GenericApp_DstAddr;

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

  • LOCAL FUNCTIONS */ static void GenericApp_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg ); static void GenericApp_HandleKeys( byte shift, byte keys ); static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pckt ); static void GenericApp_SendTheMessage( void );

#if defined( IAR_ARMCM3_LM )
static void GenericApp_ProcessRtosMessage( void );
#endif
#define LED P1_2
/*********************************************************************

  • NETWORK LAYER CALLBACKS */

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

  • PUBLIC FUNCTIONS */

void GenericApp_Init( uint8 task_id )
{
GenericApp_TaskID = task_id;
GenericApp_NwkState = DEV_INIT;
GenericApp_TransID = 0;
InitIO(); //初始化电机IO引脚
P0DIR&=~(1<<5|1<<6);//P05 P06输入
P1DIR|=0X01;//P10输出
motorstatus=0;//电机状态标志0
//Cmotorstatus=1;
// 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().

//以下三句,是设置终端向协调器单播
GenericApp_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
GenericApp_DstAddr.endPoint = GENERICAPP_ENDPOINT;
GenericApp_DstAddr.addr.shortAddr = 0x0000;

// Fill out the endpoint description.
GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT;
GenericApp_epDesc.task_id = &GenericApp_TaskID;
GenericApp_epDesc.simpleDesc
= (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc;
GenericApp_epDesc.latencyReq = noLatencyReqs;

// Register the endpoint description with the AF
afRegister( &GenericApp_epDesc );

// Register for all key events - This app will handle all key events
RegisterForKeys( GenericApp_TaskID );

// Update the display
#if defined ( LCD_SUPPORTED )
HalLcdWriteString( "GenericApp", HAL_LCD_LINE_1 );
#endif

ZDO_RegisterForZDOMsg( GenericApp_TaskID, End_Device_Bind_rsp );
ZDO_RegisterForZDOMsg( GenericApp_TaskID, Match_Desc_rsp );

#if defined( IAR_ARMCM3_LM )
// Register this task with RTOS task initiator
RTOS_RegisterApp( task_id, GENERICAPP_RTOS_MSG_EVT );
#endif
}

uint16 GenericApp_ProcessEvent( uint8 task_id, uint16 events )
{
uint8 i;
afIncomingMSGPacket_t *MSGpkt;
afDataConfirm_t *afDataConfirm;

// Data Confirmation message fields
byte sentEP;
ZStatus_t sentStatus;
byte sentTransID; // This should match the value sent
(void)task_id; // Intentionally unreferenced parameter

if ( events & SYS_EVENT_MSG )
{
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
while ( MSGpkt )
{
switch ( MSGpkt->hdr.event )
{
case ZDO_CB_MSG:
GenericApp_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
break;

    case KEY_CHANGE://此处是按键事件函数是S1按键按下触发的事件,主要是实现光敏的控制
      GenericApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );//按键触发函数执行
      break;

    case AF_DATA_CONFIRM_CMD:
      // This message is received as a confirmation of a data packet sent.
      // The status is of ZStatus_t type [defined in ZComDef.h]
      // The message fields are defined in AF.h
      afDataConfirm = (afDataConfirm_t *)MSGpkt;
      sentEP = afDataConfirm->endpoint;
      sentStatus = afDataConfirm->hdr.status;
      sentTransID = afDataConfirm->transID;
      (void)sentEP;
      (void)sentTransID;

      // Action taken when confirmation is received.
      if ( sentStatus != ZSuccess )
      {
        // The data wasn't delivered -- Do something
      }
      break;

    case AF_INCOMING_MSG_CMD://有数据接收到,所触发的函数事件
      GenericApp_MessageMSGCB( MSGpkt );//接收到协调器发来的数据,并进行执行。
      break;

    case ZDO_STATE_CHANGE:
      GenericApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
      if ( (GenericApp_NwkState == DEV_ZB_COORD)
          || (GenericApp_NwkState == DEV_ROUTER)
          || (GenericApp_NwkState == DEV_END_DEVICE) )
      {
        // Start sending "the" message in a regular interval.
        osal_start_timerEx( GenericApp_TaskID,
                            GENERICAPP_SEND_MSG_EVT,
                            GENERICAPP_SEND_MSG_TIMEOUT );
      }
      break;

    default:
      break;
  }

  // Release the memory
  osal_msg_deallocate( (uint8 *)MSGpkt );

  // Next
  MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
}

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

}

  • 写回答

5条回答 默认 最新

  • qq_35007354 2016-06-02 13:09
    关注

    // Send a message out - This event is generated by a timer
    // (setup in GenericApp_Init()).
    if ( events & GENERICAPP_SEND_MSG_EVT )
    {
    // Send "the" message
    GenericApp_SendTheMessage();//此处是终端发送烟雾状态值给到协调器的函数
    if(motorstatus%2)//判断此状态值,此状态值是奇数的时候 ,光敏可以控制电机,当其是偶数的时候,不进行控制
    {
    if(LIGHT==0&&CS==0)//此处是判断当前有光,并且CS变量为0的时候,才执行下面的语句,初始化状态下CS=0的
    {
    CS=1;//将CS变量赋值为1,上面的语句就不能执行第二次了。这样就保证了电机只工作一段时间并停止
    CCS=0;//CCS变量为0
    count=0; //时间赋值位0
    }
    if(count<20&&CS)//判断时间值是否小于20并且CS是否为1,此处时间值是0小于20成立,CS上面已经赋值为1,所以成立,执行下面的语句
    {
    count++;//变量不断进行数据加
    for(i=0;i<50;i++)//这里的for循环是实现步进电机给充足的脉冲,让其正常的运转起来
    MotorCCW(); //顺时针转动
    }

    if(LIGHT&&CCS==0)//此处是判断当前没有光,并且CCS变量为0的时候,才执行下面的语句,初始化状态下CCS=0的
    {
      CS=0;//CS变量重新为0,那么最上面的光敏判断就可以进行继续执行了。
     CCS=1;//将CSS变量赋值为1,上面的语句就不能执行第二次了。这样就保证了电机只工作一段时间并停止
      count=0;  //时间赋值位0
    }
      if(count<20&&CCS)//判断时间值是否小于20并且CS是否为1,此处时间值是0小于20成立,CCS上面已经赋值为1,所以成立,执行下面的语句
      {
          count++;//变量不断进行数据加
      for(i=0;i<50;i++)//这里的for循环是实现步进电机给充足的脉冲,让其正常的运转起来
        MotorCW();      //逆时针转动
      }
    
    }
    

    if(Cmotorstatus==0)//此变量是协调器按键S2所赋予的,协调器按键S2按下形成奇偶数,从而判断此值如果是偶数的时候,进行执行。此处的变量值是从下面的接收函数当中获取的,可以结合接收函数来看
    {
    if(ucEdDir==0X01)//如果此时方向为1,那么电机顺时针转动。方向位置也是协调器S1按键所赋予的,此处看接收函数的数据
    {

      if(Ccount<20)//判断当前的变是否小于20,默认情况下是0,所以此语句是真,可以执行
      {
        Ccount++;;//变量不断进行数据加
     for(i=0;i<50;i++)//这里的for循环是实现步进电机给充足的脉冲,让其正常的运转起来
          MotorCW();       //顺时针转动
      }
      else//当变量值超过20之后,10s完成之后
      {
      Ccount=0;//此处的计数变量重新赋值为0,这样在下一次工作的时候,就可以重新计数
      ucEdDir=0;//方向位变为0,这样子上面的IF语句就不能再执行了,那么电机就会工作一段时间之后,停止
      }
    }
    if(ucEdDir==0x02)//如果此时方向为2,那么电机逆时针转动。方向位置也是协调器S1按键所赋予的,此处看接收函数的数据
    {
    
      if(Ccount<20)//判断当前的变是否小于20,默认情况下是0,所以此语句是真,可以执行
      {
        Ccount++;//变量不断进行数据加
          for(i=0;i<50;i++)//这里的for循环是实现步进电机给充足的脉冲,让其正常的运转起来
          MotorCCW();      //逆时针转动
      }
      else
      {
       Ccount=0;//此处的计数变量重新赋值为0,这样在下一次工作的时候,就可以重新计数
          ucEdDir=0;//方向位变为0,这样子上面的IF语句就不能再执行了,那么电机就会工作一段时间之后,停止
      }
    
    }
    }
    else//当S2协调器按键按下形成奇数的时候,此处的电机应该停止
    {
      MotorStop();//执行停止函数
    

    // Ccount=0;
    // ucEdDir=0;
    }
    // Setup to send message again
    osal_start_timerEx( GenericApp_TaskID,
    GENERICAPP_SEND_MSG_EVT,
    GENERICAPP_SEND_MSG_TIMEOUT );//此处是系统调用时间函数,
    、//意思是每GENERICAPP_SEND_MSG_TIMEOUT调用一次此处的函数执行,此处是200ms

    // return unprocessed events
    return (events ^ GENERICAPP_SEND_MSG_EVT);
    

    }

    #if defined( IAR_ARMCM3_LM )
    // Receive a message from the RTOS queue
    if ( events & GENERICAPP_RTOS_MSG_EVT )
    {
    // Process message from RTOS queue
    GenericApp_ProcessRtosMessage();

    // return unprocessed events
    return (events ^ GENERICAPP_RTOS_MSG_EVT);
    

    }
    #endif

    // Discard unknown events
    return 0;
    }

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

    • Event Generation Functions */

    static void GenericApp_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg )
    {
    switch ( inMsg->clusterID )
    {
    case End_Device_Bind_rsp:
    if ( ZDO_ParseBindRsp( inMsg ) == ZSuccess )
    {
    // Light LED
    HalLedSet( HAL_LED_4, HAL_LED_MODE_ON );
    }
    #if defined( BLINK_LEDS )
    else
    {
    // Flash LED to show failure
    HalLedSet ( HAL_LED_4, HAL_LED_MODE_FLASH );
    }
    #endif
    break;

    case Match_Desc_rsp:
      {
        ZDO_ActiveEndpointRsp_t *pRsp = ZDO_ParseEPListRsp( inMsg );
        if ( pRsp )
        {
          if ( pRsp->status == ZSuccess && pRsp->cnt )
          {
            GenericApp_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
            GenericApp_DstAddr.addr.shortAddr = pRsp->nwkAddr;
            // Take the first endpoint, Can be changed to search through endpoints
            GenericApp_DstAddr.endPoint = pRsp->epList[0];
    
            // Light LED
            HalLedSet( HAL_LED_4, HAL_LED_MODE_ON );
          }
          osal_mem_free( pRsp );
        }
      }
      break;
    

    }
    }

    /*********************************************************************
    按键事件触发函数,此处主要是S1按键,
    *

    • @return none
      */
      static void GenericApp_HandleKeys( uint8 shift, uint8 keys )
      {

      if ( keys & HAL_KEY_SW_6 )//S1按键按下,对应的键值是SW_6。协议栈规定的设计
      {
      motorstatus++;//电机状态变量加。就实现S1按键控制光敏继续工作与不工作的状态
      HalLedBlink( HAL_LED_2, 2,50, 500 ); //LED1闪烁提示,表示按键按下
      }

    }

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

    • LOCAL FUNCTIONS */

    static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
    {
    uint8 buf[3];
    uint16 i;
    switch ( pkt->clusterId )
    {
    case GENERICAPP_zigbee1://接收协调器发的数据
    osal_memcpy(buf, pkt->cmd.Data, pkt->cmd.DataLength);//将接收到的数据赋值给一个buf的数组变量进行存储
    if(buf[0] == 0x30) //0x30 正反转标记,S1按键按下此处结合协调器了解到他的数据第一位。来判断后面接收的数据具体是什么功能
    {
    ucEdDir = buf[1]; //获取转动方向,有了转动方向。上面的电机工作函数就可以通过此变量对电机进行正转或者是反转

    }
    else if(buf[0] == 0x31)//0x31 此处是S2按键按下标志,
    {
      Cmotorstatus=buf[1]%2;//buf[1]%2的意思是对buf[1]变量进行对2取余数。.那么获取的变量付给标志位,这样在上面的函数
      //根据此值来判断电机是继续转动还是停止工作。
    

    }
    break;
    }
    }

    /*********************************************************************
    无线数据发送函数,此处是将终端的数据发送协调器
    */
    static void GenericApp_SendTheMessage( void )
    {
    uint8 data;
    if(SMOKE)//判断到当前的P06端口为高电平,表明没有烟雾,
    data=0;//给变量data赋值为0,这样
    else//有烟雾
    data=1;//变量赋值为1,当协调器接收到此数据为1的时候,就闪烁LED灯
    if ( AF_DataRequest( &GenericApp_DstAddr, &GenericApp_epDesc,
    GENERICAPP_zigbee1,
    1 ,
    (byte *)&data,
    &GenericApp_TransID,
    AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
    {//发送一个字节给到协调器,协调器通过判断此数据是1或者是0,来决定是否闪烁LED进行报警。、
    }
    else
    {
    // Error occurred in request to send.
    }
    }

    #if defined( IAR_ARMCM3_LM )

    static void GenericApp_ProcessRtosMessage( void )
    {
    osalQueue_t inMsg;

    if ( osal_queue_receive( OsalQueue, &inMsg, 0 ) == pdPASS )
    {
    uint8 cmndId = inMsg.cmnd;
    uint32 counter = osal_build_uint32( inMsg.cbuf, 4 );

    switch ( cmndId )
    {
      case CMD_INCR:
        counter += 1;  /* Increment the incoming counter */
                       /* Intentionally fall through next case */
    
      case CMD_ECHO:
      {
        userQueue_t outMsg;
    
        outMsg.resp = RSP_CODE | cmndId;  /* Response ID */
        osal_buffer_uint32( outMsg.rbuf, counter );    /* Increment counter */
        osal_queue_send( UserQueue1, &outMsg, 0 );  /* Send back to UserTask */
        break;
      }
    
      default:
        break;  /* Ignore unknown command */    
    }
    

    }
    }
    #endif

    //---------------------------------------------
    //步进电机驱动部分
    static void MotorData(uchar data)
    {
    A1 = 1&(data>>4);
    B1 = 1&(data>>5);
    C1 = 1&(data>>6);
    D1 = 1&(data>>7);
    }

    //顺时针转动
    static void MotorCW(void)
    {
    uchar i;
    for(i=0;i<4;i++)
    {
    MotorData(phasecw[i]);
    Delay_MS(ucSpeed);//转速调节
    }
    }
    //逆时针转动
    static void MotorCCW(void)
    {
    uchar i;
    for(i=0;i<4;i++)
    {
    MotorData(phaseccw[i]);
    Delay_MS(ucSpeed);//转速调节
    }
    }

    //停止转动
    static void MotorStop(void)
    {
    MotorData(0x00);
    }

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

    • 名 称: InitIO()
    • 功 能: 初始化IO口程序
    • 入口参数: 无
    • 出口参数: 无
      ****************************************************************************/
      static void InitIO(void)
      {
      P1SEL &= 0x0F;
      P1DIR |= 0xF0;

      MotorCW();//停止转动
      }

    static void Delay_MS(uint Time)// 1ms延时
    {
    char i;

    while(Time--)
    {
    for(i=0;i<100;i++)
    MicroWait(10);
    }
    }
    /*********************************************************************
    */

    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面