piclist 2002\12\03\045626a >
Thread: 10 bit binary value to 4 char BCD.
www.piclist.com/techref/microchip/devices.htm?key=pic
face picon face BY : Jan-Erik Soderholm email (remove spam text)



Many thanks Bill, I like that idea, as you said,
"...is easy for other's and even you, to understand..."

:-)

A bit more code then the examples on the PIClist archive,
but much easier to read (well, for me, that is).

Speed is very dependent of the number of "ones" in the
ADC value, from aprox 30 cycles (all zeros) to
aprox 180 cycles (all ones). Acceptable...
66 instructions including bcd_adjust subroutine.

The key here is the DAW (Decimal Adjust W) instruction
on the 18F-series. By doing DAW between each bit test, there
is no need for the ending adjustment (loop) as in your
example, right ? And I'm using two bytes with a packed
BCD value instead of four bytes with a single BCD value.

I'v thought of something like this (18F-code).
(It's not tested, I'll load it in MPLAB later today.)

ADCH : high byte (bit 8 and 9) of ADC value.
ADCL : low byte (bit 0-7) of ADC value.

BCDH : high byte of BCD result (packed BCD)
BCDL : low byte of BCD result (packed BCD)

   clrf   BCDH             ; zero the BCD value
   clrf   BCDL

test_adc_bit_9

   btfss  ADCH,1           ; is bit 9 of ADC value set ?
   goto   test_adc_bit_8   ; no, go check next lower bit
   movlw  0x05             ; yes, move hex '0512' to result
   movwf  BCDH
   movlw  0x12
   movwf  BCDL             ; (No need to BCD adjust here...)

test_adc_bit_8

   btfss  ADCH,0           ; is bit 8 of ADC value set ?
   goto   test_adc_bit_7   ; no, go check next lower bit
   movlw  0x02             ; yes, add hex '0256' to result
   addwf  BCDH
   movlw  0x56
   addwf  BCDL
   call   bcd_adjust       ; BCD adjust BCD value

test_adc_bit_7

   btfss  ADCL,7           ; is bit 7 of ADC value set ?
   goto   test_adc_bit_6   ; no, go check next lower bit
   movlw  0x01             ; yes, add hex '0128' to result
   addwf  BCDH
   movlw  0x28
   addwf  BCDL
   call   bcd_adjust       ; BCD adjust BCD value

test_adc_bit_6

   btfss  ADCL,6           ; is bit 6 of ADC value set ?
   goto   test_adc_bit_5   ; no, go check next lower bit
   movlw  0x64             ; yes, add hex '0064' to result
   addwf  BCDL             ; (from now on, no need to add the high byte)
   call   bcd_adjust       ; BCD adjust BCD value

test_adc_bit_5

   btfss  ADCL,5           ; is bit 5 of ADC value set ?
   goto   test_adc_bit_4   ; no, go check next lower bit
   movlw  0x32             ; yes, add hex '0032' to result
   addwf  BCDL
   call   bcd_adjust       ; BCD adjust BCD value


test_adc_bit_4

   btfss  ADCL,4           ; is bit 4 of ADC value set ?
   goto   test_adc_bit_3   ; no, go check next lower bit
   movlw  0x16             ; yes, add hex '0016' to result
   addwf  BCDL
   call   bcd_adjust       ; BCD adjust BCD value

test_adc_bit_3

   btfss  ADCL,3           ; is bit 3 of ADC value set ?
   goto   test_adc_bit_2   ; no, go check next lower bit
   movlw  0x08             ; yes, add hex '0008' to result
   addwf  BCDL
   call   bcd_adjust       ; BCD adjust BCD value

test_adc_bit_2

   btfss  ADCL,2           ; is bit 2 of ADC value set ?
   goto   test_adc_bit_1   ; no, go check next lower bit
   movlw  0x04             ; yes, add hex '0004' to result
   addwf  BCDL
   call   bcd_adjust       ; BCD adjust BCD value

test_adc_bit_1

   btfss  ADCL,1           ; is bit 1 of ADC value set ?
   goto   test_adc_bit_0   ; no, go check next lower bit
   movlw  0x02             ; yes, add hex '0002' to result
   addwf  BCDL
   call   bcd_adjust       ; BCD adjust BCD value

test_adc_bit_0

   btfss  ADCL,1           ; is bit 0 of ADC value set ?
   goto   bin2bcd_ok       ; no, we're done !!
   movlw  0x01             ; yes, add hex '0001' to result
   addwf  BCDL
   call   bcd_adjust       ; BCD adjust BCD value

bin2bcd_ok

   ; Now we have the packed BCD value in BCDH:BCDL
   return


bcd_adjust

   ; BCD Adjust value in BCDH:BCDL
   ;
   bcf    status, carry    ; clear carry
   movf   BCDL, w
   daw                     ; Adjust BCDL (set carry if carry)
   movwf  BCDL
   movf   BCDH, w
   daw                     ; Adjust BCDH (with carry if set)
   movwf  BCDH
   return







A quick and simple way would be to set up 4 zero
bytes.  Then bit test each bit of the a/d and if
it is on, add the 4 digit value to the bytes.
When done, go through and test each byte for
greater than 9, if it is, subtract 10 and inc the
next highest byte.  Lots of code but
understandable and may not take that much machine
time.

[snipped adding example...]

When done, just add hex 30 or something to each
byte and you have ascii.

This can be writting very straight foward and
without many loops and such.  The only loops you
need is when you do adjust a byte, go back and
test it again as in the case where the units was
20.

Some will call this coding bad names.  And try to
add shift registers, loops and yes, even exclusive
or's.  But resist them.  The code can be written
quickly, tested  easily and best of all it works
and is easy for other's and even you, to
understand later.

Bill


Jan-Erik Svderholm
S:t Anna Data
tel : +46 121 42161
mob : +46 70 5241690

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


<200212030955.gB39trN15392@d1o1121.telia.com>

See also: www.piclist.com/techref/microchip/devices.htm?key=pic
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) 10 bit binary value to 4 char BCD.

month overview.

new search...