Author Topic: PIC16F886 and NRF24L01  (Read 3306 times)

0 Members and 1 Guest are viewing this topic.

Offline iulicaradTopic starter

  • Newbie
  • Posts: 2
PIC16F886 and NRF24L01
« on: March 03, 2014, 12:36:27 am »
Hello,

I am new to PIC programing and I have a project that uses some PIC16F886 and some NRF24L01 in order to communicate in a star topology. I managed to make the SPI to work on PIC16F886, but I have no clue how to send and receive data with the NRF24L01 module. Can anyone help me?

Thank you,
Iulian
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: PIC16F886 and NRF24L01
« Reply #1 on: March 03, 2014, 02:34:36 am »
It typically starts with reading the datasheet and presenting your code so others can help you.
================================
https://dannyelectronics.wordpress.com/
 

Offline iulicaradTopic starter

  • Newbie
  • Posts: 2
Re: PIC16F886 and NRF24L01
« Reply #2 on: March 04, 2014, 01:18:04 pm »
I have tried to use/adapt the code from here: http://blog.diyembedded.com/2007/06/tutorials-1-3-for-pic-completed.html , from tutorial1, but it does nothing. I can not put my whole code here because is huge, but what i have done is: rewrite my SPI code witch looks like this:

Code: [Select]


#define ss RA5
  void init_io()
{
    TRISA0=0;           //activity led
 
    TRISA5=0;           //SS pin
    TRISC5=0;           //SDO pin as output
    TRISC3=0;           //SCK pin as output
    TRISC4=1;           //SDI pin as input
    TRISC2=0;           //CE pin as output
    ANSEL = 0b00000000; // Make all PORTA pins digital I/O
 
}

unsigned char spi_send_read_byte(unsigned char byte)
{
    ss=0;
    SSPIF=0;
    SSPBUF = byte;
    while (!SSPIF){};  //wait for transmission complete
    ss=1;
    return SSPBUF;
}

void spi_init()
{
    SSPEN = 0; //disable spi interface
    SSPCON= 0b00000000;

    SSPCON = 0b00000010; // SPI Master, Idle High, Fosc/64
    SSPSTAT = 0b01000000;

    SSPEN = 1;       //enable spi interface
    SSPIE = 1;      //enable spi interrupt
}


then, because I use MPLABX with XC8 and i can not write library files, I copied the code from nrfl24l01.c and nrf24l01.h into my main project and adapted the pin and ports for my PIC and implemented this main:

for remote:
Code: [Select]
int main(int argc, char** argv) {

        unsigned char data; //register to hold char

Initialize(); //initialize IO, set up nRF24L01 as TX
        init_io();   //initialize IO for SPI
        spi_init();  //initialize SPI

        while(1)
{
            if (RA1==0)
            { data=0x00;}
            else { data=0xFF;};

           nrf24l01_write_tx_payload(&data, 1, true); //transmit char over RF

//wait until the packet has been sent or the maximum number of retries has been reached
while(!(nrf24l01_irq_pin_active() && nrf24l01_irq_tx_ds_active()));

nrf24l01_irq_clear_all(); //clear all interrupts in the 24L01

ToggleLED(); //toggle the on-board LED as visual indication that the loop has completed
        }
   
}

for local:

Code: [Select]

int main(int argc, char** argv) {

        unsigned char data; //register to hold char
       
Initialize(); //initialize IO, set up nRF24L01 as RX
        init_io();
        spi_init();

        while(1)
{
             //wait until a packet has been received
              while(!(nrf24l01_irq_pin_active() && nrf24l01_irq_rx_dr_active()));

              nrf24l01_read_rx_payload(&data, 1); //read the packet into data
              nrf24l01_irq_clear_all(); //clear all interrupts in the 24L01

              __delay_us(130); //wait for the other 24L01 to come from standby to RX

              if (data==0)
                {RA1=0;}
                 else RA1=1;

              ToggleLED(); //toggle the on-board LED as visual indication that the loop has completed

        }
 }


Can anyone help me please?
 

Offline macboy

  • Super Contributor
  • ***
  • Posts: 2254
  • Country: ca
Re: PIC16F886 and NRF24L01
« Reply #3 on: March 05, 2014, 02:41:17 pm »
How do you know that you are not transmitting data? The way I see that code, it will (assuming it works) transmit characters very quickly, forever, toggling the LED pin each time. It will transmit so fast that the LED will look like it is glowing constantly, not blinking. Put a delay of 1 second or so into the Tx side loop if you want to see the toggle.

PIC16F886 should have enough I/O to support ICD. Do you have a Pickit3 (or better) to do ICD?
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf