'18F4520 32MHz INT Button ON TX OFF RX 211016 1000
Define SIMULATION_WAITMS_VALUE = 1  'else 0 for PIC
Define CLOCK_FREQUENCY = 4
AllDigital
'Be sure to REM out the line "__CONFIG    _CONFIG1H, _XT_OSC_1H" in the \PBP\18F1320.INC file!
'@ __CONFIG    _CONFIG1H, _INTIO2_OSC_1H
'@ __CONFIG  _CONFIG3H, _MCLRE_OFF_3H
'------------------------------------------------------------------------------------------------------------------------------------
'# Name          |I/O| Alias       | Var    | Description
'------------------------------------------------------------------------------------------------------------------------------------
'1 MCLR/VPP/RE3            |D 1|RSTButton    |        | Goes low when button is pressed
'2 RA0/AN0                 |D 0|LED          |        | LED on when high
'3 RA1/AN1                 |D 0|             |        |
'4 RA2/AN2/VREF-/CVREF     |D 0|             |        |
'5 RA3/AN3/VREF+           |   |             |        |
'6 RA4/T0CKI/C1OUT         |D 0|TX_ANT       |        | Selector for TX Mode             |        |
'7 RA5/AN4/SS/HLVDIN/C2OUT |D 0|RX_ANT       |        | Selector for RX Mode             |        |
'8 RE0/RD/AN5              |D 1|
'9 RE1/WR/AN6              |D 0|nSel         |        | Bring low to select RFM
'10 RE2/CS/AN7             |D 0|
'11 VDD                    |D 0|             |        | +V
'12 VSS                    |D 0|             |        | Ground
'13 OSC1/CLKI/RA7          |D 0|             |        |
'14 OSC2/CLKO/RA6          |   |             |        |
'15 RC0/T1OSO/T13CKI       |D 0|             |        |
'16 RC1/T1OSI/CCP2(1       |D 0|             |        |
'17 RC2/CCP1/P1A           |D 0|CS           |        | Goes low on interrupt from RFM
'18 RC3/SCK/SCL            |D 0|SCK          |        | Clock signal to RFM
'19 RD0/PSP0               |D 0|nButton      |        | Goes low when button is pressed
'20 RD1/PSP1               |D 0|LED          |        |
'21 RD2/PSP2               |D 0|             |        |
'22 RD3/PSP3               |D 0|             |        |
'23 RC4/SDI/SDA            |D 0|SDI          |        | Output to RFM input
'24 RC5/SDO                |D 1|SDO          |        | Input from RFM output
'25 RC6/TX/CK              |D 0|             |        |
'26 RC7/RX/DT              |D 0|             |        |
'27 RD4/PSP4               |  0|
'28 RD5/PSP5/P1B           |D 0|             |        |
'29 RD6/PSP6/P1C           |D 0|             |        |
'30 RD7/PSP7/P1D           |D 0|             |        |
'31 VSS                    |D 0|             |        | Ground
'32 VDD                    |   |             |        | +V
'33 RB0/INT0/FLT0/AN12     |D 0|             |        |
'34 RB1/INT1/AN10          |D 0|             |        |
'35 RB2/INT2/AN8           |D 0|             |        |
'36 RB3/AN9/CCP2(1)        |D 0|
'37 RB4/KBI0/AN11          |
'38 RB5/KBI1/PGM           |   |             |        |
'39 RB6/KBI2/PGC           |D 0|             |        |
'40 RB7/KBI3/PGD           |D 0|             |        |
'###############################################################################
'SPI_SCK_REG  '- defines the port where sck line is connected To
'SPI_SCK_BIT  '- defines the pin where SCK line is connected To
'SPI_SDI_REG  '- defines the port where SDI line is connected To
'SPI_SDI_BIT  '- defines the pin where SDI line is connected To
'SPI_SDO_REG  '- defines the port where SDO line is connected To
'SPI_SDO_BIT  '- defines the pin where SDO line is connected To
'SPI_CS_REG  '- defines the port where CS line is connected To
'SPI_CS_BIT  '- defines the pin where cs line is connected To
Define SPI_SCK_REG = PORTC
Define SPI_SCK_BIT = 3
Define SPI_SDI_REG = PORTC
Define SPI_SDI_BIT = 4
Define SPI_SDO_REG = PORTC
Define SPI_SDO_BIT = 5
Define SPI_CS_REG = PORTC
Define SPI_CS_BIT = 2
'###############################################################################
PORTA = %00000000
PORTB = %00000010
PORTC = %00010000
PORTD = %00000000
PORTE = %00000101

TRISA = %000001
TRISB = %010001
'**********
'DEFINES
'***********
'CONSTANTS
'***********
'packetsize con 3>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>..
'****************
'Hardware Setup
'****************
'All Ports Digital
'un-rem this line for 18F1320...
ADCON1 = $7f
'*********
'ALIASES (SYMBOL)
'*********
'nbutton var PORTA.0>>>>>>>>>>>>>>>>>>>>>>>>>>>.
'led var PORTA.1>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'sdo var PORTB.4>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'sdi var PORTB.3>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'sck var PORTB.2>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'nsel var PORTB.1>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'nirq var PORTB.0>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'tx_ant var PORTB.5>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'rx_ant var PORTB.6>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'***********
'VARIABLES
'***********
'naddress var Byte>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'noutdata var Byte>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'nindata var Byte>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'nstatus1 var Byte>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'nstatus2 var Byte>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'i var Byte>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'anrxbuf var Byte[35]  'at least 2x the packet length>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'antxbuf var Byte[packetsize]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'nchecksum var Byte>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


'*********
Symbol rstbutton = PORTE.3
Symbol nbutton = PORTD.0
Symbol led = PORTD.1
Symbol sdo = PORTC.5
Symbol sdi = PORTC.4
Symbol sck = PORTC.3
Symbol nsel = PORTE.1
Symbol nirq = PORTE.0
Symbol tx_ant = PORTA.4
Symbol rx_ant = PORTA.5
'***********
'VARIABLES
'***********
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Dim packet As Byte  'Packet = packetsize-1 XXXXXXXXXXXXXXXXXXXXXXXX
Dim packetsize As Byte
Dim txi As Byte
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Dim naddress As Byte
Dim noutdata As Byte
Dim nindata As Byte
Dim nstatus1 As Byte
Dim nstatus2 As Byte
Dim i As Byte
Dim anrxbuf(35) As Byte  '(35)  'at least 2x the packet length>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
Dim antxbuf As Byte  '(packetsize)  '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
Dim nchecksum As Byte
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'Symbol packetsize = antxbuf
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'**********************
'Initialize Variables
'**********************
'Dim addr As Byte
'Dim value As Byte
SPIPrepare
Call writetoreg(0x04, 0x01)
Call writetoreg(0x05, 0x02)
Call writetoreg(0x08, 0x03)

End                                               

Proc writetoreg(addr As Byte, value As Byte)
'put spi code here to write value to addr
SPICSOn
SPISend addr
SPISend value
SPICSOff
End Proc                                          