'RF transceiver code modified by john schimmel 'original code by amos, brett, daniel and chris '########################################################################## 'rf test will blink an led on porta.1 when it receives anything '############################################################################## PAUSE 500 DEFINE OSC 4 'OSC MUST BE SET TO HS 'include modedefs.bas for shiftin and out modes include "modedefs.bas" 'general purpose variables i VAR BYTE counter var byte 'pin definitions for comm w/ Laipec TRW-24G RF_CE var PORTB.7 RF_CS var PORTB.3 RF_DR var PORTB.2 'channel 1 RF_CLK1 var PORTB.1 RF_DATA_In var PORTB.0 RF_DATA_Out var PORTB.0 switch var portc.4 receiveLED var portc.3 sendLED var portc.2 ' THIS IS THE ADDRESS FOR THE w/ Laipec TRW-24G WhichAddress1 var byte 'Channel 1 WhichAddress2 var byte 'Channel 2 ' DEFINE VARIABLES FOR SERIAL TO PC inv9600 con 16468 ' baudmode for serin2 and serout2: 9600 8-N-1 inverted non9600 con 84 ' baudmode for serin2 and serout2: 9600 8-N-1 non-inverted 'constants for RF configuration modes CONRFRX var byte CONRFTX var byte 'indicates state of current RF configuration - TX is 0, RX is 1 RFCurrentState var bit 'hold data for RF message RFDataArrayIN var byte[4] RFDataArrayOUT var byte[4] 'hold data for RF configuration RFConfigArray var Byte[16] 'variables for each received or sent message msgFromID var byte msgToID var byte msgData1 var byte msgData2 var byte me var byte friend var byte me = 1 'change this for the correct jar friend = 2 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' bootup: WhichAddress1 =$BA WhichAddress2 =$BA PORTA = %00000000; TRISA = %00000001; ' 0 = Output, 1 = Input (A.1 is RxPC) PORTB = %00000000; TRISB = %01101010; ' 0 = Output, 1 = Input (C.1 is RF_DATA_In, C.3 is RF_DR) main: 'intialize RF module configuration 'constants to define receive/transmit modes at 2402 Mhz frequency CONRFRX = $05 CONRFTX = $04 RFCurrentState = CONRFRX GOSUB initRFConfig msgToID = 0 ' CHANGE THIS TP WHO SHOULD RECIEVE MESSAGE msgFromID = me ' CHANGE THIS TO ID OF SENDER msgData1 = 0 ' DATA SENT msgData2 =0 ' DATA SENT '************************BEGIN MAIN LOOP**************************************' '******************************************************************************' '******************************************************************************' while 1 low RF_CS ' bring CS low before turning CE high - only one high at a time! HIGH RF_CE ' bring CE high to bring RF into active mode 'if there's a message waiting in the RX buffer, read it if RF_DR = 1 Then INPUT RF_DATA_In GOSUB receiveRF EndIF IF switch = 1 THEN 'if the switch is closed send a message msgToID = friend ' CHANGE THIS TP WHO SHOULD RECIEVE MESSAGE msgData1 = "y" ' DATA SENT msgData2 =4 gosub sendData ' goto the sendData function msgToID = 0 ' CHANGE THIS TP WHO SHOULD RECIEVE MESSAGE msgData1 =0 ' DATA SENT msgData2 =0 ENDIF WEND '******************************************************************************' '******************************************************************************' '******************************************************************************' '******************************************************************************' receiveRF: 'flash LED to indicate that we have a message 'High DEBUG_LED2 INPUT RF_DATA_In 'power down RF front-end for current conservation LOW RF_CE 'clock in data from RF-24G to PIC for i=0 to 3 'clock should follow data by at least 50nsec shiftin RF_DATA_In, RF_CLK1, MSBPRE, [RFDataArrayIN[i]\8] NEXT 'send received data to PC for serial debugging for i=0 to 3 'SEROUT2 RxPC, 16468, [bin RFDataArrayIN[i], 13,10] NEXT 'power up RF front-end again high RF_CE msgToID = RFDataArrayIN[0] 'the receiver is in msgToID msgFromID = RFDataArrayIN[1] 'sender information in msgFromID msgData1 = RFDataArrayIN[2] 'incoming data stored in variable msgData1 msgData2 = rfdataArrayIN[3] ' and in msgData2 high receiveLED pause 500 low receiveLED pause 500 high receiveLED pause 500 low receiveLED pause 500 RETURN sendData: RFDataArrayOUT[0] = msgToID RFDataArrayOUT[1] = msgFromID RFDataArrayOUT[2] = msgData1 RFDataArrayOUT[3] = msgData2 'set RF to transmit mode... RFCurrentState = CONRFTX GOSUB setRFConfig ' for shockburst TX, the sum of RX address, payload, 'and CRC must be less than 256 bits long ' the chip then tacks on a preamble to this message, 'and the total msg is 266 bits OUTPUT RF_DATA_Out 'get back into active mode HIGH RF_CE 'Need pause between CE high and DATA clock out pauseus 100 'clock out 5 byte RF address of recipient for i = 8 to 12 'clock out this byte shiftout RF_DATA_Out, RF_CLK1, MSBFIRST, [RFConfigArray[i]\8] Next 'clock out 4 bytes of data payload for i = 0 to 3 'clock out this byte shiftout RF_DATA_Out, RF_CLK1, MSBFIRST, [RFDataArrayOUT[i]\8] Next 'start shockburst transmission low RF_CE pauseus 300 'flash LED to indicate that we transmitted high sendLED pause 500 low sendLED pause 500 high sendLED pause 500 low sendLED pause 500 'afterward, set RF back to its default receive state RFCurrentState = CONRFRX GOSUB setRFConfig RETURN setRFConfig: ' THIS IS USED TO SWITCH BETWEEN RECIEVE AND TRANSMIT MODES 'during configuration of the transmitter, we need RF_DATA as an output OUTPUT RF_DATA_Out 'set chip to configure mode (CE low, CS high) low RF_CE high RF_CS 'pause between CS high and DATA clockout pauseus 100 'RFCurrentState indicates 2402 Mhz frequency and either RX or TX state 'clock out new configuration data from PIC to RF-24G shiftout RF_DATA_Out, RF_CLK1, MSBFIRST, [RFCurrentState\8] 'configuration set on falling edge of CS low RF_CS low RF_CE 'for consistency with the rx, stick this here input RF_DR 'flash the LED on B6 to indicate that we configured 'high DEBUG_LED1 ' pause 250 'low DEBUG_LED1 RETURN initRFConfig: 'configuration is a 144 bit word 'must be clocked in MSB first 'bit map '143:120 - reserved for testing '119:112 - length of data payload section RX channel 2 '111-104 - length of data payload section RX channel 1 '103:64 - up to 5 byte address for RX channel 2 '63:24 - up to 5 byte address for RX channel 1 '23:18 - number of address bits (both RX channels) '17 - 8 or 16 bit CRC '16 - enable on-chip CRCD generation/checking '15 - enable two channel receive mode '14 - communication mode (direct or shockburst) '13 - RF data rate (1Mbps requires 16Mhz crystal) '12:10 - crystal frequency '9:8 - RF output power '7:1 - frequency channel '0 - RX or TX operation 'setup RFConfigArray with config bytes, see datasheet for default configuration 'this array is backwards so we can clock from MSB to LSB by increasing the index 'Data bits 119-112: Max data width on channel 2 (excluding CRC and adrs) RFConfigArray[1] = $20 'payloadSize, 4 BYTES 'Data bits 111-104: Max data width on channel 1 (excluding CRC and adrs) RFConfigArray[2] = $20 'payloadSize, 4 BYTES 'Data bits 103-64: Channel 2 address, 5 bytes (40 bits) RFConfigArray[3] = WhichAddress2 RFConfigArray[4] = WhichAddress2 RFConfigArray[5] = WhichAddress2 RFConfigArray[6] = WhichAddress2 RFConfigArray[7] = WhichAddress2 'Data bits 63-24: Channel 1 address, 5 bytes (40 bits) RFConfigArray[8] = WhichAddress1 RFConfigArray[9] = WhichAddress1 RFConfigArray[10] = WhichAddress1 RFConfigArray[11] = WhichAddress1 RFConfigArray[12] = WhichAddress1 'Data bits 23-16: Address width and CRC - 40 bit addr, 16 bit CRC, CRC enabled RFConfigArray[13] = $A3 'Data bits 15-8: One Channel Receive, Shockburst Enabled, 250 kilobits, etc RFConfigArray[14] = $4F 'Data bits 7-0: 2402 Mhz frequency, RX enabled/disabled (depending on RFCurrentState) RFConfigArray[15] = RFCurrentState '=================================================================== 'During configuration of the transmitter, we need RF_DATA as an output INPUT RF_DR INPUT RF_DATA_In OUTPUT RF_DATA_Out 'set chip to configure mode (CE low, CS high) low RF_CE high RF_CS 'pause between CS high and DATA clockout pauseus 100 'clock out configuration bytes from PIC to RF-24G for i = 1 to 15 'set config one byte at a time from MSB to LSB shiftout RF_DATA_Out, RF_CLK1, MSBFIRST, [RFConfigArray[i]\8] next 'configuration is set on the falling edge of CS, bring it low low RF_CS low RF_CE 'flash LED on pin B6 to debug that we configured high sendLED pause 250 low sendLED pause 250 high receiveLED pause 250 low receiveLED pause 250 high sendLED pause 250 low sendLED high receiveLED pause 250 low receiveLED pause 250 RETURN