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 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
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){
}
}
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 ....
}
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
}