PIC16F886 and NRF24L01 - Page 1
Forum section: electronics / general

author: iulicarad
author_rank: Newbie
timestamp: March 03, 2014, 12:36:27 am
content: 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

author: dannyf
author_rank: Super Contributor
timestamp: March 03, 2014, 02:34:36 am
content: It typically starts with reading the datasheet and presenting your code so others can help you.

author: iulicarad
author_rank: Newbie
timestamp: March 04, 2014, 01:18:04 pm
content: 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 RA5void init_io(){TRISA0=0; //activity ledTRISA5=0; //SS pinTRISC5=0; //SDO pin as outputTRISC3=0; //SCK pin as outputTRISC4=1; //SDI pin as inputTRISC2=0; //CE pin as outputANSEL = 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 completess=1;return SSPBUF;}void spi_init(){SSPEN = 0; //disable spi interfaceSSPCON= 0b00000000;SSPCON = 0b00000010; // SPI Master, Idle High, Fosc/64SSPSTAT = 0b01000000;SSPEN = 1; //enable spi interfaceSSPIE = 1; //enable spi interrupt}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 charInitialize(); //initialize IO, set up nRF24L01 as TXinit_io(); //initialize IO for SPIspi_init(); //initialize SPIwhile(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 reachedwhile(!(nrf24l01_irq_pin_active() && nrf24l01_irq_tx_ds_active()));nrf24l01_irq_clear_all(); //clear all interrupts in the 24L01ToggleLED(); //toggle the on-board LED as visual indication that the loop has completed}}for local:Code:int main(int argc, char** argv) {unsigned char data; //register to hold charInitialize(); //initialize IO, set up nRF24L01 as RXinit_io();spi_init();while(1){//wait until a packet has been receivedwhile(!(nrf24l01_irq_pin_active() && nrf24l01_irq_rx_dr_active()));nrf24l01_read_rx_payload(&data, 1); //read the packet into datanrf24l01_irq_clear_all(); //clear all interrupts in the 24L01__delay_us(130); //wait for the other 24L01 to come from standby to RXif (data==0){RA1=0;}else RA1=1;ToggleLED(); //toggle the on-board LED as visual indication that the loop has completed}}Can anyone help me please?

author: macboy
author_rank: Super Contributor
timestamp: March 05, 2014, 02:41:17 pm
content: How do you know that you arenottransmitting 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?