详情看代码吧
单片机源程序如下:
- #include "OSAL.h"
- #include "AF.h"
- #include "ZDApp.h"
- #include "ZDObject.h"
- #include "ZDProfile.h"
- #include "Common.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"
- #include "OSAL_Nv.h"
- const cId_t SampleApp_ClusterList[SampleApp_MAX_CLUSTERS] =
- {
- REPORT_CLUSTER_ID
- };
-
- 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, // byte AppNumInClusters;
- (cId_t *)SampleApp_ClusterList, // byte *pAppInClusterList;
- 0, // byte AppNumInClusters;
- (cId_t *)NULL // (cId_t *)NULL byte *pAppInClusterList;
- };
- //static uint8 SerialApp_TxLen;
- endPointDesc_t SampleApp_epDesc;
- uint8 SampleApp_TaskID;
- devStates_t SampleApp_NwkState;
- uint8 SampleApp_TransID; // This is the unique message ID (counter)
- afAddrType_t SampleApp_Dst_DstAddr;
- void SampleApp_MessageMSGCB(afIncomingMSGPacket_t *pkt);
- void SampleApp_Init( byte task_id )
- {
- halUARTCfg_t uartConfig;
- SampleApp_TaskID = task_id;
- SampleApp_TransID = 0;
-
- SampleApp_epDesc.endPoint = SampleApp_ENDPOINT;
- SampleApp_epDesc.task_id = &SampleApp_TaskID;
- SampleApp_epDesc.simpleDesc
- = (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc;
- SampleApp_epDesc.latencyReq = noLatencyReqs;
- afRegister( &SampleApp_epDesc );
- uartConfig.configured = TRUE;
- uartConfig.baudRate = BAUD;
- uartConfig.flowControl = FALSE;
- uartConfig.rx.maxBufSize = RX_SZ;
- uartConfig.tx.maxBufSize = TX_SZ;
- uartConfig.idleTimeout = IDLE;
- uartConfig.intEnable = TRUE;
- uartConfig.callBackFunc =NULL;
- HalUARTOpen (PORT, &uartConfig); //配置串口
-
- P1DIR|=0x0E;
- LED1 =0;
- LED2 =0;
- }
- UINT16 SampleApp_ProcessEvent( byte task_id, UINT16 events )
- {
- afIncomingMSGPacket_t *MSGpkt;
- if ( events & SYS_EVENT_MSG )
- {
- MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
- while ( MSGpkt )
- {
- switch ( MSGpkt->hdr.event )
- {
- case AF_INCOMING_MSG_CMD:
- SampleApp_MessageMSGCB(MSGpkt);
- LED2=!LED2;
- break;
- 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) )
- {
- LED1=1; //若成功创建网络则点亮LED2
- }
- else
- {
- LED1=0;// Device is no longer in the network
- }
- break;
- default:
- break;
- }
- osal_msg_deallocate( (uint8 *)MSGpkt );
- MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
- }
- return (events ^ SYS_EVENT_MSG);
- }
-
- return 0;
- }
- void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
- {
- unsigned char buffer[2];
- unsigned char WenDuChars[2];
- unsigned char ShiDuChars[2];
-
- switch (pkt->clusterId) {
- case REPORT_CLUSTER_ID:
- osal_memcpy(buffer,pkt->cmd.Data,2);
- if(buffer[0]!=0)
- {
- WenDuChars[0]=buffer[0]/10+'0';
- WenDuChars[1]=buffer[0]%10+'0';
- HalUARTWrite(0,"采集到的温度是:",16);
- HalUARTWrite(0,WenDuChars,2);
- }
- else
- {
- HalUARTWrite(0,"It's Wrong",10);
- HalUARTWrite(0,"\n",1);
- }
- if(buffer[1]!=0)
- {
- ShiDuChars[0]=buffer[1]/10+'0';
- ShiDuChars[1]=buffer[1]%10+'0';
- HalUARTWrite(0,"采集到的湿度是:",16);
- HalUARTWrite(0,ShiDuChars,2);
- HalUARTWrite(0,"\n",1);
- }
- else
- {
- HalUARTWrite(0,"It's Wrong",10);
- HalUARTWrite(0,"\n",1);
- }
- break;
- }
- }
复制代码
所有资料51hei提供下载:
C2530-read-GY-30-LED.zip
(32.98 KB, 下载次数: 40)
|