piclist 1997\02\18\090305a >
Thread: Binary to Decimal output ???
www.piclist.com/techref/method/math/radixs.htm?key=binary+to+decimal
flavicon
face BY : Andy Kunz email (remove spam text)



At 10:51 PM 2/17/97 -0500, you wrote:
>Greetings,
>
>I am working on a micro controller project using a PIC16C73...  Due to a
>change in end user requirements I need to output a two byte binary counter
>as a signed decimal number in the range of -32767 to +32767.
>
>Has anyone written a routine that I can use for this ???
>
>After all, there is no extra credit for reinventing the wheel...
>
>Bob Segrest

Bob,

The attached was something I posted to TakeThisOuTpicsKILLspamspam.....parallaxinc.com several weeks
ago.  Hope it fills the bill.  It will give you unsigned values.  You simply
need to test for the MS bit being a "1", and if so display a "-", then
2-complement the value, then call the routine below.

Pretty simple mod.

Andy







Return-Path: <owner-picsRemoveMEspamsun.parallaxinc.com>
X-Sender: spam_OUTmontanaRemoveMEspampop.fast.net
Date: Sat, 01 Feb 1997 13:12:23 -0500
To: spampicsTakeThisOuTspam.....parallaxinc.com
From: Andy Kunz <KILLspammontanaKILLspamspamSTOPspamfast.net>
Subject: [PICS] 16-bit rtn for CCS and Assemblers
Sender: @spam@owner-picsspamspamspamparallaxinc.com
Reply-To: STOPspampicsspamBeGonespamEraseMEparallaxinc.com

Hey all, have something to share...

I was rather set back by CCS not having a way to handle printf's of 16-bit
("long" - hah!) integers, so I had to make my own.  The attached routine is
written for CCS C, and has been compiled using version 2.264 and later,
possibly it will work with earlier versions, running on 14-bit cores.  It
was adapted from, I think, a Microchip app note.  But then I'm not really
sure, and it's been changed other times as well...

Anybody needing the same thing for your Parallax or Microchip assemblers
won't have any problem converting it.  If you do, find another line of work <G>

You give the routine a 16-bit value and a pointer to where you want the
result, and it converts it to ASCII for you.  I have it coded to provide
leading zeros.  If you want, you can strip them yourself with a for loop.

Alternatives to this routine are to make Decimal[] a global declaration
(then you can change the prototype and remove the memcpy from the end).  You
can also extend the precision of the thing to support longer (or shorter, to
save time).  Adding code to handle signed values is very simple as well.

Limitations?  Oh, yes!  All the local variables must be org'd to the same
data bank.  Check your .LST file to make sure of where they are before
running your code.

I you have any comments or questions, please feel free to write.  Maybe you
can write spamccsspam_OUTspamspamBeGoneexecpc.com and tell them you'd like to see the 16-bit printf's
handled properly.

I will be making other postings to enhance your CCS C and assembly routines
as I have time.  Next up will probably be an interrupt handler to reduce the
overhead that the default one takes (talk about latency!).

Andy




#define INDIRECT        0x00
#define STATUS          0x03
#define FSR             0x04

#define F               1
#define W               0
#define C               STATUS,0
#define DC              STATUS,1
#define Z               STATUS,2
#define PD              STATUS,3
#define TO              STATUS,4
#define RP0             STATUS,5
#define RP1             STATUS,6
#define IRP             STATUS,7

#define WORD            long int
#define BYTE            int

void Bin2ASCII (WORD Binary, char* DecString)
       {
       BYTE    BitCntr,
               Work;
       char    Decimal[6];     // Only need enough room to handle 16 bits.  For
more
bits, expand this declaration

#asm
       movlw   16              // number of bits to work
       movwf   BitCntr

       clrf    &Decimal[0]     // Clear out the destination
       clrf    &Decimal[1]
       clrf    &Decimal[2]
       clrf    &Decimal[3]
       clrf    &Decimal[4]
       clrf    &Decimal[5]

       bcf     C               // clear carry bit first
loop16:
       rlf     Binary,F        // for more bits coming to convert, just extend
this shifting
appropriately...
       rlf     &Binary+1,F
       rlf     &Decimal[5],F
       rlf     &Decimal[4],F
       rlf     &Decimal[3],F

       decfsz  BitCntr,F
       goto    AdjustDEC
                                       // Convert packed-BCD into ASCII now...
                                       // NOTE:  Only LSN of Decimal[3] is vali
d.  High will always be 0.  This is
                                       //        because we started with a 16-b
it value, max=65535.  If you do more
                                       //        than 16 bits, you need to unde
rstand how the remainder of this
                                       //        works, too.
       movf    &Decimal[3],W
       andlw   0x0f
       iorlw   0x30
       movwf   &Decimal[0]
       swapf   &Decimal[4],W
       andlw   0x0f
       iorlw   0x30
       movwf   &Decimal[1]
       movf    &Decimal[4],W
       andlw   0x0f
       iorlw   0x30
       movwf   &Decimal[2]
       swapf   &Decimal[5],W
       andlw   0x0f
       iorlw   0x30
       movwf   &Decimal[3]
       movf    &Decimal[5],W
       andlw   0x0f
       iorlw   0x30
       movwf   &Decimal[4]
       clrf    &Decimal[5]             // Marks end of C string
       goto    Done

AdjustDEC:
       movlw   &Decimal[5]
       movwf   FSR
       call    AdjustBCD

       movlw   &Decimal[4]
       movwf   FSR
       call    AdjustBCD

       movlw   &Decimal[3]
       movwf   FSR
       call    AdjustBCD
       goto    loop16

AdjustBCD:
       movlw   3                // enter with FSR pointing
       addwf   INDIRECT,W       // to the BCD digit being shifted
       movwf   Work
       btfsc   Work,3
       movwf   INDIRECT
       movlw   0x30
       addwf   INDIRECT,W
       movwf   Work
       btfsc   Work,7
       movwf   INDIRECT
       return
Done:
#endasm
       memcpy (DecString, &Decimal[0],sizeof(Decimal));
       }
==================================================================
Andy Kunz - Montana Design - 409 S 6th St - Phillipsburg, NJ 08865
         Hardware & Software for Industry & R/C Hobbies
       "Go fast, turn right, and keep the wet side down!"
==================================================================


- To subscribe -or- unsubscribe send e-mail to KILLspammajordomospam_OUTspamRemoveMEparallaxinc.com and
- put SUBSCRIBE pics -or- UNSUBSCRIBE pics in the body of the message


==================================================================
Andy Kunz - Montana Design - 409 S 6th St - Phillipsburg, NJ 08865
         Hardware & Software for Industry & R/C Hobbies
       "Go fast, turn right, and keep the wet side down!"
==================================================================
<1.5.4.32.19970218140227.00693edc@pop.fast.net>

See also: www.piclist.com/techref/method/math/radixs.htm?key=binary+to+decimal
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) Binary to Decimal output ???

month overview.

new search...