'RF transceiver code modified by john schimmel 'original code by amos, brett, daniel and chris '########################################################################## ' SUBROUTINE DESCRIPTIONS ' callFireflies SENDS OUT 4 BYTES STORED IN RFDataArrayOUT ' recieveRF READS IN DATA FROM THE TRANSCIEVER INTO RFDataArrayIN THEN LOADS ' DATA INTO 4 VARIABLES msgFromID, msgToID, msgData1, msgData2 ' ' initRFConfig MUST BE CALLED AT THE BEGINNING OF THE PROGRAM ' IT IS USED TO SET UP THE TRW-24G THE ADDRESSES MUST BE ' CHANGED TO AVOID INTERFERENCE WITH OTHER DEVICES USING ' THIS CODE ' setRFConfig IS USED TO TOGGLE THE TRW-24G BETWEEN SEND AND RECIEVE MODES '############################################################################## 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 P var byte Q var byte counter var byte newSet var bit randomChannel var byte theBeat var bit(256) 'pin definitions for comm w/ Laipec TRW-24G RF_CE var PORTB.0 RF_CS var PORTB.4 RF_DR var PORTB.3 RF_CLK1 var PORTB.2 RF_DATA_In var PORTB.1 RF_DATA_Out var PORTB.1 qproxPin var portb.5 ' THIS IS THE ADDRESS FOR THE w/ Laipec TRW-24G WhichAddress1 var byte 'Channel 1 WhichAddress2 var byte 'Channel 2 'pin definitions for debug LEDs and switch 'DEBUG_SWITCH1 var PORTB.6 'switch used to transmit rf message 'DEBUG_LED1 var PORTA.2 'led goes on during rf module configuration 'DEBUG_LED2 var PORTA.2 'led goes on during rf message rx or tx RxPC var PORTA.1 ' serial out to pc ' 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 greenBeat var bit(256) yellowBeat var bit(256) redBeat var bit(256) '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 Sender var byte greenLED var porta.4 yellowLED var porta.3 redLED var porta.2 'arrays to hold digital pot channels for the pairs of fireflies greenFlies var byte[2] 'channel 0 , 1 greenFlies[0]=0 greenFlies[1]=1 yellowFlies var byte[2] 'channel 2, 3 yellowFlies[0]=2 yellowFlies[1]=3 redFlies var byte[2] 'channel 4, 5 redFlies[0]=4 redFlies[1]=5 currFly var byte[3] 'current fly array [0]=green, [1]=yellow, [2]=white currFly[0]=greenFlies[0] 'starting green fly on channel 0 currFly[1]=yellowFlies[0] 'starting yellow fly on channel 2 currFly[2]=redFlies[0] 'starting red fly on channel 4 channelSwitch var bit[3] 'switch between flies in pauses time var byte ' digital pot variables SDO VAR PORTA.2 SCLK VAR PORTA.3 CS VAR PORTA.4 channel VAR BYTE nextChannel var byte brightness var byte(6) direction var byte(6) level var byte dataOut var word playcount var byte me var byte me = 1 'change this for the correct jar noTouch var bit noTouch=1 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' bootup: WhichAddress1 =$BC WhichAddress2 =$BC 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) '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 for i=0 to 255 ' set all the fireflies beat to off greenBeat(i)=0 yellowBeat(i)=0 redBeat(i)=0 next for i=0 to 2 'boolean used for switching fireflies/pair channelswitch(i)=0 next time=0 '************************BEGIN MAIN LOOP**************************************' '******************************************************************************' '******************************************************************************' main: 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 'playback '------------------------------------------------------- for playcount=0 to 4 gosub playBackBeat next gosub normalPulseFireFlies WEND '******************************************************************************' '******************************************************************************' receiveRF: 'flash LED to indicate that we have a message 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 'power up RF front-end again high RF_CE msgToID = RFDataArrayIN[0] Sender = RFDataArrayIN[1] msgData1 = RFDataArrayIN[2] msgData2 = rfdataArrayIN[3] 'for i=0 to 3 'SEROUT2 RxPC, inv9600, [msgFromID,13,10] 'NEXT RETURN callFireFlies: 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 '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 RETURN '============================================================================ normalPulseFireFlies: 'fade the LEDs up, one by one for channel=0 to 5 for level = 0 to 255 brightness(channel) = level gosub fadeChannel pause 3 gosub checkInput next next pause 1200 'fade the leds down for channel =0 to 255 for level = 255 to 0 step 20 brightness(channel) = level gosub fadeChannel pause 1 gosub checkInput next next return '============================================================================ checkQPROX: if qproxPin=0 then 'count taps for 4 seconds channel = currFly(me) for Q=0 to 255 if qproxPin=0 then select case me case 0 greenBeat(Q) = 1 msgData1 = greenBeat(q) case 1 yellowBeat(Q) = 1 msgData1 = yellowBeat(q) case 2 redBeat(Q) = 1 msgData1 = redBeat(q) end select level=255 else select case me case 0 greenBeat(Q) = 0 msgData1 = greenBeat(Q) case 1 yellowBeat(Q) = 0 msgData1 = yellowBeat(Q) case 2 redBeat(Q) = 0 msgData1 = redBeat(q) end select level=0 endif 'talk to digital pot brightness(channel)=level gosub fadeChannel gosub switchFlies channel = currFly(sender) brightness(channel)= level gosub fadeChannel gosub callFireFlies pause 16 next gosub PairOn pause 250 gosub PairOff pause 250 gosub PairOn pause 250 gosub PairOff pause 250 goto main endif return '============================================================================ playBackBeat: 'high porta.1 for p=0 to 255 'green flies channel = currFly[0] if greenBeat(p)=1 then brightness(channel) = 255 channelSwitch(0)=0 else brightness(channel) = 0 if not channelSwitch(0) then 'switch to second fly of the pair gosub switchFlies channelSwitch(0) = 1 endif endif 'talk to digital pot gosub fadeChannel 'yellow flies channel = currFly[1] if yellowBeat(p)=1 then brightness(channel) = 255 channelSwitch(1)=0 else brightness(channel) = 0 if not channelSwitch(1) then 'switch to second fly of the pair gosub switchFlies channelSwitch(1) = 1 endif endif 'talk to digital pot gosub fadeChannel 'red flies channel = currFly[2] if redBeat(p)=1 then brightness(channel) = 255 channelSwitch(2)=0 else brightness(channel) = 0 if not channelSwitch(2) then 'switch to second fly of the pair gosub switchFlies channelSwitch(2) = 1 endif endif 'talk to digital pot gosub fadeChannel gosub checkInput pause 16 next 'low porta.1 return '--------------------------------------------------------------------- checkInput: ' did you tap your jar? '------------------------------------------------------- 'if there's a message waiting in the RX buffer, read it if qproxPin = 0 then sender=me gosub checkQPROX endif ' check for incoming taps '------------------------------------------------------- if RF_DR = 1 Then INPUT RF_DATA_In gosub receiveRF channel=currFly(sender) 'count taps for 4 seconds for Q=0 to 255 INPUT RF_DATA_In gosub receiveRF select case sender case 0 greenBeat(Q) = msgData1 case 1 yellowBeat(Q) = msgData1 case 2 redBeat(Q) = msgData1 end select level=msgData1*255 'talk to digital pot brightness(channel)=level gosub fadeChannel gosub switchFlies channel = currFly(sender) brightness(channel)= level gosub fadeChannel pause 16 next q gosub PairOn pause 250 gosub PairOff pause 250 gosub PairOn pause 250 gosub PairOff pause 250 while rf_dr=1 gosub receiveRF gosub PairOn pause 250 gosub PairOff pause 250 wend 'goto main endif ' low porta.1 return '============================================================================ PairOn: channel = currFly(sender) brightness(channel)=255 gosub fadeChannel 'switch channel on current pair gosub switchFlies channel = currFly(Sender) brightness(channel)=255 gosub fadeChannel return PairOff: channel = currFly(Sender) brightness(channel)=0 gosub fadeChannel gosub switchFlies channel = currFly(Sender) brightness(channel)=0 gosub fadeChannel return '============================================================================ playClean: for i=0 to 3 'high porta.1 for p=0 to 255 'green flies channel = currFly[0] if greenBeat(p)=1 then brightness(channel) = 255 channelSwitch(0)=0 else brightness(channel) = 0 if not channelSwitch(0) then 'switch to second fly of the pair gosub switchFlies channelSwitch(0) = 1 endif endif 'talk to digital pot gosub fadeChannel 'yellow flies channel = currFly[1] if yellowBeat(p)=1 then brightness(channel) = 255 channelSwitch(1)=0 else brightness(channel) = 0 if not channelSwitch(1) then 'switch to second fly of the pair gosub switchFlies channelSwitch(1) = 1 endif endif 'talk to digital pot gosub fadeChannel 'red flies channel = currFly[2] if redBeat(p)=1 then brightness(channel) = 255 channelSwitch(2)=0 else brightness(channel) = 0 if not channelSwitch(2) then 'switch to second fly of the pair gosub switchFlies channelSwitch(2) = 1 endif endif 'talk to digital pot gosub fadeChannel pause 16 next p next i goto main return return '============================================================================ fadeChannel: 'brightness(channel) = level 'take chip select low to activate ad5206 low cs 'put channel number in first byte to shift out dataOut.highbyte = channel 'put brightness in second byte to shift out dataout.lowbyte = brightness(channel) 'shift out 11 bits from dataOut shiftout sdo, sclk, MSBFIRST, [dataOut\11] high cs return switchFlies: select case channel case 0 'change to channel 1 currFly[0]=1 case 1 'change to channel 0 currFly[0]=0 case 2 'change to channel 3 currFly[1]=3 case 3 'change to channel 2 currFly[1]=2 Case 4 'change to channel 5 currFly[2]=5 case 5 'change to channel 4 currFly[2]=4 case else currFly[0]=0 currFly[1]=2 currFly[2]=4 end select return