# nRF24L01 with PIC code

**URL:** <https://community.sparkfun.com/t/nrf24l01-with-pic-code/24414>\
**Category:** Wireless & IoT\
**Created:** [June 14, 2012, 9:01pm UTC](https://community.sparkfun.com/t/nrf24l01-with-pic-code/24414 "2012-06-14T21:01:19Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrdongus](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@mrdongus](https://community.sparkfun.com/u/mrdongus)\
**Post date:** [June 14, 2012, 9:01pm UTC](https://community.sparkfun.com/t/nrf24l01-with-pic-code/24414/1 "2012-06-14T21:01:19Z")

</div>

Hi. I use 2 nRF24L01 with 2 PIC18F2550, CCS C compiler. I make as the remote control. I wanna send a packet from the first 24L01 to the remote 24L01. Next, the remote 24L01 send back that packet to the first 24L01.

This first time I work with PIC and nRF24L01. Could you share with me code C to make my application. Maybe It is a base code to sent/ receive …help to complete the application.

Thank you !

Sorry, my English is not good !

---

<div class="post-metadata">

**Author:** ![leon\_heller](https://yyz1.discourse-cdn.com/sparkfun/user_avatar/community.sparkfun.com/leon_heller/32/7602_2.png) [@leon\_heller](https://community.sparkfun.com/u/leon_heller)\
**Post date:** [June 15, 2012, 9:45am UTC](https://community.sparkfun.com/t/nrf24l01-with-pic-code/24414/2 "2012-06-15T09:45:04Z")

</div>

I have some C18 test code here:

[http://www.leonheller.com/Wireless%20Se … 24L01+.zip](http://www.leonheller.com/Wireless%20Sensor/nRF24L01+.zip)

---

<div class="post-metadata">

**Author:** ![mrdongus](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@mrdongus](https://community.sparkfun.com/u/mrdongus)\
**Post date:** [June 16, 2012, 8:19am UTC](https://community.sparkfun.com/t/nrf24l01-with-pic-code/24414/3 "2012-06-16T08:19:21Z")

</div>

> leon\_heller:  
> I have some C18 test code here:
> 
> [http://www.leonheller.com/Wireless%20Se … 24L01+.zip](http://www.leonheller.com/Wireless%20Sensor/nRF24L01+.zip)

Thank you, Leon\_heller !

Do you have the circuit and description for the codes.

🙂

---

<div class="post-metadata">

**Author:** ![leon\_heller](https://yyz1.discourse-cdn.com/sparkfun/user_avatar/community.sparkfun.com/leon_heller/32/7602_2.png) [@leon\_heller](https://community.sparkfun.com/u/leon_heller)\
**Post date:** [June 16, 2012, 8:26am UTC](https://community.sparkfun.com/t/nrf24l01-with-pic-code/24414/4 "2012-06-16T08:26:43Z")

</div>

Here is the schematic:

[http://www.leonheller.com/Wireless%20Se … Sensor.pdf](http://www.leonheller.com/Wireless%20Sensor/Wireless%20Sensor.pdf)

What do you mean by “description for the codes”?

---

<div class="post-metadata">

**Author:** ![mrdongus](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@mrdongus](https://community.sparkfun.com/u/mrdongus)\
**Post date:** [June 20, 2012, 6:50pm UTC](https://community.sparkfun.com/t/nrf24l01-with-pic-code/24414/5 "2012-06-20T18:50:22Z")

</div>

Hi Leon\_heller,

I dont know where spi.h and timers.h are in the header. And can I use the 18F2550 with your code ? suitable ?

---

<div class="post-metadata">

**Author:** ![leon\_heller](https://yyz1.discourse-cdn.com/sparkfun/user_avatar/community.sparkfun.com/leon_heller/32/7602_2.png) [@leon\_heller](https://community.sparkfun.com/u/leon_heller)\
**Post date:** [June 20, 2012, 7:29pm UTC](https://community.sparkfun.com/t/nrf24l01-with-pic-code/24414/6 "2012-06-20T19:29:15Z")

</div>

spi.h and timers.h are part of the C18 distribution. Read the documentation.

You should be able to use the 18F2550, but you might need to make a few changes.

---

<div class="post-metadata">

**Author:** ![mrdongus](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@mrdongus](https://community.sparkfun.com/u/mrdongus)\
**Post date:** [June 21, 2012, 8:53pm UTC](https://community.sparkfun.com/t/nrf24l01-with-pic-code/24414/7 "2012-06-21T20:53:44Z")

</div>

Hi Leon\_heller,

I try to run your code in my C18 compiler download from Microchip website.

In MPLAB I selected the device 18f24j11. and Build All, but BUILD FAILED:

```auto
Clean: Deleting intermediary and output files.
Clean: Deleted file "D:\Mon hoc HK2-011-012\Luan van RF\Code PIC\Test3\test3.o".
Clean: Done.
Executing: "C:\Program Files\Microchip\mplabc18\v3.42\bin\mcc18.exe" -p=18F24J11 "test3.c" -fo="test3.o" -D__DEBUG -Ou- -Ot- -Ob- -Op- -Or- -Od- -Opa-
Executing: "C:\Program Files\Microchip\mplabc18\v3.42\bin\mplink.exe" /l"C:\Program Files\Microchip\mplabc18\v3.42\lib" "C:\Program Files\Microchip\mplabc18\v3.42\bin\LKR\18f24j11_g.lkr" "test3.o" "C:\Program Files\Microchip\mplabc18\v3.42\lib\p18lf24j11.lib" /u_CRUNTIME /u_DEBUG /z __MPLAB_BUILD=1 /z__ MPLAB_DEBUG=1 /o"test3.cof" /M"test3.map" /W
MPLINK 4.43, Linker
Device Database Version 1.8
Copyright (c) 1998-2011 Microchip Technology Inc.
Error - Device not specified. Use /p option to specify a device.
Errors : 1

Link step failed.

```

I dont know the reason. Can you help me ?

---

<div class="post-metadata">

**Author:** ![leon\_heller](https://yyz1.discourse-cdn.com/sparkfun/user_avatar/community.sparkfun.com/leon_heller/32/7602_2.png) [@leon\_heller](https://community.sparkfun.com/u/leon_heller)\
**Post date:** [June 22, 2012, 12:09am UTC](https://community.sparkfun.com/t/nrf24l01-with-pic-code/24414/8 "2012-06-22T00:09:21Z")

</div>

It looks like you are compiling a program called test3.c, which isn’t mine. Try compiling my programs.

The linker step has failed, I think you need to specify the device.

---

<div class="post-metadata">

**Author:** ![mrdongus](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@mrdongus](https://community.sparkfun.com/u/mrdongus)\
**Post date:** [June 22, 2012, 7:51am UTC](https://community.sparkfun.com/t/nrf24l01-with-pic-code/24414/9 "2012-06-22T07:51:16Z")

</div>

I copy your TX.c code into the program test3.c to test.

the device I selected in MPLAB is 18F24j11 as your code you use.

---

<div class="post-metadata">

**Author:** ![leon\_heller](https://yyz1.discourse-cdn.com/sparkfun/user_avatar/community.sparkfun.com/leon_heller/32/7602_2.png) [@leon\_heller](https://community.sparkfun.com/u/leon_heller)\
**Post date:** [June 22, 2012, 8:59am UTC](https://community.sparkfun.com/t/nrf24l01-with-pic-code/24414/10 "2012-06-22T08:59:18Z")

</div>

Look at the error message, you need to specify the device to the linker! The project builds OK for me without any problems:

* * *

Debug build of project `C:\PIC\18F\18F24J11\Wireless Sensor\Tx.mcp’ started.

Language tool versions: mpasmwin.exe v5.44, mplink.exe v4.42, mcc18.exe v3.41, mplib.exe v4.42

Preprocessor symbol `\_\_DEBUG’ is defined.

Fri Jun 22 09:58:18 2012

* * *

Clean: Deleting intermediary and output files.

Clean: Deleted file “C:\PIC\18F\18F24J11\Wireless Sensor\Tx.o”.

Clean: Deleted file “C:\PIC\18F\18F24J11\Wireless Sensor\Tx.cof”.

Clean: Deleted file “C:\PIC\18F\18F24J11\Wireless Sensor\Tx.hex”.

Clean: Deleted file “C:\PIC\18F\18F24J11\Wireless Sensor\Tx.mcs”.

Clean: Done.

Executing: “C:\Program Files (x86)\Microchip\mplabc18\v3.41\bin\mcc18.exe” -p=18F24J11 “Tx.c” -fo=“Tx.o” -D\_\_DEBUG -Ou- -Ot- -Ob- -Op- -Or- -Od- -Opa-

Executing: “C:\Program Files (x86)\Microchip\mplabc18\v3.41\bin\mplink.exe” /p18F24J11 /l"C:\Program Files (x86)\Microchip\mplabc18\v3.40\lib" “Tx.o” /u\_CRUNTIME /u\_DEBUG /z\_\_MPLAB\_BUILD=1 /z\_\_MPLAB\_DEBUG=1 /o"Tx.cof" /M"Tx.map" /W

MPLINK 4.42, Linker

Device Database Version 1.7

Copyright (c) 1998-2011 Microchip Technology Inc.

Errors : 0

MP2HEX 4.42, COFF to HEX File Converter

Copyright (c) 1998-2011 Microchip Technology Inc.

Errors : 0

Loaded C:\PIC\18F\18F24J11\Wireless Sensor\Tx.cof.

* * *

Debug build of project `C:\PIC\18F\18F24J11\Wireless Sensor\Tx.mcp’ succeeded.

Language tool versions: mpasmwin.exe v5.44, mplink.exe v4.42, mcc18.exe v3.41, mplib.exe v4.42

Preprocessor symbol `\_\_DEBUG’ is defined.

Fri Jun 22 09:58:25 2012

* * *

BUILD SUCCEEDED

---

<div class="post-metadata">

**Author:** ![mrdongus](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@mrdongus](https://community.sparkfun.com/u/mrdongus)\
**Post date:** [June 22, 2012, 12:07pm UTC](https://community.sparkfun.com/t/nrf24l01-with-pic-code/24414/11 "2012-06-22T12:07:48Z")

</div>

Hi Leon Heller,

I upgrade new version of MPLAB from v8.56 to v8.85 and I can run your code well. BUILD SUCCEEDED !!!

```auto
 Debug build of project `D:\Mon hoc HK2-011-012\Luan van RF\Code PIC\Test3\test3.mcp' started.
Language tool versions: mpasmwin.exe v5.45, mplink.exe v4.43, mcc18.exe v3.42, mplib.exe v4.43
Preprocessor symbol `__DEBUG' is defined.
Fri Jun 22 18:49:21 2012
----------------------------------------------------------------------
Clean: Deleting intermediary and output files.
Clean: Deleted file "D:\Mon hoc HK2-011-012\Luan van RF\Code PIC\Test3\test3.o".
Clean: Deleted file "D:\Mon hoc HK2-011-012\Luan van RF\Code PIC\Test3\test3.mcs".
Clean: Done.
Executing: "C:\Program Files\Microchip\mplabc18\v3.42\bin\mcc18.exe" -p=18F24J11 "test3.c" -fo="test3.o" -D__DEBUG -Ou- -Ot- -Ob- -Op- -Or- -Od- -Opa-
Executing: "C:\Program Files\Microchip\mplabc18\v3.42\bin\mplink.exe" /p18F24J11 /l"C:\Program Files\Microchip\mplabc18\v3.42\lib" "C:\Program Files\Microchip\mplabc18\v3.42\bin\LKR\18f24j11_g.lkr" "test3.o" "C:\Program Files\Microchip\mplabc18\v3.42\lib\p18lf24j11.lib" /u_CRUNTIME /u_DEBUG /z __MPLAB_BUILD=1 /z__ MPLAB_DEBUG=1 /o"test3.cof" /M"test3.map" /W
MPLINK 4.43, Linker
Device Database Version 1.8
Copyright (c) 1998-2011 Microchip Technology Inc.
Errors : 0

MP2HEX 4.43, COFF to HEX File Converter
Copyright (c) 1998-2011 Microchip Technology Inc.
Errors : 0

Loaded D:\Mon hoc HK2-011-012\Luan van RF\Code PIC\Test3\test3.cof.
----------------------------------------------------------------------
Debug build of project `D:\Mon hoc HK2-011-012\Luan van RF\Code PIC\Test3\test3.mcp' succeeded.
Language tool versions: mpasmwin.exe v5.45, mplink.exe v4.43, mcc18.exe v3.42, mplib.exe v4.43
Preprocessor symbol `__DEBUG' is defined.
Fri Jun 22 18:49:25 2012
----------------------------------------------------------------------
BUILD SUCCEEDED

```

In the header, Can I change #include \<p18fcxxx.h\> by #include \<p18f24j11.h\>? I try to run it and BUILD SUCCEEDED, also. Is it right !?

Thank you for answering my trouble. Hope to continuous receive your answer. 😃

---

<div class="post-metadata">

**Author:** ![leon\_heller](https://yyz1.discourse-cdn.com/sparkfun/user_avatar/community.sparkfun.com/leon_heller/32/7602_2.png) [@leon\_heller](https://community.sparkfun.com/u/leon_heller)\
**Post date:** [June 22, 2012, 12:48pm UTC](https://community.sparkfun.com/t/nrf24l01-with-pic-code/24414/12 "2012-06-22T12:48:49Z")

</div>

Look at p18fcxxx.h and you will see that they do the same thing.

---

<div class="post-metadata">

**Author:** ![mrdongus](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@mrdongus](https://community.sparkfun.com/u/mrdongus)\
**Post date:** [June 23, 2012, 7:03pm UTC](https://community.sparkfun.com/t/nrf24l01-with-pic-code/24414/13 "2012-06-23T19:03:42Z")

</div>

Hi Leon Heller,

I use external oscillator with 20 MHz crystal. And system clock is 48 MHz.

In the Pragmas I wrote 48 MHz below:

```auto
 #pragma config FOSC = HSPLL_HS
#pragma config PLLDIV = 4
#pragma config CPUDIV = OSC1_PLL2

```

And in the init (void). I need not write this as your code ?

```auto
// run internal oscillator at 8 MHz
   OSCCON = OSCCON | 0b01110000;
   while (!OSCCONbits.OSTS) // wait for OSTS to go high

```

The Delay function

```auto
void dly(unsigned int c)
{
   INTCONbits.TMR0IF = 0;
   WriteTimer0(c);
   while (!INTCONbits.TMR0IF)
      ;

```

Could you tell me more How it un and calculate c . ex: c = 65000, ```  
dly(65000); //delay 69 ms

```How

Thank you !
```

---

<div class="post-metadata">

**Author:** ![leon\_heller](https://yyz1.discourse-cdn.com/sparkfun/user_avatar/community.sparkfun.com/leon_heller/32/7602_2.png) [@leon\_heller](https://community.sparkfun.com/u/leon_heller)\
**Post date:** [June 23, 2012, 8:01pm UTC](https://community.sparkfun.com/t/nrf24l01-with-pic-code/24414/14 "2012-06-23T20:01:06Z")

</div>

Everything you need is in the data sheet.
