zigbee 串行通信

串口通信需要三个函数

// Initialize UART at the startup
//-------------------------------------------------------------------
void halUartInit(uint32 baud);

//-------------------------------------------------------------------
// Read a buffer from the UART
//-------------------------------------------------------------------
extern uint16 halUartRead(uint8 *pBuffer, uint16 length);

//-------------------------------------------------------------------
// Write a buff to the uart 
//-------------------------------------------------------------------
extern uint16 halUartWrite(uint8 *pBuffer, uint16 length);

代码
 

#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_CHANNEL                11         // 频道 11~26
#define PAN_ID                    0x1111     //网络id 
#define MY_ADDR                   0xAAAA     //本机模块地址
#define SEND_ADDR                 0xBBBB     //发送地址
/**************************************************/

#define LED1 P1_0
#define LED2 P1_1
#define SW1  P1_2


static basicRfCfg_t basicRfConfig;
// 无线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();
}

/********************MAIN************************/



void main(void)
{
    halBoardInit();//选手不得在此函数内添加代码
    ConfigRf_Init();//选手不得在此函数内添加代码
    
    halUartInit(57600);
    P1DIR |= 0X03;
    
    LED2 = 0;
    LED1 = 1;
    halMcuWaitMs(3000);
    LED1 = 0;
    
    uint8 buf[1];  
    while(1)
    {
    /* user code start */
      if(halUartRead(buf,1)){  //如果有数据,把数据读到buf里面
        if(buf[0]==0xE1){
          LED1 = 1;
          halUartWrite("The LED1 is Open!\n",strlen("The LED1 is Open!\n"));
        }
        if(buf[0]==0xE2){
          LED1 = 0;
          halUartWrite("The LED1 is Closed!\n",strlen("The LED1 is Closed!\n"));
        }
        if(buf[0]==0xF1){
          LED2 = 1;
          halUartWrite("The LED2 is Open!\n",strlen("The LED2 is Open!\n"));
        }
        if(buf[0]==0xF2){
          LED2 = 0;
          halUartWrite("The LED2 is Closed!\n",strlen("The LED1 is Closed!\n"));
        }
        
      }
      
    /* user code end */
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值