Help with serin, serout, Manchester encoding
I am trying to transmit 4 bits from a TLP-315 transmitter to a RLP-315 receiver for a project. At the receiver, I am taking the four bits and performing some comparisons on it and getting an output. I am using two PIC16F88 microcontrollers. I was wondering whether I coded the Manchester coding and used the serin, serout correct. Also, since I am using Manchester coding, do I have to send a preamble? I have attached my code below.
'Transmit
Include "Modedefs.bas"
v var word 'v is a byte to be encoded
counter var byte 'counter is a byte
encoded var word 'encoded is a word sized variable that holds the encoded byte v
'Code to be transmitted
v = 1010
'Manchester Encoding
For counter = 0 to 3 'number of bits to be encoded (4 bits)
If v.0[counter]=0 Then
encoded.0[counter*2]=0
encoded.0[counter*2+1]=1
Else
encoded.0[counter*2]=1
encoded.0[counter*2+1]=0
EndIf
Next counter
'Serial Out
serout PORTB.0, n2400, [encoded]
'Receive
'Include "Modedefs.bas"
s var word 's is a byte to be encoded
B0 var byte 'B0 is a byte
B1 var byte 'B1 is a byte
'counter var byte
encoded1 var word 'encoded1 word sized variable that holds the encoded byte v
action var byte 'action variable
alert var byte 'alert variable
B0=0 'hardwired address
B1=1 'hardwired address
'Decoding
loop:
serin PORTB.0, n2400, [encoded1]
For counter = 0 to 3
s.0[counter]=encoded1.0[counter*2]
Next counter
goto loop
End
'Tracking code
If (s.2==0) & (s.3==0) Then 'If the code matches 00 then it is in the tracking mode
If (s.0==B0) & (s.1==B1) Then 'If the address matches then
s.2=1 'Change the third bit to 1
action = s.2
alert = 0
serout PORTB.1, n2400, [s] 'Send the changed word back
Else
s.2=0 'Since the addresses does not match do not change anything
action = s.2
alert = 0
serout PORTB.1, n2400, [s] 'Send the changed word back
Endif
Endif
'Tracking and Finding code
If (s.2==0) & (s.3==1) Then 'If the code matches 01 then it is in the tracking and finding mode
If (s.0==B0) & (s.1==B1) Then 'If the address matches then
s.2=1 'Change the third bit to 1
action=s.2
alert=1
serout PORTB.1, n2400, [s] 'Send the changed word back
High 2
Else
s.2=0 'Since the address does not match do not change anything
action=s.2
alert=0
serout PORTB.1, n2400, [s] 'Send te changed word back
Endif
Endif
Encoding for RF communications
Simple RF receivers need a preamble to condition the data slicer. I have had great success with low power Rf comms by using a pseudo Manchester strategy.
Manchester code is usually recommended because it guarantees at least one transition per bit so a long run of 000 or 111 can be successfully decoded.
The following also works. Assuming a 4 bit nibble is to be sent, build a character of start bit, 8 data bits and one or preferably 2 or more stop bits. Use Char_Pacing to get extra stop bits. 8 bit bytes need to be broken into two halves before sending. A checksum on any data block is highly recommended.
Byte bit 0 is nibble bit 0
Byte bit 1 is inverted nibble bit 0
Byte bit 2 is nibble bit 1
Byte bit 3 is inverted nibble bit 1
Byte bit 4 is nibble bit2
Byte bit 5 is inverted nibble bit 2
Byte bit 6 is nibble bit 3
Byte bit 7 is inverted nibble bit 3
Now transmit this character many times - say 32 or more. The first few characters act as the preamble and the data stream is very nearly 50:50 duty cycle so the receiver data slicer is happy. Depending on link characteristics you may be able to cut the transmitted data down to 16 or even less repetitions.
Perform tests on the received character stream to make sure you receive it, say, at least 3 consecutive times, where the characters are identical to each other AND the data structure of bit/inverted-bit is obeyed. Activating parity can also give you an extra check.
I have used this with expensive 154 MHz transceivers and cheap 315 and 433 MHz Tx/Rx modules with great success.
HTH
Brian