MRF24J40MA with a PIC18LF13K50


Closed Thread
Results 1 to 12 of 12
  1. #1
    scumari's Avatar
    scumari Guest

    Default MRF24J40MA with a PIC18LF13K50

    Hi
    Trying to create a universal code for a MRF24J40MA with a PIC18LF13K50 controller in MELabs PICBASIC PRO 2.6, that should work as a receiver aswell as a transmitter, I encountered a few problems.
    The code I use and the drawing can be downloaded here.
    Pressing the send button should send one randomized byte in the air. The receiving unit shout receive it and copy it to the LEDS on the C-port.

    The point is, I can't get it to work at all!
    Did anyone have it working in PICBASIC?

    Thanx in advance

  2. #2
    Join Date
    Oct 2004
    Posts
    440

    Default Re: MRF24J40MA with a PIC18LF13K50

    It's been done in Basic.
    See MRF24J40MA RF Code working.
    Warning other compiler.

    I thought I saw it also on the WiKi but can't find it now.

    Norm

  3. #3
    scumari's Avatar
    scumari Guest

    Default Re: MRF24J40MA with a PIC18LF13K50

    Translated that one for PICBASIC, did't work at all!

    Thanx anyway

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924

    Default Re: MRF24J40MA with a PIC18LF13K50

    Maybe if you show you translated code someone might be able to help. And please do not expect folks to download and open a ZIP, just post the info.
    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Oct 2004
    Posts
    440

    Default Re: MRF24J40MA with a PIC18LF13K50

    Quote Originally Posted by scumari View Post
    Translated that one for PICBASIC, did't work at all!

    Thanx anyway
    It does work, I have it working but not in PicBasicPro.

    Norm

  6. #6
    scumari's Avatar
    scumari Guest

    Default Re: MRF24J40MA with a PIC18LF13K50

    It is a ZIP because it exsists of three files, the basic source, an inc file and a pdf with the drawing of the connections.
    If you are scared of downloading ZIP files, a good virus scanner should be more than sufficient to be save.
    It's downloadable from my own site and should be save..............

    The Proton listing uses a different microcontroller and gives me SPI.inc errors while compiling from the start! Unusable for me.
    At first I used an old proton source and translated it directly to PICBASIC PRO. It compiled but was not complete.

    Then I recreated the Initializing, transmitting and receiving code with the MRF pdf file as guide.
    Now I'm stuck.

  7. #7
    scumari's Avatar
    scumari Guest

    Default Re: MRF24J40MA with a PIC18LF13K50

    For those still affraid of ZIP files:

    '************************************************* ***************
    '* Name : MRF.BAS *
    '* Author : SCUMARI Tec *
    '* Notice : *
    '* : All Rights Reserved *
    '* Date : 7-6-2012 *
    '* Version : 1.0 *
    '* Notes : *
    '* : *
    '************************************************* ***************
    ' PIC18LF13K50
    ' ___ ___
    ' VDD O|1 - 20|O VSS
    ' RA5 OSC1 OSCIN O|2 19|O RA0 + PGD
    ' RA4 AN3 OSC2 CLKOUT O|3 18|O RA1 D- PGC
    ' RA3 MCLR VPP O|4 17|O VUSB
    ' RC5 CCP P1A T0CKI O|5 16|O RC0 AN4 C12IN+ INT0 VREF+
    ' RC4 P1B C12OUT SRQ O|6 15|O RC1 AN5 C12IN1- INT1 VREF-
    ' RC3 AN7 P1C C12IN3- PGM O|7 14|O RC2 AN6 P1D C12IN2- CVREF INT2
    ' RC6 AN8 SS T13CKI T1OSCI O|8 13|O RB4 AN10 SDI SDA
    ' RC7 AN9 SDO T1OSCO O|9 12|O RB5 AN11 RX DT
    ' RB7 TX CK O|10 11|O RB6 SCK SCL
    ' -------
    '18F14K50
    @ __CONFIG _CONFIG1L, 000h
    @ __CONFIG _CONFIG1H, 018h
    @ __CONFIG _CONFIG2L, 000h
    @ __CONFIG _CONFIG2H, 000h
    @ __CONFIG _CONFIG3H, 080h ;080h is MCLRE
    @ __CONFIG _CONFIG4L, 000h
    @ __CONFIG _CONFIG5L, 003h
    @ __CONFIG _CONFIG5H, 0C0h
    @ __CONFIG _CONFIG6L, 003h
    @ __CONFIG _CONFIG6H, 0E0h
    @ __CONFIG _CONFIG7L, 003h
    @ __CONFIG _CONFIG7H, 040h

    'INCLUDE "modedefs.bas"
    include "MRF24J40def.inc"

    DEFINE OSC 16

    OSCCON = %01100010 '4550 op $FE / K50 op 118 / 689 op 112
    OSCTUNE = $1F '4550 op $0F / K50 op 31 / 689 op 15

    ADCON0 = 0
    ANSEL = 0
    ANSELH = 0
    UCON = 0
    IOCA = 3
    IOCB = 0
    INTCON = 0

    PORTA = 0
    TRISA = %00001111
    PORTB = 0
    TRISB = %01000000
    PORTC = 0
    TRISC = 0

    intx VAR PORTA.1
    res VAR PORTA.4
    cs VAR PORTA.5
    sdi VAR PORTB.4
    sck VAR PORTB.5
    sdo VAR PORTB.6
    wake vAR PORTB.7

    butt var PORTA.0

    addr var byte
    addrm var word
    temp VAR word
    tmp VAR BYTE
    indat VAR BYTE
    outdat VAR BYTE
    i VAR BYTE

    channel var byte
    txRadioBuffer var byte[128]
    rxRadioBuffer var byte[128]

    txDestination VAR word
    txPayLoad VAR BYTE
    HeaderLength VAR BYTE
    FrameLength VAR BYTE
    FrameControl VAR word
    IEEEseqNumber VAR BYTE
    wcnt VAR BYTE
    bRetVal VAR BYTE
    IntStatus var Byte

    TX_BUFFER_SIZE con 128
    RX_BUFFER_SIZE con 128

    'Times
    tlong con 100
    tmid con 50
    tshort con 25

    cs = 1
    res = 1

    pause 100
    PORTC = 129
    pause 50
    PORTC = 66
    pause 50
    PORTC = 36
    pause 50
    PORTC = 24
    pause 50
    PORTC = 24
    pause 50
    PORTC = 36
    pause 50
    PORTC = 66
    pause 50
    PORTC = 129
    pause 50
    PORTC = 0
    pause 100

    'IntFlag = 0 'remember to remove this
    gosub initialize

    'ON INTERRUPT GOTO ReceiveData
    'disable
    'INTCON = %10010000 ' Enable RB0 interrupt
    'On_Hardware_Interrupt GoTo Radio_ISR
    ' GIE = 0 'disable global int
    ' IPEN = 0 'disable high pri interupts
    ' INT2IP =0 'disable int2 high pri not using priorty
    ' INT2IE = 0 'disable int2 portb.2
    ' INTEDG2 = 0 'rising

    main:
    enable
    if butt = 0 then
    'PORTC = 255
    GoSub SendMessage
    pause 100
    endif
    'goto main
    'disable
    ReceiveData:
    if intx = 1 then
    'DISABLE ' Disable interrupts in handler
    'PORTC = 255
    gosub RecMessage
    'RESUME ' Return to main program
    'ENABLE ' Enable interrupts after handler
    endif
    goto main

    Initialize:
    res = 0
    pause 10
    res = 1
    pause 10

    'Example steps to initialize the MRF24J40:
    '1. SOFTRST (0x2A) = 0x07 – Perform a software Reset. The bits will be automatically cleared to ‘0’ by hardware.
    '2. PACON2 (0x18) = 0x98 – Initialize FIFOEN = 1 and TXONTS = 0x6.
    '3. TXSTBL (0x2E) = 0x95 – Initialize RFSTBL = 0x9.
    '4. RFCON1 (0x201) = 0x01 – Initialize VCOOPT = 0x01.
    '5. RFCON2 (0x202) = 0x80 – Enable PLL (PLLEN = 1).
    '6. RFCON6 (0x206) = 0x90 – Initialize TXFIL = 1 and 20MRECVR = 1.
    '7. RFCON7 (0x207) = 0x80 – Initialize SLPCLKSEL = 0x2 (100 kHz Internal oscillator).
    '8. RFCON8 (0x208) = 0x10 – Initialize RFVCO = 1.
    '9. SLPCON1 (0x220) = 0x21 – Initialize CLKOUTEN = 1 and SLPCLKDIV = 0x01.
    addr = SOFTRST: gosub ShAddr: outdat = 7: gosub DataWr
    addr = PACON2: gosub ShAddr: outdat = $98: gosub DataWr
    addr = TXSTBL: gosub ShAddr: outdat = $95: gosub DataWr
    addrm = RFCON1: gosub LoAddr: outdat = $01: gosub LDataWr
    addrm = RFCON2: gosub LoAddr: outdat = $80: gosub LDataWr
    addrm = RFCON6: gosub LoAddr: outdat = $90: gosub LDataWr
    addrm = RFCON7: gosub LoAddr: outdat = $80: gosub LDataWr
    addrm = RFCON8: gosub LoAddr: outdat = $10: gosub LDataWr
    addrm = SLPCON1: gosub LoAddr: outdat = $21: gosub LDataWr

    '3.8.2.1 Configuring Nonbeacon-Enabled PAN Coordinator
    'The following steps configure the MRF24J40 as a coordinator in a nonbeacon-enabled network:
    '1. Set the PANCOORD (RXMCR 0x00<3>) bit = 1 to configure as the PAN coordinator.
    '2. Clear the SLOTTED (TXMCR 0x11<5>) bit = 0 to configure Unslotted CSMA-CA mode.
    '3. Configure BO (ORDER 0x10<7:4>) value = 0xF.
    '4. Configure SO (ORDER 0x10<3:0>) value = 0xF.
    addr = RXMCR: gosub ShAddr: gosub DataRd
    outdat = indat or 8 'bit 3 = 1
    addr = RXMCR: gosub ShAddr: gosub DataWr
    addr = TXMCR: gosub ShAddr: gosub DataRd
    outdat = indat and 223 'bit 5 = 0
    addr = TXMCR: gosub ShAddr: gosub DataWr

    '>>> 3.8.2.2 Configuring Nonbeacon-Enabled Device
    'As above but comment out the line below
    addr = ORDER: gosub ShAddr: outdat = $FF: gosub DataWr


    '(see Section 3.8 “Beacon-Enabled and Nonbeacon-Enabled Networks”):
    '10. BBREG2 (0x3A) = 0x80 – Set CCA mode to ED.
    '11. RSSITHCCA (0x3F) = 0x60 – Set CCA ED threshold.
    '12. BBREG6 (0x3E) = 0x40 – Set appended RSSI value to RXFIFO.
    '13. Enable interrupts – See Section 3.3 “Interrupts”.
    '14. Set channel – See Section 3.4 “Channel Selection”.
    '15. RFCTL (0x36) = 0x04 – Reset RF state machine.
    '16. RFCTL (0x36) = 0x00.
    '17. Delay at least 192 ms.
    addr = BBREG2: gosub ShAddr: outdat = $80: gosub DataWr
    addr = RSSITHCCA: gosub ShAddr: outdat = $60: gosub DataWr
    addr = BBREG6: gosub ShAddr: outdat = $60: gosub DataWr
    addr = INTCON: gosub ShAddr: outdat = 8: gosub DataWr
    addrm = RFCON0: gosub ShAddr: outdat = $82: gosub DataWr 'Channel 19
    addr = RFCTL: gosub ShAddr: outdat = 4: gosub DataWr
    addr = RFCTL: gosub ShAddr: outdat = 0: gosub DataWr

    pauseUS 200 'as per datasheet wait at least 192uS

    For i = 0 To TX_BUFFER_SIZE
    txRadioBuffer[i] = i '0x00 put in some data
    Next
    For i = 0 To RX_BUFFER_SIZE
    rxRadioBuffer[i] = 0
    Next

    return

    SendMessage:
    For i = 0 To 127 'make sure there is valid data in array
    txRadioBuffer[i] = i
    Next
    txDestination = $FFFF ' Set the destination address
    txPayLoad = 16 ' set amount of data to xmitt from array(payload)

    're-write first 11 bytes with hdrLen, frm Len and Header
    '// Compute and send header and frame length
    HeaderLength = 9 '(index 2 to 10, 9 bytes)
    FrameLength = HeaderLength + txPayLoad '9 (0-8)+ 16 (0-15) = 25
    ''address 0-1 '2 bytes
    txRadioBuffer[0] = HeaderLength
    txRadioBuffer[1] = FrameLength
    txRadioBuffer[2] = FrameControl.LowByte 'framecontrol[0] 0x26
    txRadioBuffer[3] = FrameControl.HighByte 'framecontrol[1] 0x22
    txRadioBuffer[4] = IEEEseqNumber 'IEEEseqNumber++
    txRadioBuffer[5] = $01 'myPANID[0]
    txRadioBuffer[6] = $02 'myPANID[1]
    txRadioBuffer[7] = txDestination.LowByte 'networkTX.dest & 0x00FF
    txRadioBuffer[8] = txDestination.HighByte 'networkTX.dest >> 8
    txRadioBuffer[9] = $cd 'myShortAddress[0]
    txRadioBuffer[10] = $ab 'myShortAddress[1]

    random temp
    txRadioBuffer[11] = temp.Byte0
    IEEEseqNumber = IEEEseqNumber + 1
    wcnt = 0
    'fifo = 0 = i
    For i = 0 To FrameLength +1
    addrm = i: gosub LoAddr: outdat = txRadioBuffer[i]: gosub LDataWr
    Next

    '2. If the packet requires an Acknowledgment, the Acknowledgment request bit in the frame control
    ' field should be set to ‘1’ in the MAC Header (MHR) when the host microcontroller loads the TX
    ' Normal FIFO, and set the TXNACKREQ (TXNCON 0x1B<2>) bit = 1. Refer to Section 3.13 “Acknowledgement”
    ' for more information about Acknowledgment configuration.
    addr = TXSTAT: gosub ShAddr: gosub DataRd
    outdat = indat or 192 'bit 6 and 7 = 1
    addr = TXSTAT: gosub ShAddr: gosub DataWr

    '3. If the frame is to be encrypted, the security enabled bit in the frame control field should be
    ' set to ‘1’ in the MAC Header (MHR) when the host microcontroller loads the TX Normal FIFO,
    ' and set the TXNSECEN (TXNCON 0x1B<1>) bit = 1. Refer to Section 3.17 “Security” for
    ' more information about Security modes.

    '4. Transmit the packet by setting the TXNTRIG (TXNCON 0x1B<0>) bit = 1. The bit will be
    ' automatically cleared by hardware.
    addr = TXNCON: gosub ShAddr: gosub DataRd
    outdat = indat or 5 'bit 0 and 2 = 1
    addr = TXNCON: gosub ShAddr: gosub DataWr

    SendMessage1:
    '5. A TXNIF (INTSTAT 0x31<0>) interrupt will be issued. The TXNSTAT (TXSTAT 0x24<0>) bit
    ' indicates the status of the transmission:
    'TXNSTAT = 1: Transmission was successful
    'TXNSTAT = 0: Transmission failed, retry count exceeded
    'The number of retries of the most recent transmission is contained in the TXNRETRY
    '(TXSTAT 0x24<7:6>) bits. The CCAFAIL
    '(TXSTAT 0x24<5>) bit = 1 indicates if the failed transmission was due to the channel busy
    ' (CSMA-CA timed out). '// Transmission acknowledgement request
    addr = INTSTAT: gosub ShAddr: gosub DataRd
    if indat.0 = 1 then goto SendMessage1

    addr = TXSTAT: gosub ShAddr: gosub DataRd
    tmp = PORTC
    if indat.0 = 1 then
    PORTC = 255
    else
    PORTC = 0
    endif
    pause 100
    PORTC = txRadioBuffer[11]

    Return

    RecMessage:
    addr = INTSTAT: gosub ShAddr: gosub DataRd
    IntStatus = indat

    if INTSTAT and 8 = 8 then return

    '*************receive packet**********************
    getMACframe:

    ' Example steps to read the RXFIFO:
    ' 1. Receive RXIF interrupt. (INSTAT.3)
    ' 2. Disable host microcontroller interrupts.
    ' 3. Set RXDECINV = 1; disable receiving packets off air. (BBREG1.2)
    ' 4. Read address, 0x300; get RXFIFO frame length value.
    ' 5. Read RXFIFO addresses, 0x301 through (0x300 + Frame Length + 2); read packet data plus LQI and RSSI.
    ' 6. Clear RXDECINV = 0; enable receiving packets.
    ' 7. Enable host microcontroller interrupts.

    '// Disable interrupts, disable reception
    ' 'RFIE = 0;

    addr = BBREG1: gosub ShAddr: outdat = $04: gosub DataWr

    bRetVal = 0
    addrm = RX_FIFO: gosub loAddr: gosub LDataRd
    rxRadioBuffer[0] = indat + 2

    For i = 1 To rxRadioBuffer[0]
    addrm = (RX_FIFO + i): gosub loAddr: gosub LDataRd
    rxRadioBuffer[i] = indat
    Next
    addr = BBREG1: gosub shAddr: outdat = $00: gosub DataWr

    PORTC = rxRadioBuffer[11]

    Return

    '**************** releaseRXbuffer *************
    releaseRXbuffer:

    'INT2IE = 0 'disable interrupt
    addr = RXFLUSH: gosub shAddr: outdat = $01: gosub DataWr 'flush buffer
    addr = RXMCR: gosub shAddr: outdat = $00: gosub DataWr 're-enable Ack response
    'radioStatus_rxFifoHasData = 0 'reset
    'rxsize = 0 don't have yet
    'INT2IE = 1 'enable interrupt

    Return

    ''################################################ ###########################
    ShAddr:
    cs = 0
    pauseus tlong
    sdi = 0
    sck = 1
    pauseus tmid
    sck = 0
    for tmp = 0 to 5
    sdi = addr.5
    sck = 1
    pauseus tmid
    sck = 0
    addr = addr << 1
    pauseus tmid
    next tmp
    return

    LoAddr:
    cs = 0
    pauseus tlong
    sdi = 1
    sck = 1
    pauseus tmid
    sck = 0
    for tmp = 0 to 9
    sdi = addrm.9
    sck = 1
    pauseus tmid
    sck = 0
    addrm = addrm << 1
    pauseus tmid
    next tmp
    return

    DataRd:
    sdi = 0
    sck = 1
    pauseus tmid
    sck = 0
    indat = 0
    for tmp = 0 to 7
    sck = 1
    pauseus tmid
    indat = indat + sdo
    sck = 0
    indat = indat << 1
    pauseus tmid
    next tmp
    cs = 1
    return

    DataWr:
    sdi = 1
    sck = 1
    pauseus tmid
    sck = 0
    'Read data
    indat = 0
    for tmp = 0 to 7
    sdi = outdat.7
    sck = 1
    pauseus tmid
    sck = 0
    outdat = outdat << 1
    pauseus tmid
    next tmp
    cs = 1
    return

    LDataRd:
    sdi = 0
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    sck = 1
    pauseus tmid
    sck = 0
    indat = 0
    for tmp = 0 to 7
    sck = 1
    pauseus tmid
    indat = indat + sdo
    sck = 0
    indat = indat << 1
    pauseus tmid
    next tmp
    cs = 1
    return

    LDataWr:
    sdi = 1
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    sck = 1
    pauseus tmid
    sck = 0
    indat = 0
    for tmp = 0 to 7
    sdi = outdat.7
    sck = 1
    pauseus tmid
    sck = 0
    outdat = outdat << 1
    pauseus tmid
    next tmp
    cs = 1
    return


    Maybe someone can point me in the right direction......................
    Thanx
    Last edited by scumari; - 10th October 2012 at 09:08.

  8. #8
    scumari's Avatar
    scumari Guest

    Default Re: MRF24J40MA with a PIC18LF13K50

    I have it working !

  9. #9
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588

    Default Re: MRF24J40MA with a PIC18LF13K50

    Any chance of sharing your code?

    Robert

  10. #10
    scumari's Avatar
    scumari Guest

    Default Re: MRF24J40MA with a PIC18LF13K50

    Sure, here is the PicBasic listing for a DEVICE, for a COORDINATOR just comment the device line (line 219) and uncomment line 212 in the initialization routine.
    One must be a coordinator, all others should be devices.

    '************************************************* ***************************
    '* Name : MRFshort.BAS *
    '* Author : SCUMARI Tec *
    '* Notice : Copyright (c) 2013 SCUMARI Group *
    '* : All Rights Reserved *
    '* Date : 3-6-2013 *
    '* Version : 1.0 *
    '* Notes : PIC18LF13K50 controller for the MRF24J40MA module *
    '* : This is standard software for both sender and receiver. *
    '* : Both modules start in receive mode.When the button is pressed,*
    '* : a byte is randomized and send over. Both receiver and sender *
    '* : displays the value of 1 byte on a LED bar. *
    '************************************************* ***************************
    ' PIC18LF13K50
    ' ___ ___
    ' VDD O|1 - 20|O VSS
    ' RA5 OSC1 OSCIN O|2 19|O RA0 + PGD
    ' RA4 AN3 OSC2 CLKOUT O|3 18|O RA1 D- PGC
    ' RA3 MCLR VPP O|4 17|O VUSB
    ' RC5 CCP P1A T0CKI O|5 16|O RC0 AN4 C12IN+ INT0 VREF+
    ' RC4 P1B C12OUT SRQ O|6 15|O RC1 AN5 C12IN1- INT1 VREF-
    ' RC3 AN7 P1C C12IN3- PGM O|7 14|O RC2 AN6 P1D C12IN2- CVREF INT2
    ' RC6 AN8 SS T13CKI T1OSCI O|8 13|O RB4 AN10 SDI SDA
    ' RC7 AN9 SDO T1OSCO O|9 12|O RB5 AN11 RX DT
    ' RB7 TX CK O|10 11|O RB6 SCK SCL
    ' -------
    '18F13K50
    @ __CONFIG _CONFIG1L, 000h
    @ __CONFIG _CONFIG1H, 018h
    @ __CONFIG _CONFIG2L, 000h
    @ __CONFIG _CONFIG2H, 000h
    @ __CONFIG _CONFIG3H, 080h ;080h is MCLRE
    @ __CONFIG _CONFIG4L, 000h
    @ __CONFIG _CONFIG5L, 003h
    @ __CONFIG _CONFIG5H, 0C0h
    @ __CONFIG _CONFIG6L, 003h
    @ __CONFIG _CONFIG6H, 0E0h
    @ __CONFIG _CONFIG7L, 003h
    @ __CONFIG _CONFIG7H, 040h

    INCLUDE "modedefs.bas"
    include "MRF24J40def.inc"

    DEFINE OSC 16

    OSCCON = %01100010 '4550 op $FE / K50 op 118 / 689 op 112
    OSCTUNE = $1F '4550 op $0F / K50 op 31 / 689 op 15

    ADCON0 = 0
    ANSEL = 0
    ANSELH = 0
    UCON = 0
    IOCA = 3
    IOCB = 0
    INTCON = 0

    PORTA = 0
    TRISA = %00111111
    PORTB = 0
    TRISB = %00000000
    PORTC = 0
    TRISC = 0

    intx VAR PORTA.4
    res VAR PORTB.4
    sdi VAR PORTB.5
    sck VAR PORTB.6
    sdo VAR PORTA.5
    cs VAR PORTB.7
    'wake vAR PORTB.7

    butt var PORTA.0

    addr var byte
    addrm var word
    addrl VAR addrm.byte0
    addrh VAR addrm.byte1
    temp VAR word
    tmp VAR BYTE
    indat VAR BYTE
    outdat VAR BYTE
    i VAR BYTE

    channel var byte
    txRadioBuffer var byte[128]
    rxRadioBuffer var byte[128]
    txHeader var Byte[11]

    txDestination VAR word
    txPayLoad VAR BYTE
    HeaderLength VAR BYTE
    FrameLength VAR BYTE
    FrameControl VAR word
    IEEEseqNumber VAR BYTE
    wcnt VAR BYTE
    bRetVal VAR BYTE
    IntStatus var Byte
    fifo var word
    sdata var word
    RadioState var byte

    ' '******** Setup interrupt 18F4520**********
    ' GIE var INTCON.7 'global interrupt enable
    ' INT0IE var INTCON.4 'enable int0, portb.0
    ' INT0IF var INTCON.1 'interrupt flag
    ' INTEDG0 var INTCON2.6 'SLPCON0.1 'Interrupt Edge Select
    ' 'Symbol INT0IP con INTCON3.7
    ' IPEN var RCON.7
    ' GIE = 0
    ' INT0IE = 0

    ' '**********************************
    TX_BUFFER_SIZE con 127
    RX_BUFFER_SIZE con 127

    'Times
    tlong con 50
    tmid con 25
    tshort con 15

    'FrameControl = %0010001000100110
    FrameControl = %1001100001000001
    'index Value
    ' frametype 3 bits 001
    ' securityEnabled 1 0
    ' framepending 1 0
    ' ackRequest 1 0
    ' intraPAN 1 1
    ' don't care 1 0
    ' don't care 2 00
    ' destAddressMode 2 10 0x02
    ' frameVersion 2 01 datasheet says this is reserved
    ' sourceAddressMode 2 10
    cs = 1
    res = 1

    pause 100
    PORTC = 129
    pause 50
    PORTC = 66
    pause 50
    PORTC = 36
    pause 50
    PORTC = 24
    pause 50
    PORTC = 24
    pause 50
    PORTC = 36
    pause 50
    PORTC = 66
    pause 50
    PORTC = 129
    pause 50
    PORTC = 0
    pause 100

    'IntFlag = 0 'remember to remove this
    gosub initialize

    main:
    addr = INTSTAT: gosub DataRd
    'PORTC = indat
    'pause 100
    if indat.3 = 1 then gosub receive
    'if intx = 0 then gosub Receive
    If butt = 0 Then gosub Send
    'pause 250
    GoTo main

    '>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<
    Initialize:
    res = 0
    pause 10
    res = 1
    pause 10

    'Example steps to initialize the MRF24J40:
    '1. SOFTRST (0x2A) = 0x07 – Perform a software Reset. The bits will be automatically cleared to ‘0’ by hardware.
    '2. PACON2 (0x18) = 0x98 – Initialize FIFOEN = 1 and TXONTS = 0x6.
    '3. TXSTBL (0x2E) = 0x95 – Initialize RFSTBL = 0x9.
    '4. RFCON1 (0x201) = 0x01 – Initialize VCOOPT = 0x01.
    '5. RFCON2 (0x202) = 0x80 – Enable PLL (PLLEN = 1).
    '6. RFCON6 (0x206) = 0x90 – Initialize TXFIL = 1 and 20MRECVR = 1.
    '7. RFCON7 (0x207) = 0x80 – Initialize SLPCLKSEL = 0x2 (100 kHz Internal oscillator).
    '8. RFCON8 (0x208) = 0x10 – Initialize RFVCO = 1.
    '9. SLPCON1 (0x220) = 0x21 – Initialize CLKOUTEN = 1 and SLPCLKDIV = 0x01.
    addr = SOFTRST: outdat = 7: gosub DataWr
    init2:
    Indat = 0
    addr = SOFTRST: gosub DataRd
    if indat <> 0 then goto init2

    addr = PACON2: outdat = $98: gosub DataWr
    addr = TXSTBL: outdat = $95: gosub DataWr
    addrm = RFCON1: outdat = $01: gosub LDataWr
    addrm = RFCON2: outdat = $80: gosub LDataWr
    addrm = RFCON6: outdat = $90: gosub LDataWr
    addrm = RFCON7: outdat = $80: gosub LDataWr
    addrm = RFCON8: outdat = $10: gosub LDataWr
    addrm = SLPCON1: outdat = $21: gosub LDataWr

    '** Device or Coordinator ************************************************** ****
    '3.8.2.1 Configuring Nonbeacon-Enabled PAN Coordinator
    'The following steps configure the MRF24J40 as a coordinator in a nonbeacon-enabled network:
    '1. Set the PANCOORD (RXMCR 0x00<3>) bit = 1 to configure as the PAN coordinator.
    '2. Clear the SLOTTED (TXMCR 0x11<5>) bit = 0 to configure Unslotted CSMA-CA mode.
    '3. Configure BO (ORDER 0x10<7:4>) value = 0xF.
    '4. Configure SO (ORDER 0x10<3:0>) value = 0xF.
    'addr = TXMCR: gosub DataRd
    'outdat = indat: indat.5 = 0 'bit 5 = 0
    'addr = TXMCR: gosub DataWr
    ' addr = RXMCR: outdat = 8: gosub DataWr 'PROMI & COORD To make it a Coordinator
    'addr = ORDER: outdat = $FF: gosub DataWr

    '3.8.2.1 Configuring Nonbeacon-Enabled device
    'The following steps configure the MRF24J40 as a coordinator in a nonbeacon-enabled network:
    '1. Set the PANCOORD (RXMCR 0x00<3>) bit = 0 to configure as the device.
    '2. Clear the SLOTTED (TXMCR 0x11<5>) bit = 0 to configure Unslotted CSMA-CA mode.
    addr = RXMCR: outdat = 0: gosub DataWr 'PROMI To make it a Device

    addr = TXMCR: gosub DataRd
    outdat = indat: indat.5 = 0 'bit 5 = 0
    addr = TXMCR: gosub DataWr
    addr = ORDER: outdat = $FF: gosub DataWr

    '** Device or Coordinator ************************************************** ****


    '(see Section 3.8 “Beacon-Enabled and Nonbeacon-Enabled Networks”):
    '10. BBREG2 (0x3A) = 0x80 – Set CCA mode to ED.
    '11. RSSITHCCA (0x3F) = 0x60 – Set CCA ED threshold.
    '12. BBREG6 (0x3E) = 0x40 – Set appended RSSI value to RXFIFO.
    '13. Enable interrupts – See Section 3.3 “Interrupts”.
    '14. Set channel – See Section 3.4 “Channel Selection”.
    '15. RFCTL (0x36) = 0x04 – Reset RF state machine.
    '16. RFCTL (0x36) = 0x00.
    '17. Delay at least 192 ms.
    addr = BBREG2: outdat = $80: gosub DataWr
    addr = CCAEDTH: outdat = $60: gosub DataWr
    addr = BBREG6: outdat = $60: gosub DataWr
    addr = INTCON: outdat = 8: gosub DataWr 'RXIE
    addrm = RFCON0: outdat = $82: gosub LDataWr 'Channel 19
    addr = RFCTL: outdat = 4: gosub DataWr
    'pauseUS 10
    addr = RFCTL: outdat = 0: gosub DataWr

    addr = PANIDL: outdat = $01: gosub DataWr
    addr = PANIDH: outdat = $02: gosub DataWr
    addr = SADRL: outdat = $CD: gosub DataWr
    addr = SADRH: outdat = $AB: gosub DataWr

    pauseUS 10 'as per datasheet wait at least 192uS

    For i = 0 To TX_BUFFER_SIZE
    txRadioBuffer[i] = i '0x00 put in some data
    Next
    For i = 0 To RX_BUFFER_SIZE
    rxRadioBuffer[i] = 0
    Next

    addr = RXFLUSH: outdat = 1: gosub DataWr

    return

    '***** RECEIVING ************************************************** ***************
    Receive:
    'Indat = 0
    'addr = INTSTAT: gosub DataRd 'what caused the interrupt
    addr = BBREG1: outdat = 4: gosub DataWr 'RXDECINV = 1

    bRetVal = 0
    addrm = RX_FIFO: gosub LDataRd
    rxRadioBuffer[0] = indat
    For i = 1 To (rxRadioBuffer[0] + 2)
    addrm = (RX_FIFO + i): gosub LDataRd
    rxRadioBuffer[i] = indat
    Next
    PORTC = rxRadioBuffer[10]

    addr = RXFLUSH: outdat = 1: gosub DataWr

    addr = BBREG1: outdat = 0: gosub DataWr 'RXDECINV = 0

    pause 250
    Return

    '***** SENDING ************************************************** ***************
    Send:

    For i = 0 To 127 'make sure there is valid data in array
    txRadioBuffer[i] = i
    Next
    txDestination = $FFFF ' Set the destination address
    txPayLoad = 16 ' set amount of data to xmitt from array(payload)

    're-write first 11 bytes with hdrLen, frm Len and Header
    '// Compute and send header and frame length
    HeaderLength = 9 '(index 2 to 10, 9 bytes)
    FrameLength = HeaderLength + txPayLoad '9 (0-8)+ 16 (0-15) = 25
    'address 0-1 '2 bytes
    txHeader[0] = HeaderLength
    txHeader[1] = FrameLength
    'address 2-10 '8 bytes
    txHeader[2] = FrameControl.LowByte 'framecontrol[0] 0x26
    txHeader[3] = FrameControl.HighByte 'framecontrol[1] 0x22
    txHeader[4] = IEEEseqNumber 'IEEEseqNumber++
    txHeader[5] = $01 'myPANID[0]
    txHeader[6] = $02 'myPANID[1]
    txHeader[7] = txDestination.LowByte 'networkTX.dest & 0x00FF
    txHeader[8] = txDestination.HighByte 'networkTX.dest >> 8
    txHeader[9] = $cd 'myShortAddress[0]
    txHeader[10] = $ab 'myShortAddress[1]

    IEEEseqNumber = IEEEseqNumber + 1
    fifo = 0
    For i = 0 To HeaderLength + 1 'header & hdr len & frame len =11
    addrm = fifo: outdat = txHeader[i]: gosub LDataWr
    fifo = fifo + 1
    Next

    random temp
    PORTC = temp
    For i = 0 To txPayLoad-1 'FrameLength +1
    addrm = fifo: outdat = temp: gosub LDataWr
    fifo = fifo + 1
    Next

    addr = TXNCON: outdat = 1: gosub DataWr

    pause 250

    return


    '################################################# ##########################
    DataWr:
    cs = 0
    sdi = 0
    pauseus tshort
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    for tmp = 0 to 5
    sdi = addr.5
    pauseus tshort
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    addr = addr << 1
    next tmp

    sdi = 1
    pauseus tshort
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    'Read data
    indat = 0
    for tmp = 0 to 7
    sdi = outdat.7
    pauseus tshort
    sck = 1
    pauseus tmid
    sck = 0
    outdat = outdat << 1
    pauseus tmid
    next tmp
    cs = 1
    return

    DataRd:
    indat = 0
    cs = 0
    sdi = 0
    pauseus tshort
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    for tmp = 0 to 5
    sdi = addr.5
    pauseus tshort
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    addr = addr << 1
    next tmp

    sdi = 0
    pauseus tshort
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    indat = 0
    for tmp = 0 to 7
    indat = indat << 1
    sck = 1
    pauseus tmid
    indat = indat + sdo
    sck = 0
    pauseus tmid
    next tmp
    cs = 1
    return

    LDataWr:
    cs = 0
    sdi = 1
    pauseus tshort
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    for tmp = 0 to 9
    sdi = addrm.9
    pauseus tshort
    sck = 1
    pauseus tmid
    sck = 0
    addrm = addrm << 1
    pauseus tmid
    next tmp

    sdi = 1
    pauseus tshort
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    indat = 0
    for tmp = 0 to 7
    sdi = outdat.7
    pauseus tshort
    sck = 1
    pauseus tmid
    sck = 0
    outdat = outdat << 1
    pauseus tmid
    next tmp
    cs = 1
    return

    LDataRd:
    indat = 0
    cs = 0
    sdi = 1
    pauseus tshort
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    for tmp = 0 to 9
    sdi = addrm.9
    pauseus tshort
    sck = 1
    pauseus tmid
    sck = 0
    addrm = addrm << 1
    pauseus tmid
    next tmp

    sdi = 0
    pauseus tshort
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    sck = 1
    pauseus tmid
    sck = 0
    pauseus tmid
    indat = 0
    for tmp = 0 to 7
    indat = indat << 1
    sck = 1
    pauseus tmid
    indat = indat + sdo
    sck = 0
    pauseus tmid
    next tmp
    cs = 1
    return

    Below is the MFR24J40def.inc include file

    ''================================================ =============================
    '' MRF24J40 Definitions
    ''
    '' This file is similar to the original MRF24J40.h provided by Microchip.
    '' The register names have been modified to reflect the names used in the
    '' documentation (DS39776B). Also the Short Address Register definitions
    '' are NOT pre-formatted for SPI transmission to the transceiver. In other
    '' words, they are just addresses. This is, in my opinion, much easier to
    '' comprehend.
    '' Also included in this file are individual register definitions to be added
    '' as needed. They are each defined as a union of an symbol byte and an
    '' anonymous structure defining the individual fields. This eliminates the
    '' tedious task of including a ".bits" member qualifier with each reference to
    '' a member field. This is compatible with the MPLAB C18 compiler; other
    '' compilers...maybe not so much.


    Symbol BEACON_FRAME = 0
    Symbol DATA_FRAME = 1
    Symbol ACK_FRAME = 2
    Symbol MAC_CMD_FRAME = 3

    Symbol NO_ADDR = 0
    Symbol ADDR_16 = 2
    Symbol ADDR_64 = 3


    '' Define FIFO addresses
    Symbol NORMAL_TX_FIFO = $0
    Symbol BEACON_TX_FIFO = $80
    Symbol GTS1_TX_FIFO = $100
    Symbol GTS2_TX_FIFO = $180
    Symbol RX_FIFO = $300
    Symbol SECURITY_FIFO = $280

    '//Long Address Control Registers
    Symbol RFCON0 = $200
    Symbol RFCON1 = $201
    Symbol RFCON2 = $202
    Symbol RFCON3 = $203
    Symbol RFCON4 = $204
    Symbol RFCON5 = $205
    Symbol RFCON6 = $206
    Symbol RFCON7 = $207
    Symbol RFCON8 = $208
    Symbol SLPCAL1 = $209
    Symbol SLPCAL2 = $20a
    Symbol SLPCAL3 = $20b
    Symbol SFCNTRH = $20c ' Now reserved?
    Symbol SFCNTRM = $20d ' Now reserved?
    Symbol SFCNTRL = $20e ' Now reserved?
    Symbol RFSTATE = $20f

    Symbol RSSI = $210
    Symbol SLPCON0 = $211
    Symbol SRCADRMODE = $212 ' Now reserved?
    Symbol SRCADDR0 = $213 ' Now reserved?
    Symbol SRCADDR1 = $214 ' Now reserved?
    Symbol SRCADDR2 = $215 ' Now reserved?
    Symbol SRCADDR3 = $216 ' Now reserved?
    Symbol SRCADDR4 = $217 ' Now reserved?
    Symbol SRCADDR5 = $218 ' Now reserved?
    Symbol SRCADDR6 = $219 ' Now reserved?
    Symbol SRCADDR7 = $21a ' Now reserved?
    Symbol RXFRAMESTATE = $21b ' Now reserved?
    Symbol SECSTATUS = $21c ' Now reserved?
    Symbol STCCMP = $21d ' Now reserved?
    Symbol HLEN = $21e ' Now reserved?
    Symbol FLEN = $21f ' Now reserved?

    Symbol SLPCON1 = $220
    Symbol WAKETIMEL = $222
    Symbol WAKETIMEH = $223
    Symbol REMCNTL = $224
    Symbol REMCNTH = $225
    Symbol MAINCNT0 = $226
    Symbol MAINCNT1 = $227
    Symbol MAINCNT2 = $228
    Symbol MAINCNT3 = $229
    Symbol RFMANUALCTRLEN = $22a ' Now reserved?
    Symbol RFMANUALCTRL = $22b ' Now reserved?
    'symbol RFRXCTRL RFMANUALCTRL
    Symbol TxDACMANUALCTRL = $22c ' Now reserved?
    Symbol RFMANUALCTRL2 = $22d ' Now reserved?
    Symbol TESTRSSI = $22e ' Now reserved?
    Symbol TESTMODE = $22f

    Symbol ASSOEADR0 = $230
    Symbol ASSOEADR1 = $231
    Symbol ASSOEADR2 = $232
    Symbol ASSOEADR3 = $233
    Symbol ASSOEADR4 = $234
    Symbol ASSOEADR5 = $235
    Symbol ASSOEADR6 = $236
    Symbol ASSOEADR7 = $237
    Symbol ASSOSADR0 = $238
    Symbol ASSOSADR1 = $239

    Symbol UPNONCE0 = $240
    Symbol UPNONCE1 = $241
    Symbol UPNONCE2 = $242
    Symbol UPNONCE3 = $243
    Symbol UPNONCE4 = $244
    Symbol UPNONCE5 = $245
    Symbol UPNONCE6 = $246
    Symbol UPNONCE7 = $247
    Symbol UPNONCE8 = $248
    Symbol UPNONCE9 = $249
    Symbol UPNONCE10 = $24A
    Symbol UPNONCE11 = $24B
    Symbol UPNONCE12 = $24C

    '' Short Address Register definitions:
    Symbol RXMCR = $0
    Symbol PANIDL = $01
    Symbol PANIDH = $02
    Symbol SADRL = $03
    Symbol SADRH = $04
    Symbol EADR0 = $05
    Symbol EADR1 = $06
    Symbol EADR2 = $07
    Symbol EADR3 = $08
    Symbol EADR4 = $09
    Symbol EADR5 = $0A
    Symbol EADR6 = $0B
    Symbol EADR7 = $0C
    Symbol RXFLUSH = $0D
    Symbol TXSTATE0 = $0E
    Symbol TXSTATE1 = $0F

    Symbol ORDER = $10
    Symbol TXMCR = $11
    Symbol ACKTMOUT = $12
    Symbol ESLOTG1 = $13
    Symbol SYMTICKL = $14
    Symbol SYMTICKH = $15
    Symbol PACON0 = $16
    Symbol PACON1 = $17
    Symbol PACON2 = $18
    Symbol CSMACR = $19
    Symbol TXBCON0 = $1A
    Symbol TXNCON = $1B
    Symbol TXG1CON = $1C
    Symbol TXG2CON = $1D
    Symbol ESLOTG23 = $1E
    Symbol ESLOTG45 = $1F

    Symbol ESLOTG67 = $20
    Symbol TXPEND = $21
    Symbol WAKECON = $22
    Symbol FRMOFFSET = $23
    Symbol TXSTAT = $24
    Symbol TXBCON1 = $25
    Symbol GATECLK = $26
    Symbol TXTIME = $27
    Symbol HSYMTMRL = $28
    Symbol HSYMTMRH = $29
    Symbol SOFTRST = $2A
    Symbol BISTCR = $2B
    Symbol SECCON0 = $2C
    Symbol SECCON1 = $2D
    Symbol TXSTBL = $2E
    Symbol SECISR = $2F

    Symbol RXSR = $30
    Symbol INTSTAT = $31
    Symbol INTCONT = $32
    Symbol GPIO = $33
    Symbol TRISGPIO = $34
    Symbol SLPACK = $35
    Symbol RFCTL = $36
    Symbol SECCR2 = $37
    Symbol BBREG0 = $38
    Symbol BBREG1 = $39
    Symbol BBREG2 = $3A
    Symbol BBREG3 = $3B
    Symbol BBREG4 = $3C
    Symbol BBREG5 = $3D
    Symbol BBREG6 = $3E
    Symbol CCAEDTH = $3F


    '' TXSTAT: TX MAC STATUS REGISTER (ADDRESS: 0x24)

    'symbol TXNSTAT = ' TX Normal FIFO Release Status bit
    'symbol TXG1STAT:1; ' TX GTS1 FIFO Release Status bit
    'symbol TXG2STAT:1; ' TX GTS2 FIFO Release Status bit
    'symbol TXG1FNT:1; ' TX GTS1 FIFO transmission failed (timeout
    'symbol TXG2FNT:1; ' TX GTS2 FIFO transmission failed (timeout
    'symbol CCAFAIL:1; ' Clear Channel Assessment Status of last transmit
    'symbol TXNRETRY:2; ' TX Normal FIFO Retry Times

    '' INTSTAT: INTERRUPT STATUS REGISTER (ADDRESS: 0x31

    'symbol TXNIF = ISRSTS.0 ' TX Normal FIFO Release Interrupt bit
    'symbol TXG1IF = ISRSTS.1 ' TX GTS1 FIFO Transmission Interrupt bit
    'symbol TXG2IF = ISRSTS.2 ' TX GTS2 FIFO Transmission Interrupt bit
    'symbol RXIF = ISRSTS.3 ' RX FIFO Reception Interrupt bit
    'symbol SECIF = ISRSTS.4 ' Security Key Request Interrupt bit
    'symbol HSYMTMRIF = ISRSTS.5 ' Half Symbol Timer Interrupt bit
    'symbol WAKEIF = ISRSTS.6 ' Wake-up Alert Interrupt bit
    'symbol SLPIF = ISRSTS.7 ' Sleep Alert Interrupt bit

    '' INTCON: INTERRUPT CONTROL REGISTER (ADDRESS: 0x32

    'symbol TXNIE:1; ' TX Normal FIFO Transmission Interrupt Enable bit
    'symbol TXG1IE:1; ' TX GTS1 FIFO Transmission Interrupt Enable bit
    'symbol TXG2IE:1; ' TX GTS2 FIFO Transmission Interrupt Enable bit
    'symbol RXIE:1; ' RX FIFO Reception Interrupt Enable bit
    'symbol SECIE:1; ' Security Key Request Interrupt Enable bit
    'symbol HSYMTMRIE:1; ' Half Symbol Timer Interrupt Enable bit
    'symbol WAKEIE:1; ' Wake-up Alert Interrupt Enable bit
    'symbol SLPIE:1; ' Sleep Alert Interrupt Enable bit

    Have fun with it
    Last edited by scumari; - 7th June 2013 at 07:00.

  11. #11
    Join Date
    Oct 2004
    Posts
    440

    Default Re: MRF24J40MA with a PIC18LF13K50

    scumari

    Thanks for the MRF24J40MA code.
    Tip: Select the code and press the quote button in the editor to display in a compact format.

    My code
    Norm

  12. #12
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588

    Default Re: MRF24J40MA with a PIC18LF13K50

    Thanks for sharing!

    Robert

Similar Threads

  1. PIC18LF13K50 Extremly low power counter
    By pedja089 in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 17th October 2010, 03:01
  2. Replies: 0
    Last Post: - 29th August 2008, 15:55

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts