Now then....
Remove the wire between the 2 PICs. Connect the transmitter input to TXPIC-PortB.2, connect the receiver digital output to RXPIC-PortB.5. You're not ready to send data yet. You have to encode the data from the TXPIC and then decode it at the RXPIC using manchester encoding/decoding techniques.
The receiver module relies on bit transitions, not steady state levels. So therefore, with manchester encoding, 1 bit become 2 bits, either a low-high or high-low transition. So, out of one byte (256 values), we can only send 1/2 byte (16 values) of code. 2 of these values have to be reserved for 'training' the receiver, getting the data slicer in the receiver charged up (according to the datasheets for the core of this module, you have to do it for about 5ms).
If we sent all 1's, the data slicer would be charged up to a full 1, if we sent all 0's, the data slicer wouldn't get charged at all, so we send alternating 1's and 0's, hence the 2 reserved byte values, $AA (10101010) and $55 (01010101). Remember, each bit becomes 2 bits in manchester encoding, 0 becomes 01, 1 becomes 10. This gives us 16 values to play with in a byte
hex value----binary value-----manchester encoded binary (hex) value
0------------0000------------01 01 01 01 ($55)
1------------0001------------01 01 01 10 ($56)
2------------0010------------01 01 10 01 ($59)
3------------0011------------01 01 10 10 ($5A)
4------------0100------------01 10 01 01 ($65)
5------------0101------------01 10 01 10 ($66)
6------------0110------------01 10 10 01 ($69)
7------------0111------------01 10 10 10 ($6A)
8------------1000------------10 01 01 01 ($95)
9------------1001------------10 01 01 10 ($96)
A------------1010------------10 01 10 01 ($99)
B------------1011------------10 01 10 10 ($9A)
C------------1100------------10 10 01 01 ($A5)
D------------1101------------10 10 01 10 ($A6)
E------------1110------------10 10 10 01 ($A9)
F------------1111------------10 10 10 10 ($AA)
Only hex $0 and hex $F (left column) are true repeating manchester binary patterns with true alternating values (right column) , so you reserve those for training the receiver only. Everything else from hex $1 - $f (left column) are free.
So, this is what we'll do:
Modify the transmitter code to send the code for each LED 1-4 out the serial port to the transmitter in manchester format, but instead of using hex $0 for reset the LEDs, we'll use hex $5. And don't forget, we have to train the receiver (by sending data with the transmitter) each time we send a new code.
So the transmitter code changes to:
INCLUDE "modedefs.bas"
DEFINE OSC 20 'use external 20mhz crystal
CMCON = 7 : ANSEL = 0 : ADCON1 = 7
txout var portb.2 : output txout : dataout var byte
ledcount var byte
led1 var porta.0 : output led1 : led2 var porta.1 : output led2
led3 var porta.2 : output led3 : led4 var porta.3 : output led4
key var portb.0 : input btn 'push button on portb.0
'1K-10K resistor from portb.0 to ground (pulldown)
'push button is wired between portb.0 and +5v
'initial LED check
led1 = 1 : pause 500 : led1 = 0 : led2 = 1 : pause 500 : led2 = 0
led3 = 1 : pause 500 : led3 = 0 : led4 = 1 : pause 500 : led4 = 0
mainloop:
if key = 0 then 'button not pressed
goto mainloop
endif
if key = 1 then
pause 50 'wait 50ms for switch to debounce then check again
if key = 1 then 'if it's still pressed, then increment the count
ledcount = ledcount + 1
dataout = $55 ($55 = manchester encoded $0)
'train the receiver by sending 5 each of the $55's, may need more
'just copy the line below to make it send out more characters
serout txout, n2400, [ dataout, dataout, dataout, dataout, dataout ]
endif
endif
if ledcount = 0 then 'all leds off
dataout = $66 'manchester encoded $5, use because $0 is reserved
serout txout, n2400, [ dataout ]
'may have to send data more than once depending on how well the receiver
'can capture the data. Shouldn't have a problem sending it once
led1 = 0 : led2 = 0 : led3 = 0 : led4 = 0
endif
if ledcount = 1 then '1st led on
dataout = $56 'manchester encoded $1
serout txout, n2400, [ dataout ]
led1 = 1 : led2 = 0 : led3 = 0 : led4 = 0
endif
if ledcount = 2 then '2nd led on and so on and so on down the line....
dataout = $59 'manchester encoded $2
serout txout, n2400, [ dataout ]
led1 = 0 : led2 = 1 : led3 = 0 : led4 = 0
endif
if ledcount = 3 then
dataout = $5a 'manchester encoded $3
serout txout, n2400, [ dataout ]
led1 = 0 : led2 = 0 : led3 = 1 : led4 = 0
endif
if ledcount = 4 then
dataout = $65 'manchester encoded $4
serout txout, n2400, [ dataout ]
led1 = 0 : led2 = 0 : led3 = 0 : led4 = 1
endif
if ledcount = 5 then 'reset led count, roll it back to 0
ledcount = 0 'and turn leds off since count is back to 0
dataout = $66 'manchester encoded $5 (same thing as ledcount = 0 above)
serout txout, n2400, [ dataout ]
led1 = 0 : led2 = 0 : led3 = 0 : led4 = 0
endif
goto mainloop
END
This code should still change the LEDs on TXPIC, but won't do anything for RXPIC until we change that code...
Receiver code in next post....
JDG
Bookmarks