'18F4520 32MHz-INT Button change registers and TX_RX 261016 1400
Define SIMULATION_WAITMS_VALUE = 1  'else 0 for PIC
Define CLOCK_FREQUENCY = 32
AllDigital
WaitMs 500  'wait for SX1276 to load default registers. (Change to minimum time length)
'------------------------------------------------------------------------------------------------------------------------------------
'# Name          |I/O| Alias       | Var    | Description
'------------------------------------------------------------------------------------------------------------------------------------
'1 MCLR/VPP/RE3            |D 1|RSTButton    |        | Goes low when button is pressed
'2 RA0/AN0                 |D 0|Button       |        |Selection Button
'3 RA1/AN1                 |D 0|LED          |        |LED on when high
'4 RA2/AN2/VREF-/CVREF     |D 0|             |        |
'5 RA3/AN3/VREF+           |D 0|             |        |
'6 RA4/T0CKI/C1OUT         |D 0|TX_ANT       |        | Selector for TX Mode             |        |
'7 RA5/AN4/SS/HLVDIN/C2OUT |D 1|RX_ANT       |        | Selector for RX Mode             |        |
'8 RE0/RD/AN5              |D 0|
'9 RE1/WR/AN6              |D 0|             |        |
'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|             |        | Goes low when button is pressed
'20 RD1/PSP1               |D 0|             |        |
'21 RD2/PSP2               |D 0|             |        |
'22 RD3/PSP3               |D 0|             |        |
'23 RC4/SDI/SDA            |D 0|SDI          |        | Output to SX1276 SDI input
'24 RC5/SDO                |D 1|SDO          |        | Input from SX1276 SDO out
'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|             |        |
'###############################################################################
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 = %00100001
PORTB = %00000010
PORTC = %00100000
PORTD = %00000000
PORTE = %00000001

'TRISA = %000001'CHECK>>>>>>>>>>>>>>>>>>>>>>>>
'TRISB = %010001
'packetsize con 3>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>..
ADCON1 = $7f
'*********
'SYMBOLS
'*********
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
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'Symbol packetsize = antxbuf
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'***********
'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
'**********************
'Initialize Variables
'**********************
'Dim addr As Byte
'Dim value As Byte
'SX1276 register addresses
'??????????????????????????????????????????????????????????????????????????
Dim regfifo As Byte  'Address of Module REG regfifo'
regfifo = 0x00
Dim regopmode As Byte
regopmode = 0x01
'?????????????????????????????????????????????????????????????????????????
'Define regfrmsb = 0x06 'These all need DIMing and ADDRing
'Define regfrmid = 0x07
'Define regfrlsb = 0x08
'Define regpaconfig = 0x09
'Define regocp = 0x0b
'Define reglna = 0x0c
'Define regfifoaddrptr = 0x0d
'Define regfiforxcurrentaddr = 0x10
'Define regirqflags = 0x12
'Define regpktrssivalue = 0x1a
'Define regmodemconfig1 = 0x1d
'Define regmodemconfig2 = 0x1e
'Define regpreamblemsb = 0x20
'Define regpreamblelsb = 0x21
'Define regpayloadlength = 0x22
'Define regdetectoptimize = 0x31
'Define regdetectionthreshold = 0x37
'Define regsyncword = 0x39
'Define regdiomapping1 = 0x40
'Define regpadac = 0x4d

'*************************************************************
'DATA
'*************************************************************
Dim standby As Byte  'Set radio to standby in RegOpMode
standby = 0x81

SPIPrepare
'Load changes to registers i,e,
Call writetosx1276reg(regfifo, 0x00)  'regfifo=0x00
Call writetosx1276reg(regopmode, standby)  'regopmode=0x01-setting radio To standby=0x81
Call writetosx1276reg(0x08, 0x03)

mainloop:
'radio transmit and receive depending on either button or data trigger
'push button to leave loop and reset reg addresses i,e, change frequency.

Goto mainloop

End                                               

Proc writetosx1276reg(addr As Byte, value As Byte)
'put spi code here to write value to addr
SPICSOn
SPISend addr
SPISend value
SPICSOff
End Proc