基于CC2530的zIgbee传感器无线数据采集系统开发(部分关键源码)

时间:2021-11-24 03:48:31

这是本人的一个小项目,分享给大家,有什么错误的地方,还请大家指导(完整的工程文件已经上传到资源了)

工程的数据包格式为:

         |--|--|-----,------,-----|----------------|---|

 

|数据包长度|数据包序号|数据1,数据2,数据3|mac地址数据|异或校验|

 

|31|01|00000,01200,02003|eab12345ddfd998a|9fd|

 

串口自检测设计(可用来设计上位机自检测串口):

向串口发送字符串FC

串口返回字符串FE


项目包含的传感器有:

                                                                           最后测试结果

        

P2.5 OK   P0_7   序号5                                              成功

DHT11 OK       P1_1        序号1                                          成功

人体红外        待检验 序号2                                              成功         未检出输出1

MQ2        OK    P1_2         序号3                                           成功  未检出输出0

MQ7                 待检验 序号10                                           成功  未检出输出0

雨滴                 待检验 序号9                                              成功  未检出输出1

土壤湿度        待检验 序号6                                              成功  未检出输出1

光敏            待检验序号12                                           成功  未检出输出1

DS18B20 OK   P0_6   序号13                                     成功  输出结果形如264表                                                                           示26.4

MQ3        OK    P2_0         序号14                                         成功  未检出输出1

MQ4        OK    P2_0         序号15                                         成功  未检出输出1

MQ8        OK    P2_0         序号16                                         成功  未检出输出0

MQ135   OK    P2_0         序号17                                         成功  未检出输出1

ULTRASONIC OK P1_0 P0_0 序号18//超声波                      成功

倾斜 OK  P1_2  序号19                                   成功  未发生倾倒输出1

轻触开关   OK  P2_0序号20                                          成功   未按输出1

 麦克风  OK  P2_0序号21                              成功  没声音输出1

 干簧管  OK  P2_0序号22                              成功  无信号输出0

红外光电         OK  P2_0 序号23                                    成功  无光输出0

双路光敏   OK   P0-1 P0-2 序号24                              成功  无光输出 1 1

火光检测   OK   P1-2    序号25                              成功  无火光输出1

测速模块        OK P1_2    序号26                                   成功  有障碍物阻挡输出1

电流模块         OK P1_2   序号27                                     成功  电流未通过输出0

继电器    OK   P1_3  命令代码字符串“FD”             成功

加速度    OK          序号28                                  成功         未区分正负

霍尔开关  OK   P2_0  序号29                                    成功  未检出输出1

电子陀螺仪 OK  P1_6(SCL) P1_7(SDA)  序号30 成功  输出为213 4                                                                                (213.4)

接近开关         OK P2_0  序号31                                     等待测试

ADXL345数字三轴重力加速度倾斜度模块 序号32       成功     未区分正负

GY30                                              序号33       成功       未区分正负

 

部分传感器功能没有完全使用,若需详细数据请大家结合传感器功能自行设计

 

 

 

 

SampleApp.C

 

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

 Filename:       SampleApp.c

 Revised:        $Date: 2009-03-1815:56:27 -0700 (Wed, 18 Mar 2009) $

 Revision:       $Revision: 19453 $

 

 Description:    Sample Application(no Profile).

 

 

 Copyright 2007 Texas Instruments Incorporated. All rights reserved.

 

 IMPORTANT: Your use of this Software is limited to those specific rights

 granted under the terms of a software license agreement between the user

  whodownloaded the software, his/her employer (which must be your employer)

  andTexas Instruments Incorporated (the "License").  You may not use this

 Software unless you agree to abide by the terms of the License. TheLicense

 limits your use, and you acknowledge, that the Software may not bemodified,

 copied or distributed unless embedded on a Texas Instrumentsmicrocontroller

  orused solely and exclusively in conjunction with a Texas Instruments radio

 frequency transceiver, which is integrated into your product.  Other than for

  theforegoing purpose, you may not use, reproduce, copy, prepare derivative

 works of, modify, distribute, perform, display or sell this Softwareand/or

  itsdocumentation for any purpose.

 

  YOUFURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE

 PROVIDED 揂S IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,

 INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,

 NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL

 TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDERCONTRACT,

 NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER

 LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES

 INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE

  ORCONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT

  OFSUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES

 (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILARCOSTS.

 

 Should you have any questions regarding your right to use this Software,

 contact Texas Instruments Incorporated at www.TI.com.

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

 

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

 This application isn't intended to do anything useful, it is

 intended to be a simple example of an application's structure.

 

 This application sends it's messages either as broadcast or

 broadcast filtered group messages. The other (more normal)

 message addressing is unicast. Most of the other sample

 applications are written to support the unicast message model.

 

  Keycontrol:

   SW1:  Sends a flash command to alldevices in Group 1.

   SW2:  Adds/Removes (toggles) thisdevice in and out

         of Group 1.  This will enable anddisable the

         reception of the flash command.

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

 

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

 *INCLUDES

 */

 

#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 "MT_UART.h"

#include "MT_APP.h"

#include "MT.h"

#include "DHT11.h"

#include "RE200B.h"

#include "Ascii_to_bcd.h"

#include "crc.h"

#include <string.h>

#include <stdlib.h>

#include <stdio.h>

#include "BCD_To_ASCII.h"

#include "sht11.h"

#include "MQ7.h"

#include "Light.h"

#include "Rain.h"

#include "PH.h"

#include"PM2_5.h"

#include"ds18b20.h"

#include"HMC5388L.h"

#include"ultrasonic.h"

#include"Bais.h"

#include"YF.h"

#include"mic.h"

#include"ghg.h"

#include"inf_photo.h"

#include"TWO_LIGHTS.h"

#include"FIRE.h"

#include"SPEED.h"

#include"CURRENT.h"

#include"SDR_SENSOR.h"

#include"mma8451.h"

#include"huer_switch.h"

#include"app_switch.h"

#include"ADXL345.h"

#include"GY30.h"

////////////////////////////////

#define MAXLEN 128

unsigned char temp_ASCII[128]={0};

unsigned char temp_BCD[128]={0};

unsigned char data_package[128]={0};

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

 *MACROS

 */

 

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

 *CONSTANTS

 */

 

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

 *TYPEDEFS

 */

 

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

 *GLOBAL VARIABLES

 */

 

// This list should be filled withApplication specific Cluster IDs.

const cId_tSampleApp_ClusterList[SAMPLEAPP_MAX_CLUSTERS] =

{

 //SAMPLEAPP_PERIODIC_CLUSTERID,

 SAMPLEAPP_FLASH_CLUSTERID

};

 

const SimpleDescriptionFormat_tSampleApp_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 itis define in RAM.

endPointDesc_t SampleApp_epDesc;

 

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

 *EXTERNAL VARIABLES

 */

 

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

 *EXTERNAL FUNCTIONS

 */

 

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

 *LOCAL VARIABLES

 */

uint8 SampleApp_TaskID;   // Task ID for internal task/eventprocessing

                          // This variable willbe 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;

 

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( uint16flashTime );

void SampleApp_SerialCMD(mtOSALSerialData_t*cmdMsg);

 

void SampleApp_SendDHT11Message(void);//

void SampleApp_Send18B20Message(void);

voidSampleApp_Send_Rain_Message(void);//up_tech

voidSampleApp_Send_MQ7_Message(void);//up_tech

voidSampleApp_SendRE200BMessage(void);//up_tech

voidSampleApp_Send_MQ2_Message(void);//up_tech

void SampleApp_Send_MQ3_Message(void);

void SampleApp_Send_MQ4_Message(void);

void SampleApp_Send_MQ8_Message(void);

void SampleApp_Send_MQ135_Message(void);

void SampleApp_Send_SHT11_Message( void);//up_tech

voidSampleApp_Send_PH_Message(void);//up_tech

void SampleApp_Send_PM2_5_Message(void);//

void SampleApp_Send_Light_Message(void);

void SampleApp_Send5883Message(void);

voidSampleApp_Send_ultrasonic_Message(void);

void SampleApp_Send_Bias_Message(void);

void SampleApp_Send_YF_Message(void);

void SampleApp_Send_MIC_Message(void);

void SampleApp_Send_GHG_Message(void);

void SampleApp_Send_INF_PHOTO_Message(void);

voidSampleApp_Send_TWO_LIGHTS_Message(void);

void SampleApp_Send_FIRE_Message(void);

void SampleApp_Send_SPEED_Message(void);

void SampleApp_Send_CURRENT_Message(void);

void SampleApp_Send_MMA8452_Message(void);

void SampleApp_Send_HUER_Message(void);

void SampleApp_Send_approach_Message(void);

void SampleApp_Send_ADXL345_Message(void);

void SampleApp_Send_GY30_Message(void);

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

 *NETWORK LAYER CALLBACKS

 */

 

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

 *PUBLIC FUNCTIONS

 */

 

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

 *@fn      SampleApp_Init

 *

 *@brief   Initialization function for theGeneric 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 byOSAL.  This ID should be

 *                    used to send messages andset timers.

 *

 *@return  none

 */

void SampleApp_Init( uint8 task_id )

{

 SampleApp_TaskID = task_id;

 SampleApp_NwkState = DEV_INIT;

  SampleApp_TransID= 0;

 

 

 /***********串口初始化************/

 

 //P0DIR &= ~0x40;//P0_6

 MT_UartInit();//初始化

 MT_UartRegisterTaskID(task_id);//登记任务号 

 //HalUARTWrite(0,"welcome\n",9);

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

 

 #ifdefined ( BUILD_ALL_DEVICES )

  //The "Demo" target is setup to have BUILD_ALL_DEVICES andHOLD_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 andwait 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 );

 

  //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 eventprocessor.  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( uint8task_id, uint16 events )

{

 //uint8 temp_mac_addr_Bcd[16]={0};

 //uint8 temp_mac_addr_Ascii[16]={0};

 /*unsigned int i;

  intflag;

 uint8 *temp_mac=0;

  */

 //temp_mac=malloc(8);

 afIncomingMSGPacket_t *MSGpkt;

 (void)task_id;  // Intentionallyunreferenced parameter

 

  if( events & SYS_EVENT_MSG )

  {

    MSGpkt= (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );

   while ( MSGpkt )

    {

     switch ( MSGpkt->hdr.event )

     {

       

       case CMD_SERIAL_MSG:  //串口收到数据后由MT_UART层传递过来的数据,用网蜂方法接收,编译时不定义MT相关内容,

              SampleApp_SerialCMD((mtOSALSerialData_t*)MSGpkt);

       break;

       

       // 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) )//路由端和终端设备实行单播

         {

             SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;

             SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;

             SampleApp_Periodic_DstAddr.addr.shortAddr = 0x0000;

             

           //Start sending the periodicmessage in a regular interval.

            osal_start_timerEx( SampleApp_TaskID,

                           SAMPLEAPP_SEND_PERIODIC_MSG_EVT,

                           SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT);

         }

          else

           if(SampleApp_NwkState == DEV_ZB_COORD)//协调器实行广播

         {

             SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;

             SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;

              SampleApp_Periodic_DstAddr.addr.shortAddr= 0xffff;

         }

           else

           {

           

           }

         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_SendDHT11Message();//温湿度感应器信息发送函数OK  

   //SampleApp_Send18B20Message();//温度感应器信息发送函数OK

   //SampleApp_SendRE200BMessage();//RE200B感应器信息发送函数OK

   //SampleApp_Send_MQ2_Message();//OK

   //SampleApp_Send_MQ3_Message();//OK

   //SampleApp_Send_MQ4_Message();//OK

   //SampleApp_Send_MQ8_Message();//OK

   //SampleApp_Send_MQ135_Message();//OK

   //SampleApp_Send_SHT11_Message();//

   //SampleApp_Send_Rain_Message();//RAin OK

   //SampleApp_Send_MQ7_Message();//OK

   //SampleApp_Send_PH_Message();//OK 路由节点

   //SampleApp_Send5883Message();// OK

   //SampleApp_Send_PM2_5_Message(); //OK

   //SampleApp_Send_Light_Message();//光敏   OK

   //SampleApp_Send_ultrasonic_Message();//超声波  OK

   //SampleApp_Send_Bias_Message();//倾斜开关Bais   OK

   //SampleApp_Send_YF_Message();//轻触开关  OK

   //SampleApp_Send_MIC_Message();//麦克风  OK

   //SampleApp_Send_GHG_Message();//单簧管  OK   路由节点

   //SampleApp_Send_INF_PHOTO_Message();//红外光电  OK

   //SampleApp_Send_TWO_LIGHTS_Message();//双头光敏  OK

   //SampleApp_Send_FIRE_Message();//火光检测   OK

   //SampleApp_Send_SPEED_Message();//测速模块  OK

   //SampleApp_Send_CURRENT_Message();//电流检测  OK

   //SampleApp_Send_MMA8452_Message();//加速度  OK

   //SampleApp_Send_HUER_Message();//霍尔开关  OK

  //SampleApp_Send_approach_Message();//接近开关  test

   //SampleApp_Send_ADXL345_Message();

   //SampleApp_Send_GY30_Message();

   // Setup to send message again in normal period (+ a little jitter)

   //发送节点MAC地址到协调器

   //temp_mac=NLME_GetExtAddr();

   //HalUARTWrite(0,NLME_GetExtAddr(),8);

   /*for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     return 0;

   }*/

   /*if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                      SAMPLEAPP_devExt_CLUSTERID,

                       16,temp_mac_addr_Ascii,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

   */

  //memset(temp_mac,0,8);

  //temp_mac=NULL;

   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 thisdevice.

 *

 *@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;  // Intentionallyunreferenced 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 processorcallback.  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 )//信息接收

{

 /*16进制转ASCII码表-网蜂*/

 //uint8asc_16[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

 //uint16 flashTime;

  intlen_data,temp,flog=0,crc;

 switch ( pkt->clusterId )

  {

  

  case SAMPLEAPP_DHT11_CLUSTERID://接收由周期函数发来的信息  //1

    //HalUARTWrite(0,pkt->cmd.Data,strlen(pkt->cmd.Data));

     /***********温度打印***************/

    

    temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='0';

    temp_ASCII[3]='1';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

     temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]=pkt->cmd.Data[0];

    temp_ASCII[13]=pkt->cmd.Data[1];

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[2];

    temp_ASCII[18]=pkt->cmd.Data[3];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+4,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(1)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;

    temp_ASCII[36]=crc%100/10+48;

    temp_ASCII[37]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

    

    /*

    temp_ASCII[0]='1';

    temp_ASCII[1]='8';

    temp_ASCII[2]='0';

    temp_ASCII[3]='2';

    temp_ASCII[4]=pkt->cmd.Data[2];

    temp_ASCII[5]=pkt->cmd.Data[3];

    strcpy(temp_ASCII+6,pkt->cmd.Data+4);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,22,flog);

    if(temp)

    {

      HalUARTWrite(0,"(2)asc_to_bcd is wrong\n",28);

      return;

    }

    crc=crc_sum(temp_BCD,11);

    temp_ASCII[22]=crc/100+48;

    temp_ASCII[23]=crc%100/10+48;

    temp_ASCII[24]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data/2);

    */

    /***************湿度打印****************/

    //HalUARTWrite(0,"Humidity is:",12);    //提示接收到数据

    //HalUARTWrite(0,&pkt->cmd.Data[2],2); //湿度

    //HalUARTWrite(0,"\n",1);              // 回车换行  

     

     break;

     

     

 case SAMPLEAPP_RE200B_CLUSTERID: //RE200B红外释电 //2

    temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='0';

    temp_ASCII[3]='2';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

     temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(2)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;

    temp_ASCII[36]=crc%100/10+48;

    temp_ASCII[37]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

    /*temp_ASCII[0]='1';

    temp_ASCII[1]='8';

    temp_ASCII[2]='0';

    temp_ASCII[3]='4';

    strcpy(temp_ASCII+4,&pkt->cmd.Data[0]);

    //HalUARTWrite(0,&pkt->cmd.Data[0],3);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,22,flog);

    if(temp)

    {

      HalUARTWrite(0,"(4)asc_to_bcd is wrong\n",28);

      return;

    }

    crc=crc_sum(temp_BCD,11);

    temp_ASCII[22]=crc/100+48;

    temp_ASCII[23]=crc%100/10+48;

    temp_ASCII[24]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

     //HalUARTWrite(0,"re200\n",7);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data/2);

     //P=pkt->cmd.Data[0]-48;

     //if(P==0)

       //HalUARTWrite(0,"someone at there \n",20);//有人

     //else

      // HalUARTWrite(0,"NO one \n",7);//没人

       */

 break;

 

 

 case SAMPLEAPP_MQ2_CLUSTERID: //3

    temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='0';

    temp_ASCII[3]='3';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

     temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(1)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;

    temp_ASCII[36]=crc%100/10+48;

    temp_ASCII[37]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

   /*

    temp_ASCII[0]='1';

    temp_ASCII[1]='8';

    temp_ASCII[2]='0';

    temp_ASCII[3]='3';

    strcpy(temp_ASCII+4,&pkt->cmd.Data[0]);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,22,flog);

    if(temp)

    {

      HalUARTWrite(0,"(3)asc_to_bcd is wrong\n",28);

      return;

    }

    crc=crc_sum(temp_BCD,11);

    temp_ASCII[22]=crc/100+48;

    temp_ASCII[23]=crc%100/10+48;

    temp_ASCII[24]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

     //HalUARTWrite(0,"re200\n",7);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data/2);

     //P=pkt->cmd.Data[0]-48;

     //if(P==1)

     //  HalUARTWrite(0,"worning\n",10);//有烟

     //else

     //  HalUARTWrite(0,"it is ok\n",10);//无烟

     */

     break;

          

   case SAMPLEAPP_COM_CLUSTERID://接收由串口无线发来的信息,用来修好配置,还没写//9

     {

       char BUF[5]={0};

      //HalUARTWrite(0,"SDR1\n",strlen("SDR\n"));

       memcpy(BUF,pkt->cmd.Data,4);

       HalUARTWrite(0,BUF,strlen(BUF));

      if(!strcmp(BUF,"OPEN"))

      {

         static int temp=0;

         temp=~temp;

         Init_SDR_SENSOR_Io();

         OPEN_CLOSE_SDR(temp);

         HalUARTWrite(0,"SDR2\n",strlen("SDR\n"));

      }

     //len=pkt->cmd.Data[0];

     //for(i=0;i<len;i++)

     //HalUARTWrite(0,&pkt->cmd.Data[i+1],1);//ASCII码发给PC机

     //HalUARTWrite(0,"\n",1);                     // 回车换行

     }

     break;

    

   case SAMPLEAPP_devExt_CLUSTERID://mac_address信息接收//4

        HalUARTWrite(0,pkt->cmd.Data,16);

        break;

  

   case SAMPLEAPP_FLASH_CLUSTERID://9

     {

       int flashTime=0;

       //flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2] );

       flashTime=2000;

       HalLedBlink( HAL_LED_4, 4, 50, (flashTime / 4) );

     }

     break;

   

   case SAMPLEAPP_STH11_CLUSTERID://6

        /***********STH11温度打印***************/

   /*

    temp_ASCII[0]='1';

    temp_ASCII[1]='8';

    temp_ASCII[2]='0';

    temp_ASCII[3]='7';

    temp_ASCII[4]=pkt->cmd.Data[0];

    temp_ASCII[5]=pkt->cmd.Data[1];

    strcpy(temp_ASCII+6,pkt->cmd.Data+4);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,22,flog);

    if(temp)

    {

      HalUARTWrite(0,"(7)asc_to_bcd is wrong\n",28);

      return;

    }

    crc=crc_sum(temp_BCD,11);

    temp_ASCII[22]=crc/100+48;

    temp_ASCII[23]=crc%100/10+48;

    temp_ASCII[24]=crc%10+48;

     strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data/2);

    

    

    temp_ASCII[0]='1';

    temp_ASCII[1]='8';

    temp_ASCII[2]='0';

    temp_ASCII[3]='7';

    temp_ASCII[4]=pkt->cmd.Data[2];

    temp_ASCII[5]=pkt->cmd.Data[3];

    strcpy(temp_ASCII+6,pkt->cmd.Data+4);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,22,flog);

    if(temp)

    {

      HalUARTWrite(0,"(7)asc_to_bcd is wrong\n",28);

      return;

    }

    crc=crc_sum(temp_BCD,11);

    temp_ASCII[22]=crc/100+48;

    temp_ASCII[23]=crc%100/10+48;

    temp_ASCII[24]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data/2);*/

        break;

 case SAMPLEAPP_MQ7_CLUSTERID:

    temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='1';

    temp_ASCII[3]='0';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

     

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(10)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

   temp_ASCII[35]=crc/100+48;

    temp_ASCII[36]=crc%100/10+48;

    temp_ASCII[37]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

     memset(temp_BCD,0,len_data);

    /*temp_ASCII[0]='1';

    temp_ASCII[1]='8';

    temp_ASCII[2]='1';

    temp_ASCII[3]='0';

    strcpy(temp_ASCII+4,&pkt->cmd.Data[0]);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,22,flog);

    if(temp)

    {

      HalUARTWrite(0,"(3)asc_to_bcd is wrong\n",28);

      return;

    }

    crc=crc_sum(temp_BCD,11);

    temp_ASCII[22]=crc/100+48;

    temp_ASCII[23]=crc%100/10+48;

    temp_ASCII[24]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

     //HalUARTWrite(0,"re200\n",7);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data/2);

     //P=pkt->cmd.Data[0]-48;

     //if(P==1)

     //  HalUARTWrite(0,"worning\n",10);//有烟

     //else

     //  HalUARTWrite(0,"it is ok\n",10);//无烟

      */break;//5

 

 case SAMPLEAPP_Rain_CLUSTERID:

    temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='0';

    temp_ASCII[3]='9';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(9)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

       return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;

    temp_ASCII[36]=crc%100/10+48;

    temp_ASCII[37]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

   /*

      temp_ASCII[0]='1';

      temp_ASCII[1]='8';

      temp_ASCII[2]='0';

      temp_ASCII[3]='9';

      strcpy(temp_ASCII+4,&pkt->cmd.Data[0]);

      temp=asc_to_bcd(temp_BCD,temp_ASCII,22,flog);

      if(temp)

      {

        HalUARTWrite(0,"(3)asc_to_bcd is wrong\n",28);

        return;

       }

      crc=crc_sum(temp_BCD,11);

      temp_ASCII[22]=crc/100+48;

      temp_ASCII[23]=crc%100/10+48;

      temp_ASCII[24]=crc%10+48;

      strcpy(data_package,temp_ASCII);

      len_data=strlen(data_package);

      HalUARTWrite(0,data_package,len_data);

       //HalUARTWrite(0,"re200\n",7);

      crc=0;

      memset(temp_ASCII,0,len_data);

      memset(data_package,0,len_data);

      memset(temp_BCD,0,len_data/2);*/

   break;//7

 

 case SAMPLEAPP_PM2_5_CLUSTERID:

    temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='0';

    temp_ASCII[3]='5';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

     temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    /*

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]=pkt->cmd.Data[0];

    */

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+16,pkt->cmd.Data,19);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(5)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;

    temp_ASCII[36]=crc%100/10+48;

    temp_ASCII[37]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

    /*

    temp_ASCII[0]='1';

    temp_ASCII[1]='8';

    temp_ASCII[2]='0';

    temp_ASCII[3]='5';

    strcpy(temp_ASCII+4,&pkt->cmd.Data[0]);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,22,flog);

    if(temp)

    {

      HalUARTWrite(0,"(5)asc_to_bcd is wrong\n",28);

      return;

    }

    crc=crc_sum(temp_BCD,11);

    temp_ASCII[22]=crc/100+48;

    temp_ASCII[23]=crc%100/10+48;

    temp_ASCII[24]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

     //HalUARTWrite(0,"re200\n",7);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data/2);*/

    break;

 

 case SAMPLEAPP_PH_CLUSTERID:

    temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='0';

    temp_ASCII[3]='6';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(6)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;

    temp_ASCII[36]=crc%100/10+48;

    temp_ASCII[37]=crc%10+48;

    strcpy(data_package,temp_ASCII);

     len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

     /*

      temp_ASCII[0]='1';

      temp_ASCII[1]='8';

      temp_ASCII[2]='0';

      temp_ASCII[3]='6';

      strcpy(temp_ASCII+4,&pkt->cmd.Data[0]);

      temp=asc_to_bcd(temp_BCD,temp_ASCII,22,flog);

      if(temp)

      {

        HalUARTWrite(0,"(3)asc_to_bcd is wrong\n",28);

        return;

      }

      crc=crc_sum(temp_BCD,11);

      temp_ASCII[22]=crc/100+48;

      temp_ASCII[23]=crc%100/10+48;

      temp_ASCII[24]=crc%10+48;

      strcpy(data_package,temp_ASCII);

      len_data=strlen(data_package);

      HalUARTWrite(0,data_package,len_data);

       //HalUARTWrite(0,"re200\n",7);

      crc=0;

      memset(temp_ASCII,0,len_data);

      memset(data_package,0,len_data);

      memset(temp_BCD,0,len_data/2);

   */

   break;

 case SAMPLEAPP_Light_CLUSTERID://12

    temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='1';

    temp_ASCII[3]='2';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(12)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;

    temp_ASCII[36]=crc%100/10+48;

    temp_ASCII[37]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

   /*

    temp_ASCII[0]='1';

    temp_ASCII[1]='8';

    temp_ASCII[2]='1';

    temp_ASCII[3]='1';

    strcpy(temp_ASCII+4,&pkt->cmd.Data[0]);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,22,flog);

    if(temp)

    {

      HalUARTWrite(0,"(3)asc_to_bcd is wrong\n",28);

      return;

    }

    crc=crc_sum(temp_BCD,11);

    temp_ASCII[22]=crc/100+48;

    temp_ASCII[23]=crc%100/10+48;

    temp_ASCII[24]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data/2);*/

   break;

   case SAMPLEAPP_DS18B20_CLUSTERID://13

    temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='1';

    temp_ASCII[3]='3';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

 

    memcpy(temp_ASCII+16,pkt->cmd.Data,19);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(13)asc_to_bcd iswrong",strlen("(13)asc_to_bcd is wrong"));

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;

    temp_ASCII[36]=crc%100/10+48;

    temp_ASCII[37]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

   break;

   case SAMPLEAPP_MQ3_CLUSTERID: //14

    temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='1';

    temp_ASCII[3]='4';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(14)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;

    temp_ASCII[36]=crc%100/10+48;

    temp_ASCII[37]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

    break;

    case SAMPLEAPP_MQ4_CLUSTERID: //15

    temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='1';

    temp_ASCII[3]='5';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(15)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;

    temp_ASCII[36]=crc%100/10+48;

    temp_ASCII[37]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

    break;

    case SAMPLEAPP_MQ8_CLUSTERID: //16

    temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='1';

    temp_ASCII[3]='6';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(16)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;

    temp_ASCII[36]=crc%100/10+48;

    temp_ASCII[37]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

    break;

    case SAMPLEAPP_MQ135_CLUSTERID: //17

    temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='1';

    temp_ASCII[3]='7';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(17)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;    temp_ASCII[36]=crc%100/10+48; temp_ASCII[37]=crc%10+48; 

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

    break;

 case SAMPLEAPP_ULTRASONIC_CLUSTERID://18

    //HalUARTWrite(0,pkt->cmd.Data,strlen(pkt->cmd.Data));

    temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='1';

    temp_ASCII[3]='8';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

   

    memcpy(temp_ASCII+16,pkt->cmd.Data,19);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(18)asc_to_bcd iswrong",strlen("(18)asc_to_bcd is wrong"));

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;    temp_ASCII[36]=crc%100/10+48; temp_ASCII[37]=crc%10+48; 

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

    break;

 case SAMPLEAPP_Bais_CLUSTERID:

     temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='1';

    temp_ASCII[3]='9';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(19)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;    temp_ASCII[36]=crc%100/10+48; temp_ASCII[37]=crc%10+48; 

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

   break;

   case  SAMPLEAPP_YF_CLUSTERID:

     temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='2';

    temp_ASCII[3]='0';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(20)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;    temp_ASCII[36]=crc%100/10+48; temp_ASCII[37]=crc%10+48; 

    strcpy(data_package,temp_ASCII);

     len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

     break;

     

     

     case  SAMPLEAPP_MIC_CLUSTERID:

    temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='2';

    temp_ASCII[3]='1';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(21)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;    temp_ASCII[36]=crc%100/10+48; temp_ASCII[37]=crc%10+48; 

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

     break;

     

     

     case  SAMPLEAPP_GHG_CLUSTERID:

     temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='2';

    temp_ASCII[3]='2';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(22)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;    temp_ASCII[36]=crc%100/10+48; temp_ASCII[37]=crc%10+48; 

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

     break;

     

      case SAMPLEAPP_INF_PHOTO_CLUSTERID:

    temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='2';

    temp_ASCII[3]='3';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(23)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;    temp_ASCII[36]=crc%100/10+48; temp_ASCII[37]=crc%10+48; 

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

     break;

     

 case SAMPLEAPP_TWO_LIGHTS_CLUSTERID://双路光敏

    temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='2';

    temp_ASCII[3]='4';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(24)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;    temp_ASCII[36]=crc%100/10+48; temp_ASCII[37]=crc%10+48; 

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

   break;

    

 case SAMPLEAPP_FIRE_CLUSTERID:

    temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='2';

    temp_ASCII[3]='5';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(25)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;    temp_ASCII[36]=crc%100/10+48; temp_ASCII[37]=crc%10+48; 

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

   break;

     

   

 case SAMPLEAPP_SPEED_CLUSTERID:

     temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='2';

    temp_ASCII[3]='6';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

     temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(26)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;    temp_ASCII[36]=crc%100/10+48; temp_ASCII[37]=crc%10+48; 

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

   break;

   

 case SAMPLEAPP_CURRENT_CLUSTERID:

     temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='2';

    temp_ASCII[3]='7';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(27)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;    temp_ASCII[36]=crc%100/10+48; temp_ASCII[37]=crc%10+48; 

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

   break;

   

 case SAMPLEAPP_ACCELERATION_CLUSTERID:

    {

     temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='2';

    temp_ASCII[3]='8';

 

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+4,pkt->cmd.Data,31);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(28)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;    

    temp_ASCII[36]=crc%100/10+48; 

    temp_ASCII[37]=crc%10+48; 

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

    }

   break;

   

 case SAMPLEAPP_HUER_CLUSTERID:

    {

     temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='2';

    temp_ASCII[3]='9';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

     temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(29)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

   temp_ASCII[35]=crc/100+48;

    temp_ASCII[36]=crc%100/10+48;

    temp_ASCII[37]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

    }

   break;

   

   

 case SAMPLEAPP_5388_CLUSTERID:

    {

     temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='3';

    temp_ASCII[3]='0';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]=pkt->cmd.Data[0];

    temp_ASCII[12]=pkt->cmd.Data[1];

    temp_ASCII[13]=pkt->cmd.Data[2];

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]='0';

    temp_ASCII[18]=pkt->cmd.Data[3];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+4,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(30)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

   temp_ASCII[35]=crc/100+48;

    temp_ASCII[36]=crc%100/10+48;

    temp_ASCII[37]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

    }

   break;

   

   

 case SAMPLEAPP_APP_CLUSTERID:

    {

     temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='3';

    temp_ASCII[3]='1';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

    temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

    

    temp_ASCII[14]='0';

    temp_ASCII[15]='0';

    temp_ASCII[16]='0';

    temp_ASCII[17]=pkt->cmd.Data[0];

    temp_ASCII[18]=pkt->cmd.Data[1];

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+19,pkt->cmd.Data+2,16);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(31)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

   temp_ASCII[35]=crc/100+48;

    temp_ASCII[36]=crc%100/10+48;

    temp_ASCII[37]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

    }

   break;

   

   

 case SAMPLEAPP_ADXL345_CLUSTERID:

    {

    temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='3';

    temp_ASCII[3]='2';

 

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+4,pkt->cmd.Data,31);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(32)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

    temp_ASCII[35]=crc/100+48;    

    temp_ASCII[36]=crc%100/10+48; 

    temp_ASCII[37]=crc%10+48; 

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

    }

   break;

   

   

 case SAMPLEAPP_GY30_CLUSTERID:

    {

     temp_ASCII[0]='3';

    temp_ASCII[1]='1';

    temp_ASCII[2]='3';

    temp_ASCII[3]='3';

    

    temp_ASCII[4]='0';

    temp_ASCII[5]='0';

    temp_ASCII[6]='0';

    temp_ASCII[7]='0';

    temp_ASCII[8]='0';

    

    temp_ASCII[9]='0';

     temp_ASCII[10]='0';

    temp_ASCII[11]='0';

    temp_ASCII[12]='0';

    temp_ASCII[13]='0';

   

    

    //strcpy(temp_ASCII+13,pkt->cmd.Data+4);

    memcpy(temp_ASCII+14,pkt->cmd.Data,21);

    temp=asc_to_bcd(temp_BCD,temp_ASCII,strlen(temp_ASCII),flog);//前补零

    if(temp)

    {

      HalUARTWrite(0,"(33)asc_to_bcd is wrong\n",28);

      HalUARTWrite(0,temp_ASCII,strlen(temp_ASCII));

      return;

    }

    crc=BC_Sum(temp_BCD,((strlen(temp_ASCII)/2)+(strlen(temp_ASCII)%2)));

   temp_ASCII[35]=crc/100+48;

    temp_ASCII[36]=crc%100/10+48;

    temp_ASCII[37]=crc%10+48;

    strcpy(data_package,temp_ASCII);

    len_data=strlen(data_package);

    HalUARTWrite(0,data_package,len_data);

    crc=0;

    memset(temp_ASCII,0,len_data);

    memset(data_package,0,len_data);

    memset(temp_BCD,0,len_data);

    }

   break;

   default :HalUARTWrite(0,"no massage\n",12);

  }

}

 

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

 *@fn      SampleApp_SendPeriodicMessage

 *

 *@brief   Send the periodic message.

 *

 *@param   none

 *

 *@return  none

 */

void SampleApp_SendDHT11Message( )//这里是DHT11模块要发送数据的地方

{

   unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[8],temp[MAXLEN];   //温度+提示符    

   DHT11_TEST();   //温度检测   

   temp[0]=T[0]=wendu_shi+48;

   temp[1]=T[1]=wendu_ge+48;

 

   temp[2]=T[3]=shidu_shi+48;

   temp[3]=T[4]=shidu_ge+48;

 

    temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(temp+4,temp_mac_addr_Ascii);

   HalUARTWrite(0,temp,strlen(temp));

  if( AF_DataRequest( &SampleApp_Periodic_DstAddr, &SampleApp_epDesc,

                      SAMPLEAPP_DHT11_CLUSTERID,

                       strlen(temp),

                       temp,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

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

* @fn     SampleApp_RE200BMessage

 *

 *@brief   Send the RE200B message toCoordinator.

 *

 *@return  none

*/

void SampleApp_SendRE200BMessage()

{  

   unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   PeopleInit();

   T[0]='0';

   T[1]='0'+PeopleScan();

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                      SAMPLEAPP_RE200B_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

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

* @fn     SampleApp_Mq2_Message

 *

 *@brief   Send the RE200B message toCoordinator.

 *

 *@return  none

*/

 

void SampleApp_Send_MQ2_Message()

{  

   unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   MQ2_Init();

    T[0]='0';

   T[1]='0'+MQ2_Scan();

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

    if ( AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc,

                       SAMPLEAPP_MQ2_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

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

 *@fn      SampleApp_Send_SHT11_Message

 *

 *@brief   SampleApp_Send_SHT11_Message.

 *

 *@param   none

 *

 *@return  none

 */

void SampleApp_Send_SHT11_Message()

{

   char temp_buf[7]={0};//温度缓存

   char humi_buf[7]={0};//湿度缓存

   char i;

   uint8 buf[MAXLEN]={0};   //温湿度数据包

   uint8 buf_data[MAXLEN]={0};

   uint8 *temp_mac=0;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   float humi,temp;

   temp_mac=NLME_GetExtAddr();

   for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   

   //HalUARTWrite(0,temp_mac_addr_Ascii,16);

       //HalUARTWrite(0,"\n",2);

   strcpy(buf_data+4,temp_mac_addr_Ascii);

   if(GetHumiAndTemp(&humi,&temp) == 0)

    {

       sprintf(humi_buf, (char *)"%f", humi);//将humi的值转换成字符存入humi_buf中

       sprintf(temp_buf, (char *)"%f", temp);

       /*for(i=0; i<7; i++)

       {

         buf[i] = (unsigned char)temp_buf[i];

         buf[i+7] = (unsigned char)humi_buf[i];

       }*/

       for(i=0; i<2; i++)

       {

         buf_data[i] = (unsignedchar)temp_buf[i];

         buf_data[i+2] = (unsigned char)humi_buf[i];

       }      

     

       HalUARTWrite(0,buf_data,20);

       HalUARTWrite(0,"\n",2);

       AF_DataRequest( &SampleApp_Periodic_DstAddr,

                          &SampleApp_epDesc,

                          SAMPLEAPP_STH11_CLUSTERID,

                           20,

                           buf_data,

                          &SampleApp_TransID,

                           AF_DISCV_ROUTE,

                           AF_DEFAULT_RADIUS );

     }

     //HalUARTWrite(0,buf,30);

     //HalUARTWrite(0,"\n",2);

}

 

 

 

 

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

 *@fn      SampleApp_Send_Rain_Message

 *

 *@brief  

 *

 *@param  

 *

 *@return  none

 */

void SampleApp_Send_Rain_Message()

{

   unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   IO_init();

    T[0]='0';

   T[1]='0'+Rain_Scan();

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                      SAMPLEAPP_Rain_CLUSTERID,

                      strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

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

 *@fn      SampleApp_Send_MQ7_Message()

 *

 *@brief  

 *

 *@param  

 *

 *@return  none

 */

void SampleApp_Send_MQ7_Message()

{

   unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   MQ7_init();

    T[0]='0';

   T[1]='0'+MQ7_Scan();

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

    if ( AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc,

                       SAMPLEAPP_MQ7_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

 

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

 *@fn      SampleApp_Send_PH_Message()

 *

 *@brief  

 *

 *@param  

 *

 *@return  none

 */

void SampleApp_Send_PH_Message()

{

   unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   PH_init();

    T[0]='0';

   T[1]='0'+PH_Scan();

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                       SAMPLEAPP_PH_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

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

 *@fn      SampleApp_Send_PM2_5_Message()

 *

 *@brief  

 *

 *@param  

 *

 *@return  none

 */

void SampleApp_Send_PM2_5_Message()

{

   unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   int data=0;

   data=getdata();

   T[0]='0'+data%1000/100;

   T[1]='0'+data%100/10;

   T[2]='0'+data%10;

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+3,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                      SAMPLEAPP_PM2_5_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

 

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

 *@fn      SampleApp_Send_Light_Message()

 *

 *@brief  

 *

 *@param  

 *

 *@return  none

 */

void SampleApp_Send_Light_Message()

{

   unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   LightInit();

    T[0]='0';

   T[1]='0'+LightScan();

   temp_mac=NLME_GetExtAddr();

   for(i=0;i<8;i++)

     {

        temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

        temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

     }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                      SAMPLEAPP_Light_CLUSTERID,

                      strlen(T),

                       T,

                       &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 group1.

 *

 *@param   flashTime - in milliseconds

 *

 *@return  none

 */

void SampleApp_SendFlashMessage( uint16flashTime )

{

 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_SerialCMD(mtOSALSerialData_t*cmdMsg)   //串口信息发送

{

 uint8 i,len,*str=NULL,BUF[3]={0}; //len有用数据长度

 str=cmdMsg->msg;        //指向数据开头

 len=*str;    

 for(i=1;i<=2;i++)

 BUF[i-1]=str[i];

 //msg里的第1个字节代表后面的数据长度

 if(!strcmp("FC",BUF))

   HalUARTWrite(0,"FE",strlen("FE"));

 else

  if(!strcmp("FD",BUF))

   {

     if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                      SAMPLEAPP_COM_CLUSTERID,//自己定义一个

                      strlen("OPEN"),                  // 数据长度        

                       "OPEN",                    //数据内容

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

              {

               

              }

              else

              {

                // Error occurred in request tosend.

              }

   }

}

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

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

 

 

 

void SampleApp_Send18B20Message(void)

{

   int Temp_18B20=0;

   unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   Ds18b20Initial();

   Temp_18B20=Temp_test();

   //T[0]='0'+Temp_18B20/1000;

   T[0]='0'+Temp_18B20%1000/100;

   T[1]='0'+Temp_18B20%100/10;

   T[2]='0'+Temp_18B20%10;

   HalUARTWrite(0,T,3);

   temp_mac=NLME_GetExtAddr();

   for(i=0;i<8;i++)

     {

        temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

        temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

     }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   memcpy(T+3,temp_mac_addr_Ascii,16);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                      SAMPLEAPP_DS18B20_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

void SampleApp_Send5883Message(void){

 unsigned int i;

  intflag;

 uint8 temp_mac_addr_Bcd[16]={0};

 uint8 temp_mac_addr_Ascii[16]={0};

 uint8 *temp_mac=0;

 uint8 T[MAXLEN]={0};

  /*初始化加速度传感器*/

 P1DIR |= 0x01;     //P1_0定义为输出

 P1INP |= 0X01;     //打开下拉

 Init_HMC5388L();

 EA=0;

 Multiple_read_HMC5388L();

 EA=1;

 strcpy(T,HMC5388L_DATABUF);

 memset(HMC5388L_DATABUF,0,strlen(HMC5388L_DATABUF));

 temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcat(T,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                       SAMPLEAPP_5388_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

///////////////////////////////////////////////////////////

void SampleApp_Send_MQ3_Message()

{  

   unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   MQ3_Init();

    T[0]='0';

   T[1]='0'+MQ3_Scan();

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                       SAMPLEAPP_MQ3_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

 

///////////////////////////////////////////////////////

void SampleApp_Send_MQ4_Message()

{  

   unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

    MQ4_Init();

    T[0]='0';

   T[1]='0'+MQ4_Scan();

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                       SAMPLEAPP_MQ4_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

 

 

///////////////////////////////////////////////////////////////

void SampleApp_Send_MQ8_Message()

{  

   unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   MQ8_Init();

    T[0]='0';

   T[1]='0'+MQ8_Scan();

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                       SAMPLEAPP_MQ8_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

 

 

/////////////////////////////////////////////////////////////////

void SampleApp_Send_MQ135_Message()

{  

   unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   MQ135_Init();

    T[0]='0';

   T[1]='0'+MQ135_Scan();

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

    strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                      SAMPLEAPP_MQ135_CLUSTERID,

                       strlen(T),

                       T,

                      &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

////////////////////////////////////////////////////////////

void SampleApp_Send_ultrasonic_Message()

{

 

  intTemp_chao=0;

   unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

     ///////////初始化超声波///////////////////

     Init_IO();

     Init_timer1();

   Temp_chao=CN_DISTANCE();   

   T[0]='0'+Temp_chao%1000/100;

   T[1]='0'+Temp_chao%100/10;

   T[2]='0'+Temp_chao%10;

   HalUARTWrite(0,T,3);

   temp_mac=NLME_GetExtAddr();

   for(i=0;i<8;i++)

     {

        temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

        temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

     }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   memcpy(T+3,temp_mac_addr_Ascii,16);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                      SAMPLEAPP_ULTRASONIC_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

 

}

 

void SampleApp_Send_Bias_Message(void)

{

 unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   Bais_Init();

    T[0]='0';

   T[1]='0'+Bais_Scan();

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                      SAMPLEAPP_Bais_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

 

void SampleApp_Send_YF_Message(void)

{

 unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   YF_Init();

    T[0]='0';

   T[1]='0'+YF_Scan();

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

    strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                       SAMPLEAPP_YF_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

void SampleApp_Send_MIC_Message(void)

{

 unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   MIC_Init();

    T[0]='0';

   T[1]='0'+MIC_Scan();

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                      SAMPLEAPP_MIC_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

  else

  {

   // Error occurred in request to send.

  }

}

 

 

void SampleApp_Send_GHG_Message(void)

{

 unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   GHG_Init();

    T[0]='0';

   T[1]='0'+GHG_Scan();

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                       SAMPLEAPP_GHG_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

 

void SampleApp_Send_INF_PHOTO_Message(void)

{

 unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   INF_PHOTO_Init();

    T[0]='0';

   T[1]='0'+INF_PHOTO_Scan();

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                      SAMPLEAPP_INF_PHOTO_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

void SampleApp_Send_TWO_LIGHTS_Message()

{

  unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   TWO_LIGHTS_Init();

   T[0]='0'+TWO_LIGHTS_Scan()/10;

   T[1]='0'+TWO_LIGHTS_Scan()%10;

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                      SAMPLEAPP_TWO_LIGHTS_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

void SampleApp_Send_FIRE_Message()

{

 unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   FIRE_Init();

   T[0]='0';

   T[1]='0'+FIRE_Scan()%10;

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                      SAMPLEAPP_FIRE_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

 

void SampleApp_Send_SPEED_Message()

{

  unsignedint i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   SPEED_Init();

   T[0]='0';

   T[1]='0'+SPEED_Scan()%10;

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                       SAMPLEAPP_SPEED_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

 

void SampleApp_Send_CURRENT_Message(void)

{

 unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

    CURRENT_Init();

   T[0]='0';

   T[1]='0'+CURRENT_Scan()%10;

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                      SAMPLEAPP_CURRENT_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

 

void SampleApp_Send_MMA8452_Message(void)

{

 unsigned int i;

  intflag;

 uint8 temp_mac_addr_Bcd[16]={0};

 uint8 temp_mac_addr_Ascii[16]={0};

 uint8 *temp_mac=0;

 uint8 T[MAXLEN]={0};

  /*初始化加速度传感器*/

 P1DIR |= 0x01;     //P1_0定义为输出

 P1INP |= 0X01;     //打开下拉

 Init_MMA8452();

 EA=0;

 Multiple_read_MMA8452();

 EA=1;

 strcpy(T,MMA8452_DATABUF);

 memset(MMA8452_DATABUF,0,strlen(MMA8452_DATABUF));

 temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcat(T,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                      SAMPLEAPP_ACCELERATION_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

 

void SampleApp_Send_HUER_Message(void)

{

 unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   Init_HUER_SWITCH_Io();

    T[0]='0';

   T[1]='0'+HUER_SWITCH_Scan();

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                      SAMPLEAPP_HUER_CLUSTERID,

                      strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

 

void SampleApp_Send_approach_Message()

{

  unsigned int i;

   int flag;

   uint8 temp_mac_addr_Bcd[16]={0};

   uint8 temp_mac_addr_Ascii[16]={0};

   uint8 *temp_mac=0;

   uint8 T[MAXLEN]={0};

   Init_APP_SWITCH_Io();

    T[0]='0';

   T[1]='0'+APP_SWITCH_Scan();

   temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcpy(T+2,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                       SAMPLEAPP_APP_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

void SampleApp_Send_ADXL345_Message(void)

{

 unsigned int i;

  intflag;

 uint8 temp_mac_addr_Bcd[16]={0};

 uint8 temp_mac_addr_Ascii[16]={0};

 uint8 *temp_mac=0;

 uint8 T[MAXLEN]={0};

  /*初始化加速度传感器*/

 P1DIR |= 0x01;     //P1_0定义为输出

 P1INP |= 0X01;     //打开下拉

 Init_ADXL345();

 EA=0;

 Multiple_read_ADXL345();

 EA=1;

 strcpy(T,ADXL345_DATABUF);

 memset(ADXL345_DATABUF,0,strlen(ADXL345_DATABUF));

 temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcat(T,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                      SAMPLEAPP_ADXL345_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                      AF_DEFAULT_RADIUS ) ==afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

 

void SampleApp_Send_GY30_Message(void)

{

 unsigned int i;

  intflag;

 uint8 temp_mac_addr_Bcd[16]={0};

 uint8 temp_mac_addr_Ascii[16]={0};

 uint8 *temp_mac=0;

 uint8 T[MAXLEN]={0};

  /*初始化加速度传感器*/

 P1DIR |= 0x01;     //P1_0定义为输出

 P1INP |= 0X01;     //打开下拉

 Init_GY30();

 EA=0;

 Multiple_read_GY30();

 EA=1;

 strcpy(T,GY30_DATABUF);

 memset(GY30_DATABUF,0,strlen(GY30_DATABUF));

 temp_mac=NLME_GetExtAddr();

 for(i=0;i<8;i++)

    {

      temp_mac_addr_Bcd[2*i]=(uint8)(*(temp_mac+i)>>4&0xf);

      temp_mac_addr_Bcd[2*i+1]=(uint8)(*(temp_mac+i)&0xf);

    }

   flag=BCD_To_ASCII(temp_mac_addr_Bcd,temp_mac_addr_Ascii,16);

   if(flag)

    {

     HalUARTWrite(0,"BCD TO ASCII ERROR",19);

     HalUARTWrite(0,"\n",2);

     //return 0;

    }

   strcat(T,temp_mac_addr_Ascii);

   HalUARTWrite(0,T,strlen(T));

   if ( AF_DataRequest( &SampleApp_Periodic_DstAddr,&SampleApp_epDesc,

                      SAMPLEAPP_GY30_CLUSTERID,

                       strlen(T),

                       T,

                       &SampleApp_TransID,

                       AF_DISCV_ROUTE,

                       AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )

  {

  }

 else

  {

   // Error occurred in request to send.

  }

}

 

 

 

 

SampleApp.h

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

 Filename:       SampleApp.h

 Revised:        $Date: 2007-10-2717:22:23 -0700 (Sat, 27 Oct 2007) $

 Revision:       $Revision: 15795 $

 

 Description:    This file containsthe Sample Application definitions.

 

 

 Copyright 2007 Texas Instruments Incorporated. All rights reserved.

 

 IMPORTANT: Your use of this Software is limited to those specific rights

 granted under the terms of a software license agreement between the user

  whodownloaded the software, his/her employer (which must be your employer)

  andTexas Instruments Incorporated (the "License").  You may not use this

 Software unless you agree to abide by the terms of the License. TheLicense

 limits your use, and you acknowledge, that the Software may not bemodified,

 copied or distributed unless embedded on a Texas Instrumentsmicrocontroller

  orused solely and exclusively in conjunction with a Texas Instruments radio

 frequency transceiver, which is integrated into your product.  Other than for

  theforegoing purpose, you may not use, reproduce, copy, prepare derivative

 works of, modify, distribute, perform, display or sell this Softwareand/or

  itsdocumentation for any purpose.

 

  YOUFURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE

 PROVIDED 揂S IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,

 INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,

 NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL

 TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDERCONTRACT,

 NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER

 LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES

 INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE

  ORCONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT

  OFSUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES

 (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILARCOSTS.

 

 Should you have any questions regarding your right to use this Software,

 contact Texas Instruments Incorporated at www.TI.com.

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

 

#ifndef SAMPLEAPP_H

#define SAMPLEAPP_H

 

#ifdef __cplusplus

extern "C"

{

#endif

 

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

 *INCLUDES

 */

#include "ZComDef.h"

 

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

 *CONSTANTS

 */

 

// These constants are only for example andshould be changed to the

// device's needs

#define SAMPLEAPP_ENDPOINT           20

 

#define SAMPLEAPP_PROFID             0x0F08

#define SAMPLEAPP_DEVICEID           0x0001

#define SAMPLEAPP_DEVICE_VERSION     0

#define SAMPLEAPP_FLAGS              0

 

#define SAMPLEAPP_MAX_CLUSTERS       6

//#define SAMPLEAPP_PERIODIC_CLUSTERID 7

#define SAMPLEAPP_FLASH_CLUSTERID    9

#define SAMPLEAPP_COM_CLUSTERID      10

 

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

#define SAMPLEAPP_DHT11_CLUSTERID 1

#define SAMPLEAPP_RE200B_CLUSTERID 2

#define SAMPLEAPP_MQ2_CLUSTERID  3

#define SAMPLEAPP_devExt_CLUSTERID 4

#define SAMPLEAPP_MQ7_CLUSTERID  5

#define SAMPLEAPP_STH11_CLUSTERID 6

#define SAMPLEAPP_Rain_CLUSTERID 7

#define SAMPLEAPP_PM2_5_CLUSTERID 8

#define SAMPLEAPP_PH_CLUSTERID   11

#define SAMPLEAPP_Light_CLUSTERID   12

#define SAMPLEAPP_DS18B20_CLUSTERID 13

#define SAMPLEAPP_MQ3_CLUSTERID  14

#define SAMPLEAPP_MQ4_CLUSTERID  15

#define SAMPLEAPP_MQ8_CLUSTERID  16

#define SAMPLEAPP_MQ135_CLUSTERID  17

#define SAMPLEAPP_ULTRASONIC_CLUSTERID  18

#define SAMPLEAPP_Bais_CLUSTERID  19

#define SAMPLEAPP_YF_CLUSTERID  20

#define SAMPLEAPP_MIC_CLUSTERID  21

#define SAMPLEAPP_GHG_CLUSTERID  22

#define SAMPLEAPP_INF_PHOTO_CLUSTERID  23

#define SAMPLEAPP_TWO_LIGHTS_CLUSTERID  24

#define SAMPLEAPP_FIRE_CLUSTERID  25

#define SAMPLEAPP_SPEED_CLUSTERID  26

#define SAMPLEAPP_CURRENT_CLUSTERID  27

#defineSAMPLEAPP_ACCELERATION_CLUSTERID  28

#define SAMPLEAPP_HUER_CLUSTERID  29

#define SAMPLEAPP_5388_CLUSTERID 30

#define SAMPLEAPP_APP_CLUSTERID 31

#define SAMPLEAPP_ADXL345_CLUSTERID  32

#define SAMPLEAPP_GY30_CLUSTERID  33

// Send Message Timeout

#defineSAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT  5000     // Every 5 seconds

#define SAMPLEAPP_5883_PERIODIC_MSG_TIMEOUT   500    // Every 1 seconds

// Application Events (OSAL) - These arebit weighted definitions.

#defineSAMPLEAPP_SEND_PERIODIC_MSG_EVT      0x0001

 

// Group ID for Flash Command

#define SAMPLEAPP_FLASH_GROUP                  0x0001

 

// Flash Command Duration - in milliseconds

#define SAMPLEAPP_FLASH_DURATION               1000

 

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

 *MACROS

 */

 

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

 *FUNCTIONS

 */

 

/*

 *Task Initialization for the Generic Application

 */

extern void SampleApp_Init( uint8 task_id);

 

/*

 *Task Event Processor for the Generic Application

 */

extern UINT16 SampleApp_ProcessEvent( uint8task_id, uint16 events );

 

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

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

 

#ifdef __cplusplus

}

#endif

 

#endif /* SAMPLEAPP_H */

 

 

 

ADXL345.C

 

#include <ioCC2530.h>

#include "hal_types.h"

#include"ADXL345.h"

#include "OnBoard.h"

#include<string.h>

 

#define     SlaveAddress   0xA6     //定义器件在IIC总线中的从地址,根据SA0地址引脚不同修改

#define SCL P1_6      //IIC时钟引脚定义

#define SDA P1_7      //IIC数据引脚定义

 

#define SDA_OUT()   P1DIR |= 0x80

#define SDA_IN()    P1DIR &= ~0x80

#define SCL_OUT()   P1DIR |= 0x40

 

int ADXL345X,ADXL345Y,ADXL345Z;

charADXL345X_str[7]={0},ADXL345Y_str[7]={0},ADXL345Z_str[7]={0};

char ADXL345_DATABUF[255]={0};

static void ADXL345_Delay5us(void);

static void ADXL345_Start(void);

static void ADXL345_Stop(void);

static void ADXL345_SendACK(char ack);

static char ADXL345_RecvACK(void);

static void ADXL345_SendByte(unsigned chardat);

static unsigned charADXL345_RecvByte(void);

static void Single_Write_ADXL345(unsignedchar REG_Address,unsigned char REG_data);

unsigned char Single_Read_ADXL345(unsignedchar REG_Address);

void Init_ADXL345(void);

void Multiple_read_ADXL345(void);

void type_change_ADXL345(int num,char*str);

 

 

 

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

延时5微秒

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

static void ADXL345_Delay5us(void)//uint16microSecs)

{

 MicroWait(20);

 /*uint8 microSecs = 5;

 while(microSecs--)

  {*/

   /* 32 NOPs == 1 usecs */

 /*  asm("nop");asm("nop"); asm("nop"); asm("nop"); asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop");

  }*/

}

 

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

起始信号

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

static void ADXL345_Start()

{

  SDA= 1;                    //拉高数据线

  SCL= 1;                    //拉高时钟线

 ADXL345_Delay5us();                

  SDA= 0;                    //产生下降沿

 ADXL345_Delay5us();             //延时

  SCL= 0;                    //拉低时钟线

 ADXL345_Delay5us();        

}

 

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

停止信号

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

static void ADXL345_Stop()

{

  SDA= 0;                    //拉低数据线

  SCL= 1;                    //拉高时钟线

 ADXL345_Delay5us();                //延时

  SDA= 1;                    //产生上升沿

 ADXL345_Delay5us();                //延时

}

 

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

发送应答信号

入口参数:ack (0:ACK 1:NAK)

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

static void ADXL345_SendACK(char ack)

{

  SDA= ack;                  //写应答信号

  SCL= 1;                    //拉高时钟线

 ADXL345_Delay5us();                //延时

  SCL= 0;                    //拉低时钟线

 ADXL345_Delay5us();                //延时

}

 

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

接收应答信号

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

static char ADXL345_RecvACK()

{

  SCL= 1;                    //拉高时钟线

 ADXL345_Delay5us();           

 SDA_IN();

  CY= SDA;                   //读应答信号

 SDA_OUT();

  SCL= 0;                    //拉低时钟线

 ADXL345_Delay5us();                //延时

 

 return CY;

}

 

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

向IIC总线发送一个字节数据

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

static void ADXL345_SendByte(unsigned chardat)

{

 uint8 i;

 

  for(i=0; i<8; i++)         //8位计数器

  {

   //dat <<= 1;              //移出数据的最高位

   if((0x80>>i)&dat)

    {

     SDA = 1;

    }

   else

    {

     SDA = 0;

   }//送数据口

   SCL = 1;                //拉高时钟线

   ADXL345_Delay5us();             //延时

   SCL = 0;                //拉低时钟线

   ADXL345_Delay5us();             //延时

  }

 ADXL345_RecvACK();

}

 

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

从IIC总线接收一个字节数据

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

static unsigned char ADXL345_RecvByte()

{

 uint8 i;

 uint8 dat = 0;

 uint8 test;

 

  SDA= 1;                    //使能内部上拉,准备读取数据,

  for(i=0; i<8; i++)         //8位计数器

  {

   SCL = 1;                //拉高时钟线

   ADXL345_Delay5us();             //延时

   SDA_IN();

   if(SDA)

    {

     test=0x01;

     test <<= (7-i);

     dat |= test;

    }

   else

    {

     test=0x01;

     test <<= (7-i);

     dat &= ~test;

    }

   SDA_OUT();

   SCL = 0;                //拉低时钟线

   ADXL345_Delay5us();             //延时

  }

 return dat;

}

 

//******单字节写入*******************************************

static void Single_Write_ADXL345(unsignedchar REG_Address,unsigned char REG_data)

{

 ADXL345_Start();                 //起始信号

 ADXL345_SendByte(SlaveAddress);  //发送设备地址+写信号

 ADXL345_SendByte(REG_Address);   //内部寄存器地址

 ADXL345_SendByte(REG_data);      //内部寄存器数据

 ADXL345_Stop();                  //发送停止信号

}

 

//********单字节读取*****************************************

 

unsigned char Single_Read_ADXL345(unsignedchar REG_Address)

 uint8 REG_data;

 ADXL345_Start();                         //起始信号

 ADXL345_SendByte(SlaveAddress);          //发送设备地址+写信号

 ADXL345_SendByte(REG_Address);                   //发送存储单元地址,从0开始      

 ADXL345_Start();                         //起始信号

 ADXL345_SendByte(SlaveAddress+1);        //发送设备地址+读信号

 REG_data=ADXL345_RecvByte();             //读出寄存器数据

 ADXL345_SendACK(1);  

 ADXL345_Stop();                           //停止信号

 return REG_data;

}

 

 

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

 

//初始化ADXL345,根据需要请参考pdf进行修改************************

void Init_ADXL345(void)

{

 P1SEL &= ~0xc0; //作为普通 IO 口

 //P1INP &= ~0Xc0; //打开上拉

 //P1INP |= 0Xc0; //打开下拉

 SDA_OUT();

 SCL_OUT();

  

  Single_Write_ADXL345(0x31,0x0B);  //测量范围,正负16g,13位模式

  Single_Write_ADXL345(0x2C,0x08);  //速率设定为12.5 参考pdf13页

  Single_Write_ADXL345(0x2D,0x08);  //选择电源模式   参考pdf24页

  Single_Write_ADXL345(0x2E,0x80);  //使能 DATA_READY 中断

  Single_Write_ADXL345(0x1E,0x00);  //X 偏移量根据测试传感器的状态写入pdf29页

  Single_Write_ADXL345(0x1F,0x00);  //Y 偏移量根据测试传感器的状态写入pdf29页

  Single_Write_ADXL345(0x20,0x05);  //Z 偏移量根据测试传感器的状态写入pdf29页

}

 

 

 

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

//

//连续读出ADXL345内部加速度数据,地址范围0x01~0x06

//

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

void Multiple_read_ADXL345(void)

{  

 int8 BUF[6];                        //接收数据缓存区 

  uint8i;

 ADXL345_Start();                         //起始信号

 ADXL345_SendByte(SlaveAddress);          //发送设备地址+写信号

 ADXL345_SendByte(0x32);                  //发送存储单元地址,从0x01开始       

 ADXL345_Start();                         //起始信号

 ADXL345_SendByte(SlaveAddress+1);        //发送设备地址+读信号

  for(i=0; i<6; i++)                     //连续读取6个地址数据,存储中BUF

  {

   BUF[i] = ADXL345_RecvByte();         //BUF[0]存储0x32地址中的数据

   if (i == 5)

    {

     ADXL345_SendACK(1);               //最后一个数据需要回NOACK

    }

   else

    {

     ADXL345_SendACK(0);               //回应ACK

    }

  }

 ADXL345_Stop();                         //停止信号

   

 ADXL345X=(BUF[1]<<8)|BUF[0];

 ADXL345Y=(BUF[3]<<8)|BUF[2];

 ADXL345Z=(BUF[5]<<8)|BUF[4];

 

 HalUARTWrite(0,"the data is ",12);

 type_change_ADXL345(ADXL345X,ADXL345X_str);

 HalUARTWrite(0,"  ",2);

 type_change_ADXL345(ADXL345Y,ADXL345Y_str);

 HalUARTWrite(0,"  ",2);

 type_change_ADXL345(ADXL345Z,ADXL345Z_str);

 HalUARTWrite(0,"\n",1);

 strcat(ADXL345_DATABUF,&ADXL345X_str[1]);

 strcat(ADXL345_DATABUF,&ADXL345Y_str[1]);

 strcat(ADXL345_DATABUF,&ADXL345Z_str[1]);

 

 HalUARTWrite(0,ADXL345_DATABUF,strlen(ADXL345_DATABUF));

 HalUARTWrite(0,"\n",1);

 

 memset(ADXL345X_str,0,strlen(ADXL345X_str));

 memset(ADXL345Y_str,0,strlen(ADXL345Y_str));

 memset(ADXL345Z_str,0,strlen(ADXL345Z_str));

}

 

void type_change_ADXL345(int num,char *str)

{

 uint8 temp[5];

 if(num<0)

  {

   HalUARTWrite(0,"-",1);

   str[0]='-';

   num = -num;

  }

 else

  {

   HalUARTWrite(0,"+",1);

   str[0]='+';

  }

 str[1]=temp[0] = num/10000+0x30;

 str[2]=temp[1] = num%10000/1000+0x30;

 str[3]=temp[2] = num%1000/100+0x30;

 str[4]=temp[3] = num%100/10+0x30;

 str[5]=temp[4] = num%10+0x30;

 HalUARTWrite(0,&temp[0],5);

}

 

 

ADXL345.H

#ifndef _ADXL345_H_

#define _ADXL345_H_

 

 

extern int ADXL345X,ADXL345Y,ADXL345Z;

extern void Init_ADXL345(void);

extern void Multiple_read_ADXL345(void);

extern unsigned charSingle_Read_ADXL345(unsigned char REG_Address);

extern char ADXL345_DATABUF[255];

 

#endif

 

 

 

 

APP.C

#include<iocc2530.h>

#include<Onboard.h>

#include"APP_SWITCH.h"

#define APP_SWITCH P2_0

void Init_APP_SWITCH_Io(void);

unsigned char APP_SWITCH_Scan(void);

 

void Init_APP_SWITCH_Io()

{

  APP_SWITCH=0;

  P2DIR&=~0x01;

  P2SEL&=~0x01;

  P2INP&=~0x01;

}

 

unsigned char APP_SWITCH_Scan()

{

   if(APP_SWITCH==1)

  {

     Onboard_wait(10000);  

     if(APP_SWITCH==1)

     {     

       return 1; 

     }

  }

 return 0;      

}

 

APP.h

 

#ifndef _APP_SWITCH__H//继电器控制端

#define _APP_SWITCH__H

extern void Init_APP_SWITCH_Io(void);

extern unsigned char APP_SWITCH_Scan(void);

#endif

 

 

Ascii_to_bcd.c

/*

 *Name: Ascii_to_bcd.c

 */

 

/*

 *exchange the ascii code to bcd code

 */

// 当covn_len是奇数时,type = 0bcd 前部0,type =1 bcd后补0

// 当covn_len是偶数时,type 不起作用

// return 0: 成功

// return -1:失败,ascci不合法

#include "MT_UART.h"

#include "Ascii_to_bcd.h"

int asc_to_bcd ( unsigned char   * bcd_buf, unsigned char   * ascii_buf, int conv_len, unsignedchar  type )

{

 

int        cnt;

unsigned char  ch, ch1;

// 设置一个标志ch1 = 0xaa 表示需要先暂存BCD的高字节,把这个字节就放在ch1里

// 如果循环判断ch1 不是0xaa ,表示高字节已经来了,就把高字节左移4位与当前字节组合成一个字节,然后再把ch1设置成0xaa

             if ( conv_len & 0x01 &&type )

              {

        // conv_len & 0x1 如果是真,表示ascci_buf的长度是奇数,type 是1的话,表示前补0

       //所以此时ch1 = 0,表示高字节已经来了。

                ch1 = 0;

       }

             else

             {

              //表示高字节没来,需要先把高字节暂存

                ch1 = 0xaa;

       }

             for ( cnt = 0; cnt < conv_len;ascii_buf ++, cnt ++ ) {

                //HalUARTWrite(0,ascii_buf,2);

                if (( * ascii_buf >='a' ) && ( * ascii_buf <= 'f' ))

                     ch = * ascii_buf - 'a' + 10;

                else if (( *ascii_buf>= 'A' )&&( *ascii_buf <= 'F' ))

                     ch = * ascii_buf - 'A' + 10;

                else if (( * ascii_buf>= '0' )&&( * ascii_buf <= '9' ))

                     ch = * ascii_buf - '0';

                else

                     return -1;

 

                if ( ch1 == 0xaa )

                        ch1 = ch;

                else {

                     * bcd_buf ++ = ( ch1 << 4 ) | ch;

                     ch1 = 0xaa;

                }

           }

// 最后需要判断ch1如果是0xaa,表示全部处理完毕。否则说明留一个高字节,需要左移4位,后补0

              if ( ch1 != 0xaa )

                * bcd_buf = ch1 <<4;

             return 0;

}

 

Ascii_to_bcd.h

#ifndef __ASCII_TO_BCD_H__

#define __ASCII_TO_BCD_H__

int asc_to_bcd ( unsigned char   * bcd_buf, unsigned char   * ascii_buf, int conv_len, unsignedchar  type );

#endif

 

 

Bais.c

 

#include<iocc2530.h>

#include<Onboard.h>

#include"Bais.h"

#define Bais  P2_0

void Bais_Init();

uchar Bais_Scan();

void Bais_Init()

{

  Bais=0;

 P2DIR&=~0x01;

 P2SEL&=~~0x01;

 P2INP&=~0x01;

}

uchar Bais_Scan()

{

   if(Bais==1)

  {

     Onboard_wait(10000);  

     if(Bais==1)

     {     

       return 1; 

     }

  }

 return 0;      

}

 

Bais.h

#ifndef _BAIS_H__

#define _BAIS_H__

#define uchar unsigned char

#define uint unsigned int

extern void Bais_Init();

extern uchar Bais_Scan();

#endif

 

 

 

BcdtoASCII.c

#include"BCD_To_ASCII.h"

#include<string.h>

int BCD_To_ASCII(unsigned char*BCD_BUF,unsigned char *ASCII_BUF,int len)

{

   int i;

   for(i=0;i<len;i++)

    {

      if(BCD_BUF[i]<=0x09)

        ASCII_BUF[i]=BCD_BUF[i]+0x30;

      else

      if(0x09<BCD_BUF[i])

        ASCII_BUF[i]=BCD_BUF[i]-0x0a+'a';

      else

        return -1;

      

     /*BCD_BUF[i]<=0x09?ASCII_BUF[i]=BCD_BUF[i]+0x30:

     0x09<BCD_BUF[i]&&BCD_BUF[i]<=0x0f?ASCII_BUF[i]=BCD_BUF[i]-0x0a+'a':return-1;

     return 0;*/

    }

   return 0;

}

 

BcdtoASCII.h

#ifndef __BCD_To_ASCII_H__

#define __BCD_To_ASCII_H__

int BCD_To_ASCII(unsigned char*BCD_BUF,unsigned char *ASCII_BUF,int len);

#endif

 

 

 

CRC.c

 

#include "crc.h"

int crc_sum(unsigned char *bcd_buf,int len)

{

  inti,crc=0;

 for(i=0;i<len;i++)

{

    crc+=bcd_buf[i];

    //fprintf(stderr,"%2x",bcd_buf[i]);

 }

 return crc;

}

 

 

int BC_Sum(unsigned char *bcd_buf,int len)

{

  int i,bc=0;

 for(i=0;i<len;i++)

{

    bc^=bcd_buf[i];

    //fprintf(stderr,"%2x",bcd_buf[i]);

 }

 return bc;

}

 

 

 

CRC.h

#ifndef __CRC_H__

#define __CRC_H__

int crc_sum(unsigned char *bcd_buf,intlen);//校验和

int BC_Sum(unsigned char *bcd_buf,intlen);//异或校验

#endif

 

 

 

Current.c

#include<iocc2530.h>

#include<Onboard.h>

#include"CURRENT.h"

#define CURRENT  P2_0

void CURRENT_Init();

uchar CURRENT_Scan();

void CURRENT_Init()

{

  CURRENT=0;

    P2DIR&=~0x01;

 //P2SEL&=~~0x01;

 //P2INP&=~0x01;

}

uchar CURRENT_Scan()

{

   if(CURRENT==1)

  {

     Onboard_wait(10000);  

     if(CURRENT==1)

     {     

       return 1;  

     }

  }

 return 0;      

}

 

 

Current.h

#ifndef _CURRENT_H__

#define _CURRENT_H__

#define uchar unsigned char

#define uint unsigned int

extern void CURRENT_Init();

extern uchar CURRENT_Scan();

#endif

 

 

Delay.c

#include<iocc2530.h>

#include"Delay.h"

#include"type.h"

void delayus_1(uint16 n);

void delayus_10(uint16 n);//延时n*10us

void delayMs(uint16 n);

void delayus_1(uint16 n)

{

 while (n--)

  {

   asm("NOP");

   asm("NOP");

   asm("NOP");

  }

}

 

void delayus_10(uint16 n)//延时n*10us

{

 uint16 timeout=n*10;

 while (timeout--)

  {

   asm("NOP");

   asm("NOP");

   asm("NOP");

  }

 

}

 

//32M

void delayMs(uint16 n)

{

 uint16 i,j;

 for(i=0;i<n;i++)

  {

   for(j=0;j<1774;j++);

  }

}

 

 

 

Delay.h

#ifndef _DELAY_H__

#define _DELAY_H__

#include"type.h"

extern void delayus_1(uint16 n);

extern void delayus_10(uint16 n);//延时n*10us

extern void delayMs(uint16 n);

 

#endif

 

DHT11.c

 

#include <ioCC2530.h>

#include "OnBoard.h"

 

#define uint unsigned int

#define uchar unsigned char

 

#define wenshi P0_6

//#define SRD    P2_1

/*******函数声明*********/

void Delay_us(void); //1 us延时

void Delay_10us(void); //10 us延时

void Delay_ms(uint Time);//n ms延时

void COM(void);       // 温湿写入

void DHT11_TEST(void) ;  //温湿传感启动

 

 

//温湿度定义

uchar ucharFLAG,uchartemp;

ucharshidu_shi,shidu_ge,wendu_shi,wendu_ge=4;

ucharucharT_data_H,ucharT_data_L,ucharRH_data_H,ucharRH_data_L,ucharcheckdata;

uchar ucharT_data_H_temp,ucharT_data_L_temp,ucharRH_data_H_temp,ucharRH_data_L_temp,ucharcheckdata_temp;

uchar ucharcomdata;

 

uchar temp[2]={0,0};

uchar temp1[5]="temp=";

uchar humidity[2]={0,0};

uchar humidity1[9]="humidity=";

 

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

//延时函数

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

void Delay_us(void) //1 us延时

 

{

   MicroWait(1);  

}

 

void Delay_10us(void) //10 us延时

{

  MicroWait(10);

}

 

void Delay_ms(uint Time)//n ms延时

{

 unsigned char i;

 while(Time--)

  {

   for(i=0;i<100;i++)

    Delay_10us();

  }

}

 

 

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

   温湿度传感

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

void COM(void)         // 温湿写入

{    

   uchar i;        

   for(i=0;i<8;i++)   

    {

    ucharFLAG=2;

    while((!wenshi)&&ucharFLAG++);

    Delay_10us();

    Delay_10us();

    Delay_10us();

    uchartemp=0;

    if(wenshi)uchartemp=1;

    ucharFLAG=2;

    while((wenshi)&&ucharFLAG++);  

    if(ucharFLAG==1)break;   

    ucharcomdata<<=1;

    ucharcomdata|=uchartemp;

    }   

}

 

void DHT11_TEST(void)   //温湿传感启动

{

   wenshi=0;

   Delay_ms(19);  //>18MS

   wenshi=1;

   P0DIR&=~0x40;

   //P0SEL&=~0x40;

   //P0INP&=~0x40;

   //P2DIR|=0x02;

   Delay_10us();

   Delay_10us();                                                

   Delay_10us();

   Delay_10us(); 

    if(!wenshi)

    {

     ucharFLAG=2;

     while((!wenshi)&&ucharFLAG++);

     ucharFLAG=2;

     while((wenshi)&&ucharFLAG++);

     COM();

     ucharRH_data_H_temp=ucharcomdata;

     COM();

     ucharRH_data_L_temp=ucharcomdata;

     COM();

     ucharT_data_H_temp=ucharcomdata;

     COM();

     ucharT_data_L_temp=ucharcomdata;

     COM();

     ucharcheckdata_temp=ucharcomdata;

     wenshi=1;

     uchartemp=(ucharT_data_H_temp+ucharT_data_L_temp+ucharRH_data_H_temp+ucharRH_data_L_temp);

      if(uchartemp==ucharcheckdata_temp)

     {

         ucharRH_data_H=ucharRH_data_H_temp;

         ucharRH_data_L=ucharRH_data_L_temp;

         ucharT_data_H=ucharT_data_H_temp;

         ucharT_data_L=ucharT_data_L_temp;

         ucharcheckdata=ucharcheckdata_temp;

      }

        wendu_shi=ucharT_data_H/10;

        wendu_ge=ucharT_data_H%10;

          

        shidu_shi=ucharRH_data_H/10;

        shidu_ge=ucharRH_data_H%10;

        /*if(ucharT_data_H > 25|| ucharRH_data_H>40)

         SRD=1;//开继电器

        else

         SRD=0;//关继电器

        */

    }

   else //没用成功读取,返回0

    {

        wendu_shi=0;

        wendu_ge=0;

          

        shidu_shi=0;

        shidu_ge=0; 

        

    }

   P0DIR|=0x40;

}

 

Ds18b20.c

#include"iocc2530.h"

#include"ds18b20.h"

#include"mt_uart.h"

#include"OnBoard.h"

void display(int num)

{

   char T[4]={0};

   T[0]='0'+num/1000;

   T[1]='0'+num%1000/100;

   T[2]='0'+num%100/10;

   T[3]='0'+num%10;

   HalUARTWrite(0,T,4);

}

 

 

/*//时钟频率为32M

void Onboard_wait(uint k) //oK

{

 

 uint i,j;

 for(i=0;i<k;i++)

 for(j=0;j<2;j++);

}

*/

void Ds18b20InputInitial(void)//设置端口为输入  OK

{

 P0DIR&=~0x40;

 P0SEL&=~0x40;

}

 

void Ds18b20OutputInitial(void)//设置端口为输出  OK

{

  P0DIR|=0x40;

 P0SEL&=~0x40;

}

 

//ds18b20初始化

//初始化成功返回0x00,失败返回0x01   OK

uchar Ds18b20Initial(void)

{

 uchar Status = 0x00;

 uint CONT_1 = 0;

 uchar Flag_1 = ON;

 Ds18b20OutputInitial();

 Ds18b20Data = 1;

 Onboard_wait(260);

 Ds18b20Data = 0;

 Onboard_wait(750);

 Ds18b20Data = 1;

 Ds18b20InputInitial();

 while((Ds18b20Data != 0)&&(Flag_1 == ON))//等待ds18b20响应,具有防止超时功能

 {                                       //等待约60ms左右

   CONT_1++;

   Onboard_wait(10);

   if(CONT_1 > 8000)Flag_1 = OFF;

   Status = Ds18b20Data;

  }

 Ds18b20OutputInitial();

 Ds18b20Data = 1;

 Onboard_wait(100);

 return Status;

}

 

 

void Ds18b20Write(uchar infor)//OK

{

 uint i;

 Ds18b20OutputInitial();

 for(i=0;i<8;i++)

  {

 if((infor & 0x01))

  {

 Ds18b20Data = 0;

 Onboard_wait(6);

 Ds18b20Data = 1;

 Onboard_wait(50);

  }

 else

  {

 Ds18b20Data = 0;

 Onboard_wait(50);

 Ds18b20Data = 1;

 Onboard_wait(6);

  }

 infor >>= 1;

  }

}

 

uchar Ds18b20Read(void)   //OK

{

 uchar Value = 0x00;

 uint i;

 for(i=0;i<8;i++)

  {

   Ds18b20OutputInitial();

   Ds18b20Data = 1;

   Onboard_wait(1);

   Value >>= 1;

   Ds18b20Data = 0;

   Onboard_wait(3);

   Ds18b20Data = 1;

   Onboard_wait(2);

   Ds18b20InputInitial();

   if(Ds18b20Data == 1) Value |= 0x80;

   Onboard_wait(15);

  }

 //display(Value);

 return Value;

}

 

 

int Temp_test(void) //温度读取函数

{

 uchar V1,V2;

 float temp_float=0;

  inttemp_18B20=0; //储存温度信息

  inttemp=0;

 Ds18b20Initial();

 Onboard_wait(1);

 Ds18b20Write(0xcc);

 Ds18b20Write(0x44);

 

 Ds18b20Initial();

 Onboard_wait(1);

 Ds18b20Write(0xcc);

 Ds18b20Write(0xbe);

 

  V1=Ds18b20Read();

  V2=Ds18b20Read();

 temp_18B20 =V2<<8|V1;

 temp_float=temp_18B20*0.0625;

 temp_18B20=temp_float*10+0.5;

 //temp = ((V1 >> 4)+((V2 & 0x07)*16)); 

 //display(temp_18B20);

 return temp_18B20;//精确到小数点后一位

 //return temp;//不能精确到小数点

}

 

Ds18B20.h

#ifndef _DS18B20_H__

#define _DS18B20_H__

#define uint unsigned int

#define uchar unsigned char

 

#define Ds18b20Data P0_6 //温度传感器引脚

 

#define ON 0x01  //读取成功返回0x00,失败返回0x01

#define OFF 0x00

 

void display(int num);

void Ds18b20Delay(uint k);

void Ds18b20InputInitial(void);//设置端口为输入

void Ds18b20OutputInitial(void);

uchar Ds18b20Initial(void);

void Ds18b20Write(uchar infor);

uchar Ds18b20Read(void);

int Temp_test(void);

   

#endif

 

Fire.c

#include<iocc2530.h>

#include<Onboard.h>

#include"FIRE.h"

#define FIRE  P2_0

void FIRE_Init();

uchar FIRE_Scan();

void FIRE_Init()

{

  FIRE=0;

  P2DIR&=~0x01;

  P2SEL&=~0x01;

  P2INP&=~0x01;

}

uchar FIRE_Scan()

{

   if(FIRE==1)

  {

     Onboard_wait(10000);  

     if(FIRE==1)

     {     

       return 1;   //有火

     }

  }

 return 0;      

}

 

Fire.h

#ifndef _FIRE_H__//红外光电

#define _FIRE_H__

#define uchar unsigned char

#define uint unsigned int

extern void FIRE_Init();

extern uchar FIRE_Scan();

#endif

 

 

GHG.c

#include<iocc2530.h>

#include<Onboard.h>

#include"GHG.h"

#define GHG P2_0

void GHG_Init();

uchar GHG_Scan();

void GHG_Init()

{

  GHG=0;

  P2DIR&=~0x01;

  P2SEL&=~0x01;

  P2INP&=~0x01;

}

uchar GHG_Scan()

{

   if(GHG==1)

  {

     Onboard_wait(10000);  

     if(GHG==1)

     {     

       return 1;   //有烟

     }

  }

 return 0;        //无烟

}

 

GHG.h

#ifndef _GHG_H__//单簧管

#define _GHG_H__

#define uchar unsigned char

#define uint unsigned int

extern void GHG_Init();

extern uchar GHG_Scan();

#endif

 

 

HMC5388L.c

#include <ioCC2530.h>

#include "hal_types.h"

#include"HMC5388L.h"

#include "OnBoard.h"

#include<string.h>

#include <math.h>

 

#define     SlaveAddress   0x3C     //定义器件在IIC总线中的从地址,根据SA0地址引脚不同修改

#define SCL P1_6      //IIC时钟引脚定义

#define SDA P1_7      //IIC数据引脚定义

 

#define SDA_OUT()   P1DIR |= 0x80

#define SDA_IN()    P1DIR &= ~0x80

#define SCL_OUT()   P1DIR |= 0x40

intmma_5388X_5388=0,mma_5388Y_5388=0,mma_5388Z_5388=0;

char mma_5388X_5388_str[7]={0},mma_5388Y_5388_str[7]={0},mma_5388Z_5388_str[7]={0};

//char mma_data_str[7]={0};

char HMC5388L_DATABUF[255]={0};

static void mma_5388_Delay5us(void);

static void HMC5388L_Start(void);

static void HMC5388L_Stop(void);

static void HMC5388L_SendACK(char ack);

static char HMC5388L_RecvACK(void);

static void HMC5388L_SendByte(unsigned chardat);

static unsigned charHMC5388L_RecvByte(void);

static void Single_Write_HMC5388L(unsignedchar REG_Address,unsigned char REG_data);

unsigned char Single_Read_HMC5388L(unsignedchar REG_Address);

void Init_HMC5388L(void);

void Multiple_read_HMC5388L(void);

void type_change_5388(int num,char *str);

 

 

 

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

延时5微秒

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

static void mma_5388_Delay5us(void)//uint16microSecs)

{

 MicroWait(20);

 /*uint8 microSecs = 5;

 while(microSecs--)

  {*/

   /* 32 NOPs == 1 usecs */

 /*  asm("nop");asm("nop"); asm("nop"); asm("nop");asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop");

  }*/

}

 

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

起始信号

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

static void HMC5388L_Start()

{

  SDA= 1;                    //拉高数据线

  SCL = 1;                    //拉高时钟线

 mma_5388_Delay5us();                

  SDA= 0;                    //产生下降沿

 mma_5388_Delay5us();            //延时

  SCL= 0;                    //拉低时钟线

 mma_5388_Delay5us();        

}

 

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

停止信号

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

static void HMC5388L_Stop()

{

  SDA= 0;                    //拉低数据线

  SCL= 1;                    //拉高时钟线

 mma_5388_Delay5us();                //延时

  SDA= 1;                    //产生上升沿

 mma_5388_Delay5us();                //延时

}

 

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

发送应答信号

入口参数:ack (0:ACK 1:NAK)

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

static void HMC5388L_SendACK(char ack)

{

  SDA= ack;                  //写应答信号

  SCL= 1;                    //拉高时钟线

 mma_5388_Delay5us();                //延时

  SCL= 0;                    //拉低时钟线

 mma_5388_Delay5us();                //延时

}

 

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

接收应答信号

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

static char HMC5388L_RecvACK()

{

  SCL= 1;                    //拉高时钟线

 mma_5388_Delay5us();           

 SDA_IN();

  CY= SDA;                   //读应答信号

 SDA_OUT();

  SCL= 0;                    //拉低时钟线

 mma_5388_Delay5us();                //延时

 

 return CY;

}

 

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

向IIC总线发送一个字节数据

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

static void HMC5388L_SendByte(unsigned chardat)

{

 uint8 i;

 

  for(i=0; i<8; i++)         //8位计数器

  {

   //dat <<= 1;              //移出数据的最高位

   if((0x80>>i)&dat)

    {

     SDA = 1;

    }

   else

    {

     SDA = 0;

   }//送数据口

   SCL = 1;                //拉高时钟线

   mma_5388_Delay5us();            //延时

   SCL = 0;                //拉低时钟线

   mma_5388_Delay5us();            //延时

  }

 HMC5388L_RecvACK();

}

 

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

从IIC总线接收一个字节数据

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

static unsigned char HMC5388L_RecvByte()

{

 uint8 i;

 uint8 dat = 0;

 uint8 test;

 

  SDA= 1;                    //使能内部上拉,准备读取数据,

  for(i=0; i<8; i++)         //8位计数器

  {

   SCL = 1;                //拉高时钟线

   mma_5388_Delay5us();            //延时

   SDA_IN();

   if(SDA)

    {

     test=0x01;

     test <<= (7-i);

     dat |= test;

    }

   else

    {

     test=0x01;

     test <<= (7-i);

     dat &= ~test;

    }

   SDA_OUT();

   SCL = 0;                //拉低时钟线

   mma_5388_Delay5us();            //延时

  }

 return dat;

}

 

//******单字节写入*******************************************

static void Single_Write_HMC5388L(unsignedchar REG_Address,unsigned char REG_data)

{

 HMC5388L_Start();                 //起始信号

 HMC5388L_SendByte(SlaveAddress);  //发送设备地址+写信号

 HMC5388L_SendByte(REG_Address);   //内部寄存器地址

 HMC5388L_SendByte(REG_data);      //内部寄存器数据

 HMC5388L_Stop();                   //发送停止信号

}

 

//********单字节读取*****************************************

 

unsigned char Single_Read_HMC5388L(unsignedchar REG_Address)

 uint8 REG_data;

 HMC5388L_Start();                          //起始信号

 HMC5388L_SendByte(SlaveAddress);           //发送设备地址+写信号

 HMC5388L_SendByte(REG_Address);                   //发送存储单元地址,从0开始  

 HMC5388L_Start();                          //起始信号

 HMC5388L_SendByte(SlaveAddress+1);         //发送设备地址+读信号

 REG_data=HMC5388L_RecvByte();              //读出寄存器数据

 HMC5388L_SendACK(1);  

 HMC5388L_Stop();                           //停止信号

 return REG_data;

}

 

 

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

 

//初始化HMC5388L,根据需要请参考pdf进行修改************************

void Init_HMC5388L(void)

{

 P1SEL &= ~0xc0; //作为普通 IO 口

 //P1INP &= ~0Xc0; //打开上拉

 //P1INP |= 0Xc0; //打开下拉

 SDA_OUT();

 SCL_OUT();

 

 Single_Write_HMC5388L(0x02,0x00);  

 //Single_Write_HMC5388L(0x2B,0x02); 

}

 

 

 

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

//

//连续读出HMC5388L内部加速度数据,地址范围0x01~0x06

//

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

void Multiple_read_HMC5388L(void)

{  

 uint8 BUF[6]={0};                        //接收数据缓存区 

 uint8 i;

 double angle=0;

 HMC5388L_Start();                          //起始信号

 HMC5388L_SendByte(SlaveAddress);           //发送设备地址+写信号

 HMC5388L_SendByte(0x03);                   //发送存储单元地址,从0x01开始   

 HMC5388L_Start();                          //起始信号

 HMC5388L_SendByte(SlaveAddress+1);         //发送设备地址+读信号

  for(i=0; i<6; i++)                     //连续读取6个地址数据,存储中BUF

  {

   BUF[i] = HMC5388L_RecvByte();         //BUF[0]存储0x32地址中的数据

   if (i == 5)

    {

     HMC5388L_SendACK(1);               //最后一个数据需要回NOACK

    }

   else

    {

     HMC5388L_SendACK(0);               //回应ACK

    }

  }

 

 

 mma_5388X_5388=((BUF[1]<<8)&0xff00)|BUF[0];

 mma_5388Y_5388=((BUF[3]<<8)&0xff00)|BUF[2];

 mma_5388Z_5388=((BUF[5]<<8)&0xff00)|BUF[4];

 

  angle= atan2((double)mma_5388Y_5388,(double)mma_5388X_5388) * (180 /3.14159265) + 180; // angle in degrees

  angle*=10;

  HalUARTWrite(0,"the data is ",12);

  type_change_5388(angle,HMC5388L_DATABUF);

  HalUARTWrite(0,"  ",2);

  /*

 HalUARTWrite(0,"the data is ",12);

 type_change_5388(mma_5388X_5388,mma_5388X_5388_str);

 HalUARTWrite(0,"  ",2);

 type_change_5388(mma_5388Y_5388,mma_5388Y_5388_str);

 HalUARTWrite(0,"  ",2);

 type_change_5388(mma_5388Z_5388,mma_5388Z_5388_str);

 HalUARTWrite(0,"\n",1);

 strcat(HMC5388L_DATABUF,&mma_5388X_5388_str[1]);

 strcat(HMC5388L_DATABUF,&mma_5388Y_5388_str[1]);

 strcat(HMC5388L_DATABUF,&mma_5388Z_5388_str[1]);

 

 HalUARTWrite(0,HMC5388L_DATABUF,strlen(HMC5388L_DATABUF));

 HalUARTWrite(0,"\n",1);*/

  //memset(mma_5388X_5388_str,0,strlen(mma_5388X_5388_str));

 //memset(mma_5388Y_5388_str,0,strlen(mma_5388Y_5388_str));

 //memset(mma_5388Z_5388_str,0,strlen(mma_5388Z_5388_str));

}

 

void type_change_5388(int num,char *str)

{

 uint8 temp[5];

 if(num<0)

  {

   //HalUARTWrite(0,"-",1);

   str[0]='-';

   num = -num;

  }

 else

  {

   //HalUARTWrite(0,"+",1);

   temp[0]='+';

  }

  /*

 str[1]=temp[0] = num/10000+0x30;

 str[2]=temp[1] = num%10000/1000+0x30;

 str[3]=temp[2] = num%1000/100+0x30;

 str[4]=temp[3] = num%100/10+0x30;

 str[5]=temp[4] = num%10+0x30;

  */

 str[0]=temp[1] = num%10000/1000+0x30;

 str[1]=temp[2] = num%1000/100+0x30;

 str[2]=temp[3] = num%100/10+0x30;

 str[3]=temp[4] = num%10+0x30;

 HalUARTWrite(0,&temp[0],5);

}

 

HMC5388L.h

#ifndef _HMC5388L_H_

#define _HMC5388L_H_

 

 

 

extern intmma_5388X_5388,mma_5388Y_5388,mma_5388Z_5388;

extern void Init_HMC5388L(void);

extern void Multiple_read_HMC5388L(void);

extern unsigned charSingle_Read_HMC5388L(unsigned char REG_Address);

extern char HMC5388L_DATABUF[255];

 

#endif

 

HUER_SWITCH.C

#include<iocc2530.h>

#include<Onboard.h>

#include"HUER_SWITCH.h"

#define HUER_SWITCH P2_0

void Init_HUER_SWITCH_Io(void);

unsigned char HUER_SWITCH_Scan(void);

 

void Init_HUER_SWITCH_Io()

{

  HUER_SWITCH=0;

  P2DIR&=~0x01;

  P2SEL&=~0x01;

  P2INP&=~0x01;

}

 

unsigned char HUER_SWITCH_Scan()

{

   if(HUER_SWITCH==1)

  {

     Onboard_wait(10000);  

     if(HUER_SWITCH==1)

     {     

       return 1; 

     }

  }

 return 0;      

}

 

 

 

HUER_SWITCH.h

 

#ifndef _HUER_SWITCH__H//继电器控制端

#define _HUER_SWITCH__H

extern void Init_HUER_SWITCH_Io(void);

extern unsigned charHUER_SWITCH_Scan(void);

#endif

 

 

Inf_PHOTO.C

#include<iocc2530.h>

#include<Onboard.h>

#include"INF_PHOTO.h"

#define INF_PHOTO  P2_0

void INF_PHOTO_Init();

uchar INF_PHOTO_Scan();

void INF_PHOTO_Init()

{

  INF_PHOTO=0;

  P2DIR&=~0x01;

  P2SEL&=~0x01;

  P2INP&=~0x01;

}

uchar INF_PHOTO_Scan()

{

   if(INF_PHOTO==1)

  {

     Onboard_wait(10000);  

     if(INF_PHOTO==1)

     {     

        return 1;  //有烟

     }

  }

 return 0;        //无烟

}

 

INF_PHOTO.h

#ifndef _INF_PHOTO_H__//红外光电

#define _INF_PHOTO_H__

#define uchar unsigned char

#define uint unsigned int

extern void INF_PHOTO_Init();

extern uchar INF_PHOTO_Scan();

#endif

 

 

MMA8451.c

#include <ioCC2530.h>

#include "hal_types.h"

#include"mma8451.h"

#include "OnBoard.h"

#include<string.h>

 

#define     SlaveAddress   0x38      //定义器件在IIC总线中的从地址,根据SA0地址引脚不同修改

#define SCL P1_6      //IIC时钟引脚定义

#define SDA P1_7      //IIC数据引脚定义

 

#define SDA_OUT()   P1DIR |= 0x80

#define SDA_IN()    P1DIR &= ~0x80

#define SCL_OUT()   P1DIR |= 0x40

 

int mmaX,mmaY,mmaZ;

charmmaX_str[7]={0},mmaY_str[7]={0},mmaZ_str[7]={0};

char MMA8452_DATABUF[255]={0};

static void mma_Delay5us(void);

static void MMA8452_Start(void);

static void MMA8452_Stop(void);

static void MMA8452_SendACK(char ack);

static char MMA8452_RecvACK(void);

static void MMA8452_SendByte(unsigned chardat);

static unsigned charMMA8452_RecvByte(void);

static void Single_Write_MMA8452(unsignedchar REG_Address,unsigned char REG_data);

unsigned char Single_Read_MMA8452(unsignedchar REG_Address);

void Init_MMA8452(void);

void Multiple_read_MMA8452(void);

void type_change(int num,char *str);

 

 

 

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

延时5微秒

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

static void mma_Delay5us(void)//uint16microSecs)

{

 MicroWait(20);

 /*uint8 microSecs = 5;

 while(microSecs--)

  {*/

   /* 32 NOPs == 1 usecs */

 /*  asm("nop");asm("nop"); asm("nop"); asm("nop");asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop");

  }*/

}

 

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

起始信号

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

static void MMA8452_Start()

{

  SDA= 1;                    //拉高数据线

  SCL= 1;                    //拉高时钟线

 mma_Delay5us();                

  SDA= 0;                    //产生下降沿

 mma_Delay5us();             //延时

  SCL= 0;                    //拉低时钟线

 mma_Delay5us();        

}

 

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

停止信号

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

static void MMA8452_Stop()

{

  SDA= 0;                    //拉低数据线

  SCL= 1;                    //拉高时钟线

 mma_Delay5us();                 //延时

  SDA= 1;                    //产生上升沿

 mma_Delay5us();                 //延时

}

 

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

发送应答信号

入口参数:ack (0:ACK 1:NAK)

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

static void MMA8452_SendACK(char ack)

{

  SDA= ack;                  //写应答信号

  SCL= 1;                    //拉高时钟线

 mma_Delay5us();                 //延时

  SCL= 0;                    //拉低时钟线

 mma_Delay5us();                 //延时

}

 

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

接收应答信号

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

static char MMA8452_RecvACK()

{

  SCL= 1;                    //拉高时钟线

 mma_Delay5us();           

 SDA_IN();

  CY= SDA;                   //读应答信号

 SDA_OUT();

  SCL= 0;                    //拉低时钟线

 mma_Delay5us();                 //延时

 

 return CY;

}

 

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

向IIC总线发送一个字节数据

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

static void MMA8452_SendByte(unsigned chardat)

{

 uint8 i;

 

  for(i=0; i<8; i++)         //8位计数器

  {

   //dat <<= 1;              //移出数据的最高位

   if((0x80>>i)&dat)

    {

     SDA = 1;

    }

   else

    {

     SDA = 0;

   }//送数据口

   SCL = 1;                //拉高时钟线

   mma_Delay5us();             //延时

   SCL = 0;                //拉低时钟线

   mma_Delay5us();             //延时

  }

 MMA8452_RecvACK();

}

 

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

从IIC总线接收一个字节数据

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

static unsigned char MMA8452_RecvByte()

{

 uint8 i;

 uint8 dat = 0;

 uint8 test;

 

  SDA= 1;                    //使能内部上拉,准备读取数据,

  for(i=0; i<8; i++)         //8位计数器

  {

   SCL = 1;                //拉高时钟线

   mma_Delay5us();             //延时

   SDA_IN();

   if(SDA)

    {

     test=0x01;

     test <<= (7-i);

     dat |= test;

    }

   else

    {

     test=0x01;

     test <<= (7-i);

     dat &= ~test;

    }

   SDA_OUT();

   SCL = 0;                //拉低时钟线

   mma_Delay5us();             //延时

  }

 return dat;

}

 

//******单字节写入*******************************************

static void Single_Write_MMA8452(unsignedchar REG_Address,unsigned char REG_data)

{

 MMA8452_Start();                 //起始信号

 MMA8452_SendByte(SlaveAddress);  //发送设备地址+写信号

 MMA8452_SendByte(REG_Address);   //内部寄存器地址

 MMA8452_SendByte(REG_data);      //内部寄存器数据

 MMA8452_Stop();                  //发送停止信号

}

 

//********单字节读取*****************************************

 

unsigned char Single_Read_MMA8452(unsignedchar REG_Address)

 uint8 REG_data;

 MMA8452_Start();                         //起始信号

 MMA8452_SendByte(SlaveAddress);          //发送设备地址+写信号

 MMA8452_SendByte(REG_Address);                   //发送存储单元地址,从0开始   

 MMA8452_Start();                         //起始信号

 MMA8452_SendByte(SlaveAddress+1);        //发送设备地址+读信号

 REG_data=MMA8452_RecvByte();             //读出寄存器数据

 MMA8452_SendACK(1);  

 MMA8452_Stop();                          //停止信号

 return REG_data;

}

 

 

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

 

//初始化MMA8452,根据需要请参考pdf进行修改************************

void Init_MMA8452(void)

{

 P1SEL &= ~0xc0; //作为普通 IO 口

  //P1INP&= ~0Xc0; //打开上拉

 //P1INP |= 0Xc0; //打开下拉

 SDA_OUT();

 SCL_OUT();

 

 Single_Write_MMA8452(0x2A,0x01);  

 Single_Write_MMA8452(0x2B,0x02); 

}

 

 

 

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

//

//连续读出MMA8452内部加速度数据,地址范围0x01~0x06

//

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

void Multiple_read_MMA8452(void)

{  

 int8 BUF[6];                        //接收数据缓存区 

 uint8 i;

 MMA8452_Start();                         //起始信号

 MMA8452_SendByte(SlaveAddress);          //发送设备地址+写信号

 MMA8452_SendByte(0x01);                  //发送存储单元地址,从0x01开始    

 MMA8452_Start();                         //起始信号

 MMA8452_SendByte(SlaveAddress+1);        //发送设备地址+读信号

  for(i=0; i<6; i++)                      //连续读取6个地址数据,存储中BUF

  {

   BUF[i] = MMA8452_RecvByte();         //BUF[0]存储0x32地址中的数据

   if (i == 5)

    {

     MMA8452_SendACK(1);               //最后一个数据需要回NOACK

    }

   else

    {

     MMA8452_SendACK(0);               //回应ACK

    }

  }

 MMA8452_Stop();                         //停止信号

   

 mmaX=(BUF[0]<<8)|BUF[1];

 mmaX>>=4;

 mmaY=(BUF[2]<<8)|BUF[3];

 mmaY>>=4;

 mmaZ=(BUF[4]<<8)|BUF[5];

 mmaZ>>=4;

 HalUARTWrite(0,"the data is ",12);

 type_change(mmaX,mmaX_str);

 HalUARTWrite(0,"  ",2);

 type_change(mmaY,mmaY_str);

 HalUARTWrite(0,"  ",2);

 type_change(mmaZ,mmaZ_str);

 HalUARTWrite(0,"\n",1);

 strcat(MMA8452_DATABUF,&mmaX_str[1]);

 strcat(MMA8452_DATABUF,&mmaY_str[1]);

 strcat(MMA8452_DATABUF,&mmaZ_str[1]);

 

 HalUARTWrite(0,MMA8452_DATABUF,strlen(MMA8452_DATABUF));

 HalUARTWrite(0,"\n",1);

 

 memset(mmaX_str,0,strlen(mmaX_str));

 memset(mmaY_str,0,strlen(mmaY_str));

 memset(mmaZ_str,0,strlen(mmaZ_str));

}

 

void type_change(int num,char *str)

{

 uint8 temp[5];

 if(num<0)

  {

   HalUARTWrite(0,"-",1);

   str[0]='-';

   num = -num;

  }

 else

  {

   HalUARTWrite(0,"+",1);

   str[0]='+';

  }

 str[1]=temp[0] = num/10000+0x30;

 str[2]=temp[1] = num%10000/1000+0x30;

 str[3]=temp[2] = num%1000/100+0x30;

 str[4]=temp[3] = num%100/10+0x30;

 str[5]=temp[4] = num%10+0x30;

 HalUARTWrite(0,&temp[0],5);

}

 

 

 

MMA8451.h

#ifndef _MMA8451_H_

#define _MMA8451_H_

 

 

extern int mmaX,mmaY,mmaZ;

extern void Init_MMA8452(void);

extern void Multiple_read_MMA8452(void);

extern unsigned charSingle_Read_MMA8452(unsigned char REG_Address);

extern char MMA8452_DATABUF[255];

 

#endif

 

 

PM2.5.c

#include"PM2_5.h"

#include<stdio.h>

void init_ADC()

{

   ADCCON3=0x40;

   ADCCON1|=0x30;

   ADCCON1|=0x40;

}

 

void init_CLOCK()

{

   CLKCONCMD&=~0X40;

   while(CLKCONSTA&0x40);

   CLKCONCMD&=~0x47;

}

 

unsigned char getdata()

{

   

   unsigned char value=0;

  long int i,temp;

  init_ADC();

   init_CLOCK();

    init_IO();

     for(i=0;i<1000;i++)

     {

        init_ADC();

        while(!(ADCCON1&0x80));

        value=(uchar)ADCL>>2;

        value|=(uchar)ADCH<<6;

        temp+=value;

     }

   return temp/1000;

}

 

 

void init_IO()

{

   P0DIR&=~0x81;//P0_7

   P0SEL&=~0x81;

}

 

PM2.5.h

#ifndef __PM2_5_h__

#define __PM2_5_h__

#include <iocc2530.h>

#define uchar unsigned char

#define uint unsigned int

void init_ADC(void);

void init_CLOCK(void);

unsigned char getdata(void);

void init_IO(void);

#endif

 

GY30.c

#include <ioCC2530.h>

#include "hal_types.h"

#include"GY30.h"

#include "OnBoard.h"

#include<string.h>

 

#define     SlaveAddress   0x46      //定义器件在IIC总线中的从地址,根据SA0地址引脚不同修改

#define SCL P1_6      //IIC时钟引脚定义

#define SDA P1_7      //IIC数据引脚定义

 

#define SDA_OUT()   P1DIR |= 0x80

#define SDA_IN()    P1DIR &= ~0x80

#define SCL_OUT()   P1DIR |= 0x40

 

int GY30X;

char GY30X_str[7]={0};

char GY30_DATABUF[255]={0};

static void delay_nms(int n);

static void GY30_Delay5us(void);

static void GY30_Start(void);

static void GY30_Stop(void);

static void GY30_SendACK(char ack);

static char GY30_RecvACK(void);

static void GY30_SendByte(unsigned chardat);

static unsigned char GY30_RecvByte(void);

static void Single_Write_GY30(unsigned charREG_Address);

unsigned char Single_Read_GY30(unsignedchar REG_Address);

void Init_GY30(void);

void Multiple_read_GY30(void);

void type_change_GY30(int num,char *str);

 

 

 

void delay_nms(int n)

{

  inti;

 for(i=0;i<n*200;i++)

  {

    GY30_Delay5us();

  }

}

 

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

延时5微秒

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

static void GY30_Delay5us(void)//uint16microSecs)

{

 MicroWait(20);

 /*uint8 microSecs = 5;

 while(microSecs--)

  {*/

   /* 32 NOPs == 1 usecs */

 /*  asm("nop");asm("nop"); asm("nop"); asm("nop");asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop"); asm("nop");asm("nop"); asm("nop");

   asm("nop"); asm("nop");

  }*/

}

 

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

起始信号

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

static void GY30_Start()

{

  SDA= 1;                    //拉高数据线

  SCL= 1;                    //拉高时钟线

 GY30_Delay5us();                

  SDA= 0;                    //产生下降沿

 GY30_Delay5us();             //延时

  SCL= 0;                    //拉低时钟线

 GY30_Delay5us();        

}

 

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

停止信号

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

static void GY30_Stop()

{

  SDA= 0;                    //拉低数据线

  SCL= 1;                    //拉高时钟线

 GY30_Delay5us();                //延时

  SDA= 1;                    //产生上升沿

 GY30_Delay5us();                //延时

}

 

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

发送应答信号

入口参数:ack (0:ACK 1:NAK)

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

static void GY30_SendACK(char ack)

{

  SDA= ack;                  //写应答信号

  SCL= 1;                    //拉高时钟线

 GY30_Delay5us();                //延时

  SCL= 0;                    //拉低时钟线

 GY30_Delay5us();                //延时

}

 

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

接收应答信号

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

static char GY30_RecvACK()

{

  SCL= 1;                    //拉高时钟线

 GY30_Delay5us();           

 SDA_IN();

  CY= SDA;                   //读应答信号

 SDA_OUT();

  SCL= 0;                    //拉低时钟线

 GY30_Delay5us();                //延时

 

 return CY;

}

 

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

向IIC总线发送一个字节数据

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

static void GY30_SendByte(unsigned chardat)

{

 uint8 i;

 

  for(i=0; i<8; i++)         //8位计数器

  {

   //dat <<= 1;              //移出数据的最高位

   if((0x80>>i)&dat)

    {

     SDA = 1;

    }

   else

    {

     SDA = 0;

   }//送数据口

   SCL = 1;                //拉高时钟线

   GY30_Delay5us();             //延时

   SCL = 0;                //拉低时钟线

   GY30_Delay5us();             //延时

  }

 GY30_RecvACK();

}

 

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

从IIC总线接收一个字节数据

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

static unsigned char GY30_RecvByte()

{

 uint8 i;

 uint8 dat = 0;

 uint8 test;

 

  SDA= 1;                    //使能内部上拉,准备读取数据,

  for(i=0; i<8; i++)         //8位计数器

  {

   SCL = 1;                //拉高时钟线

   GY30_Delay5us();             //延时

   SDA_IN();

   if(SDA)

    {

     test=0x01;

     test <<= (7-i);

     dat |= test;

    }

   else

    {

     test=0x01;

     test <<= (7-i);

     dat &= ~test;

    }

   SDA_OUT();

   SCL = 0;                //拉低时钟线

   GY30_Delay5us();             //延时

  }

 return dat;

}

 

//******单字节写入*******************************************

//static void Single_Write_GY30(unsignedchar REG_Address,unsigned char REG_data)

static void Single_Write_GY30(unsigned charREG_Address)

{

 GY30_Start();                  //起始信号

 GY30_SendByte(SlaveAddress);   //发送设备地址+写信号

 GY30_SendByte(REG_Address);    //内部寄存器地址

 //GY30_SendByte(REG_data);      //内部寄存器数据

 GY30_Stop();                   //发送停止信号

}

 

//********单字节读取*****************************************

 

unsigned char Single_Read_GY30(unsignedchar REG_Address)

 uint8 REG_data;

 GY30_Start();                         //起始信号

 GY30_SendByte(SlaveAddress);          //发送设备地址+写信号

 GY30_SendByte(REG_Address);                   //发送存储单元地址,从0开始    

 GY30_Start();                         //起始信号

 GY30_SendByte(SlaveAddress+1);        //发送设备地址+读信号

 REG_data=GY30_RecvByte();             //读出寄存器数据

 GY30_SendACK(1);  

 GY30_Stop();                          //停止信号

 return REG_data;

}

 

 

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

 

//初始化GY30,根据需要请参考pdf进行修改************************

void Init_GY30(void)

{

 P1SEL &= ~0xc0; //作为普通 IO 口

 //P1INP &= ~0Xc0; //打开上拉

 //P1INP |= 0Xc0; //打开下拉

 SDA_OUT();

 SCL_OUT();

  

  Single_Write_GY30(0x01);    

}

 

 

 

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

//

//连续读出GY30内部加速度数据,地址范围0x01~0x06

//

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

void Multiple_read_GY30(void)

{  

 int8 BUF[6]={0};                        //接收数据缓存区 

 uint8 i;

 Single_Write_GY30(0x01);   //power on

 Single_Write_GY30(0x10);   // H-resolution mode

 

 delay_nms(180);

 

 GY30_Start();                         //起始信号

 //GY30_SendByte(SlaveAddress);          //发送设备地址+写信号

 //GY30_SendByte(0x32);                  //发送存储单元地址,从0x01开始 

 //GY30_Start();                         //起始信号

 GY30_SendByte(SlaveAddress+1);        //发送设备地址+读信号

  for(i=0; i<3; i++)                     //连续读取6个地址数据,存储中BUF

  {

   BUF[i] = GY30_RecvByte();         //BUF[0]存储0x32地址中的数据

   if (i == 3)

    {

     GY30_SendACK(1);                //最后一个数据需要回NOACK

    }

   else

    {

     GY30_SendACK(0);                //回应ACK

    }

  }

 GY30_Stop();                         //停止信号

   

 GY30X=(BUF[0]<<8)|BUF[1]; 

 HalUARTWrite(0,"the data is ",12);

 type_change_GY30(GY30X,GY30X_str);

 

 HalUARTWrite(0,"\n",1);

 strcat(GY30_DATABUF,&GY30X_str[1]);

 

 HalUARTWrite(0,GY30_DATABUF,strlen(GY30_DATABUF));

 HalUARTWrite(0,"\n",1);

 

 memset(GY30X_str,0,strlen(GY30X_str));

}

 

void type_change_GY30(int num,char *str)

{

 uint8 temp[5];

 if(num<0)

  {

   HalUARTWrite(0,"-",1);

   str[0]='-';

   num = -num;

  }

 else

  {

   HalUARTWrite(0,"+",1);

   str[0]='+';

  }

 str[1]=temp[0] = num/10000+0x30;

 str[2]=temp[1] = num%10000/1000+0x30;

 str[3]=temp[2] = num%1000/100+0x30;

 str[4]=temp[3] = num%100/10+0x30;

 str[5]=temp[4] = num%10+0x30;

 HalUARTWrite(0,&temp[0],5);

}

 

GY30.h

#ifndef _GY30_H_

#define _GY30_H_

 

 

extern int GY30X,GY30Y,GY30Z;

extern void Init_GY30(void);

extern void Multiple_read_GY30(void);

extern unsigned charSingle_Read_GY30(unsigned char REG_Address);

extern char GY30_DATABUF[255];

 

#endif