You are on page 1of 8

CCS :: View topic - Interfacing MRF24J40MA with PIC ??

http://www.ccsinfo.com/forum/viewtopic.php?t=38199

FAQ Forum Help Register Profile in

Official CCS Support

Search

Please go to www.mcuspace.com/mcuforum for posts concerning PIC MCU CCS does not monitor this forum on a regular basis.

Log in to check your private messages

Log

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

Interfacing MRF24J40MA with PIC ??


Goto page 1, 2 Next

CCS Forum Index -> General CCS C Discussion View previous topic :: View next topic Author jjacob Interfacing MRF24J40MA with PIC ??
Posted: Sat Mar 14, 2009 4:45 am Joined: 08 Mar 2008 Posts: 47 Location: PORTUGAL (PORTO)

Message

Hi all. I'm trying to make a wireless sensor network. So i'll have a PIC, connect the sensor to the ADC, then use SPI to 'talk' to the "MRF24J40MA". My problem, is that i don't understand how do i configure and control the "MRF24J40MA". In Microchip site i found : http://ww1.microchip.com/downloads/en/AppNotes/01192a.pdf and of course the datasheet : http://ww1.microchip.com/downloads/en/DeviceDoc/70329a.pdf Can anybody help me ? Just a little thing for me to start ? Thank you in advance. Jacob

PCM programmer

Posted: Sat Mar 14, 2009 10:57 pm

Joined: 06 Sep 2003 Posts: 16720

That's the data sheet for the module. It says to look at the data sheet for the MRF24J40 chip, to learn about the SPI interface: http://ww1.microchip.com/downloads/en/DeviceDoc/39776b.pdf Look in this section on page 15:
Quote: 4.0 SERIAL PERIPHERAL INTERFACE (SPI) 4.1 Overview

It tells you that it uses SPI mode 0. Here are the constants for the SPI modes. You can use the appropriate one in your setup_spi() statement. Put these statements near the top of your MRF24J40 driver file.
Code: #define #define #define #define SPI_MODE_0 SPI_MODE_1 SPI_MODE_2 SPI_MODE_3 (SPI_L_TO_H | SPI_XMIT_L_TO_H) (SPI_L_TO_H) (SPI_H_TO_L) (SPI_H_TO_L | SPI_XMIT_L_TO_H)

Next, you need to know the maximum SPI clock frequency that can be

1 trong 8

9/6/2012 5:42 AM

CCS :: View topic - Interfacing MRF24J40MA with PIC ??

http://www.ccsinfo.com/forum/viewtopic.php?t=38199

used. The data sheet doesn't explictly say, but on page 45 it gives the high and low times for SCK as 50 ns min. So, the clock period would be 100 ns min, which would be 10 MHz max for SCK. Refer to this figure:
Quote: FIGURE 10-1: EXAMPLE SPI SLAVE MODE TIMING

You didn't say what the oscillator speed of your PIC is (or what PIC you're using), so let's assume it's 20 MHz. You could use a divisor of 4, which would give you an SCK frequency of 5 MHz. Given all this information, you can now write the setup_spi() statement:
Code: setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_4);

The next thing you need to do is write the low level routines which read and write to the MRF24J40 via the SPI interface. On page 16 of the data sheet, they have these sections:
Quote: 4.2.2 WRITING SHORT ADDRESS REGISTERS FIGURE 4-3: SHORT ADDRESS READ EXAMPLE 4-1: SHORT ADDRESS READ EXAMPLE

They give some C source code. We need to write a CCS version of it. Here is their Example 4-1 code:
Code: void SetShortRAMAddress(BYTE address, BYTE value) { CSn = 0; SPIPut(((address<<1)&0b01111111)|0x01); SPIPut(value); CSn = 1; }

Here is the CCS version of it:


Code: #define MRF24J40_CS PIN_C0

void SetShortRAMAddress(int8 address, int8 value) { output_low(MRF24J40_CS); spi_write(((address<<1) & 0b01111111) | 0x01); spi_write(value); output_high(MRF24J40_CS); }

I have chosen Pin C0 for the \CS signal. It's presumably a spare i/o pin on your PIC. Now let's do the next routine in the MRF24J40 data sheet, which is this one:
Quote: 4.3.1 READING LONG ADDRESS REGISTERS EXAMPLE 4-3: LONG ADDRESS READ EXAMPLE

Here is their sample code:


Code:

2 trong 8

9/6/2012 5:42 AM

CCS :: View topic - Interfacing MRF24J40MA with PIC ??

http://www.ccsinfo.com/forum/viewtopic.php?t=38199

BYTE GetLongRAMAddress(WORD address) { BYTE toReturn; CSn = 0; SPIPut(((address>>3)&0b01111111)|0x80); SPIPut(((address<<5)&0b11100000)); toReturn = SPIGet(); CSn = 1; return toReturn; }

Here is the CCS version of it:


Code: int8 GetLongRAMAddress(int16 address) { int8 toReturn; output_low(MRF24J40_CS); spi_write(((address>>3) & 0b01111111) | 0x80); spi_write(((address<<5) & 0b11100000)); toReturn = spi_read(0); // 0 parameter needed to make SCK output_high(MRF24J40_CS); return(toReturn); }

With this information, you should be able to write these remaining routines:
Quote: 4.3.2 WRITING LONG ADDRESS REGISTERS EXAMPLE 6-1: INITIALIZING THE MRF24J40

They don't show it in their code, but at the start of the init routine, you should: -- Set the MRF24J40_CS signal to a high level. -- Set the MRF24J40_RESET signal to a high level. -- Call the setup_spi() function, as given above. At the beginning of their sample init routine, they do two software delay loops. They don't tell you how long these are. Worst case, they might be using a 4 MHz PIC oscillator frequency, so each loop iteration might be 10 us. So their delay loops might be 3 ms each. If you substitute the following CCS code for each of those delay loops, I think it would certainly be safe:
Code: delay_ms(10); jjacob Interfacing MRF24J40MA with PIC ??
Posted: Sun Mar 15, 2009 9:41 am Joined: 08 Mar 2008 Posts: 47 Location: PORTUGAL (PORTO)

Thank you PCM programmer. You gave me TOP LEVEL information And you were also VERY specific in your answer... With this help i think that i can do the work, and now i understood my mistake I had some problems relating the pages and the URL of the datasheet that you gave : http://ww1.microchip.com/downloads/en/DeviceDoc/39776b.pdf Then i search and found : http://ww1.microchip.com/downloads/en/DeviceDoc/39776a.pdf

3 trong 8

9/6/2012 5:42 AM

CCS :: View topic - Interfacing MRF24J40MA with PIC ??

http://www.ccsinfo.com/forum/viewtopic.php?t=38199

Now almost all the pages are OK. What was the file that you used ? The important thing is that with your help i understood how things work with the MRF24F40. THANK YOU ONCE AGAIN. Jacob
enkavak
Guest

respond
Posted: Mo n May 18, 2009 8:51 am

Hello jjacob Could you share your files if you had some progress on MRF24J40. Kind regards engin
jjacob
Posted: Wed May 20, 2009 2:40 pm

Hello.
Joined: 08 Mar 2008 Posts: 47 Location: PORTUGAL (PORTO)

I haven't tried any code I only tested the hardware. Sorry. A priority project has appeared. As soon as I have some code, I'll post it ... Jacob

Fram_fr

Posted: Thu May 28, 2009 4:46 am

Hi all,
Joined: 29 Oct 2008 Posts: 12

Because I will use MRF24J40 in some weeks, I start to write a driver for this chipboard, so here is a first piece of code (the low level routines which read and write to the MRF24J40 via the SPI interface) inspired by PCM programmer: First need to define CS pin: #define MRF24J40_CS PIN_xx
Code:

/********************************************************************* * Function: void SetShortRAMAddress(int8 address, int8 value) * * PreCondition: Communication port to the MRF24J40 initialized * * Input: BYTE address is the address of the short RAM address * that you want to write to. Should use the * WRITE_ThisAddress definition in the MRF24J40 * include file. * BYTE value is the value that you want to write to * that register * * Output: None * * Side Effects: The register value is changed * * Overview: This function writes a value to a short RAM address ********************************************************************/ void SetShortRAMAddress(int8 address, int8 value) { output_low(MRF24J40_CS);

4 trong 8

9/6/2012 5:42 AM

CCS :: View topic - Interfacing MRF24J40MA with PIC ??

http://www.ccsinfo.com/forum/viewtopic.php?t=38199

spi_write(((address<<1) & 0b01111111) | 0x01); spi_write(value); output_high(MRF24J40_CS); } /********************************************************************* * Function: void SetLongRAMAddress(int16 address, int8 value) * * PreCondition: Communication port to the MRF24J40 initialized * * Input: WORD address is the address of the LONG RAM address * that you want to write to * BYTE value is the value that you want to write to * that register * * Output: None * * Side Effects: The register value is changed * * Overview: This function writes a value to a LONG RAM address ********************************************************************/ void SetLongRAMAddress(int16 address, int8 value) { output_low(MRF24J40_CS); spi_write(((address>>3)&0b01111111)|0x80); spi_write(((address<<5)&0b11100000)|0x10); spi_write(value); output_high(MRF24J40_CS); } /********************************************************************* * Function: void GetShortRAMAddress(int8 address) * * PreCondition: Communication port to the MRF24J40 initialized * * Input: BYTE address is the address of the short RAM address * that you want to read from. Should use the * READ_ThisAddress definition in the MRF24J40 * include file. * * Output: BYTE the value read from the specified short register * * Side Effects: None * * Overview: This function reads a value from a short RAM address ********************************************************************/ int8 GetShortRAMAddress(int8 address) { int8 toReturn; output_low(MRF24J40_CS); spi_write(((address<<1) & 0b01111110)); toReturn = spi_read(0); // 0 parameter needed to make SCK output_high(MRF24J40_CS); return(toReturn); }

/********************************************************************* * Function: int8 GetLongRAMAddress(int16 address) * * PreCondition: Communication port to the MRF24J40 initialized * * Input: WORD address is the address of the long RAM address * that you want to read from. * * Output: BYTE the value read from the specified Long register * * Side Effects: None * * Overview: This function reads a value from a long RAM address ********************************************************************/ int8 GetLongRAMAddress(int16 address) { int8 toReturn;

5 trong 8

9/6/2012 5:42 AM

CCS :: View topic - Interfacing MRF24J40MA with PIC ??

http://www.ccsinfo.com/forum/viewtopic.php?t=38199

output_low(MRF24J40_CS); spi_write(((address>>3) & 0b01111111) | 0x80); spi_write(((address<<5) & 0b11100000)); toReturn = spi_read(0); // 0 parameter needed to make SCK output_high(MRF24J40_CS); return(toReturn); } Gus
Guest

Interfacing MRF24J40MA with PIC


Posted: Fri Sep 04, 2009 3:51 pm

Hi. I did all our friend PCM programmer said, but when I run this program in proteus,
Code: #include <WSN.h> #define #define #define #define #define #define #define #define SPI_MODE_0 SPI_MODE_1 SPI_MODE_2 SPI_MODE_3 (SPI_L_TO_H | SPI_XMIT_L_TO_H) (SPI_L_TO_H) (SPI_H_TO_L) (SPI_H_TO_L | SPI_XMIT_L_TO_H)

RESET PIN_C0 WAKE PIN_C1 CS PIN_C5 SCK PIN_B6

void SetShortRAMAddress(int direccion, int valor) { output_low(CS); spi_write(((direccion<<1) & 0b01111111) | 0x01); spi_write(valor); output_high(CS); } void main() { int prueba = 0; printf("\fComenzando Programa"); setup_adc_ports(sAN0|sAN1|VSS_VDD); setup_adc(ADC_OFF); setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_4); output_high(CS); setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); setup_timer_1(T1_DISABLED); setup_timer_2(T2_DISABLED,0,1); setup_comparator(NC_NC_NC_NC); enable_interrupts(INT_EXT); enable_interrupts(INT_RDA); enable_interrupts(GLOBAL); ext_int_edge( H_TO_L ); // TODO: USER CODE!! SetShortRAMAddress(0x3A,0x09); printf("\n\r\n\rPrograma Concluido."); while(TRUE); }

I always get this output: http://i25.tinypic.com/16i6x3m.jpg

6 trong 8

9/6/2012 5:42 AM

CCS :: View topic - Interfacing MRF24J40MA with PIC ??

http://www.ccsinfo.com/forum/viewtopic.php?t=38199

My PIC never sends a second complete word. It seems like something stops the SPI transmission. Please someone can explain me why this happens???
quique
Posted: Mo n Oct 26, 2009 11:42 am

Hi:
Joined: 30 Sep 2009 Posts: 2

I'm Enrique, I'm working with MRF24J40MA in my final project carrer. I'm industrial engineering and I am not capable to do...anything. I've built a little robot and i want connect with PC. In my robot I have a 18F2550 and I use MiWi because it is enough. To begin I want put in the Hyperterminal a message when a contact sensor is active but I don't know how... Can you help me? Thanks for advance Best Regards Enrique
MONZURUL
Guest

Zigbee basics
Posted: Fri Nov 27, 2009 7:53 pm

Hi, I've just started with MRF24J40MA. Any tutorial code? Very simple with PIC32 or any other? Just to send and receive data from PC ? Regards, ALAM
bkamen
Posted: Fri Nov 27, 2009 8:28 pm

You are releasing slave select before the buffer is empty.


Joined: 07 Jan 2004 Posts: 1271 Location: Central Illinois, USA

It's common for this to happen with the hardware SPI. Consider: You push a byte into the TX register and then maybe(?) delay and then go release nSS. but how does the code know when the TX buff is actually empty? if you don't check, you don't know and end up releasing nSS early. (oops). use a delay -- OR -- I think the empty flag for the TXbuf (I'd have to look at the datasheet again). -Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D

rimaz_786

need ur help
Posted: Tue Dec 29, 2009 11:06 pm

Joined: 29 Dec 2009 Posts: 2

Hi Jacob and other reply friends.. I'm currently doing a project on alarm using RF security remote control system. I have bought MICROCHIP - MRF24J40MA-I/RM - MODULE,RF, TRANSCEIVER, 802.15.4 STD and MICROCHIP - PIC18LF2550-I/SP - MCU, 8-BIT, 32K FLASH, SPDIP28

7 trong 8

9/6/2012 5:42 AM

CCS :: View topic - Interfacing MRF24J40MA with PIC ??

http://www.ccsinfo.com/forum/viewtopic.php?t=38199

Please give some ideas on this project. I'm a 1st year engineering student so I don't have a very good knowledge to end up this project. I hope you will do your best for me. Waiting for your reply. rimaz_786@yahoo.com rimaz
Lord_kian
Posted: Tue Apr 06, 2010 5:32 pm

Hi people
Joined: 06 Apr 2010 Posts: 1

I'm also working with this module and I need help with the miwi protocol to be used with a PIC18F452, if someone can help me to change the code to work with this pic. Thanks
Bonk MRF24J40MA
Posted: Mo n Apr 19, 2010 5:37 pm Joined: 19 Apr 2010 Posts: 1 Location: Planet Earth

If you still need some help with MRF24J40MA, then please email me or visit my website. If PIC18F452 is a Microchip product, then send me a data sheet that has a description of the SPI module. www.MRF24J40MA.com Life is short, the things that make you curse as just a motivation to work harder!

temopic

Posted: Wed Apr 28, 2010 5:59 pm

Does anyone have an example for the module?


Joined: 20 Feb 2008 Posts: 27 Location: mexico

Rocket

Posted: Sat May 28, 2011 3:02 am

Joined: 29 Aug 2005 Posts: 18

Hi. This might be very optimistic, but is there someone who is willing to share working code with the rest of us plz. _________________ J.I.L.

Display posts from previous: All Posts

Oldest First

Go
All times are GMT - 6 Hours

CCS Forum Index -> General CCS C Discussion Page 1 of 2

Goto page 1, 2 Next

Jump to: General CCS C Discussion You cannot post new topics You cannot reply to topics You cannot edit your posts You cannot delete your posts You cannot vote in polls in in in in in this this this this this

Go
forum forum forum forum forum

Powered by phpBB 2001, 2005 phpBB Group

8 trong 8

9/6/2012 5:42 AM

You might also like