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