This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to set NRF24LE1 in register retention with analog comparator wakeup? Sample code needed. Please help.

I want to set up nrf24le1 in register retention mode with analog comparator wake up. The moment it wakes up it should do few ADC conversions and then again go back to sleep. I could not find much information on this or any example code. I just found this:

hal_ancmp_set_input_channel(8);//P1.0
hal_ancmp_set_ref_voltage_scale(0x01);//50%
hal_ancmp_set_reference(0);
hal_ancmp_enable(true);
PWRDWN=0x04

from this thread here: https://devzone.nordicsemi.com/f/nordic-q-a/20741/how-to-wakeup-with-analogcomparator-on-nrf24le1

But I do not know how that code works or how to change it to do ADC conversions on wake up and then go back to power saving mode. Someone please help.

  • Update:

    Here is my code below

    #include"reg24le1.h" // I/O header file for NRF24LE1
    #include"hal_uart.h" // library containing serial communication functions
    #include"hal_delay.h" // header file containing delay functions
    #include"hal_clk.h"
    #include"hal_ancmp.h" 
    #include"hal_delay.h"
    // Repeated putchar to print a string
    void putstring(char *s)
    {
      while(*s != 0)
         hal_uart_putchar(*s++);
    }
     
     
    // main function
    void main()
    {
    	
    P0DIR = 0xf0;  // set P03 as output and P04 as input;
     
    // Initializes the UART
      hal_uart_init(UART_BAUD_9K6);
     
    // Enable global interrupts
      EA = 1;
     
    //infinite loop
    	hal_ancmp_set_input_channel(8);//P1.0
    hal_ancmp_set_ref_voltage_scale(0x01);//50%
    hal_ancmp_set_reference(0);
    hal_ancmp_enable(true);
    	putstring("\r\nHello RANJAN\r\n") ;
    	delay_ms(500);
    PWRDWN=0x04;
    while(1)
    {
    // Print "Hello Everyone"
    putstring("\r\nHello Everyone!!\r\n") ;
    
    }
     
    }
     
    
     

    Now for this to wake up I give 3V to pin P1.0 but nothing happens after that.It does not wake up and does not enter into the while loop?

  • For new development I highly recommend to check out the nRF51822 or nRF52832. There have been no new code written for the nRF24L-series for 6 years, and it's unlikely there will be new code examples now. After a bit searching I did find an example that use both analog comparator and wakeup on tick, you can try that also, just to compare if it might be code related.

    /*
     The analog comparator doesn't generate an interrupt, so you need to check for
     it after wakeup, for instance by reading the PWRDWN register. I have made a
     small example that show usage of the analog comparator.
     */
     
    #include <Nordic\reg24le1.h>
    #include "hal_clk.h"
    #include "hal_rtc.h"
    #include "hal_ancmp.h"
    
    void mcu_init(void)
    {
      P0CON = 0x70; // Disable digital input buffer, pin used for 32kHz.
      P0CON = 0x71; // Disable digital input buffer, pin used for 32kHz.
      P0CON = 0x73; // Disable digital input buffer, pin used for ADC
    
      P0DIR = 0x07;  // Two pins used for 32 kHz XO, one pin for ADC.
      P1DIR = 0x00;
      P2DIR = 0x00;
      P3DIR = 0x00;
    
      P0 = 0x00;
      P1 = 0x00;
      P2 = 0x00;
      P3 = 0x00;
    }
    
    // RTC wakeup interrupt every 1sec
    void wakeup_tick() interrupt INTERRUPT_TICK
    {
      P04 = !P04;
    }
    
    void main(void)
    {
      uint16_t i;
      mcu_init();
      
      // The RTC is required for analog comparator operation.
      // In this case I also enable wakeup every 1sec.
      hal_rtc_start(false);
      hal_clklf_set_source(HAL_CLKLF_XOSC32K);          
      hal_rtc_set_compare_mode(HAL_RTC_COMPARE_MODE_0);
      hal_rtc_set_compare_value(0x7FFF);
      hal_rtc_start(true);              
    
      // Wait for the 32kHz to startup (change phase)
      while((CLKLFCTRL&0x80)==0x80);
      while((CLKLFCTRL&0x80)!=0x80);
    
      // Setup analog comparator
      hal_ancmp_set_polarity(HAL_ANCMP_NON_INVERTING);
      hal_ancmp_set_ref_voltage_scale(HAL_ANCMP_REF_SCALE_50);
      hal_ancmp_set_reference(HAL_ANCMP_REF_VDD);
      hal_ancmp_set_input_channel(HAL_ANCMP_INP_AIN2);
      hal_ancmp_enable(true);
    
      // Enable RTC wakeup
      IEN1 = 0x20;
      EA = 1;  
                      
      while(1)
      {
        // Enter register retention mode
        PWRDWN = 0x04;
        
        // If wakeup due to comparator
        if((PWRDWN & 0x20) == 0x20)
          P05 = 1;    
        // else if wakeup is due to RTC tick, then wait in standby mode for interrupt to occur (XO startup time)
        else 
          PWRDWN = 0x07;
        
        // Clear PWRDWN
        PWRDWN = 0x00;
        
        // Continue to run some dummy code 
        while(i--)
          ;
    
        // Disable analog comparator wakeup indication
        P05 = 0; 
      };
    }
    
    
    

  • I tried the above code. It does not go into power saving mode atleast that is what the current consumption indicates. The current consumption with the above code is 4.2mA and when I give a voltage to AIN2 pin the consumption does not change at all. Now if I set hal_clk_set_freq(HAL_CLK_125KHZ); then the power consumption goes down to 1.12mA but still does not go to 0.002mA like the register retention mode should do. So the above code is not pushing the NRF24LE1 to power saving mode of 0.002mA current consumption and analog comparator wake up does not work.

Related