Hi Mel,
Yep I thought it had a bug, actually there is another ...(I swear I am not picking on you.... ) the NEXT counterA is missing in the EncodeManchester routine.
For a while I had a problem understanding how the shift right >> instruction ran. (Not using this before)
Also I assumed that the MyData BITS are stored in the registers as [0,1,2,3,4,5,6,7], but actually they are the other way around (same goes for the ManchesterWord bits).
Watching the code run through ICD makes a lot of sense. I have attached the code for anyone interested. It is fully working on a 16F628 chip and great to pause the code and swap a few bits in the manchesterword to trigger the ErrorFlag Bit.
Thanks again Melanie
Now then, where did I put those RF modules?.....
' ==========================
' DEVICE PROGRAMMING OPTIONS
' ==========================
' PRESET FUSES
@ DEVICE PIC16F628, WDT_ON
@ DEVICE PIC16F628, INTRC_OSC_NOCLKOUT
@ DEVICE PIC16F628, BOD_ON
@ DEVICE PIC16F628, PROTECT_OFF
@ DEVICE PIC16F628, MCLR_ON
' =============
' USER DEFINES
' =============
DEFINE OSC 4
' =================
' SET USART PARAMS
' =================
DEFINE HSER_RCSTA 90h ' Set receive register to receiver enabled
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 4800 ' Set baud rate
DEFINE HSER_SPBRG 51 ' Set SPBRG directly (normally set by HSER_BAUD)
' ===========================
' CONFIGURE COMPARATOR MODULE
' ===========================
CMCON = 7 ' TURN ANALOG COMPARATOR MODE OFF
' ==============
' PROGRAM VARS
' ==============
COUNTERA var Byte
ERRORFLAG var Bit
MYDATA var Byte
MANCHESTERWORD var Word
' ===================
' START
' ===================
START:
HSERIN [MYDATA]
HSEROUT ["ENTERED VALUE: ",MYDATA,13,10]
HSEROUT ["ENCODING TO MANCHESTER NOW",13,10]
GOSUB EncodeManchester
HSEROUT ["ENCODING COMPLETED",13,10]
PAUSE 1000
HSEROUT ["DECODING MANCHESTER NOW",13,10]
GOSUB DECODEMANCHESTER
HSEROUT ["DECODING COMPLETED",13,10]
IF ERRORFLAG THEN
HSEROUT ["VALUE CORRUPTED!: ",MYDATA,13,10,13,10]
ELSE
HSEROUT ["FINAL VALUE: ",MYDATA,13,10,13,10]
ENDIF
GOTO START:
' Subroutine Encodes Manchester Word
' Enter subroutine with data in MyData
' Exit Subroutine with encoded ManchesterWord
' MyData contents are destroyed on exit
ENCODEMANCHESTER:
ManchesterWord=0
For CounterA=0 to 7
If MyData.0=0 then
ManchesterWord.14=1
else
ManchesterWord.15=1
endif
If CounterA<7 then ManchesterWord=ManchesterWord>>2
MyData=MyData>>1
Next CounterA
Return
' Subroutine Decodes Manchester Word
' Enter subroutine with encoded data in ManchesterWord
' Exit subroutine with decoded data in MyData
' ManchesterWord contents are destroyed on exit
' Also on exit, ErrorFlag=0 then Data is good
' ErrorFlag=1 then Data is suspect/corrupt
DECODEMANCHESTER:
ErrorFlag=0
For CounterA=0 to 7
If ManchesterWord.1=0 then
MyData.7=0
If ManchesterWord.0=0 then ErrorFlag=1
else
MyData.7=1
If ManchesterWord.0=1 then ErrorFlag=1
endif
ManchesterWord=ManchesterWord>>2
If CounterA<7 then MyData=MyData>>1
NEXT COUNTERA
Return





Bookmarks