application.c

来自「这是nrf24lu1的无线鼠标源代码,应用平台是keil c」· C语言 代码 · 共 195 行

C
195
字号
/* Copyright (c) 2007 Nordic Semiconductor. All Rights Reserved.
 *
 * The information contained herein is confidential property of Nordic Semiconductor. The use,
 * copying, transfer or disclosure of such information is prohibited except by express written
 * agreement with Nordic Semiconductor.
 *
 * $LastChangedRevision: 1769 $
 */

 /** @file
 *
 * The nRF24LU1 example application.
 *
 * @author Runar Kjellhaug
 *
 */

#include "hal_nrf.h"
#include "system.h"
#include "radio.h"
#include "application.h"
#include "cklf.h"

extern bool device_op_mode;
extern bool rf_wakeup;
extern uint8_t radio_status;
extern uint8_t pload[];
extern uint8_t wd_ctr;

uint8_t ctr;

#define _DELAY_ 250

/** Button <B>B1</B> press function. This function creates a data_packet(1-byte, CMD1), the "simple_data_packet", transfers it to the 
 * radio and transmits it. It waits until the packet are sent, or MAX_RT, i.e. all tries done, then crates 
 * a led blink based on if the packet was sent or not. Finally "radio_status" is set to <B>RF_IDLE</B> and LED1 is 
 * switched off.
*/
void b1_press(void);

/** Button <B>B2</B> press function. This function creates a data_packet(1-byte, CMD2), the "pwr_dwn_packet", transfers it to the 
 * radio and transmits it. It waits until the packet are sent, or MAX_RT, i.e. all tries done, then crates 
 * a led blink based on if the packet was sent or not. Finally "radio_status" is set to <B>RF_IDLE</B> and LED1 is 
 * switched off.
*/
void b2_press(void);

/** Button <B>B3</B> press function. This function resets the watchdog timer register, makes a LED3 blink and returns.
 * Since the watchdog timer is running in PTX mode, when this function returns, the program is "halted" until this button
 * is released. If B3 is not released within approximately 2 seconds, the watchdog timer times out, and the CPU are reset. 
*/
void b3_press(void);

void device_ptx_mode(void)
{
  while(true)
  {
    cklf_regxc_write(WWD, 0x3e80); 
    while(radio_status == RF_BUSY)         // wait until radio ready
      ;
    
    if(!B1)
    {
      LED1_ON();                           // trans.; flash LED1...
      b1_press();
      while(!B1)
      {
        cklf_regxc_write(WWD, 0x3e80);     // reset wd counter
      }
    }
    if(!B2)
    {
      LED1_ON();                           // trans.; flash LED1...
      b2_press();
      while(!B2)
      {
        cklf_regxc_write(WWD, 0x3e80);     // reset wd counter
      }                                    // wait until B1 relesed...
    }
    if(!B3)
    {
      b3_press();
      while(!B3)                           // wait until B1 relesed...
        ;
      cklf_regxc_write(WWD, 0x3e80);       // reset wd counter
      wd_ctr = 0x00;
    }  
  }
}

void device_prx_mode(void)
{
  while(true)
  {
    if(radio_status == HAL_NRF_RX_DR)
    {
      switch(pload[0])
      {
        case CMD1:                          // simple "link-packet" received
          LED1_BLINK();
          break;

        case CMD2:                          // "power_down" message received
          pwr_down();                       // enter power down
          break;

        default:  
          break;
      }
      
      radio_status = RF_IDLE;
    }
  }
}

void b1_press(void)
{
  radio_send_packet(CMD1);                        // send command:CMD1(simple-link packet)
  while(radio_status == RF_BUSY)                 // wait for respons, TX_DS or MAX_RT
    ;

  switch(radio_status)
  {
    case HAL_NRF_TX_DS:                     // packet sent, flash LED2
      LED2_BLINK();
      break;

    case HAL_NRF_MAX_RT:                    // packet lost, flash LED3
      LED3_BLINK();
      break;
    
    default:
      break;
  }
  radio_status = RF_IDLE;                   // return to radio:RF_IDLE
  
  LED1_OFF();
}

void b2_press(void)
{
  radio_send_packet(CMD2);                  // send command:CMD2(pwr_dwn message)
  while(radio_status == RF_BUSY)            // wait for respons, TX_DS or MAX_RT
    ;

  switch(radio_status)
  {
    case HAL_NRF_TX_DS:                     // packet sent, flash LED2
      LED2_BLINK();
      break;

    case HAL_NRF_MAX_RT:                    // packet lost, flash LED3
      LED3_BLINK();
      break;
    
    default:
      break;
  }
  radio_status = RF_IDLE;                   // return to radio:RF_IDLE
  
  LED1_OFF();
}

void b3_press(void)
{
  cklf_regxc_write(WWD, 0x3e80);                    // reset wd counter
  LED3_BLINK();
}

void pwr_down(void)
{
  device_boot_mess();
  device_boot_mess();
  
  if(rf_wakeup == true)                       // RFIRQ wakeup enabled?
  {
    PWRDWN = 0x01;                            // enter "pwr_dwn"
    device_boot_mess();
    LED2_ON();
    LED3_ON();
  }
  else
  {
    RFCKEN = 0;                               // disable radio before entering pwr_dwn
    hal_nrf_set_power_mode(HAL_NRF_PWR_DOWN); // power down radio
    
    PWRDWN = 0x01;                            // enter "pwr_dwn"
    
    RFCKEN = 1;
    hal_nrf_set_power_mode(HAL_NRF_PWR_UP);   // power up radio
    device_boot_mess();
    LED3_ON();
  }
}

⌨️ 快捷键说明

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