辜渝傧

13037102709

027-87870986

教学实训

您当前的位置:首页 > 教学资源 > 实训方案 >

RFID实训|Zigbee无线通信

发布者:唯众    布时间:2020-12-02 15:01:24    点击量:

电路搭建

电路搭建电路搭建\


程序实现
#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"
 
#include "GenericApp.h"
#include "DebugTrace.h"
#include "OSAL_Nv.h"
#include "OnBoard.h"
 
#include "hal_oled_chinese.h"
#include "hal_digital.h"
#include "hal_max7219.h"
#include "aps_groups.h"
 
const cId_t GenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS];
 
const SimpleDescriptionFormat_t GenericApp_SimpleDesc =
{
         GENERICAPP_ENDPOINT,              //  int Endpoint;
         GENERICAPP_PROFID,                //  uint16 AppProfId[2];
         GENERICAPP_DEVICEID,              //  uint16 AppDeviceId[2];
         GENERICAPP_DEVICE_VERSION,        //  int   AppDevVer:4;
         GENERICAPP_FLAGS,                 //  int   AppFlags:4;
         GENERICAPP_MAX_CLUSTERS,          //  byte  AppNumInClusters;
         (cId_t *)GenericApp_ClusterList,  //  byte *pAppInClusterList;
         GENERICAPP_MAX_CLUSTERS,          //  byte  AppNumInClusters;
         (cId_t *)GenericApp_ClusterList   //  byte *pAppInClusterList;
};
endPointDesc_t GenericApp_epDesc;
afAddrType_t GenericApp_DstAddr;
 
devStates_t GenericApp_NwkState;
RfPacket_t RfTx,RfRx;
NodeData_t Node;
ModbusRecv MBRecv;
ModbusSend MBSend;
Digital Dig;
 
uint8 *IeeeAddr=NULL;//物理地址
uint8 SensorId;
uint8 SyjcFlag=0;
uint16 LedDisplayLen=16;
 
char Str1[16],Str2[16];
uint8 TempBuf[30];
uint16 Dht11Value;
uint16 ShortAddr=0;
int8_t RxBuf[25];
int8_t Rxdata[25];
 
byte GenericApp_TransID;
byte GenericApp_TaskID; 
 
static void GenericApp_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg );
static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
static void GenericApp_SendTheMessage( void );
 
 
void GenericApp_Init( uint8 task_id )
{
         GenericApp_TaskID = task_id;
         GenericApp_NwkState = DEV_INIT;
         GenericApp_TransID = 0;        
        
         GenericApp_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;//单播
         GenericApp_DstAddr.endPoint = GENERICAPP_ENDPOINT;
         GenericApp_DstAddr.addr.shortAddr = 0x0000;//向协调器单播          
         GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT;
         GenericApp_epDesc.task_id = &GenericApp_TaskID;
         GenericApp_epDesc.simpleDesc
                   = (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc;
         GenericApp_epDesc.latencyReq = noLatencyReqs;        
         afRegister( &GenericApp_epDesc );
                  
         init_buzzer();  
         init_key();                  
         init_sensor_io();
         SensorId = *get_sensor_id();
        
         //初始化oled显示屏
         oled_initial();  
         oled_disp_string(1,1,"武汉唯众智创科技");
         oled_disp_string(3,1,"有限公司");
         Delay_ms(500);
        
         switch(SensorId){
                   case S12_DZ:
                            Init_MAX7219();
                   break;
                   case S20_SYJC:                 //声音检测
                            P0DIR &=~0X02;
                            P0_1=0;
                            P0IEN |=0X02;
                            PICTL &=~0X01;
                            IEN1 |=0x20;
                            P0IFG &=~0X02;
                            EA=1;                         
                   break;               
                   default:                       //P00获取传感器模拟值(单独初始化IO)  P01获取传感器开关值
                            init_data_io(0x11);
                   break;
         }
        
}
 
#pragma vector=P0INT_VECTOR
__interrupt void P0_IRQ()
{
         uint8 temp=0;
         P0IFG &= ~0X02;
         SyjcFlag = 1;    
        
         RfTx.Pack.Sid[0] = 20;
         RfTx.Pack.Sid[1] = ' ';
        
         RfTx.Pack.Cmd[0] = 'S';
         RfTx.Pack.Cmd[1] = 'N';
         RfTx.Pack.Len = 1;
         RfTx.Pack.Data[0] = temp;
         P0IF=0;    
}
 
uint16 GenericApp_ProcessEvent( uint8 task_id, uint16 events )
{
         afIncomingMSGPacket_t *MSGpkt;
         afDataConfirm_t *afDataConfirm;
        
         byte sentEP;
         ZStatus_t sentStatus;
         byte sentTransID;      
         (void)task_id;
        
         if ( events & SYS_EVENT_MSG )
         {
                   MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
                   while ( MSGpkt )
                   {
                            switch ( MSGpkt->hdr.event )
                            {
                                     case ZDO_CB_MSG:
                                     GenericApp_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
                                     break;                                  
                                    
                                     case AF_DATA_CONFIRM_CMD:
                                     afDataConfirm = (afDataConfirm_t *)MSGpkt;
                                     sentEP = afDataConfirm->endpoint;
                                     sentStatus = afDataConfirm->hdr.status;
                                     sentTransID = afDataConfirm->transID;
                                     (void)sentEP;
                                     (void)sentTransID;
                                    
                                     if ( sentStatus != ZSuccess )
                                     {
                                     }
                                     break;
                                    
                                     case AF_INCOMING_MSG_CMD:
                                     GenericApp_MessageMSGCB( MSGpkt );
                                     break;
                                    
                                     case ZDO_STATE_CHANGE:
                                     GenericApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
                                     if ((GenericApp_NwkState == DEV_END_DEVICE))
                                     {                          
                                               IeeeAddr = NLME_GetExtAddr();   
                                               ShortAddr = NLME_GetShortAddr();                          //2个字节 本设备网络地址                           
                                               oled_clear_screen();
                                               sprintf(Str1,"%4X:%d:%04X:%d",_NIB.nwkPanId,_NIB.nwkLogicalChannel,ShortAddr,SensorId);                  
                                               oled_disp_string(1,1,Str1);
                                              
                                               //启动定时器,定间隔向协调器发送数据
                                               osal_start_timerEx( GenericApp_TaskID,
                                                                                       GENERICAPP_SEND_MSG_EVT,
                                                                                       1000);
                                     }
                                     break;                                  
                                     default:
                                     break;
                            }
                            osal_msg_deallocate( (uint8 *)MSGpkt );
                            MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
                   }                
                   return (events ^ SYS_EVENT_MSG);
         }
        
         if ( events & GENERICAPP_SEND_MSG_EVT )
         {       
                   GenericApp_SendTheMessage();                                
                   osal_start_timerEx( GenericApp_TaskID,
                                                           GENERICAPP_SEND_MSG_EVT,
                                                           5000 );
                   return (events ^ GENERICAPP_SEND_MSG_EVT);
         }                
         return 0;
}
 
static void GenericApp_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg )
{
         switch ( inMsg->clusterID )
         {
                   case End_Device_Bind_rsp:
                   if ( ZDO_ParseBindRsp( inMsg ) == ZSuccess )
                   {
                   }
                   break;
                  
                   case Match_Desc_rsp:
                   {
                            ZDO_ActiveEndpointRsp_t *pRsp = ZDO_ParseEPListRsp( inMsg );
                            if ( pRsp )
                            {
                                     if ( pRsp->status == ZSuccess && pRsp->cnt )
                                     {
                                               GenericApp_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
                                               GenericApp_DstAddr.addr.shortAddr = pRsp->nwkAddr;
                                               GenericApp_DstAddr.endPoint = pRsp->epList[0];
                                     }
                                     osal_mem_free( pRsp );
                            }
                   }
                   break;
         }
}
 
static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )//接受处理函数
{
         RfPacket_t rf;
         osal_memcpy(rf.Buf,pkt->cmd.Data,sizeof(RfPacket_t));
         if(rf.Pack.Head!='#' || rf.Pack.Tail!='$')
         {                
                   oled_clear_vertical(2);
                   oled_disp_string(3,1,"Recv error!");               
                   return;
         }
         if(pkt->clusterId != SensorId) return;
}
static void GenericApp_SendTheMessage( void )
{
         uint8 temp=0;
         char charbuf[15] = "KeyName:";
         static uint8* ptr = NULL;
         static uint16_t sen=0;
         SensorId = *get_sensor_id(); 
         if(sen != SensorId){
                   sen = SensorId;
                   oled_clear_vertical(1);
                   sprintf(Str1,"%4X:%d:%04X:%d",_NIB.nwkPanId,_NIB.nwkLogicalChannel,ShortAddr,SensorId);                  
                   oled_disp_string(1,1,Str1);      
                  
                   memset(RfTx.Buf,'x',sizeof(RfPacket_t));
                   memcpy(RfTx.Pack.Laddr,IeeeAddr,8);                      //8个字节 本设备IEEE地址
                   RfTx.Pack.Head = '#';                                                                        
                   RfTx.Pack.Saddr[0] = ShortAddr;
                   RfTx.Pack.Saddr[1] = ShortAddr>>8;                                            
                   RfTx.Pack.Tail = '$';           
                   RfTx.Pack.Sid[0] = SensorId;
                   RfTx.Pack.Sid[1] = ' ';       
                   RfTx.Pack.Cmd[0] = 'S';
                   RfTx.Pack.Cmd[1] = 'N';
         }
         switch(SensorId)
         {
                   //继电器输入输出:P03输入开关量 P02输出控制继电器
                   case S3_HE:               //霍尔
                            temp = *get_dout();        
                            sprintf(Str2,"Val:%d",temp);
                            oled_clear_vertical(2);
                            oled_disp_string(3,1,Str2);               
                            RfTx.Pack.Len = 1;
                            RfTx.Pack.Data[0] = temp;
                   break;      
                   case S4_YD:               //雨滴
                            ptr = get_aout_dout();    
                   break;               
                   case S8_KQZL: //空气质量
                            ptr = get_aout_dout();    
                   break;               
                   case S17_RTHW:      //人体红外     
                            ptr = get_aout_dout();    
                   break;               
                   case S25_YW:  //烟雾
                            ptr = get_aout_dout();    
                   break;                        
                   case S18_WSD:
                            Dht11Value = DHT11();  
                            RfTx.Pack.Len = 2;
                            RfTx.Pack.Data[0] = Dht11Value>>8;
                            RfTx.Pack.Data[1] = Dht11Value;    
                            sprintf(Str2,"Tem:%2d Hum:%2d",RfTx.Pack.Data[0],RfTx.Pack.Data[1]);
                            oled_clear_vertical(2);
                            oled_disp_string(3,1,Str2);
                   break;
                   case S26_GM: //光敏:数值范围(0,156)值越大,光照越强,低于50则开关量为0,否则为1
                            ptr = get_aout_dout();    
                            ptr[2] = 0xFF - ptr[2]-100;
                            if(ptr[2]<50) ptr[0] = 0;
                            else ptr[0] = 1;
                   break;
                   case S2_YYSB:
                   break;
                   case S20_SYJC:         
                            oled_clear_vertical(2);
                            sprintf(Str2,"Sig:%d",SyjcFlag);
                            oled_disp_string(3,1,Str2);                                           
                            RfTx.Pack.Len = 1;
                            RfTx.Pack.Data[0] = SyjcFlag;
                            SyjcFlag = 0;
                   break;
                   case S19_HWSF:
                            IR_Init();
                            oled_clear_vertical(2);
                            oled_disp_string(3,1,"Input Ir key!");
                            while(1)
                            {                          
                                     if(IR_IRQ==0)
                                     {
                                               temp = IR_Decoding();
                                               if(temp==0)
                                               {                
                                                        oled_clear_screen();
                                                        memcpy(charbuf+8,IR_CodeName(HWComcode),4);
                                                        oled_disp_string(1,1,charbuf);                                     
                                                        sprintf(Str2,"A:%04X C:%04X",HWAddcode,HWComcode);
                                                        oled_disp_string(3,1,Str2);               
                                                        RfTx.Pack.Len = 8;
                                                        RfTx.Pack.Data[0] = HWAddcode>>8;
                                                        RfTx.Pack.Data[1] = HWAddcode;
                                                        RfTx.Pack.Data[2] = HWComcode>>8;
                                                        RfTx.Pack.Data[3] = HWComcode;
                                                        strncpy((char*)RfTx.Pack.Data+4,IR_CodeName(HWComcode),4);                
                                                        break;
                                               }
                                               else
                                               {
                                                        oled_clear_vertical(2);
                                                        sprintf(Str2,"Error:%d!",temp);
                                                        oled_disp_string(3,1,Str2);
                                               }
                                     }
                            }
                   break;
                   default:
                            oled_clear_vertical(2);
                            oled_disp_string(3,1,"No sensor!");
                            RfTx.Pack.Len = 0;
                   break;
         }
         if(SensorId==S4_YD||SensorId==S8_KQZL||SensorId==S17_RTHW||
            SensorId==S25_YW||SensorId==S26_GM)
         {                
                   sprintf(Str2,"Sig:%d Val:%d",ptr[0],ptr[2]);              
                   oled_clear_vertical(2);
                oled_disp_string(3,1,Str2);
                  
                RfTx.Pack.Len = 3;
                   RfTx.Pack.Data[0] = ptr[0];
                   RfTx.Pack.Data[1] = ptr[1];
                   RfTx.Pack.Data[2] = ptr[2];
         }
         temp = AF_DataRequest(
                   &GenericApp_DstAddr, &GenericApp_epDesc,
                   SensorId,
                   sizeof(RfPacket_t),
                   (byte *)&RfTx.Buf,
                   &GenericApp_TransID,
                   AF_DISCV_ROUTE,
                   AF_DEFAULT_RADIUS);
         if(temp != SUCCESS){
                   asm("NOP");
         }
}

 


上一篇:RFID实训|433M无线通信之电路搭建和程序实现

下一篇:RFID实训|125K读卡之技术介绍