CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

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

Interfacing MRF24J40MA with PIC ??
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jjacob



Joined: 08 Mar 2008
Posts: 54
Location: PORTUGAL (PORTO)

View user's profile Send private message

Interfacing MRF24J40MA with PIC ??
PostPosted: Sat Mar 14, 2009 4:45 am     Reply with quote

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Mar 14, 2009 10:57 pm     Reply with quote

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 SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)


Next, you need to know the maximum SPI clock frequency that can be
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:
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



Joined: 08 Mar 2008
Posts: 54
Location: PORTUGAL (PORTO)

View user's profile Send private message

Interfacing MRF24J40MA with PIC ??
PostPosted: Sun Mar 15, 2009 9:41 am     Reply with quote

Thank you PCM programmer.
You gave me TOP LEVEL information Very Happy
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 Sad

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

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
PostPosted: Mon May 18, 2009 8:51 am     Reply with quote

Hello jjacob

Could you share your files if you had some progress on MRF24J40.

Kind regards

engin
jjacob



Joined: 08 Mar 2008
Posts: 54
Location: PORTUGAL (PORTO)

View user's profile Send private message

PostPosted: Wed May 20, 2009 2:40 pm     Reply with quote

Hello.

I haven't tried any code Sad
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



Joined: 29 Oct 2008
Posts: 13

View user's profile Send private message

PostPosted: Thu May 28, 2009 4:46 am     Reply with quote

Hi all,

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);
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;

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
PostPosted: Fri Sep 04, 2009 3:51 pm     Reply with quote

Hi.

I did all our friend PCM programmer said, but when I run this program in proteus,

Code:

#include <WSN.h>

#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)

#define RESET   PIN_C0
#define WAKE   PIN_C1
#define CS   PIN_C5
#define 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

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



Joined: 30 Sep 2009
Posts: 2

View user's profile Send private message MSN Messenger

PostPosted: Mon Oct 26, 2009 11:42 am     Reply with quote

Hi:

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
PostPosted: Fri Nov 27, 2009 7:53 pm     Reply with quote

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



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

View user's profile Send private message

PostPosted: Fri Nov 27, 2009 8:28 pm     Reply with quote

You are releasing slave select before the buffer is empty.

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



Joined: 29 Dec 2009
Posts: 2

View user's profile Send private message Yahoo Messenger

need ur help
PostPosted: Tue Dec 29, 2009 11:06 pm     Reply with quote

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

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



Joined: 06 Apr 2010
Posts: 1

View user's profile Send private message

PostPosted: Tue Apr 06, 2010 5:32 pm     Reply with quote

Hi people

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



Joined: 19 Apr 2010
Posts: 1
Location: Planet Earth

View user's profile Send private message Visit poster's website

MRF24J40MA
PostPosted: Mon Apr 19, 2010 5:37 pm     Reply with quote

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!
Very Happy
temopic



Joined: 20 Feb 2008
Posts: 27
Location: mexico

View user's profile Send private message MSN Messenger

PostPosted: Wed Apr 28, 2010 5:59 pm     Reply with quote

Does anyone have an example for the module?
Rocket



Joined: 29 Aug 2005
Posts: 27

View user's profile Send private message

PostPosted: Sat May 28, 2011 3:02 am     Reply with quote

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:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group