Hi everyone,
First off thanks everyone for sharing your knowledge. I’m pretty new at this. Also this is a repeat post - posted this same under ‘Timing Issues’ thread, but I figured I might here quicker with a new post
I’m using a 16F88 and its SPI interface to talk with the nRF24L01. I’m currently trying to re-write example code to work with my set up - I can see my data clock into the nRF24L01 with an analyzer, but I think I either have timing issues or I’m missing something because I can’t get any comm going. I’m using a ccs picc compiler so it’s a bit different than the CCX5(?) code examples I’ve found.
Right now I’m just trying to mimick the example and have basic shockburst TX and receive a payload on the other end. Once I get basics working then I’ll work towards using the more advanced features and cleaning the code up. For now I’m breaking up all my instruction calls.
here’s my TX and RX code - if anyone sees an issue please let me know. in the meantime I’ll keep grinding. thanks
A question: Is there potential issues with using SPI? like timing, etc? I notice a lot of code using a bit-banging format and I’m wondering if there is an advantage of one over the other?
My TX code:
#include<16F88.h>
#fuses INTRC_IO,NOWDT,NOPUT,MCLR,NOPROTECT,NOBROWNOUT,NOLVP,NOFCMEN,NOIESO
#use delay(clock=4000000)
#use standard_io(A)
#use standard_io(B)
/********************************START PIC16F88 Defines***********************************************/
// Control and SPI Pin defines ////////////////////////////////////////
#define CE PIN_B5 // output Chip enable to nRF24L01 CE PIN_1
#define CSN PIN_B3 // output Chip Select to nRF24L01 CSN PIN_2
#define SCK PIN_B4 // serial clock output to nRF24L01 SCK PIN_3
#define MOSI PIN_B2 // serial data out to nRF24L01 MOSI PIN_4
#define MISO PIN_B1 // serial data in from nRF24L01 MISO PIN_5
#define IRQ PIN_B0 // input valid TX/RX from nRF24L01 IRQ PIN_6
// LED indicator Pins
#define LED0 PIN_A0
#define LED1 PIN_A1
#define LED2 PIN_A2
/********************************END PIC16F88 Defines*************************************************/
unsigned int cmd, data, j;
void spi_write1(int a){
delay_us(10);
spi_write(a);
delay_us(10);
}
// Configure transmitter
void TX_Mode(){
output_low(CE);
output_low(CSN);
cmd = 0x20; // write register, CONFIG
data = 0x38; //ptx, crc enable, mask a couple ints
spi_write1(cmd);
spi_write1(data);
output_high(CSN);
delay_us(20);
output_low(CSN);
cmd = 0x24; // write register, setup_retr
data = 0x00; // setup_retr = off
spi_write1(cmd);
spi_write1(data);
output_high(CSN);
delay_us(20);
output_low(CSN);
cmd = 0x23; // write register, setup_aw
data = 0x03; // address width = 5
spi_write1(cmd);
spi_write1(data);
output_high(CSN);
delay_us(20);
output_low(CSN);
cmd = 0x26; // write register, RF_SETUP
data = 0x0F; // data rate =2Mbps,0dBm,LNA gain
spi_write1(cmd);
spi_write1(data);
output_high(CSN);
delay_us(20);
output_low(CSN);
cmd = 0x25; // write register, RF_CH
data = 0x02; // default ch 2
spi_write1(cmd);
spi_write1(data);
output_high(CSN);
delay_us(20);
output_low(CSN);
cmd = 0x30; // write register, TX address
data = 0xE7; // set address E7E7E7E7E7
spi_write(cmd);
for(j=0;j<5;j++){
spi_write1(data);
}
output_high(CSN);
delay_us(20);
output_low(CSN);
cmd = 0x21; // write register, en_aa
data = 0x00; // disable auto-act in RX mode
spi_write1(cmd);
spi_write1(data);
output_high(CSN);
delay_us(20);
}
// Transmit data
void TX_Data(){
output_low(CSN);
cmd = 0x27; // write register, STATUS
data = 0x7E; // clear previous ints
spi_write1(cmd);
spi_write1(data);
output_high(CSN);
delay_us(20);
output_low(CSN);
cmd = 0x20; // write register, CONFIG
data = 0x3A; // power up = 1
spi_write1(cmd);
spi_write1(data);
output_high(CSN);
delay_us(20);
output_low(CSN);
cmd = 0xE1; // flush_tx
spi_write1(cmd);
output_high(CSN);
delay_us(20);
output_low(CSN);
cmd = 0xA0; // write tx_payload
data = 0x34; // 4 byte payload
spi_write1(cmd);
spi_write1(data);
data = 0x33; // 4 byte payload
spi_write1(data);
data = 0x32; // 4 byte payload
spi_write1(data);
data = 0x31; // 4 byte payload
spi_write1(data);
output_high(CSN);
delay_us(20);
output_high(CE);
delay_us(20);
output_low(CE);
}
/********************************TX/RX IRQ Interrupt Acknowledgement********************************/
//Main
void main(){
setup_oscillator(OSC_4MHZ|OSC_INTRC);
setup_adc_ports(NO_ANALOGS); // Set PortA Digital IO
setup_adc(ADC_OFF); // Turn off ADC
setup_ccp1(CCP_OFF); // Turn off CCP - will disrupt output writes on PIN_B3 if on
setup_spi(SPI_MASTER|SPI_XMIT_L_TO_H|SPI_CLK_DIV_64);
// set up IRQ (PIN_B0) interrupt for TX/RX notification
//enable_interrupts(INT_EXT);
//ext_int_edge(H_TO_L);
//enable_interrupts(GLOBAL);
//output_low(CE); // set CE low to disable transmission
output_b(0b00001000);
output_low(LED0 | LED1 | LED2); // set LEDs Low
delay_ms(2);
output_high(LED0);
TX_Mode();
output_low(LED0);
while(1){
TX_Data();
output_high(LED2);
delay_ms(10);
output_low(LED2);
delay_ms(500);
}
}
My RX code:
/* RF weapon track test code - R.Schoelerman 11/06
SPI interface between PIC16F88 and nRF24L01
nRF24L01 IO:
Chip Select RB3
Chip Enable RB5
IRQ RB6
SPI Control Pins:
Serial Data Out (SDO) RB2
Serial Data In (SDI) RB1
Serial Clock (SCK) RB4
*/
#include<16F88.h>
#fuses INTRC_IO,NOWDT,NOPUT,MCLR,NOPROTECT,NOBROWNOUT,NOLVP,NOFCMEN,NOIESO
#use delay(clock=4000000)
#use standard_io(A)
#use standard_io(B)
/********************************START PIC16F88 Defines***********************************************/
// Control and SPI Pin defines ////////////////////////////////////////
#define CE PIN_B5 // output Chip enable to nRF24L01 CE PIN_1
#define CSN PIN_B3 // output Chip Select to nRF24L01 CSN PIN_2
#define SCK PIN_B4 // serial clock output to nRF24L01 SCK PIN_3
#define MOSI PIN_B2 // serial data out to nRF24L01 MOSI PIN_4
#define MISO PIN_B1 // serial data in from nRF24L01 MISO PIN_5
#define IRQ PIN_B0 // input valid TX/RX from nRF24L01 IRQ PIN_6
// LED indicator Pins
#define LED0 PIN_A0
#define LED1 PIN_A1
#define LED2 PIN_A2
/********************************END PIC16F88 Defines*************************************************/
unsigned int data, cmd, j;
void spi_write1(int a){
delay_us(10);
spi_write(a);
delay_us(10);
}
void spi_read1(int a){
delay_us(10);
data = spi_read(a);
delay_us(10);
}
// Configure receiver
void RX_Mode(void){
int data, cmd, j;
output_low(CE);
output_low(CSN);
cmd = 0x20; // write register, CONFIG
data = 0x39; //prx, crc enable, mask a couple ints
spi_write1(cmd);
spi_write1(data);
output_high(CSN);
delay_us(20);
output_low(CSN);
cmd = 0x21; // write register, en_aa
data = 0x00; // dissable auto ack
spi_write1(cmd);
spi_write1(data);
output_high(CSN);
delay_us(20);
output_low(CSN);
cmd = 0x23; // write register, setup_aw
data = 0x03; // address width = 5
spi_write1(cmd);
spi_write1(data);
output_high(CSN);
delay_us(20);
output_low(CSN);
cmd = 0x26; // write register, RF_SETUP
data = 0x0F; // data rate =2Mbps,0dBm,LNA gain
spi_write1(cmd);
spi_write1(data);
output_high(CSN);
delay_us(20);
output_low(CSN);
cmd = 0x31; // write register, tx address
data = 0x04; // 4 byte payload
spi_write1(cmd);
spi_write1(data);
output_high(CSN);
delay_us(20);
output_low(CSN);
cmd = 0x25; // write register, rf channel
data = 0x02; // channel 2
spi_write1(cmd);
spi_write1(data);
output_high(CSN);
delay_us(20);
output_low(CSN);
cmd = 0x30; // write register, RX address
data = 0xE7; // set address E7E7E7E7E7
spi_write1(cmd);
for(j=0;j<5;j++){
spi_write1(data);
}
output_high(CSN);
delay_us(20);
output_low(CSN);
cmd = 0x20; // write register, config
data = 0x3B; // power up = 1
spi_write1(cmd);
spi_write1(data);
output_high(CSN);
output_high(CE);
delay_us(20);
}
// reset all ints
void reset_RX(void){
int data, cmd;
output_low(CSN);
cmd = 0x61; // read rx payload
//spi_write1(cmd);
spi_read1(cmd);
output_high(CSN);
delay_us(20);
output_low(CSN);
cmd = 0xE2; // flush rx
data = 0x3A; // power up = 1
spi_write1(cmd);
output_high(CSN);
delay_us(20);
output_low(CSN);
cmd = 0x27; // write register STATUS
data = 0x40; //
spi_write1(cmd);
spi_write1(data);
output_high(CSN);
}
/********************************TX/RX IRQ Interrupt Acknowledgement********************************/
//Main
void main(){
setup_oscillator(OSC_4MHZ|OSC_INTRC);
setup_adc_ports(NO_ANALOGS); // Set PortA Digital IO
setup_adc(ADC_OFF); // Turn off ADC
setup_ccp1(CCP_OFF); // Turn off CCP - will disrupt output writes on PIN_B3 if on
setup_spi(SPI_MASTER|SPI_XMIT_L_TO_H|SPI_CLK_DIV_64);
// set up IRQ (PIN_B0) interrupt for TX/RX notification
//enable_interrupts(INT_EXT);
//ext_int_edge(H_TO_L);
//enable_interrupts(GLOBAL);
//output_low(CE); // set CE low to disable transmission
output_b(0b00001000);
output_low(LED0 | LED1 | LED2); // set LEDs Low
delay_ms(2);
output_high(LED0);
RX_mode();
output_low(LED0);
while(1){
int x;
if(IRQ == 0){
for(x=0;x<5;x++){
output_high(LED2);
delay_ms(100);
output_low(LED2);
delay_ms(100);
}
delay_ms(200);
reset_RX();
}
}
}