Searching \ for 'Binary to Decimal output ???' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/method/math/radixs.htm?key=binary+to+decimal
Search entire site for: 'Binary to Decimal output ???'.

Truncated match.
PICList Thread
'Binary to Decimal output ???'
1997\02\17@225304 by Bob Segrest

picon face
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

1997\02\18@090300 by eroen Ommering

flavicon
face
Hi Bob,

At 22:51 17/02/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
>

There is a Microchip application note (AN526) with math routines. It also
describes a 16 bit binary to 5 digit BCD conversion. The minus sign must be
generated by yourself of course..

I used the routine as a basis for 8 bit to 3 BCD conversion last week....

AN526 can be found in PDF format on:
http://www.microchip2.com/appnotes/appnotes.htm

If you don't have acces to the web, send me a mail.

Jeroen
+---------------------------------------------------------------------+
| Jeroen Ommering                        Cordless Systems Development |
| Ericsson Business Mobile Networks BV                                |
| Institutenweg 25                                                    |
| P.O. Box 645, 7500 AP  Enschede        Tel:    +31 53 45 05 420     |
| The Netherlands                        Fax:    +31 53 45 05 148     |
|                                                                     |
| E-mail: spam_OUTJeroen.OmmeringTakeThisOuTspamemn.ericsson.se                             |
+---------------------------------------------------------------------+

1997\02\18@090305 by Andy Kunz

flavicon
face
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 .....picsKILLspamspam@spam@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-picsspamKILLspamsun.parallaxinc.com>
X-Sender: .....montanaKILLspamspam.....pop.fast.net
Date: Sat, 01 Feb 1997 13:12:23 -0500
To: EraseMEpicsspam_OUTspamTakeThisOuTparallaxinc.com
From: Andy Kunz <montanaspamspam_OUTfast.net>
Subject: [PICS] 16-bit rtn for CCS and Assemblers
Sender: @spam@owner-picsKILLspamspamparallaxinc.com
Reply-To: KILLspampicsKILLspamspamparallaxinc.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 RemoveMEccsTakeThisOuTspamexecpc.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 spamBeGonemajordomospamBeGonespamparallaxinc.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!"
==================================================================

1997\02\18@094722 by Bob Fehrenbach

picon face
Bob Segrest <TakeThisOuTbobslanEraseMEspamspam_OUTEROLS.COM> wrote:
>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.

Bob,

Here is a routine posted by John Payson that does tht job.  First, test
the sign of the number and save the sign.  Convert to positive, if negative
by complementing and adding 1.  Then use:


  (NB: 49 program memory locations.  Executes in
  153 to 198 clock cycles (worst case I could find)  BF)




  cblock

  NumH
  NumL
  TenK
  Thou
  Hund
  Tens
  Ones

  endc
                               ; Takes number in NumH:NumL
                               ; Returns decimal in TenK:Thou:Hund:Tens:Ones
convert:
  swapf   NumH,w
  iorlw   h'f0'
  movwf   Thou
  addwf   Thou,f
  addlw   h'E2'
  movwf   Hund
  addlw   h'32'
  movwf   Ones

  movf    NumH,w
  andlw   h'0F'
  addwf   Hund,f
  addwf   Hund,f
  addwf   Ones,f
  addlw   h'E9'
  movwf   Tens
  addwf   Tens,f
  addwf   Tens,f

  swapf   NumL,w
  andlw   h'0F'
  addwf   Tens,f
  addwf   Ones,f

  rlf     Tens,f
  rlf     Ones,f
  comf    Ones,f
  rlf     Ones,f

  movf    NumL,w
  andlw   h'0F'
  addwf   Ones,f
  rlf     Thou,f

  movlw   h'07'
  movwf   TenK

                       ; At this point, the original number is
                       ; equal to TenK*10000+Thou*1000+Hund*100+Tens*10+Ones
                       ; if those entities are regarded as two's compliment
                       ; binary.  To be precise, all of them are negative
                       ; except TenK.  Now the number needs to be normal-
                       ; ized, but this can all be done with simple byte
                       ; arithmetic.

  movlw   h'0A'                             ; Ten
Lb1:
  addwf   Ones,f
  decf    Tens,f
  btfss   3,0
   goto   Lb1
Lb2:
  addwf   Tens,f
  decf    Hund,f
  btfss   3,0
   goto   Lb2
Lb3:
  addwf   Hund,f
  decf    Thou,f
  btfss   3,0
   goto   Lb3
Lb4:
  addwf   Thou,f
  decf    TenK,f
  btfss   3,0
   goto   Lb4

  retlw   0


--
Bob Fehrenbach    Wauwatosa, WI     RemoveMEbfehrenbspamTakeThisOuTexecpc.com

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