物联网国赛zigbee点对点通讯——G卷
一、实现的功能
二、预备知识
1:zigbee的点对点可以使用协议栈或者新大陆的工程文件进行开发,在这里我使用的新大陆的点对点工程文件对题目进行开发
2:新大陆的zigbee开发板(无论是白板还是黑板(协调器))它的继电器的引脚分别是P1_7和P2_0这里拿的是新大陆的开发板举例其他开发板需要自己参考电路图
3:在通讯的时候两个zigbee之间的网络ID和频道一定要一致,选手在比赛时要注意自己设置的网络ID,频道和模块地址一定要不要与其他选手冲突!!!!
三、主节点代码
#include "hal_defs.h"#include "hal_cc8051.h"#include "hal_int.h"#include "hal_mcu.h"#include "hal_board.h"#include "hal_led.h"#include "hal_rf.h"#include "basic_rf.h"#include "hal_uart.h" #include "sensor_drv/sensor.h"#include <stdio.h>#include <string.h>#include <stdarg.h>/*****点对点通讯地址设置******/#define RF_CHANNEL11 // 频道 11~26#define PAN_ID0x0001//网络id #define MY_ADDR 0x1234//本机模块地址#define SEND_ADDR 0x4321//发送地址/**************************************************/static basicRfCfg_t basicRfConfig;int count=0;//unsigned char dat_send[20];// 无线RF初始化void ConfigRf_Init(void){basicRfConfig.panId = PAN_ID;basicRfConfig.channel= RF_CHANNEL;basicRfConfig.myAddr= MY_ADDR;basicRfConfig.ackRequest = TRUE;while(basicRfInit(&basicRfConfig) == FAILED);basicRfReceiveOn();}#define SW1 P1_2#define D4 P1_0#define D3 P1_1#define D5 P1_4#define D6 P1_3unsigned char switch_LED=0;void Init_Port()//LED配置{P1SEL &=~0x1b;P1DIR |=0x1b;P1 &=~0x1b;}void delay(unsigned int xms){while(xms--)for(unsigned int i=0;i<533;i++); }void Scan_Key(){if(SW1==0){delay(10);if(SW1==0){switch_LED=1;basicRfSendPacket(SEND_ADDR,"00",sizeof("00"));//当按键按下主节点发送字符串00给到从节点while(SW1==0);}}}/********************MAIN************************/void main(void){halBoardInit();//选手不得在此函数内添加代码ConfigRf_Init();//选手不得在此函数内添加代码Init_Port();while(1){/* user code start */Scan_Key();if(switch_LED==1){delay(500);//当按键按下执行流水灯P1=0x02;delay(500);P1=0x01;delay(500);P1=0x10;delay(500);P1=0x08;count++;//每次流完一次count++if(count>3)//当大于3的时候发送01给到从节点{basicRfSendPacket(SEND_ADDR,"01",sizeof("01"));} }/* user code end */}}
四、从节点代码
#include "hal_defs.h"#include "hal_cc8051.h"#include "hal_int.h"#include "hal_mcu.h"#include "hal_board.h"#include "hal_led.h"#include "hal_rf.h"#include "basic_rf.h"#include "hal_uart.h" #include "sensor_drv/sensor.h"#include <stdio.h>#include <string.h>#include <stdarg.h>/*****点对点通讯地址设置******/#define RF_CHANNEL11 // 频道 11~26#define PAN_ID0x0001//网络id #define MY_ADDR 0x4321//本机模块地址#define SEND_ADDR 0x1234//发送地址/**************************************************/static basicRfCfg_t basicRfConfig;unsigned char dat_recv[10];//接收数组// 无线RF初始化void ConfigRf_Init(void){basicRfConfig.panId = PAN_ID;basicRfConfig.channel= RF_CHANNEL;basicRfConfig.myAddr= MY_ADDR;basicRfConfig.ackRequest = TRUE;while(basicRfInit(&basicRfConfig) == FAILED);basicRfReceiveOn();}#define SW1 P1_2#define D4 P1_0#define D3 P1_1#define D5 P1_4#define D6 P1_3#define JD1 P1_7#define JD2 P2_0void delay(unsigned int xms){while(xms--)for(unsigned int i=0;i<533;i++); }void Init_Port()//LED配置{P1SEL &=~0x9f;P1DIR |=0x9b;P1 &=~0x9b;P2DIR |=0x01;P2SEL &=~0x01;}/********************MAIN************************/void main(void){halBoardInit();//选手不得在此函数内添加代码ConfigRf_Init();//选手不得在此函数内添加代码Init_Port();while(1){/* user code start */if(basicRfPacketIsReady())//判断是否接收到内容 返回值为true或者false{basicRfReceive(dat_recv,10,NULL);//将接收到的数据存入dat_recv当中if(strcmp(dat_recv,"00")==0)//对其进行判断使用strcmp函数 如果为00就打开继电器 也就是让对应引脚为高电平{JD1=1;JD2=1;}else if(strcmp(dat_recv,"01")==0)//如果为01关闭继电器 也就是让对应的引脚为0{halMcuWaitMs(3000); //延时大约3秒 毫秒级延时函数JD1=0; JD2=0;}}/* user code end */}}
附:新大陆zigbee点对点工程链接!!
链接:/s/1A3NtZYWm_Z-Va7kJghRn-g
提取码:1ent