zigbee(题型整理3)

题目:点对点通信,节点A的按键控制节点B的跑马灯启,停(按照D4、D3、D6、D5间隔1s)。当停止时,跑马灯需要保持原有状态。再按下时从当前位置开始跑马灯

这道题的难点主要为需要骤停这步,对于接受发送方面就不啰嗦了,直接看到我们的zigbee怎么做到不用中断做到骤停

#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                22         // 频道 11~26
#define PAN_ID                    0x8888     //网络id 
#define MY_ADDR                   0xAC3A     //本机模块地址
#define SEND_ADDR                 0x1015     //发送地址
/**************************************************/  
#define LED4 P1_1
#define LED3 P1_0
#define LED6 P1_4
#define LED5 P1_3
#define KEY1 P1_2

static basicRfCfg_t basicRfConfig;

uint8 TXBUFF[1]={1};
uint8 RXBUFF[1];
uint8 count=0;

int flag1=1;
int temp_count=1;
int temp_flag1=1;
uint8 scnakey();
int count_key=0;
// 无线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();
}

void init_in(void)
{
  P1SEL &=~0X1F;
  P1DIR |=0X1B;
  P1DIR &=~0X04;//key1
  LED4=LED3=LED6=LED5=0;

  T1CTL |=0X0C;
  T1CC0L =0X12;
  T1CC0H =0X7A;
  T1IE=1;
  T1OVFIM=1;
  EA=1;
  T1CTL |=0X03;
}

void delay(int xms)
{
  int i,j;
  for(i=0;i<xms;i++)
    for (j=0;j<240;j++)
    {
      asm("NOP");asm("NOP");asm("NOP");
    }
}


uint8 scankey()
{
  if(KEY1==0)
  {
    delay(100);
    if(KEY1==0)
      while(KEY1==0);
      return 1;
  }
  return 0;
}

void main(void)
{
    halBoardInit();//选手不得在此函数内添加代码
    ConfigRf_Init();//选手不得在此函数内添加代码
    init_in();
    while(1)
    {
      /* user code start */
      if(scankey()==1)
      {
        delay(350);
        basicRfSendPacket(SEND_ADDR,TXBUFF,1);
      }

      if(basicRfPacketIsReady()){
         if(basicRfReceive(RXBUFF,1,NULL)>0)
           if(RXBUFF[0]==1)
           {
             if(flag1==0)
             {
               flag1=temp_flag1;
               count=temp_count;
             }
             else if(flag1!=0){
               temp_flag1=flag1;
               temp_count=count;
               flag1=0;
             }

           }
      }



          if((count==2)&&(flag1==1)){
            LED4=1;
            LED5=0;
            flag1++;
          }
          else if((count==4)&&(flag1==2)){
            LED4=0;
            LED3=1;
            flag1++;
          }
          else if((count==6)&&(flag1==3)){
            LED3=0;
            LED6=1;
            flag1++;
          }
          else if((count==8)&&(flag1==4)){
            LED6=0;
            LED5=1;
            count=0;
            flag1=1;
          }
    /* user code end */
    }
}

#pragma vector = T1_VECTOR
__interrupt void T1_isp (void)
{
  T1STAT &=~0X20;
  count++;
}

我们不难看出这部分的代码是核心判断部分

if((count==2)&&(flag1==1)){
            LED4=1;
            LED5=0;
            flag1++;
          }

其中的count是用于定时的,而对于flag是判断定位的,起初我认为只需要记住flag的值就可以,但是后期的实验让我明白,其实正真起作用的是count,有兴趣的小伙伴可以试试。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值