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
Bookmarks