Grey code to decimal sketch

381 views
Skip to first unread message

Corey Renner

unread,
Jun 19, 2014, 6:21:12 PM6/19/14
to HeatSync Labs
Anyone got an arduino sketch to convert gray code to decimal?  This seems like it would be a common thing to do, but I haven't found anything.

cheers,

Robert Bell

unread,
Jun 19, 2014, 6:33:18 PM6/19/14
to heatsy...@googlegroups.com


--
You received this message because you are subscribed to the Google Groups "HeatSync Labs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to heatsynclabs...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Corey Renner

unread,
Jun 20, 2014, 2:11:37 PM6/20/14
to HeatSync Labs
Very helpful, thanks Robert.

cheers,
c
Message has been deleted
Message has been deleted
Message has been deleted

Lee Studley

unread,
Jun 21, 2014, 12:48:28 AM6/21/14
to heatsy...@googlegroups.com

Ignore the examples I gave. I don't know what I was thinking,but they aren't gray codes.

Lee Studley

unread,
Jun 21, 2014, 3:38:28 AM6/21/14
to heatsy...@googlegroups.com
Actually it did work: I was putting in the binary when I should have been putting in the gray code
Here's the example combined with bin2gry  then  gry2bin
this is using Microchip's mplab simulator.
==================================

 list      p=12c509A        ; list directive to define processor
    #include <p12c509A.inc>    ; processor specific variable definitions

    __CONFIG   _CP_OFF & _WDT_OFF & _MCLRE_OFF & _IntRC_OSC


    cblock 0x10
     gry_in
     bin_out
     count
     temp
    endc

    ORG     0x000           ; coding begins here
reset   movwf   OSCCAL          ; update register with factory cal value
    goto    init

init       clrf    STATUS            ;
    clrf    GPIO            ;
    movlw    b'00101001'     ;
    OPTION                ;
    movlw   0x10        ;
    TRIS    GPIO            ;

    clrf    GPIO            ; initialize the output bits on GPIO

;=====above is just pic setup=====================

    ; setup for conversion
    movlw 0x76 ;  0x76(hex)-->0x4D(gray) expected result

;================================
; Binary to Gray Code routine
; entry: with binary in w
; exit with Grey result: in w
; trashes gry_in contents during conversion
; uses: gry_in,temp
; ie 2 ram locations
;================================

bin2Gry
    movwf gry_in  ;
    movwf temp
    bcf STATUS,C
    rrf temp
    movf temp,w
    xorwf gry_in,w
;==end bin2gry===================



   ;setup for reverse conversion
   movwf gry_in
   nop ; place to set break point



;================================
; Gray Code to binary routine
; entry: with grey code in: gry_in
; exit with binary result:  in bin_out
; trashes gry_in contents during conversion
; uses: gry_in,bin_out,count,temp
; ie 4 ram locations
;================================

gry2bin
    movlw 0x08    ;set counter
    movwf count   
    clrf  temp
    clrf  bin_out
g_loop
    movf gry_in,w ; get gray code in w
    xorwf temp    ; xor msb w/ 0
    rlf temp,w    ; rotate into carry
    rlf bin_out   ; carry rot's into bin_out
    rlf gry_in    ; shift gry_in for next bit
    decfsz count  ; chk if 8 bits done
    goto g_loop   ; else gloop
;==end gry2bin===================


    nop ; place to set break point


;*************************************************************************

    end     ; directive 'end of program'

;*************************************************************************

Reply all
Reply to author
Forward
0 new messages