实验六 —— 火焰传感器

实验要求

![[Pasted image 20211110145422.png]]

实验源码

/*exam6coo.h*/
#ifndef FIRECOORDINATOR_H
#define FIRECOORDINATOR_H

#ifdef __cplusplus
extern "C"
{  
#endif
#include "ZComDef.h"
  
#define FireAPP_ENDPOINT           10

#define FireAPP_PROFID             0x0F04
#define FireAPP_DEVICEID           0x0001
#define FireAPP_DEVICE_VERSION     0
#define FireAPP_FLAGS              0

#define FireAPP_MAX_CLUSTERS       1
#define FireAPP_CLUSTERID          1

#define FireAPP_SEND_MSG_TIMEOUT 1000
#define  TIMEOUT_EVT_FLAG  (1<<0)
    
extern void FireApp_Init( byte task_id );
extern UINT16 FireApp_ProcessEvent( byte task_id, UINT16 events );
#ifdef __cplusplus
}
#endif
#endif /* FireAPP_H */

/*exam6coo.c*/
#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"
#include <string.h>
#include <stdio.h>
#include "FireCoordinator.h"
#include "DebugTrace.h"

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

/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"
const cId_t FireApp_ClusterList[FireAPP_MAX_CLUSTERS] =
{
  FireAPP_CLUSTERID
};

const SimpleDescriptionFormat_t FireApp_SimpleDesc =
{
  FireAPP_ENDPOINT,              //  int Endpoint;
  FireAPP_PROFID,                //  uint16 AppProfId[2];
  FireAPP_DEVICEID,              //  uint16 AppDeviceId[2];
  FireAPP_DEVICE_VERSION,        //  int   AppDevVer:4;
  FireAPP_FLAGS,                 //  int   AppFlags:4;
  FireAPP_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  (cId_t *)FireApp_ClusterList,  //  byte *pAppInClusterList;
  FireAPP_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  (cId_t *)FireApp_ClusterList   //  byte *pAppInClusterList;
};

endPointDesc_t FireApp_epDesc;


byte FireApp_TaskID;  
devStates_t FireApp_NwkState;


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

void FireApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );


void FireApp_Init( byte task_id )
{
  halUARTCfg_t config;
  FireApp_TaskID = task_id;
  FireApp_NwkState = DEV_INIT;
  FireApp_TransID = 0;  

  // Fill out the endpoint description.
  FireApp_epDesc.endPoint = FireAPP_ENDPOINT;
  FireApp_epDesc.task_id = &FireApp_TaskID;
  FireApp_epDesc.simpleDesc
            = (SimpleDescriptionFormat_t *)&FireApp_SimpleDesc;
  FireApp_epDesc.latencyReq = noLatencyReqs;
  // Register the endpoint description with the AF
  afRegister( &FireApp_epDesc ); 
 
  config.baudRate = HAL_UART_BR_115200;
  config.flowControl = FALSE;
  
 
//下面添加串口的配置代码HalUARTOpen
  HalUARTOpen(HAL_UART_PORT_0,&config);
}

UINT16 FireApp_ProcessEvent( byte task_id, UINT16 events )
{
  afIncomingMSGPacket_t *MSGpkt;
  (void)task_id;  // Intentionally unreferenced parameter

  if ( events & SYS_EVENT_MSG )
  {
    MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( FireApp_TaskID );
    while ( MSGpkt )
    {
      switch ( MSGpkt->hdr.event )
      { 
        case AF_INCOMING_MSG_CMD://协调器收到无线数据
          FireApp_MessageMSGCB( MSGpkt );
          break; 
        default:
          break;
      }
      // Release the memory
      osal_msg_deallocate( (uint8 *)MSGpkt );
      // Next
      MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( FireApp_TaskID );
    }
    // return unprocessed events
    return (events ^ SYS_EVENT_MSG);
  }
  // Discard unknown events
  return 0;
}

void FireApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
unsigned char buffer[10];
  switch ( pkt->clusterId )
  {
    case FireAPP_CLUSTERID:
      osal_memcpy(buffer,pkt->cmd.Data,10);//把数据拷贝到buffer
      if(osal_memcmp(buffer,"LEDON",5)==TRUE)
      	{
            HalLedSet (HAL_LED_2, HAL_LED_MODE_ON);
            HalUARTWrite(HAL_UART_PORT_0,buffer,7);
      	}
        if(osal_memcmp(buffer,"LEDOFF",6)==TRUE)
        {
            HalLedSet (HAL_LED_2, HAL_LED_MODE_OFF);
            HalUARTWrite(HAL_UART_PORT_0,buffer,8);
        }
          
      break;
  }
}
/*exam6end.c*/
#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"
#include <string.h>
#include <stdio.h>
#include "FireCoordinator.h"
#include "DebugTrace.h"

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

/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"
const cId_t FireApp_ClusterList[FireAPP_MAX_CLUSTERS] =
{
  FireAPP_CLUSTERID
};

const SimpleDescriptionFormat_t FireApp_SimpleDesc =
{
  FireAPP_ENDPOINT,              //  int Endpoint;
  FireAPP_PROFID,                //  uint16 AppProfId[2];
  FireAPP_DEVICEID,              //  uint16 AppDeviceId[2];
  FireAPP_DEVICE_VERSION,        //  int   AppDevVer:4;
  FireAPP_FLAGS,                 //  int   AppFlags:4;
  FireAPP_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  (cId_t *)FireApp_ClusterList,  //  byte *pAppInClusterList;
  FireAPP_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  (cId_t *)FireApp_ClusterList   //  byte *pAppInClusterList;
};


endPointDesc_t FireApp_epDesc;
byte FireApp_TaskID;   
devStates_t FireApp_NwkState;

afAddrType_t FireApp_DstAddr;
byte FireApp_TransID;  // This is the unique message ID (counter)

void FireApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
void AF_Send(unsigned char *buffer);

void FireApp_Init( byte task_id )
{
  
  FireApp_TaskID = task_id;
  FireApp_NwkState = DEV_INIT;
  FireApp_TransID = 0;  
  
  FireApp_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
  FireApp_DstAddr.endPoint = FireAPP_ENDPOINT;
  FireApp_DstAddr.addr.shortAddr = 0;
  // Fill out the endpoint description.
  FireApp_epDesc.endPoint = FireAPP_ENDPOINT;
  FireApp_epDesc.task_id = &FireApp_TaskID;
  FireApp_epDesc.simpleDesc
            = (SimpleDescriptionFormat_t *)&FireApp_SimpleDesc;
  FireApp_epDesc.latencyReq = noLatencyReqs;
  // Register the endpoint description with the AF
  afRegister( &FireApp_epDesc ); 
  P0SEL &= ~(1<<7);
  P0DIR &= ~(1<<7);
  P0INP &= ~(1<<7);
  P2INP &= ~(1<<5);
osal_start_timerEx( FireApp_TaskID,
                     TIMEOUT_EVT_FLAG,
                     FireAPP_SEND_MSG_TIMEOUT );
  
}
uint8 Led_State;
UINT16 FireApp_ProcessEvent( byte task_id, UINT16 events )
{
  (void)task_id;  // Intentionally unreferenced parameter
  
  if ( events & SYS_EVENT_MSG )
  {
   
    // return unprocessed events
    return (events ^ SYS_EVENT_MSG);
  }
  
  if ( events & TIMEOUT_EVT_FLAG )
  {
      osal_start_timerEx( FireApp_TaskID,
                     TIMEOUT_EVT_FLAG,
                     FireAPP_SEND_MSG_TIMEOUT );
     if(P0_7)
     {
         AF_Send("LEDOFF\r\n");
         HalLedSet (HAL_LED_2, HAL_LED_MODE_OFF);
     }
      else
      {
         AF_Send("LEDON\r\n");
         HalLedSet (HAL_LED_2, HAL_LED_MODE_ON);
      }
      return (events ^ TIMEOUT_EVT_FLAG);
  }
  // Discard unknown events
  return 0;
}


void AF_Send(unsigned char *buffer)
{
  AF_DataRequest( &FireApp_DstAddr, &FireApp_epDesc,
                       FireAPP_CLUSTERID,
                       (byte)osal_strlen(buffer) + 1,
                       buffer,
                       &FireApp_TransID,
                       AF_DISCV_ROUTE, 
                       AF_DEFAULT_RADIUS );
 
}

实验思路

协调器:等待接收终端发来的数据,如果是LEDON就点亮本节点的灯,同时向串口写LEDON,如果是LEDOFF就关闭本节点的灯,同时向串口写LEDOFF

终端:利用定时器每隔1s向通过无线电向协调器发送灯的状态信息,当火焰传感器检测到火焰时,灯的状态为亮,否则为灭

补充

当通过查阅芯片数据手册并配置好引脚为通用I/O,输入,上拉模式后,可直接通过sfr特殊功能寄存器P0_7来判断该引脚上的电平信号

实验现象

智能家居zigbee实验六(点击跳转)

版权声明:本文为CSDN博主「极客糟珀—肌发」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_49098053/article/details/121248990

生成海报
点赞 0

极客糟珀—肌发

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

ZigBee快速入门04

定时器 定时器简介 CC2530有5个定时器,即定时器1(16位定时器),定时器2(MAC定时器),定时器3和4,睡眠定时器 定时器1:一个16位定时器,

RT-Thread Studio移植LAN8720A驱动

RTT网络协议栈驱动移植(霸天虎) 1、新建工程 ​ 工程路径不含中文路径名,工程名用纯英文不含任何符号。 2、用CubeMx配置板子外设 2.1、配置时钟 ​ 按照自己板子配置相应时钟。

ESP8266 无限重启踩坑

最近做了一个电子墨水屏万年历,在移植屏幕代码时遇到了esp8266无限软复位的问题,如果你的串口打印是以下图片所示,那么恭喜你问题解决了。 造成软复位的原因是因为,程序里有死循环&#xf