Searching \ for 'Decoding grey code with a pic.' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'Decoding grey code with a pic.'.

Truncated match.
PICList Thread
'Decoding grey code with a pic.'
1996\08\14@045812 by Joe McCauley

picon face
Hi,

I need to get position info from a Stegman single turn absolute position
encoder. Getting the info is only a matter of clocking the unit and
shifting in the 24 bit grey code result.

Does anybody know the fastest way of converting this to binary using
a pic.

A pic is my preferred solution as I would like to do some position
comparision (ie. accept position info from another microcontroller
and read the encoder until that position has been reached.)

Other suggestions are welcome though.

Thanks.

Joe
_________________________________________________________________

Joe McCauley
Physics Department,           Tel: 353-1-6082218
Trinity College,              Fax: 353-1-6711759
Dublin 2,                     Email: spam_OUTpmcculeyTakeThisOuTspammail.tcd.ie
Ireland.

1996\08\14@054311 by eza Asensio, Roberto

flavicon
face
Hi Joe and all,

At 09:58 14/08/96 +0100, you wrote:

>I need to get position info from a Stegman single turn absolute position
>encoder. Getting the info is only a matter of clocking the unit and
>shifting in the 24 bit grey code result.
>
>Does anybody know the fastest way of converting this to binary using
>a pic.

One easy way to convert the Gray code to binary is to do the XOR of the
current word with the shifthed_rigth version (lsb droped, 0 to msb) of this
same word.

Best regards:
--
Roberto Deza Asensio        |  .....rdezaKILLspamspam@spam@popmail.cti.unav.es
Universidad de Navarra      |  rdezaspamKILLspamcun.unav.es
Centro de Proceso de Datos  |  .....rdaKILLspamspam.....cpd.unav.es

1996\08\14@084905 by eza Asensio, Roberto

flavicon
face
Hi Rick and all,

At 07:16 14/08/96 -0500, you wrote:

>>  One easy way to convert the Gray code to binary is to do the XOR of the
>> current word with the shifthed_rigth version (lsb droped, 0 to msb) of this
>> same word.
>
>That's wonderful!  :-)  I can use it!
>
>You wouldn't happen to know an easy way to *generate* grey code,
>would you?

Yes, I know the related trick:

To get the Grey code of a binary number, you do the XOR of the binary word
with the shifted_left version (0 to lsb, msb droped) of this same word.

I know this "old tricks" from a book of the year 74 related to RTL & DTL
logic (Resistor/Diode Transistor Logic...), but they have currently use.

Best regards:

P.D. I post this reply also to the list, as perhaps it will be of use to
someone.
--
Roberto Deza Asensio        |  EraseMErdezaspam_OUTspamTakeThisOuTpopmail.cti.unav.es
Universidad de Navarra      |  rdezaspamspam_OUTcun.unav.es
Centro de Proceso de Datos  |  @spam@rdaKILLspamspamcpd.unav.es

1996\08\14@114904 by fastfwd

face
flavicon
face
Joe McCauley <KILLspamPICLISTKILLspamspamMITVMA.MIT.EDU> wrote:

> Getting the info is only a matter of clocking the unit and shifting
> in the 24 bit grey code result.
>
> Does anybody know the fastest way of converting this to binary using
> a pic.

Joe:

See the answer to Question #5, in the "Other Processors/
Miscellaneous" section of the "Answers" area on my company's web
page.

-Andy

Andrew Warren - RemoveMEfastfwdTakeThisOuTspamix.netcom.com
Fast Forward Engineering, Vista, California
http://www.geocities.com/SiliconValley/2499


'Decoding grey code with a pic.'
1996\09\04@145312 by Mike Halliday
picon face
I am sorry to be a kill joy but .... the xor ideas are not quite complete, or
ambiguous.

To generate Gray code *is* just a mater of using a shifted binary copy, the
reverse isnt true. For example assume the Gray value is 7, thus the expected
binary is 7 xor 3 (7 right shift) = 4. However a binary 4 xor 8 (4 shift
left) is 0c not 07.

Gray code should be generated by Gray = binary xor (binary * 2)
But binary is extracted as follows:-

For n the nth bit of n bits, B being the binary output bit, and G the Gray
input bit

1) B(n) = G(n)
2) B(n-1) = G(n-1) xor B(n)
3) B(n-2) = G(n-2) xor B(n-1)
4) B(n-3) = G(n-3) xor B(n-2)
......
5) B(0) = G(0) xor B(1)

From this it is clear that the binary value depends on the previous higher
binary bit. The good news is if the serial comes in most significant bit
first you can do the conversion on the fly. For the serious chip by chip
design of finite state machines transitions from states ordered according to
Gray code result in minimal wiring, because the Hamming distance is always 1.

What is user experience of the UK Forrest PICDE software like?
Happy coding.
Mike

1996\09\05@013159 by fastfwd

face
flavicon
face
Mike Halliday <spamBeGonePICLISTspamBeGonespamMITVMA.MIT.EDU> wrote:

> Gray code should be generated by Gray = binary xor (binary * 2)

   Mike:

   This is ALMOST correct.  Gray code is generated by:

       Gray = binary xor (binary / 2)

   That is, you must shift the input number RIGHT one position
   before XORing it with the original number.

{Quote hidden}

   Another way of looking at it, which may be easier to implement,
   is:

   1.  Reading from left to right, accept all the "0"'s and the
       first "1".
   2.  INVERT = TRUE.
   3.  Read the next bit.  If INVERT is true, invert that bit.
       Otherwise, accept it as-is.
   4.  If the bit you just read was a "0", loop back to Step 3.
   5.  Otherwise, invert the state of the INVERT flag, then loop
       back to Step 3.

   -Andy

Andrew Warren - TakeThisOuTfastfwdEraseMEspamspam_OUTix.netcom.com
Fast Forward Engineering, Vista, California
http://www.geocities.com/SiliconValley/2499

1996\09\05@035914 by fastfwd

face
flavicon
face
I just wrote, on the subject of decoding Gray code:

>     1.  Reading from left to right, accept all the "0"'s and the
>         first "1".
>     2.  INVERT = TRUE.
>     3.  Read the next bit.  If INVERT is true, invert that bit.
>         Otherwise, accept it as-is.
>     4.  If the bit you just read was a "0", loop back to Step 3.
>     5.  Otherwise, invert the state of the INVERT flag, then loop
>         back to Step 3.

I wasn't thinking as clearly as I should have been... Here's a
slightly-more-generalized version of the above:

1.  INVERT = FALSE.
2.  Read the next (reading left-to-right) bit.  If INVERT is true,
   invert that bit. Otherwise, accept it as-is.
3.  If the bit you just read was a "1", invert the state of the
   INVERT flag.
4.  Loop back to Step 2.

Off the top of my head, an 8-bit Gray-code decoder could look like
this:

   FLAGS   EQU     [any register]
   COUNT   EQU     [any other register]
   GRAY    EQU     [yet another register]

   #DEFINE INVERT  FLAGS,0         ;TO MAKE THE CODE EFFICIENT, THIS
                                   ;FLAG MUST BE DEFINED AT
                                   ;BIT-POSITION 0.

   ; ENTER WITH THE GRAY-CODED NUMBER IN "GRAY".  EXITS WITH THE
   ; DECODED NUMBER IN "GRAY".

   DECODE:

           BCF     INVERT          ;START WITH "INVERT" FALSE.

           MOVLW   8               ;SETUP TO DECODE 8 BITS.
           MOVWF   COUNT           ;

   LOOP:

           RLF     GRAY,W          ;ROTATE THE NEXT BIT FROM BIT-
           RLF     GRAY            ;POSITION 7 TO BIT-POSITION 0,
                                   ;LEAVING A COPY OF IT IN THE
                                   ;CARRY.

           MOVLW   00000001B       ;PREPARE TO INVERT THE BIT, IF
                                   ;NECESSARY.  IF "INVERT" WAS
                                   ;DEFINED AT BIT-POSITION 0, THIS
                                   ;INSTRUCTION ALSO PREPARES US FOR
                                   ;INVERTING THE "INVERT" FLAG,
                                   ;SHOULD THAT PROVE NECESSARY.

           BTFSC   INVERT          ;IF WE SHOULDN'T INVERT THE BIT
                                   ;WE JUST READ, SKIP AHEAD.

           XORWF   GRAY            ;OTHERWISE, INVERT THE BIT.

           SKPNC                   ;IF THE BIT WE JUST READ WAS A
                                   ;"0", SKIP AHEAD WITHOUT INVERTING
                                   ;THE STATE OF THE "INVERT" FLAG.

           XORWF   FLAGS           ;OTHERWISE, INVERT THE STATE OF
                                   ;THE "INVERT" FLAG.

           DECFSZ  COUNT           ;HAVE WE DONE ALL 8 BITS?
           GOTO    LOOP            ;IF NOT, LOOP BACK.

   ; We're done... The decoded value is in "GRAY".

Twelve instructions, 82 cycles.

-Andy

Andrew Warren - RemoveMEfastfwdspamTakeThisOuTix.netcom.com
Fast Forward Engineering, Vista, California
http://www.geocities.com/SiliconValley/2499

More... (looser matching)
- Last day of these posts
- In 1996 , 1997 only
- Today
- New search...