Professional Documents
Culture Documents
http://www.ccsinfo.com/forum/viewtopic.php?t=38199
Search
Please go to www.mcuspace.com/mcuforum for posts concerning PIC MCU CCS does not monitor this forum on a regular basis.
Log
Please do not post bug Reports on this forum. Send them to support@ccsinfo.com
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
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
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; }
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
2 trong 8
9/6/2012 5:42 AM
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; }
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
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
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
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
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
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)
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); }
6 trong 8
9/6/2012 5:42 AM
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
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
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
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
Rocket
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.
Oldest First
Go
All times are GMT - 6 Hours
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
8 trong 8
9/6/2012 5:42 AM