PIC XC8 and NRF24L01 writing my library - Page 1
Forum section: electronics / general

author: Risto
author_rank: Newbie
timestamp: July 10, 2014, 10:10:18 pm
content: 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.I am writing my library for this module, and I have some troubles with it.I will upload just the code that mattersThe 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 explanationsCode:int main(void) {OSCCON = 0xF7; // int oscinit_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:void init_spi(void){TRISC = 8;SSPCONbits.SSPM= 0x01; // CLOCK = 1/16 FoscSSPCONbits.CKP = 0; // CLOCK polarity = low levelSSPSTATbits.SMP = 0; // Input data sampled at middle of data output timeSSPSTATbits.CKE = 0; // Data transmitted on rising edge of SCKSSPCONbits.SSPEN=0x01; // Enable SPI}void init_nrf(void){NRF_CE = 0; // defined as PORTCbits.RCX ....NRF_CSN = 1; // defined as PORTCbits.RCX ....}Code: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 0x20spi_write(W_REGISTER + address);spi_write(data);}if(command == read){ // read == R_REGISTER command 0x00spi_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 sspbufSPI_IF = 0; // SPI_IF is define again, I always forget the name of this registerSSPBUF = data; // load datawhile(!SPI_IF); // wait for spi_if == 1}

author: Risto
author_rank: Newbie
timestamp: July 12, 2014, 11:11:00 am
content: 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 combinationsHere is code that worked for me.So it must beSSPM = 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 someoneCode:SSPCONbits.SSPM= 0x02;SSPCONbits.CKP = 0;SSPSTATbits.SMP = 1;SSPSTATbits.CKE = 1;SSPCONbits.SSPEN=0x01;

author: dannyf
author_rank: Super Contributor
timestamp: July 12, 2014, 12:14:45 pm
content: 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).

author: dannyf
author_rank: Super Contributor
timestamp: July 12, 2014, 12:15:14 pm
content: 3. when in doubt, use software spi to get the higher level code working first.

author: Risto
author_rank: Newbie
timestamp: July 12, 2014, 07:07:18 pm
content: 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.