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

Unable to set nRF24LE1 as a receiver/transmitter and simultaneously operate the ADC and UART

I am trying to make the nRF24LE1 as a sensor node. So I want to configure it to work as a receiver/transmitter and simultaneously use the ADC and UART. But the moment I added code to set it as a receiver the ADC and UART stopped working. Here is my code. Please let me know what is wrong with the code

#include "nrf24le1.h" // I/O header file for NRF24LE1

#include "hal_clk.h"  // library containing clock functions


#include <stdint.h> // standard integers library

#include "hal_nrf.h" // library containing wireless communication functions

#include <Nordic/reg24le1.h>
//#include <reg24le1.h>
#include "nrf24le1.h"
#include "hal_adc.h"
#include <intrins.h>
#include "hal_uart.h" // library containing serial communication functions
#include "hal_delay.h" // header file containing delay functions

#include <stdio.h>

#include <string.h>

#include <math.h>

#include <stdint.h> // standard integers library

#include "hal_nrf.h" // library containing wireless communication functions

#include <stdbool.h> // standard boolean library
// Global variables

uint8_t payload[3]; // payload to be received
int val = 0 ,val1 = 0;
void delay(unsigned int us)
{
  while(us--)
  {
    _nop_();
  }
}
 
// Repeated putchar to print a string
void putstring(char *s)
{
  while(*s != 0)
     hal_uart_putchar(*s++);
	   //hal_uart_putchar(val);
}

void tostring(char str[], int num)

{

    int i, rem, len = 0, n;

 

    n = num;

    while (n != 0)

    {

        len++;

        n /= 10;

    }

    for (i = 0; i < len; i++)

    {

        rem = num % 10;

        num = num / 10;

        str[len - (i + 1)] = rem + '0';

    }

    str[len] = '\0';

}

// main function

void main()
{
	char str[10];int num=0, result=0;
	uint8_t payload[1]; // payload to be transmitted


  P0DIR = 0xf0;  // set P03 as output and P04 as input;
 
// Initializes the UART
  hal_uart_init(UART_BAUD_9K6);
 
// Enable global interrupts
  EA = 1;
 P1DIR = 0;
P10 = 0;
 
  // Configure ADC
  hal_adc_set_input_channel(HAL_ADC_INP_AIN6);
  hal_adc_set_reference(HAL_ADC_REF_VDD);
  hal_adc_set_input_mode(HAL_ADC_SINGLE);
  hal_adc_set_conversion_mode(HAL_ADC_CONTINOUS);
  hal_adc_set_sampling_rate(HAL_ADC_2KSPS);
  hal_adc_set_resolution(HAL_ADC_RES_8BIT);
  hal_adc_set_data_just(HAL_ADC_JUST_RIGHT);
 
 
#ifdef MCU_NRF24LE1

  while(hal_clk_get_16m_source() != HAL_CLK_XOSC16M)

  {
        putstring("stuck here");
    // Wait until 16 MHz crystal oscillator is running

  }

#endif


  #ifdef MCU_NRF24LU1P

  // Enable radio SPI

  RFCTL = 0x10;

  #endif


  // Set P0 as output

  //P0DIR = 0;


  // Enable the radio clock

  RFCKEN = 1;


  // Enable RF interrupt

  RF = 1;

  // Enable global interrupt

  //EA = 1;


  // Configure radio as primary receiver (PTX)

  hal_nrf_set_operation_mode(HAL_NRF_PRX);


  // Set payload width to 3 bytes

  hal_nrf_set_rx_payload_width((int)HAL_NRF_PIPE0, 3);


  // Power up radio

  hal_nrf_set_power_mode(HAL_NRF_PWR_UP);

  // Enable receiver

  CE_HIGH();


	
  // Enable MISC interrupts to enable ADC interrupt
  MISC = 1;
  // Enable global interrupts
  //EA = 1;
  // Start ADC conversion
  hal_adc_start();
//infinite loop
while(1)
{
// Print "Hello Everyone"
putstring("\nADC value is ") ;
//delay_ms(1000); // delay of 1sec
	tostring(str,val);
	putstring(str);
	delay_ms(100);
}
	

}
// ADC (MISC) interrupt
ADC_ISR()
{
  
val = hal_adc_read_LSB();
//P10 = 1;
/*delay(1000);
delay(val * 4);
P10 = 0;
delay(19000 - val * 4);*/
 
}

// Radio interrupt

NRF_ISR()

{

  uint8_t irq_flags;


  // Read and clear IRQ flags from radio

  irq_flags = hal_nrf_get_clear_irq_flags();


  // If data received

  if((irq_flags & (1<<(uint8_t)HAL_NRF_RX_DR)) > 0)

  {

    // Read payload

    while(!hal_nrf_rx_fifo_empty())

    {
			
			putstring("I am here 1\n");

      hal_nrf_read_rx_payload(payload);

    }

   putstring("I am here 2\n");
    // Write received payload[0] to port 0

    val1 = payload[0];

  }
	putstring("I am here 3\n");

}

Related