Author Topic: PIC XC8 and NRF24L01 writing my library  (Read 3800 times)

0 Members and 1 Guest are viewing this topic.

Offline RistoTopic starter

  • Newbie
  • Posts: 6
  • Country: cs
PIC XC8 and NRF24L01 writing my library
« on: July 10, 2014, 10:10:18 pm »
Hi!
This is my first post on forum, so please be easy on me if I did something wrong  :-[ .
Huge fan of eevblog by the way.  ;D

I am writing my library for this module, and I have some troubles with it.
I will upload just the code that matters

The problem is, I can read register on NRF just the first time.
NRF responds just on first command, and later it send just the status register on first byte transfered, on second byte i get nothing.

Same goes if I send write command first. Not sure if sending multiple write commands work.

 Have no idea how to try if multiple write command works, exept maybe measuring current in different stages, or trying to transmit something. Which will be difficult to verify while my code is not capable of reading registers.

Maybe I missed something from datasheet. Maybe that is normal behavior. I just want to know if my communication with chip is working before continue writing.

I uploaded screenshots from L/A, hope that clarifies my probably confusing explanations  ???

Code: [Select]
int main(void) {
    OSCCON = 0xF7;             // int osc
    init_spi();

    TRISB = 0;
    PORTB = 0;

    init_nrf();

__delay_ms(100);

PORTB = NRF_rw(read, SETUP_AW, 0x00);
PORTB = NRF_rw(read, RF_CH, 0x00);
PORTB = NRF_rw(read, RF_SETUP, 0x00);
PORTB = NRF_rw(read, EN_RXADDR, 0x00);
PORTB = NRF_rw(read, SETUP_RETR, 0x00);
PORTB = NRF_rw(read, EN_AA, 0x00);
PORTB = NRF_rw(read, RX_PW_P0, 0x00);
PORTB = NRF_rw(read, STATUS, 0x00);

    while(1){
    }
}

Code: [Select]
void init_spi(void){

    TRISC = 8;
    SSPCONbits.SSPM= 0x01;  // CLOCK = 1/16 Fosc
    SSPCONbits.CKP = 0;     // CLOCK polarity = low level
    SSPSTATbits.SMP = 0;    // Input data sampled at middle of data output time
    SSPSTATbits.CKE = 0;    // Data transmitted on rising edge of SCK
    SSPCONbits.SSPEN=0x01;  // Enable SPI
}

void init_nrf(void){
    NRF_CE = 0;         // defined as PORTCbits.RCX ....
    NRF_CSN = 1;      // defined as PORTCbits.RCX ....

}

Code: [Select]
unsigned int NRF_rw(unsigned int command, unsigned int address, unsigned int data){
    unsigned int data_rtn;

    NRF_CSN = 0;
           
            if(command == write){                                      // write == W_REGISTER command 0x20
                spi_write(W_REGISTER + address);
                spi_write(data);
             }
           
            if(command == read){                 // read == R_REGISTER command 0x00
                spi_write(address);
                spi_write(0xFF);
            }

        data_rtn = SSPBUF;

    NRF_CSN = 1;
    return data_rtn;
}

void spi_write(unsigned int data){

    unsigned int erase;
    erase = SSPBUF;           // delete sspbuf
    SPI_IF = 0;               // SPI_IF is define again, I always forget the name of this register
    SSPBUF = data;            // load data
    while(!SPI_IF);           // wait for spi_if == 1
}

« Last Edit: July 10, 2014, 10:44:23 pm by Risto »
 

Offline RistoTopic starter

  • Newbie
  • Posts: 6
  • Country: cs
Re: PIC XC8 and NRF24L01 writing my library
« Reply #1 on: July 12, 2014, 11:11:00 am »
In meanwhile i solved the problem.

I started to change everything in my code.

And i found that my SPI configuration was guilty.

Took me some time to run through all the combinations :D

 

Here is code that worked for me.

 

So it must be

SSPM = 0x02;

CKP = 0;

SMP = 1;

CKE = 1;

Anyway I am not completely sure why is this happening, i couldn't find the answer in the NRF or PIC datasheet.

Not sure if some other combination works i settled down on this one. And dont want to smack my head anymore with this. So i hope this will help someone

Code: [Select]

 

     SSPCONbits.SSPM= 0x02;
    SSPCONbits.CKP = 0;
    SSPSTATbits.SMP = 1;
    SSPSTATbits.CKE = 1;
    SSPCONbits.SSPEN=0x01;

 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: PIC XC8 and NRF24L01 writing my library
« Reply #2 on: July 12, 2014, 12:14:45 pm »
1. while the spi is capable of 10Mhz, you have to factor in load capacitance (5-7pf minimum per pin) + minimum SCK high/low time in determining the speed. I typically start with a 100Khz speed and go up from there. Starting higher than that is risky and unnecessary;
2. the spi protocol seems to be sending on the rising edge and receiving on the falling edge, per the datasheet. So I would check to see if you are receiving the right value (they could be shifted by 1 bit).
================================
https://dannyelectronics.wordpress.com/
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: PIC XC8 and NRF24L01 writing my library
« Reply #3 on: July 12, 2014, 12:15:14 pm »
3. when in doubt, use software spi to get the higher level code working first.
================================
https://dannyelectronics.wordpress.com/
 

Offline RistoTopic starter

  • Newbie
  • Posts: 6
  • Country: cs
Re: PIC XC8 and NRF24L01 writing my library
« Reply #4 on: July 12, 2014, 07:07:18 pm »
Thanks dannyf. I didn't thought that the hardware may be problem.
And the think that i work on easypic7. With 15cm of traces from mcu to headers and another 10cm of wires to nrf just make things worse.
I will try previous settings when i make pcb with shorter spi lines.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf