⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nrf905_c51demo_cfile.txt

📁 单片射频收发器nRF905,具备跳号跳频和跳址功能.这个是C51的源程序
💻 TXT
📖 第 1 页 / 共 2 页
字号:
  }
  key = ~P0;//接收键盘数据每次可取多键)
  if (key & KeyPosTab[SystemBuffers.TimeCount & 0x03]) {//有键压下(每次只取1键以实现“零耗时消除键盘抖动”)
    SystemBuffers.KeyCount[SystemBuffers.TimeCount & 0x03] ++;//压键计数
    if (SystemBuffers.KeyCount[SystemBuffers.TimeCount & 0x03] == 2) {//短压键
      SystemBuffers.WaitTime = 0;
      KeyCommandExec(0, SystemBuffers.TimeCount & 0x03);//单击键Key00()~Key03()
    }
    else if (SystemBuffers.KeyCount[SystemBuffers.TimeCount & 0x03] >= SysPushKeyTime) {//长压键150 * 20mS = 3。0S
      SystemBuffers.WaitTime = 0;
      KeyCommandExec(2, SystemBuffers.TimeCount & 0x03);//长压键Key20()~Key23()
      SystemBuffers.KeyCount[SystemBuffers.TimeCount & 0x03] = 3;//清除长压键计数器(躲过单击键)
    }
  }
  else {//无键压下(包括放键)
    if (SystemBuffers.KeyCount[SystemBuffers.TimeCount & 0x03] >= 2) {//放键事件
      SystemBuffers.WaitTime = 0;
      KeyCommandExec(1, SystemBuffers.TimeCount & 0x03);//键释放Key10()~Key13()
    }
    else {
      nRFReadWrite();//无线收发模块
    }
    SystemBuffers.KeyCount[SystemBuffers.TimeCount & 0x03] = 0;//清除压键计数器
  }
  SystemBuffers.TimeCount++;//继续下1个5mS延时
  if ((SystemBuffers.TimeCount & ~0x80) >= 100){//5mS*100=500mS
    SystemBuffers.WaitTime ++;
    if (SystemBuffers.WaitTime >= SysWaitTime) {//0.5*20=10S
      P0 = 0xff;//关显示
      SystemBuffers.WaitTime = 0;
    }
    SystemBuffers.TimeCount &= 0x80;
    SystemBuffers.TimeCount ^= 0x80;//取反闪烁位
  }
}
 
/*------------------------------------*/
void sioproc() interrupt SIO_VECTOR using 1
{
unsigned char i;
  if (RI) {//接收中断
    RI = 0;
    if (SioBuffers.SioRXCount && (SioBuffers.SioRXCount <= 32)) {//允许串口接收数据
      SioBuffers.SioRxBuff[32 - SioBuffers.SioRXCount] = SBUF;
      SioBuffers.SioRXCount --;
      if (SioBuffers.SioRXCount == 0) {
           for (i = 0; i < 32; i ++) {
          SioBuffers.SioTxBuff[i] = SioBuffers.SioRxBuff[i];//自收自发
          SystemBuffers.nRFTxBuff[i] = SioBuffers.SioTxBuff[i];//更新无线发送数据
        }
/*
        switch(SystemBuffers.nRFTxBuff[9]) {
          case 0x81: LED0 = 0;break;
          case 0x82: LED1 = 0;break;
          case 0x83: LED2 = 0;break;
          case 0x84: LED3 = 0;break;
          case 0x03: LED2 = 0;break;
          case 0x04: LED3 = 0;break;
        }
*/
        SioBuffers.SioRXCount = 32;//串口接收缓冲区满
        SioBuffers.SioTXCount = 32;//串口发送缓冲区满
        TI = 1;//立即发送
        SystemBuffers.nRFTxCount = 32;//要求间接无线发送
      }
    }
    else SioBuffers.SioRXCount = 0;//不允许串口接收数据
  }
  if (TI) {//发送中断
    TI = 0;
       if (SioBuffers.SioTXCount && (SioBuffers.SioTXCount <= 32)) {//允许串口发送数据
      SBUF = SioBuffers.SioTxBuff[32 - SioBuffers.SioTXCount];//串口显示
      SioBuffers.SioTXCount --;
    }
    else SioBuffers.SioTXCount = 0;//不允许串口发送数据
  }
}
 

//执行键盘命令
void KeyCommandExec(unsigned char commgroup, unsigned char commnum) using 0
{
unsigned int i;
unsigned key;
code void *KeyArray[3][4] = {//二维函数数组指针阵列表(散转命令地址表)
  {(void *)Key00 + 0x5b7d, (void *)Key01 + 0xa6ea, (void *)Key02 + 0xf157, (void *)Key03 + 0x4cc4},
  {(void *)Key10 + 0x9731, (void *)Key11 + 0xe2ae, (void *)Key12 + 0x3d1b, (void *)Key13 + 0x8888},
  {(void *)Key20 + 0xd3f5, (void *)Key21 + 0x2e62, (void *)Key22 + 0x79df, (void *)Key23 + 0xc44c} 
  };
  if ((commgroup < 3) && (commnum < 4)) {
    ClrWdt();//清除看门狗计数器
    key = commgroup * 4 + commnum + 1;
    i = ((key * 53 & 0xf) * 0x1000)+ ((key * 43 & 0xf) * 0x100) + ((key * 23 & 0xf) * 0x10) + (key * 13 & 0xf);
    _icall_((void *)KeyArray[commgroup][commnum] - i);//键盘命令散转
  }
}
 

void BeepSetup(unsigned char mode)
{
//  BeepBuffers.BeepCount = 1;
//  BeepBuffers.BeepMode  = mode;
//  BeepBuffers.BeepValue = mode;
}
/*----------------------------------
      蜂鸣器100mS发声程序
----------------------------------*/
void Beep(void)//蜂鸣器发声
{
//  BeepBuffers.BeepCount --;
//  if (BeepBuffers.BeepCount == 0){
//    BeepBuffers.BeepCount = 0;
//    BeepBuffers.BeepValue = BeepBuffers.BeepMode;
//    if (BeepBuffers.BeepValue >= 0x80)//单声
//      BeepBuffers.BeepMode = 0;       //单声清除
//  }
//  BeepBuffers.BeepValue <<= 1;
//  CY = ~CY;    //硬件反向
//  BeepOut = CY;//发声
}
 
void KeyInit(void) using 0//键盘初始化
{
unsigned char i;
  for (i = 0; i < 4; i++){
    SystemBuffers.KeyCount[i] = 0;//清除键盘压键次数缓冲区
  }
}
 
//收发无线读写模块
void nRFReadWrite(void)
{
unsigned char i;
unsigned int t;    
  if (!TX_EN && DR && AM) {//收到数据
    ClrWdt();
    TRX_CE = 0;//设置nRF905为待机方式
    _nop_();
    CSN=0;                        // Spi enable for write a spi command
    SpiReadWrite(RRP);                // Read payload command    
    for (i = 0; i < 32; i++)
    {
        SystemBuffers.nRFRxBuff[i]=SpiReadWrite(0xff);//读入无线接收缓冲区
    }
    CSN=1;                        // Disable spi
    TRX_CE = !TX_EN;//再回到原来的无线收发模式
//    TRX_CE = 1;//再回到接收模式
    switch(SystemBuffers.nRFRxBuff[9]) {
      case 0x81: LED0 = 0;break;
      case 0x82: LED1 = 0;break;
      case 0x83: LED2 = 0;break;
      case 0x84: LED3 = 0;break;
      case 0x01: LED0 = 0;break;
      case 0x02: LED1 = 0;break;
      case 0x03: LED2 = 0;break;
      case 0x04: LED3 = 0;break;
    }
    SystemBuffers.WaitTime = 0;
    while(DR || AM);    
    SystemBuffers.nRFRxCount = 32;//收到32个字节数据
    if (SioBuffers.SioTXCount == 0){//串口数据已发送完
      for (i = 0; i < 32; i ++)
        SioBuffers.SioTxBuff[i] = SystemBuffers.nRFRxBuff[i];//串口显示
      SioBuffers.SioTXCount = 32;//发送32个字节的显示数据
      TI = 1;//立即显示
    }
  }
  else if(!CD && (SystemBuffers.nRFRxCount == 32)) {//接收命令处理
    SystemBuffers.nRFRxCount = 0;
    for (i = 0; i < 4; i ++)
      SystemBuffers.nRFTxBuff[i] = SystemBuffers.nRFRxBuff[i + 4];//写入串口接收缓冲区
    for (i = 4; i < 8; i ++)
      SystemBuffers.nRFTxBuff[i] = SystemBuffers.nRFRxBuff[i - 4];//写入串口接收缓冲区
    for (i = 8; i < 32; i ++)
      SystemBuffers.nRFTxBuff[i] = SystemBuffers.nRFRxBuff[i];//写入串口接收缓冲区
    if (SystemBuffers.nRFRxBuff[9] == 0x80) {//系统消息请求
      SystemBuffers.nRFTxBuff[9]  = 0x00;//主机系统消息应答
      ReadnRFConfig(SystemBuffers.nRFTxBuff);
      SystemBuffers.nRFTxCount = 32;//主机要求间接无线发送
    }
    else if (SystemBuffers.nRFRxBuff[9] == 0x81) {//子机请求
//      LED0 = 0;//主机灯亮
      SystemBuffers.nRFTxBuff[9]  = 0x01;//主机应答
      SystemBuffers.nRFTxBuff[10] = 0x20;
      SystemBuffers.nRFTxBuff[11] = 0x04;
      SystemBuffers.nRFTxBuff[12] = 0x10;
      SystemBuffers.nRFTxBuff[13] = 0x18;
      SystemBuffers.nRFTxBuff[14] = 0x11;
      SystemBuffers.nRFTxBuff[15] = 0x28;
      SystemBuffers.nRFTxCount = 32;//主机要求间接无线发送
    }
    else if (SystemBuffers.nRFRxBuff[9] == 0x82) {//子机请求
//      LED1 = 0;//主机灯亮
      SystemBuffers.nRFTxBuff[9] =  0x02;//主机应答
      SystemBuffers.nRFTxBuff[10] = 0x20;
      SystemBuffers.nRFTxBuff[11] = 0x04;
      SystemBuffers.nRFTxBuff[12] = 0x10;
      SystemBuffers.nRFTxBuff[13] = 0x26;
      SystemBuffers.nRFTxBuff[14] = 0x21;
      SystemBuffers.nRFTxBuff[15] = 0x09;
      SystemBuffers.nRFTxCount = 32;//主机要求间接无线发送
    }
    else if (SystemBuffers.nRFRxBuff[9] == 0x83) {//子机变址请求
//      LED2 = 0;//主机变址灯亮
      SystemBuffers.nRFTxBuff[9]  = 0x03;//主机变址应答
      SystemBuffers.nRFTxBuff[10] = TH0;//
      SystemBuffers.nRFTxBuff[11] = TL0;
      SystemBuffers.nRFTxBuff[12] = SystemBuffers.WaitTime;
      SystemBuffers.nRFTxBuff[13] = SystemBuffers.TimeCount;
      SystemBuffers.nRFTxCount = 32;//主机要求间接无线发送
    }
    else if (SystemBuffers.nRFRxBuff[9] == 0x84) {//子机跳频请求
//      LED3 = 0;//主机跳频灯亮
      SystemBuffers.nRFTxBuff[9]  = 0x04;//主机跳频应答
      SystemBuffers.nRFTxBuff[10] = SystemBuffers.nRFTxBuff[8] + 1;//主机跳频
      SystemBuffers.nRFTxCount = 32;//主机要求间接无线发送
    }
    else if (SystemBuffers.nRFRxBuff[9] == 0x03) {//主机变址应答
      LED2 = 0;//子机变址灯亮
      SystemBuffers.nRFRxBuff[0] = SystemBuffers.nRFRxBuff[10];//改写接收地址第1字节
      SystemBuffers.nRFRxBuff[1] = SystemBuffers.nRFRxBuff[11];//改写接收地址第2字节
      SystemBuffers.nRFRxBuff[2] = SystemBuffers.nRFRxBuff[12];//改写接收地址第3字节
      SystemBuffers.nRFRxBuff[3] = SystemBuffers.nRFRxBuff[13];//改写接收地址第4字节
      WritenRFConfig(SystemBuffers.nRFRxBuff);//子机重新配置nRF905接收地址
    }
    else if (SystemBuffers.nRFRxBuff[9] == 0x04) {//主机跳频应答
//      LED3 = 0;//子机跳频灯亮
      SystemBuffers.nRFRxBuff[8] = SystemBuffers.nRFRxBuff[10];
      FastWritenRFConfig(SystemBuffers.nRFRxBuff[8]);//快速配置nRF905,子机跳频
    }
  }
  else if (!CD && (SystemBuffers.nRFTxCount == 32)) {//要求间接无线发送(发送命令处理)
    SetTxMode();//设置发送模式  
    WriteTXBuffer(SystemBuffers.nRFTxBuff);
    ClrWdt();//清除看门狗计数器
    StartTx();//开始发送
    SystemBuffers.nRFTxCount = 0;//结束发送模式
    if (SystemBuffers.nRFTxBuff[9] == 0x03) {//主机变址应答
 
      SystemBuffers.nRFRxBuff[4] = SystemBuffers.nRFTxBuff[10];//改写呼叫地址第1字节
      SystemBuffers.nRFRxBuff[5] = SystemBuffers.nRFTxBuff[11];//改写呼叫地址第2字节
      SystemBuffers.nRFRxBuff[6] = SystemBuffers.nRFTxBuff[12];//改写呼叫地址第3字节
      SystemBuffers.nRFRxBuff[7] = SystemBuffers.nRFTxBuff[13];//改写呼叫地址第4字节
    }
 
    else if (SystemBuffers.nRFTxBuff[9] == 0x04) {//主机跳频应答
      SystemBuffers.nRFRxBuff[8] = SystemBuffers.nRFTxBuff[10];//工作频率低8位
      for (t = 0; t < 2500; t ++);//延时,等待发送结束
      FastWritenRFConfig(SystemBuffers.nRFRxBuff[8]);//快速配置nRF905,主机跳频
    }
    SetRxMode();//设置接收模式
  }
}
 
//键
void Key00(void) using 0
{
unsigned char i;
  if (SystemBuffers.nRFTxCount == 0) {//无线发送缓冲区为空
    SystemBuffers.nRFTxBuff[0] = SystemBuffers.nRFRxBuff[4];//呼叫地址第1字节
    SystemBuffers.nRFTxBuff[1] = SystemBuffers.nRFRxBuff[5];//呼叫地址第2字节
    SystemBuffers.nRFTxBuff[2] = SystemBuffers.nRFRxBuff[6];//呼叫地址第3字节
    SystemBuffers.nRFTxBuff[3] = SystemBuffers.nRFRxBuff[7];//呼叫地址第4字节
 
    SystemBuffers.nRFTxBuff[4] = SystemBuffers.nRFRxBuff[0];//本机地址第1字节
    SystemBuffers.nRFTxBuff[5] = SystemBuffers.nRFRxBuff[1];//本机地址第2字节
    SystemBuffers.nRFTxBuff[6] = SystemBuffers.nRFRxBuff[2];//本机地址第3字节
    SystemBuffers.nRFTxBuff[7] = SystemBuffers.nRFRxBuff[3];//本机地址第4字节
 
    SystemBuffers.nRFTxBuff[8] = SystemBuffers.nRFRxBuff[8];//工作频率低8位
 
    SystemBuffers.nRFTxBuff[9] = 0x81;//请求命令
    for (i = 10; i < 32; i ++) SystemBuffers.nRFTxBuff[i] = 0;
    SystemBuffers.nRFTxCount = 32;//要求间接无线发送
    for (i = 0; i < 32; i ++) SioBuffers.SioTxBuff[i] = SystemBuffers.nRFTxBuff[i];
    SioBuffers.SioTXCount = 32;//要求间接串口发送
    TI = 1;//送串口显示
//    LED0 = 0;//压键灯亮
  }
}
 
//键
void Key01(void) using 0
{
unsigned char i;
  if (SystemBuffers.nRFTxCount == 0) {//无线发送缓冲区为空
    SystemBuffers.nRFTxBuff[0] = SystemBuffers.nRFRxBuff[4];//呼叫地址第1字节
    SystemBuffers.nRFTxBuff[1] = SystemBuffers.nRFRxBuff[5];//呼叫地址第2字节
    SystemBuffers.nRFTxBuff[2] = SystemBuffers.nRFRxBuff[6];//呼叫地址第3字节
    SystemBuffers.nRFTxBuff[3] = SystemBuffers.nRFRxBuff[7];//呼叫地址第4字节
 
    SystemBuffers.nRFTxBuff[4] = SystemBuffers.nRFRxBuff[0];//本机地址第1字节
    SystemBuffers.nRFTxBuff[5] = SystemBuffers.nRFRxBuff[1];//本机地址第2字节
    SystemBuffers.nRFTxBuff[6] = SystemBuffers.nRFRxBuff[2];//本机地址第3字节
    SystemBuffers.nRFTxBuff[7] = SystemBuffers.nRFRxBuff[3];//本机地址第4字节
 
    SystemBuffers.nRFTxBuff[8] = SystemBuffers.nRFRxBuff[8];//工作频率低8位
 
    SystemBuffers.nRFTxBuff[9] = 0x82;//请求
    for (i = 10; i < 32; i ++) SystemBuffers.nRFTxBuff[i] = 0;
    SystemBuffers.nRFTxCount = 32;//要求间接无线发送
    for (i = 0; i < 32; i ++) SioBuffers.SioTxBuff[i] = SystemBuffers.nRFTxBuff[i];
    SioBuffers.SioTXCount = 32;//要求间接串口发送
    TI = 1;//送串口显示
//  LED1 = 0;//压键灯亮
  }
}
 
void Key02(void) using 0
{
unsigned char i;
  if (SystemBuffers.nRFTxCount == 0) {//无线发送缓冲区为空
    SystemBuffers.nRFTxBuff[0] = SystemBuffers.nRFRxBuff[4];//呼叫地址第1字节
    SystemBuffers.nRFTxBuff[1] = SystemBuffers.nRFRxBuff[5];//呼叫地址第2字节
    SystemBuffers.nRFTxBuff[2] = SystemBuffers.nRFRxBuff[6];//呼叫地址第3字节
    SystemBuffers.nRFTxBuff[3] = SystemBuffers.nRFRxBuff[7];//呼叫地址第4字节
 
    SystemBuffers.nRFTxBuff[4] = SystemBuffers.nRFRxBuff[0];//本机地址第1字节
    SystemBuffers.nRFTxBuff[5] = SystemBuffers.nRFRxBuff[1];//本机地址第2字节
    SystemBuffers.nRFTxBuff[6] = SystemBuffers.nRFRxBuff[2];//本机地址第3字节
    SystemBuffers.nRFTxBuff[7] = SystemBuffers.nRFRxBuff[3];//本机地址第4字节
 
    SystemBuffers.nRFTxBuff[8] = SystemBuffers.nRFRxBuff[8];//工作频率低8位
 
    SystemBuffers.nRFTxBuff[9] = 0x83;//变址请求
    for (i = 10; i < 32; i ++) SystemBuffers.nRFTxBuff[i] = 0;
    SystemBuffers.nRFTxCount = 32;//要求间接无线发送
    for (i = 0; i < 32; i ++) SioBuffers.SioTxBuff[i] = SystemBuffers.nRFTxBuff[i];
    SioBuffers.SioTXCount = 32;//要求间接串口发送
    TI = 1;//送串口显示
//  LED2 = 0;//压键灯亮
  }
}
 
void Key03(void) using 0
{
unsigned char i;
  if (SystemBuffers.nRFTxCount == 0) {//无线发送缓冲区为空
    SystemBuffers.nRFTxBuff[0] = SystemBuffers.nRFRxBuff[4];//呼叫地址第1字节
    SystemBuffers.nRFTxBuff[1] = SystemBuffers.nRFRxBuff[5];//呼叫地址第2字节
    SystemBuffers.nRFTxBuff[2] = SystemBuffers.nRFRxBuff[6];//呼叫地址第3字节
    SystemBuffers.nRFTxBuff[3] = SystemBuffers.nRFRxBuff[7];//呼叫地址第4字节
 
    SystemBuffers.nRFTxBuff[4] = SystemBuffers.nRFRxBuff[0];//本机地址第1字节
    SystemBuffers.nRFTxBuff[5] = SystemBuffers.nRFRxBuff[1];//本机地址第2字节
    SystemBuffers.nRFTxBuff[6] = SystemBuffers.nRFRxBuff[2];//本机地址第3字节
    SystemBuffers.nRFTxBuff[7] = SystemBuffers.nRFRxBuff[3];//本机地址第4字节
 
    SystemBuffers.nRFTxBuff[8] = SystemBuffers.nRFRxBuff[8];//工作频率低8位
 
    SystemBuffers.nRFTxBuff[9] = 0x84;//跳频请求
    for (i = 10; i < 32; i ++) SystemBuffers.nRFTxBuff[i] = 0;
    SystemBuffers.nRFTxCount = 32;//要求间接无线发送
    for (i = 0; i < 32; i ++) SioBuffers.SioTxBuff[i] = SystemBuffers.nRFTxBuff[i];
    SioBuffers.SioTXCount = 32;//要求间接串口发送
    TI = 1;//送串口显示
//  LED3 = 0;//压键灯亮
  }
}
 
void Key10(void) using 0
{
  LED0 = 1;//放键灭灯
}
 
void Key11(void) using 0
{
  LED1 = 1;//放键灭灯
}
 
void Key12(void) using 0
{
  LED2 = 1;//放键灭灯
}
 
void Key13(void) using 0
{
  LED3 = 1;//放键灭灯
}
 
void Key20(void) using 0
{
  LED0 = 1;//长压键灭灯
  Key00();//连发
}
 
void Key21(void) using 0
{
  LED0 = 1;//长压键灭灯
  Key01();//连发
}
 
void Key22(void) using 0
{
  LED0 = 1;//长压键灭灯
  Key02();//连发
}
 
void Key23(void) using 0
{
  LED0 = 1;//长压键灭灯
  Key03();//连发
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -