Electronics > Microcontrollers

PIC16F886 and NRF24L01

(1/1)

iulicarad:
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

dannyf:
It typically starts with reading the datasheet and presenting your code so others can help you.

iulicarad:
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: ---

#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
}


--- End code ---

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: ---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
        }
   
}

--- End code ---

for local:


--- Code: ---
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

        }
 }


--- End code ---

Can anyone help me please?

macboy:
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?

Navigation

[0] Message Index

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod