[PIC] Need help with NRF24l01

Status
Not open for further replies.

Scolioza

Member level 1
Joined
Mar 5, 2013
Messages
34
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,561
Hi all.

I have a homework and I realy need help. I want to setup a simple comunication between 2 NRF24l01. The problem is that i am not allowed to use any already written software such as libraries, so i started to work on a library. I used 2 PIC 16877a and two NRF modules. I was able to read and write any of NRF registers but i cant make them to communicate. I will attach my code here (written in ccs c) and i hope that more experienced users can help me.
here is the code for general setings and read and write functions
Code:
#include <24L01_TX.h>  
#include <NRF24L01.h>   
#define IRQ     PIN_B0
#define CSN     PIN_B1                                               
#define CE      PIN_B2                                 
#use    rs232(uart1)                        
#use    fast_io(B)

int8 buffer[5];                                                                       
int8 flag=0;                                                                             
                                                        
#INT_EXT
void RB0_isr(){                           
                                                                            
    flag=1;                                              
    clear_interrupt(int_ext);
  }                                      

    //////////////////////////////////////////////////////////////
//  PRINTS ON RS232 READ VALUES 
void serial_out(int1 n_bytes, int8 to_print,int8 reg) {
                                        
      int8 i; 
      if (n_bytes==1){                              
         for(i=0;i<5;i++){
         printf("valoarea %u este ",i);              
         printf("%u",buffer[i]); 
         putc(13);
         delay_ms(1000);                                           
         }                                                                        
      }                                                                           
      else{
         printf("valoarea reg %u este",reg); 
         printf("  %u",to_print);
         putc(13);                                                     
         delay_ms(1000);                                                                      
                    
      }                                                                            
}                            

//////////////////////////////////////////////////////////////                                                         
//WRITE IN 1BYTE REGISTER           
void write_reg_1_B (int8 adresa, int8 valoare){

   int8 write_add=W_REGISTER+adresa;                                                                   
   output_low(CSN);
   delay_us(5);                               
   spi_write(write_add);                            
   int dummy=spi_read();                                                                  
   spi_write(valoare);                                                                                 
   dummy=spi_read();  
   output_high(CSN);
   delay_us(5);
} 
 
  /////////////////////////////////////////////////////////////////  
 //WRITE IN 5 BYTES REGISTER
void write_reg_5_B(int8 adresa,int8 valoare0,int8 valoare1,int8 valoare2,int8 valoare3,int8 valoare4 ){   
   int8 i; 
   int8 dummy;
   int8 write_add=W_REGISTER+adresa;     
   int8 buff[5]; 
   for(i=0;i<5;i++){
     switch(i){                                         
         case 0:buff[0]=valoare0;               
         break;
         case 1:buff[1]=valoare1;                                
         break;                                    
         case 2:buff[2]=valoare2;              
         break;                           
         case 3:buff[3]=valoare3;  
         break;                                                              
         case 4:buff[4]=valoare4;                  
         break; 
    }                                            
                                                                  
   }  
   
   output_low(CSN);
   delay_us(5);                                                                                        
   spi_write(write_add);
   dummy=spi_read();                                   
   for(i=0;i<5;i++){
      spi_write(buff[i]); 
      dummy=spi_read();
   }                                                                    
    output_high(CSN);                                                                                                        
    } 
            
//////////////////////////////////////////////////////////////
//READ 1 BYTE REGISTER           
int8 read_reg_1_B(int8 reg, int1 print){

     int read_add =R_REGISTER+reg;
     int val;
     int dummy;
     output_low(CSN);                             
     delay_us(5);                                                                                      
     spi_write(read_add);  
     dummy=spi_read();          
     spi_write(NOP);  
     val=spi_read();                        
     
     if(print==1){
        serial_out(0,val,reg); 
     }
     output_high(CSN);
     delay_us(5);                                            
     return val;
                                       
}

   //////////////////////////////////////////////////////////////
//  READ 5 BYTES REGISTER     
    
void read_reg_5_B(int8 reg,int1 print) {
   
     int8 read_add=R_REGISTER+reg;
     int8 dummy;
     int8 i;                                                 
     output_low(CSN);
     delay_us(5);
     spi_write(read_add);                                  
     dummy=spi_read();
     for(i=0;i<5;i++){                            
                                           
        spi_write(NOP);
        buffer[i]=spi_read();                        
     }                                      
    output_high(CSN);
    
    if(print==true){  
                                                
        serial_out(1,0,0) ;                       
    }                                                     
}                                 
      ///////////////////////////////////////////////////////////
//RX SET MODE                                                          
                                  
void set_rx(){          
     
   int8 CONFIG_VAL=read_reg_1_B(CONFIG,0);                                                                  
    if(!bit_test(CONFIG_VAL,PRIM_RX)){                                                                
       bit_set(CONFIG_VAL,PRIM_RX);
       write_reg_1_B(CONFIG,CONFIG_VAL);
       output_high(CE);
      }                                                    
                                                                       
   } 

                                                 
    //////////////////////////////////////////////////////////////
//TX SET MODE                                                          

void set_tx(){          
   int8 CONFIG_VAL=read_reg_1_B(CONFIG,0);         
   if(bit_test(CONFIG_VAL,PRIM_RX)){
      bit_clear(CONFIG_VAL,PRIM_RX);                      
      write_reg_1_B(CONFIG,CONFIG_VAL);
      output_low(CE); 
   }                                                   
}                              
     //////////////////////////////////////////////////////////////
//START TRANSCEIVER 
  void power_up() { 

   int8 CONFIG_VAL=read_reg_1_B(CONFIG,0);         
   if(!bit_test(CONFIG_VAL,PWR_UP)){                       
      bit_set(CONFIG_VAL,PWR_UP);
      write_reg_1_B(CONFIG,CONFIG_VAL);                          
   }                
}                                                                                     
                      
     //////////////////////////////////////////////////////////////
//STOP TRANSCEIVER                            
void power_down() { 

   int8 CONFIG_VAL=read_reg_1_B(CONFIG,0);         
   if(bit_test(CONFIG_VAL,PWR_UP)){                       
      bit_clear(CONFIG_VAL,PWR_UP);
      write_reg_1_B(CONFIG,CONFIG_VAL);                          
   }                
}   

     //////////////////////////////////////////////////////////////
//CLEAR INT. ON STATUS REGISTER  RX_DR 
void set_status_rx_dr(){
   int8 STATUS_VAL=read_reg_1_B(STATUS,1);
   if(bit_test(STATUS_VAL,RX_DR)){
     bit_set(STATUS_VAL,RX_DR);
     write_reg_1_B(STATUS,STATUS_VAL);                                                                          
   }
                        
}                                                             
     //////////////////////////////////////////////////////////////
//READ STATE OF RX_DR     
 int1 read_status_rx_dr(){
    int1 flag_rx_dr=0;
    int8 STATUS_VAL=read_reg_1_B(STATUS,1);
    if(bit_test(STATUS_VAL,RX_DR)){
        flag_rx_dr=1;  
    }
    return flag_rx_dr;                                                                                         
} 
      //////////////////////////////////////////////////////////////
//CLEAR INT. ON STATUS REGISTER TX_DS                                                  
void set_status_tx_ds(){
   int8 STATUS_VAL=read_reg_1_B(STATUS,1);
   if(bit_test(STATUS_VAL,TX_DS)){
     bit_set(STATUS_VAL,TX_DS);                                         
     write_reg_1_B(STATUS,STATUS_VAL);
    
   }
                                                             
}                           
     //////////////////////////////////////////////////////////////
//READ STATE OF TX_DS                                             
 int1 read_status_tx_ds(){
    int1 flag_tx_ds=0;
    int8 STATUS_VAL=read_reg_1_B(STATUS,1);
    if(bit_test(STATUS_VAL,TX_DS)){
        flag_tx_ds=1;                                    
    }
    return flag_tx_ds;                                                                                         
}  
      //////////////////////////////////////////////////////////////
//CLEAR INT. ON STATUS REGISTER MAX_RT                                                           
void set_status_max_rt(){                            
   int8 STATUS_VAL=read_reg_1_B(STATUS,1); 
   if(bit_test(STATUS_VAL,MAX_RT)){
     bit_set(STATUS_VAL,MAX_RT);                     
     write_reg_1_B(STATUS,STATUS_VAL);
     //int8 dummy=read_reg_1_B(STATUS,1);
                                                            
   }
                                                          
}                                          
      //////////////////////////////////////////////////////////////
//READ STATE OF MAX_RT      
int1 read_status_max_rt(){                                                                                             
    int1 flag_max_rt=0;
    int8 STATUS_VAL=read_reg_1_B(STATUS,1);
    if(bit_test(STATUS_VAL,MAX_RT)){
        flag_max_rt=1;  
    }
    return flag_max_rt;                                                                                         
}  
                      
      //////////////////////////////////////////////////////////////
//WRITE IN TX_PAYLOAD   
 
void write_tx_payload(int8 valoare0,int8 valoare1, int8 valoare2, int8 valoare3, int8 valoare4){
 int8 i;
 int8 dummy;
 int8 buff[5];
 
 for(i=0;i<5;i++){
     switch(i){                             
         case 0:buff[0]=valoare0;                                                             
         break; 
         case 1:buff[1]=valoare1;                                
         break;                                    
         case 2:buff[2]=valoare2;                               
         break;                           
         case 3:buff[3]=valoare3;                  
         break;                                                
         case 4:buff[4]=valoare4;                  
         break;                                                 
    }                                
 }  
  
 
 output_low(CSN);
 spi_write(FLUSH_TX);                            
 dummy =spi_read();
 output_high(CSN);     
 delay_ms(1); 

 output_low(CSN); 
 spi_write(W_TX_PAYLOAD);
 dummy = spi_read();
 spi_write(buff[0]);                            
 dummy=spi_read();  
  spi_write(buff[1]);                                                                                                        
 dummy=spi_read();                                                                                                            
  spi_write(buff[2]);
 dummy=spi_read(); 
  spi_write(buff[3]);
 dummy=spi_read();               
  spi_write(buff[4]);
 dummy=spi_read();          
 output_high(CSN); 
                                                                                       
 output_high(CE);                   
 delay_us(20);        
                                                           
 
 while(flag==0){printf("NO INT");putc(13);}
 output_low(CE);
}                              
       //////////////////////////////////////////////////////////////
//READ IN RX_PAYLOAD                                         
void read_rx_payload(){
   int8 i;                                   
   int8 dummy;
   
   output_low(CSN);
   output_low(CE);
   spi_write(R_RX_PAYLOAD);
   dummy=spi_read();
                                                                                                                       
   for(i=0;i<5;i++){                                   
       spi_write(NOP);                        
       buffer[i]=spi_read();
   
   }
    
   output_low(CSN);
   spi_write(FLUSH_RX);
   dummy=spi_read();
   output_high(CSN);
   delay_ms(1);
   output_high(CE);
}
and here is the main for RX:

Code:
 void main(){                                                   

   SET_TRIS_B(0b00000001);
   SET_TRIS_D(0b00000000);
   output_d(0);
   output_b(0);
   output_high(CSN);
   setup_comparator(NC_NC_NC_NC);                    
   setup_adc(ADC_OFF); 
   enable_interrupts(GLOBAL); 
   enable_interrupts(INT_EXT);
   ext_int_edge(H_TO_L);
   clear_interrupt(INT_EXT); 
    
   
   setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_XMIT_L_TO_H|SPI_CLK_DIV_4); //setare spi mod0 , 2MHZ      
   setup_uart(9600); // baud rate UART  
                                                  
   delay_ms(2000);
   printf("start___________");                                                           
   putc(13);                                                                                                                        
   delay_ms(1000);                                        
  
   int dummy;
   int8 i;
     
   power_up();  //pornire transciever
   set_rx();   // setare mod RX
                                        
   
   //CONFIGURARE REGISTRII 8 BITI                                                                                               
                                                                                                             
                                                                
                                                                
   //dummy=read_reg_1_B(STATUS,1); 
    
   write_reg_1_B(EN_AA,0b00000001);// auto ACK enabled on pipe P0                                                         
   //dummy=read_reg_1_B(EN_AA,1); 
   write_reg_1_B(EN_RXADDR,0b00000001);  // rx pipe 0 enabled
   //dummy=read_reg_1_B(EN_RXADDR,1); 
   write_reg_1_B(SETUP_AW,0b00000011);// numele adresei este pe 5 bytes
   //dummy=read_reg_1_B(SETUP_AW,1);        
   write_reg_1_B(SETUP_RETR,0b00001111); // daca nu exista ack informatia este retransmisa de max 15 ori                   
   write_reg_1_B(RF_CH,0b00000010);      // frecventa de operare este setata la 2.4 GHZ                                                                     
   write_reg_1_B(RF_SETUP,0b00000111);   // 1Mbps, putere de transmisie max    
   write_reg_5_B(RX_ADDR_P0,0XE7,0XE7,0XE7,0XE7,0XE7); // adresa la care se primesc pachete se ut. PIPE 0                                    
   write_reg_5_B(TX_ADDR,0XE7,0XE7,0XE7,0XE7,0XE7);    // adresa de la care se transmit pachete
   write_reg_1_B(RX_PW_P0,0b00000101);                 // numarul de bytes in payload - 5 bytes 
   
                                            
                                                                    
   while(TRUE)                                                            
   { 
     read_reg_1_B(CONFIG,1);   
     if(flag){                                         
     printf("am primit o intrerupere");
      int1 check_int = read_status_rx_dr();
      if(check_int){                                                    
                                                                                                           
          printf("RECEPTIE OK !!");
          read_rx_payload();
          set_status_rx_dr();
          flag=0; 
          for(i=0;i<5;i++){                    
            output_d(buffer[i]);                                                                                                          
            delay_ms(500);  
            int1 check_int = read_status_rx_dr();
          }    
        }                                                            
     }                                         
   }                                                                                                                                           
}

and here is the main for TX
Code:
void main(){                                                   

   SET_TRIS_B(0b00000001); 
   output_b(0);
   output_high(CSN);
   output_high(CE);
   
                                                                                                                                         
   setup_comparator(NC_NC_NC_NC);                    
   setup_adc(ADC_OFF); 
   enable_interrupts(GLOBAL); 
   enable_interrupts(INT_EXT);                                                                                                   
   clear_interrupt(INT_EXT);                 
   ext_int_edge(H_TO_L); 
                                                                                                                     
   setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_XMIT_L_TO_H|SPI_CLK_DIV_4); //setare spi mod0 , 2MHZ      
   setup_uart(9600); // baud rate UART  
   power_up();  
                                                
   delay_ms(2000);                                                                                                     
   printf("start___________");
    
   putc(13);                                   
   delay_ms(1000);                                        

   int dummy;                                                          
   power_up();  //pornire transciever                                                               
   set_tx();                             
                                                             
   
   //CONFIGURARE REGISTRII 8 BITI                                                                                               
                                                                                                             
                                                             
                                                                
   //dummy=read_reg_1_B(STATUS,1);
   
   //write_reg_1_B(CONFIG,) 
   write_reg_1_B(EN_AA,0b00000001);// auto ACK enabled on pipe P0                                                         
   //dummy=read_reg_1_B(EN_AA,1); 
   write_reg_1_B(EN_RXADDR,0b00000001);  // rx pipe 0 enabled
  // dummy=read_reg_1_B(EN_RXADDR,1); 
   write_reg_1_B(SETUP_AW,0b00000011);// numele adresei este pe 5 bytes
   //dummy=read_reg_1_B(SETUP_AW,1);        
   write_reg_1_B(SETUP_RETR,0b00001111); // daca nu exista ack informatia este retransmisa de max 15 ori                   
   write_reg_1_B(RF_CH,0b00000010);      // frecventa de operare este setata la 2.4 GHZ                                                                     
   write_reg_1_B(RF_SETUP,0b00000111);   // 1Mbps, putere de transmisie max    
   write_reg_5_B(RX_ADDR_P0,0XE7,0XE7,0XE7,0XE7,0XE7);// adresa la care se primesc pachete se ut. PIPE 0    
   write_reg_5_B(TX_ADDR,0XE7,0XE7,0XE7,0XE7,0XE7);    // adresa de la care se transmit pachete
   write_reg_1_B(RX_PW_P0,0b00000101);                 // numarul de bytes in payload - 5 bytes 
   read_reg_1_B(RX_PW_P0,1);
                                            
    flag=0;                                                                
   while(TRUE)                                                            
   { 
     read_reg_1_B(CONFIG,1);
     
     write_tx_payload(1,2,4,8,16);                                           
     if(flag){                                                                                                                     
     
     int1 check_int = read_status_tx_ds();
     if(check_int){
          printf("TRANSMISIE OK !!");                                                                                       
          set_status_tx_ds();                           
          flag=0;                              
                                                            
      }     
                                              
     }                                
                     
   }                                                                                                                                             
}

Any help or hint will be very valuable.
PS.
I already read lots of tutorials and I think that is a missunderstooding but i cant figure where.
Thanks in advance
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…