Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] NRF24L01 + PIC 16 + MikroC New Bie Please Guide

Status
Not open for further replies.

asking

Full Member level 5
Full Member level 5
Joined
Sep 21, 2010
Messages
279
Helped
6
Reputation
12
Reaction score
6
Trophy points
1,298
Visit site
Activity points
3,377
NRF24L01 + PIC 16 + MikroC Please Guide

Hello friends,

I am just trying my skills with RF module nRF24L01 to communicate with another nRF24L01. I just want to make wireless switch which would be able to on/off device over Relay remotely within module range.

Can anyone help me with details how to start with nRF24L01 ? Datasheet is the main thing i understand but is there anything else or any such example available ?

Please guide. i am going to make everything from ground level.

Thanks.
 

I also need help. I have a homework and i cant make the modules to comunicate. I can read or write the values over the SPI but i cant make them communicate. I am not allowed to use ant already written library. Here is my code:

Code:
#include <main.h>
#include <nrf24l01.h>               
#define IRQ     PIN_B0
#define CSN     PIN_B1                                               
#define CE      PIN_B2                                 
#use            rs232(uart1)
#use            fast_io(B)        
 
unsigned int buffer[5];

int1 flag=0;
 //ISR RB0
 #INT_EXT
void RB0_isr(){                                                                                                
   flag=1;                                                              
   clear_interrupt(int_ext);            
  } 
 //WRITE 1 BYTE REGISTER                                     
void setup_1b(int val,unsigned int reg){
   output_low(CSN);      
   spi_write(W_REGISTER|reg);              
   spi_write(val);                            
   output_high(CSN);                
}
 //READ 1 BYTE REGISTER 
int read_1b(unsigned int reg){             
   unsigned int reg_val;                                         
   output_low(CSN);                        
   spi_write(R_REGISTER|reg);                                                                                
   spi_write(NOP);                               
   reg_val=spi_read();
   output_high(CSN);
   return reg_val;
}            
//WRITE 5 BYTE REGISTER
void setup_5b(unsigned int v0,unsigned int v1,unsigned int v2,unsigned int v3,unsigned int v4,int reg){
   int i;
   int values[5];
   values[0]=v0;
   values[1]=v1;                                                 
   values[2]=v2;                                      
   values[3]=v3;                                                
   values[4]=v4;
   output_low(CSN);
   spi_write(W_REGISTER|reg);   
   for(i=0;i<5;i++){
      spi_write(values[i]);
   }
   output_high(CSN);  
}  
//READ 5 BYTE REGISTER                          
void read_5b(int reg){               
   int i; 
   output_low(CSN);
   spi_write(reg);
   for(i=0;i<5;i++){     
      spi_write(NOP);
      buffer[i]=spi_read(0);
   }
}
//PRINT DATA FOR DEBUG ON SERIAL                      
void serial_out(int reg,int1 width){
    int i;
    if(!width){
      int reg_value=read_1b(reg);
      printf("Valoarea registrului %u este %u",reg,reg_value);
      putc(13);   
    } 
    else{
      read_5b(reg);
      printf("Registrului %u are valorile:",reg);
      putc(13);
      for(i=0;i<5;i++){
        printf("BYTE%d=%u",i,buffer[i]);
        putc(13);
      }                             
    }                  
    delay_ms(500);
}                                             
// SET 1 BIT FROM REGISTER
void set_1bit(int bit_to_set,int reg){
   int buff_reg=read_1b(reg);
   int test=1<<bit_to_set;
   buff_reg =buff_reg|test;                                               
   setup_1b(buff_reg,reg);                           
}
// CLEAR 1 BIT FROM REGISTER
void clear_1bit(int bit_to_clear,int reg){
   int buff_reg=read_1b(reg);
   buff_reg=((~(1<<bit_to_clear))&buff_reg);               
   setup_1b(buff_reg,reg);                                             
}                                                   
// READ 1 BIT FROM REGISTER                                     
int1 read_1bit(int bit_to_read,int reg){
   int test = 1<<bit_to_read;
   int buff_reg=read_1b(reg);            
   test=((test & buff_reg))>>bit_to_read;                
   return test;                                                  
}
// CHECK STATUS INTERRUPT
int status_int_source(){                       
   int int_source=0b01110000;
   int buff=read_1b(STATUS);
   int_source=int_source&buff;
   return int_source;
}
// SEND DATA -- TX_MODE                            
void tx_mode(int data){
   output_low(CE);                                                                                                            
   //clear status flags
   setup_1b(V_RX_DR|V_TX_DS|V_MAX_RT,STATUS);
   // fill tx_fifo
   output_low(CSN);
   spi_write(W_TX_PAYLOAD);                     
   spi_write(data);    
   output_high(CSN);                     
   // activate tx_mode                
   setup_1b(V_EN_CRC|V_CRCO|V_PWR_UP,CONFIG);        
   delay_us(150);
   // now transmit data                     
   output_high(CE);
   delay_us(15);
   output_low(CE);
   while(flag==0){                                 
   }
   // check STATUS  
   if(flag==1){
      int int_bit = status_int_source();          
      switch (int_bit){   
         case 32:
         printf("TX ok!!");             
         set_1bit(TX_DS,STATUS);
         flag=0;                          
         break;
         case 16:                                                                                                                 
         printf("MAX_RT!!");
         putc(13);                                     
         set_1bit(MAX_RT,STATUS);
         serial_out(STATUS,0);
         output_low(CSN);
         spi_write(FLUSH_TX);             
         output_high(CSN);                              
         serial_out(STATUS,0);
         break;                
      }                            
   }                  
}                                               
// RECIEVE DATA -- RX_MODE
void rx_mode(){                           
   serial_out(RX_PW_P0,0);
   // clear STATUS
   setup_1b(V_RX_DR|V_TX_DS|V_MAX_RT,STATUS);       
   // set rx_mode
   setup_1b(V_EN_CRC|V_CRCO|V_PWR_UP|V_PRIM_RX,CONFIG);
   output_high(CE);
   delay_us(150);                                    
   while(flag==0){                       
   }
   if(flag==1){                              
      output_low(CE);           
      // read the payload             
      output_low(CSN);
      spi_write(R_RX_PAYLOAD);
      int data=spi_read();                                                                                                    
      output_high(CSN);
      output_high(CE);            
      //set_1bit(RX_DR,STATUS);
      output_d(data); 
   }                    
}             

// SETUP PIC REGISTERS                                  
void setup_pic(){
   printf("SE SETEAZA REG PIC");
   putc(13);
   SET_TRIS_B(0b00000001);
   SET_TRIS_D(0b00000000);         
   output_d(0);                      
   output_high(CSN);                     
   setup_comparator(NC_NC_NC_NC);                    
   setup_adc(ADC_OFF);                     
   disable_interrupts(GLOBAL); 
   enable_interrupts(INT_EXT);
   ext_int_edge(H_TO_L);
   enable_interrupts(GLOBAL);
   clear_interrupt(INT_EXT); 
   setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_XMIT_L_TO_H|SPI_CLK_DIV_4|SPI_SAMPLE_AT_END); //setare spi mod0 , 2MHZ      
   setup_uart(9600); // baud rate UART
   output_b(0);   
}
//SETUP NRF24L01 REGISTERS                                     
void setup_NRF24L01(){
   printf("SE SETEAZA REG NRF");
   putc(13);
   //setup EN_AA -- auto ack on pipe0                                   
   setup_1b(V_ENAA_P0, EN_AA);
   //setup EN_RXADDR -- pipe0 enabled            
   setup_1b(V_ERX_P0,EN_RXADDR);                                                                                            
   //setup SETUP_AW -- 5 bytes width in pipe0
   setup_1b(V_AW,SETUP_AW);
   //setup SETUP_RETR -- 15 retries, wait 500us before retransmit ; 
   int V_ARC=15 ,V_ARD =16;           
   setup_1b(V_ARC|V_ARD,SETUP_RETR);
   //setup RF_CH -- channel 100;
   int V_RF_CH = 87;
   setup_1b(V_RF_CH,RF_CH);                                                   
   //setup RF_SETUP -- max. power, 250Kb rate
   setup_1b(V_RF_DR|V_RF_PWR,RF_SETUP);
   //setup RX_ADDR_P0 -- 0xB1B1B1B1B1                                                                                 
   setup_5b(0XB1,0XB1,0XB1,0XB1,0XB1,RX_ADDR_P0); 
   //setup TX_ADDR -- 0xB1B1B1B1B1 
   setup_5b(0XB1,0XB1,0XB1,0XB1,0XB1,TX_ADDR);
   //setup RX_PW_P0 -- 1 byte          
   int V_RX_PW_P0=1;                         
   setup_1b(V_RX_PW_P0,RX_PW_P0);
}                                                                                                                                
 //                                                              
 
void read_all(){
  serial_out(CONFIG,0);                                        
  serial_out(EN_AA ,0);            
  serial_out(EN_RXADDR,0); 
  serial_out(SETUP_AW,0);                             
  serial_out(RF_CH,0);
  serial_out(RX_PW_P0,0);           
                        
                                       
}                             
                                                                                                                                    
void main()                                      
{                                                           
setup_pic();                                                                                                                   
setup_NRF24L01();
read_all();                            
                                                                            
   while(TRUE)                                        
   {                             
     flag=0;                                
     tx_mode(129);
     //rx_mode();    
     
   }                                 
                                            
}

It could be usefull a simple example of how you enter in tx or rx mode.

Thanks in advance.
 

It's better to google it and find some library for NRF24l01+ , ( use AVR, PIC , ... not important really )
Then check initialization function and follow it.
Rx,Tx channels, addresses , width of channels & ... must to adjust with equal values for both of Receiver and Transmitter ( It's very important for make modules to communicate )
Then adjust power up and PRIM_RX registers.
PRIM_RX = 0 for TX and 1 for RX mode.
 

Re: NRF24L01 + PIC 16 + MikroC Please Guide

libstock has example of nRF24L01.

I tried to find the Code working by myself look at the example given @ Libstock. But again m not able to get the sequence how to power --> initialize --> Transmit Data ---> receive Acknowledgement ----> Done.

Actually MikroC might be having the Library function of NRF24L01+ which i am not able to find in code below due to which looking at datasheet becomes harder for me. Please help me i want to see the complete working how the NRF24L01 is started which registers first we should make working by setting proper bits values. How can i see library functions which are not available in library manager ? Please help.

I have bolded the Functions in the code for which i am confused.

Code:
sbit Irq_pin  at PORTB.B0; sfr;
sbit Mosi_pin at LATC.B7;  sfr;
sbit Ce_pin   at LATA.B4;  sfr;
sbit Sclk_pin at LATA.B3;  sfr;
sbit Csn_pin  at LATA.B2;  sfr;
sbit Miso_pin at PORTC.B6; sfr;
    
sbit Irq_tris  at TRISB.B0; sfr;
sbit Mosi_tris at TRISC.B7; sfr;
sbit Ce_tris   at TRISA.B4; sfr;
sbit Sclk_tris at TRISA.B3; sfr;
sbit Csn_tris  at TRISA.B2; sfr;
sbit Miso_tris at TRISC.B6; sfr;
    
char Code_[32];    // addressing code (see datasheet for NRF24L01+)
char Outgoing[25]; // outgoing packet
char Incoming[25]; // incoming packet

void Init(){
  ANSELA = 0;
  ANSELB = 0;
  ANSELC = 0;
  ANSELD = 0;
  LATD   = 1;
  TRISD  = 0;
  
  INTCON = 0;                            // disable all interrupts

  // address code
  Code_[10] = 0xAA;                      // choose bytes at your wish
  Code_[11] = 0xAA;
  Code_[12] = 0xAA;
  Code_[13] = 0xAA;
  Code_[14] = 0xAA;

  [B]NRF24L01P_RF_Channel = 20;[/B]             // Radio frequency channel, in range 0..124
  [B]NRF24L01P_Init(5);[/B]                     // initialize NRF24L01+
 [B] NRF24L01P_Setup_Tx(Code_);[/B]
}

void MakeMessage(){                      // form outgoing message
  char i;
  for (i = 0; i < 20; i++){
      OutGoing[i] = i;
    };
}

char DoRF(){
 char i;
 char result;

  MakeMessage();
  result = 0;                            // no receiver in range
  while (result != 255){                 // keep trying until receiver gets the message
      memset(&incoming, 33, 20);         // prepare incoming
      i = NRF24L01P_Send(OutGoing, Incoming);   // Call receiver
      Delay_100ms();
      LATD++;                            // indicate that Tx is in progress
      if (i == 0){                       // Rx has received the message and returned the answer, which is in the "Incoming" array
          LATD = 0;                      // process Incoming array to extract the message from Rx
          result = 255;                  // All is OK, the receiver has got the message
      }
      else {
         // Rx did not receive the message
      }
  }
}

void Main(){
  Init;
  while (1)                              // keep on transmitting
    DoRF();
}
 

Re: NRF24L01 + PIC 16 + MikroC Please Guide

You can't see mikroC library code. mikroC doesn't publish library codes. You have to get help from mikroE forum.

nRF24L01 UART version and 2 more I2C versions.

**broken link removed**

**broken link removed**

**broken link removed**
 
  • Like
Reactions: asking

    asking

    Points: 2
    Helpful Answer Positive Rating
Re: NRF24L01 + PIC 16 + MikroC Please Guide

You can't see mikroC library code. mikroC doesn't publish library codes. You have to get help from mikroE forum.

Any algorithm or anything Flow chart how to initialize NRF24L01 + over PIC 16 Devices as library only for PIC 18 devices. How i can make complete code from scratch. any steps because datasheet looks to me good for only registrar's information no sequenc is given how to transmit data. It could be given but i am not getting in datasheet So throw some little light.

Thanks
 

Re: NRF24L01 + PIC 16 + MikroC Please Guide

You can't see mikroC library code. mikroC doesn't publish library codes. You have to get help from mikroE forum.

nRF24L01 UART version and 2 more I2C versions.

**broken link removed**

**broken link removed**

**broken link removed**

i am having nRF24L01+ Module ? what is difference between my module and UART Module ? My application is to remotely on off devices.
 

Re: NRF24L01 + PIC 16 + MikroC Please Guide

What is the distance of the device? If it is within 100 meters then you can use RF modules, ASK and FSK type 433 MHz.

NRF24L01 has 126 Channels ...and its good with features like Auto retransmission etc. ASK and FSK are useless for me as i want to use in home automation.

Ok i have started learning the Radio control state from Datasheet and will be soon making own sub routines to deal with.

Thanks for information :)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top