PDA

View Full Version : Another Serial data using Manchester question



tazntex
- 18th July 2008, 16:39
I have been reading many post on manchester encoding/decoding and have been trying to get this code to work. Before I was just sending the state of portb out and receiving it in and it was working, but would miss bits. So after reading about Melanie's, Bruce and Ioannis (I hope thats the correct spelling), post I tried Ioannis version. It seems to be clearer than most, but now nothing works. Any suggestions?

Here is the code I've been experimenting with:

Serial Transmitter
@ DEVICE PIC16F628a,XT_OSC
@ DEVICE pic16F628a, WDT_OFF
' Watchdog Timer
@ DEVICE pic16F628a, PWRT_ON
' Power-On Timer
@ DEVICE pic16F628a, MCLR_ON
' Master Clear Options (Internal)
@ DEVICE pic16F628a, BOD_ON
' Brown-Out Detect
@ DEVICE pic16F628a, LVP_OFF
' Low-Voltage Programming
@ DEVICE pic16F628a, CPD_OFF
' Data Memory Code Protect
' Set to CPD_OFF for Development Copy
' Set to CPD_ON for Release Copy
@ DEVICE pic16F628a, PROTECT_OFF

define osc 4
include "bs2defs.bas"
CMCON=%00000111
i var byte
encoded var word
dgood var porta.2
serpin var porta.3
nokey con %11111111
synch con 254
keyin var portb
mydata var byte
trisb = %11111111
trisa = %00010111
ro VAR PORTB.0
ri VAR PORTB.1
bkl VAR PORTB.2
es VAR PORTB.3
rb VAR PORTB.4
lb VAR PORTB.5
bl VAR PORTB.6
na VAR PORTB.7



findkey:
if rn = 0 then sw1
if lb = 0 then sw2
if ri = 0 then sw3
if ro = 0 then sw4
if es = 0 then sw0
if bl = 0 then sw5
if bkl = 0 then sw6


goto findkey
sw0:
mydata = %0100
goto dout
sw1:
mydata = %0101
goto dout
sw2:
mydata = %0110
goto dout
sw3:
mydata = %0010
goto dout
sw4:
mydata = %0001
goto dout
sw5:
mydata = %0111
goto dout
sw6:
mydata = %0011
goto dout



dout:
low dgood
pause 100
For i=0 TO 7
IF mydata.0[i]=0 Then
encoded.0[i*2]=0
encoded.0[i*2+1]=1
Else
encoded.0[i*2]=1
encoded.0[i*2+1]=0
EndIF
Next i
Return
serout serpin,N2400,[$55,$55,$55,$55,$55,synch,encoded]
if keyin = nokey then x2
goto findkey

x2:
high dgood
mydata = %00000000
serout serpin,N2400,[$55,$55,$55,$55,$55,synch,encoded]
goto findkey
end



Serial Receiver
INCLUDE "bs2defs.bas"
@ DEVICE PIC16F628a,XT_OSC
@ DEVICE pic16F628a, WDT_OFF
' Watchdog Timer
@ DEVICE pic16F628a, PWRT_ON
' Power-On Timer
@ DEVICE pic16F628a, MCLR_ON
' Master Clear Options (Internal)
@ DEVICE pic16F628a, BOD_ON
' Brown-Out Detect
@ DEVICE pic16F628a, LVP_OFF
' Low-Voltage Programming
@ DEVICE pic16F628a, CPD_OFF
' Data Memory Code Protect
' Set to CPD_OFF for Development Copy
' Set to CPD_ON for Release Copy
@ DEVICE pic16F628a, PROTECT_OFF
DEFINE OSC 4
mydata VAR byte
encoded var word
serpin VAR porta.1 'serial input pin
PORTA = 0
PORTB = 0
trisa = %00000010
trisb = %10000000
CMCON=%00000111
i var byte
PAUSE 50 ' settle time

loop:
gosub loop1
branch mydata, [rly1,rly2,rly3,rly4,rly5,rly6,rly7]
GOTO loop

rly1:
toggle 0
pause 250
goto loop

rly2:
HIGH 1
pause 50
if mydata <> %0101 then low 1
GOTO loop

rly3:
HIGH 2
pause 50
if mydata <> %0110 then low 2
GOTO loop

rly4:
HIGH 3
pause 50
if mydata <> %0010 then low 3
GOTO loop

rly5:
HIGH 4
pause 50
if mydata <> %0001 then low 4
GOTO loop

rly6:
toggle 5
pause 250
GOTO loop

rly7:
TOGGLE 6
PAUSE 250
GOTO loop



loop1:
SERIN serpin,N2400,[254],encoded
For i=0 TO 7
IF encoded.0[i*2]=0 Then
IF encoded.0[i*2+1]=1 Then
mydata.0[i]=0
EndIF
Else
mydata.0[i]=1
EndIF
Next
Return

Thank you all for you help.

skimask
- 18th July 2008, 17:15
Look at your transmitter program...Follow the code flow very carefully!!!
You'll find your error...It's in there...
Specifically, does the number of GOSUB's equal the number of RETURN's?

tazntex
- 18th July 2008, 22:11
Thanks SKIMASK, I believe you were referring to the RETURN in this section:
dout:
low dgood
pause 100
For i=0 TO 7
IF mydata.0[i]=0 Then
encoded.0[i*2]=0
encoded.0[i*2+1]=1
Else
encoded.0[i*2]=1
encoded.0[i*2+1]=0
EndIF
Next i
Return ' I took this out
serout serpin,N2400,[$55,$55,$55,$55,$55,synch,encoded]
if keyin = nokey then x2
goto findkey

Ive recompiled it, still doesn't work, I check the serial out pin with Oscope and it shows a datastream, Out of curiosity, I have a whole sleeve of the processors so I replaced both but still no luck

skimask
- 19th July 2008, 00:19
Thanks SKIMASK, I believe you were referring to the RETURN in this section:
Yes


Ive recompiled it, still doesn't work, I check the serial out pin with Oscope and it shows a datastream, Out of curiosity, I have a whole sleeve of the processors so I replaced both but still no luck
And you HAVE tried this without the RF link? Yes? Without the manchester encoding?


'Serial Transmitter
@ DEVICE PIC16F628a,XT_OSC
@ DEVICE pic16F628a, WDT_OFF ' Watchdog Timer
@ DEVICE pic16F628a, PWRT_ON ' Power-On Timer
@ DEVICE pic16F628a, MCLR_ON ' Master Clear Options (Internal)
@ DEVICE pic16F628a, BOD_ON ' Brown-Out Detect
@ DEVICE pic16F628a, LVP_OFF ' Low-Voltage Programming
@ DEVICE pic16F628a, CPD_OFF ' Data Memory Code Protect
@ DEVICE pic16F628a, PROTECT_OFF
define osc 4 <-Define's won't work unless they're in CAPS (RTFM)
include "bs2defs.bas"
CMCON=7 : i var byte : encoded var word : dgood var porta.2 : serpin var porta.3
nokey con $ff : synch con 254 : keyin var portb : mydata var byte : trisb = $ff : trisa = $17
ro VAR PORTB.0 : ri VAR PORTB.1 : bkl VAR PORTB.2 : es VAR PORTB.3 : rb VAR PORTB.4
lb VAR PORTB.5 : bl VAR PORTB.6 : na VAR PORTB.7
findkey: if rn = 0 then mydata=5 : goto dout
if lb = 0 then mydata=6 : goto dout
if ri = 0 then mydata=2 : goto dout
if ro = 0 then mydata=1 : goto dout
if es = 0 then mydata=4 : goto dout
if bl = 0 then mydata=7 : goto dout
if bkl = 0 then mydata=3 : goto dout
goto findkey
dout: dgood=0 : pause 100 : For i=0 TO 7
IF mydata.0[i] = 0 Then
encoded.0[i*2]=0 : encoded.0[i*2+1]=1
Else
encoded.0[i*2]=1 : encoded.0[i*2+1]=0
EndIF
Next i : serout serpin,N2400,[$55,$55,$55,$55,$55,synch,encoded]
if keyin = nokey then x2
goto findkey
x2: dgood=1 : mydata=0 : serout serpin,N2400,[$55,$55,$55,$55,$55,synch,mydata]
goto findkey
end

'Serial Receiver
INCLUDE "bs2defs.bas"
@ DEVICE PIC16F628a,XT_OSC
@ DEVICE pic16F628a, WDT_OFF ' Watchdog Timer
@ DEVICE pic16F628a, PWRT_ON ' Power-On Timer
@ DEVICE pic16F628a, MCLR_ON ' Master Clear Options (Internal)
@ DEVICE pic16F628a, BOD_ON ' Brown-Out Detect
@ DEVICE pic16F628a, LVP_OFF ' Low-Voltage Programming
@ DEVICE pic16F628a, CPD_OFF ' Data Memory Code Protect
@ DEVICE pic16F628a, PROTECT_OFF
DEFINE OSC 4
mydata var byte : encoded var word : serpin var porta.1 : porta=0 : portb=0 : trisa=2
trisb=$80 : CMCON=7 : i var byte : PAUSE 50
loop: gosub loop1
select case mydata
case 0
toggle 0 : pause 250
case 1
HIGH 1 : pause 50 : if mydata <> %0101 then low 1
case 2
HIGH 2 : pause 50 : if mydata <> %0110 then low 2
case 3
HIGH 3 : pause 50 : if mydata <> %0010 then low 3
case 4
HIGH 4 : pause 50 : if mydata <> %0001 then low 4
case 5
toggle 5 : pause 250
case 6
toggle 6 : pause 250
end select
goto loop
loop1: SERIN serpin,N2400,[254],encoded : For i=0 TO 7
IF encoded.0[i*2]=0 Then
IF encoded.0[i*2+1]=1 Then mydata.0[i]=0
Else
mydata.0[i]=1
EndIF
Next i : Return
End

tazntex
- 19th July 2008, 03:47
Thanks again for your advice, I had tried it before with direct cable connection when I began tinkering with this and it did work. But sometimes I would get the relays to turn on and off kind of a quick chattering. I have the flyback protection on my relays to help prevent that. Then I notice that after reading other peoples questions about using rf links and having similar problems that Manchester encoding/decoding seemed to resolve this issue. This is why I am trying to experiment with this. Perhaps I may learn from this experience. I am really interested in become proficient with PicBasic Pro, and Bruce has help me alot over the last couple of years. I hope he gets time to publish a book that has clearer examples than the PBP manual has. People like you, Bruce, Melanie and Ioannis are a real asset to this forum. Thanks again for all your help.

skimask
- 19th July 2008, 07:31
One other little 'note to self' for this particular program...
In this case, you're only sending a handful of codes back and forth, not random binary data that has to be coded and decoded 'on-the-fly'.
So, why not 'hardcode' each of those 'bytes' to their equivalent manchester values?
0 = 01010101
1 = 01010110
and so on.....
At least that way, you'll take out any chance that the 'manchester routine' is causing the problem.

tazntex
- 21st July 2008, 15:20
"In this case, you're only sending a handful of codes back and forth, not random binary data that has to be coded and decoded 'on-the-fly'.
So, why not 'hardcode' each of those 'bytes' to their equivalent manchester values?"

0 = 01010101
1 = 01010110

Would I just send and receive this as a BYTE or Word?

skimask
- 21st July 2008, 18:56
0 = 01010101
1 = 01010110

Would I just send and receive this as a BYTE or Word?

Well, since there's only 8 bits, I'd say a BYTE, 'cause there's only 8 bits.
If there was 16 bits, I'd say a WORD, or maybe 2 bytes...

tazntex
- 22nd July 2008, 20:31
Thanks again Skimask, I have it all working now after using the hardcode you recommended. I never could get the encoder decoder section to work. Everything seem good now, been on it all weekend. I found a couple of errors after rereading the program over and over. Now I have a new question, how would I keep lets say portb.1 high on the receiver as long as I was pressing the button on the transmitter but portb.1 to go low if I release the button?