Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

10 Bit Binary to 4 Digit BCD????????

Status
Not open for further replies.

chevymn1964

Full Member level 2
Joined
Apr 1, 2006
Messages
120
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,296
Location
Santa Rosa, Ca.
Activity points
2,551
10 bit binary to 16 bit bcd

Hey Everyone! I found a routine on the web that converts 10 bit binary to 4 digit BCD But I think there is a typo because there is a goto b2d2 and the b2d2 isnt listed on the code...... Anyone have an idea where the b2d2 should go???? Any other 10bit to 4 digit bcd routines???? I'm trying to display voltage on an lcd with a pic 16f676
;======= Bin2Dec10G.asm ============= Sep 04 =========
; Test of 10b binary to 4 digit conversion as
; based on division by powers of 10.
;
; E. A., Grens
;
; For most PICs - here for 16F628 with 4 MHz
; internal oscillator.
;
; Average processor cycles for execution of
; subroutine over the input range 0x000 to 0x3FF
; is 88. No variables required except the
; input blh, blo and the out BCD digits.
; Subroutine length 38.
;======================================================

list p=16f628
radix dec
#include
__config 0x3D30


;-------Set Up RAM Variables---------------------------
blo equ 0x20 ;lsbyte of 10b binary
bhi equ 0x21 ;msb
d4 equ 0x23 ;msdigit
d3 equ 0x24
d2 equ 0x25
d1 equ 0x26 ;lsd
;-------Program Code--------------------------------

org 0x00 ;Effective Reset Vector
;
nop
goto Start ;Jump by interrupt
;
Start
movlw 0x07
movwf CMCON ;Digital I/O
movlw 0x03 ;Test input
movwf bhi
movlw 0xFF
movwf blo
movlw 0x04
call Bin2decg
Hold
goto Hold ;Finished

Bin2decg ;10b binaey to BCD
clrf d1
clrf d2
clrf d3
clrf d4
clrc
rrf bhi,f ;N = 4*Q + R
rrf blo,f ;blo = Q
rrf d1,f ;d1 = R as temp, R25
goto B2d2 ;repeat
B2d3
addwf blo,f ;get remainder
; after /100, C = 0
rlf d1,f ;*4 and add R back in
rlf blo,f
rlf d1,f
rlf blo,f ;4*blo + R = N -
; 1000*d4 - 100*d3
movlw 10
B2d4
subwf blo,f ;blo = N - 1000*d4 -
; 100*d3 - 10*d2
skpc
goto B2d5 ;blo10
goto B2d4
B2d5
addwf blo,f ;put last 10 back, C=0
movf blo,w
movwf d1
return

end
 

4 digit post tester instructions

Can this be converted to all assembly??? I think this is what Im looking for:

- based on the 16 bit to 5 digit conversion written by John Payson
& explained by Scott Dattalo
- this version adds the extra bit so that the 5 digit display can
read to 99,999
- want to express the b_i's in terms of the a_i's for the equations:
N = a4*16^4 + a3*16^3 + a2*16^2 + a1*16 + a0
N = b4*10^4 + b3*10^3 + b2*10^2 + b1*10 + b0
the solution coded below:
b0 = a0 - (4 * (a4 + a3 + a2 + a1)) - 20
b1 = (4 * a4) + (6 * a2) + (2 * a1) - 198
b2 = (5 * a4) + a3 + (2 * a2) - 140
b3 = (5 * a4) + (4 * a3) - 144
b4 = (6 * a4) + 16
- written in CCS C
- since ram was plentiful, I used a 3 byte number - only one bit
of the extra byte is actually required. I also used three bytes
of scratchpad ram to increase the efficiency of the compiled code.
The equations can be coded in C exactly as shown above, but that
doesn't take advantage of common terms & progressive multiplies.
- even with just the one extra bit, the incoming number can exceed
99,999, so it needs to be checked. This code doesn't show that.
- CCS C is limited to 16 bit as the largest integer data type. To
use a 24 bit number, I use an array to make sure the bytes are
consecutive. This function operates on the array as a global,
because I think that is more efficient than passing arrays to
the function.
- the code could be quite a bit smaller - in John's original, the
portion of the code that isolated the nibbles and calculated the
digits took 32 instructions. If I code that solution in C, using
two scratchpad bytes to improve things, it compiles to 64 instructions.
This version compiles to 85 instructions, so I'm sure an asm whiz
could show quite an improvement. The C version is still much faster
than the the original way I did this, and is adequate for what I need.
*/

unsigned int TenK, Thou, Hund, Tens, Ones;
unsigned int num[3];

void BIN2DEC(void)
{
// operates on global variables:
// set num[0]-[3] (lo-hi) before calling
// returns with TenK...Ones set from 0-9

unsigned int a0, a1, a2, a3, a4;
unsigned int t1, t2, t3; // scratchpad variables

// isolate the 4 bit nibbles
// for the purposes of this routine, a4 can be only 0 or 1
a4 = num[2];
a3 = num[1] / 16;
a2 = num[1] & 0b00001111;
a1 = num[0] / 16;
a0 = num[0] & 0b00001111;

// calculate the decimal digits (the b_i's are expressed here
// as TenK...Ones, as in the original)
// all are negative, except TenK
t1 = 4 * a4;
Ones = a0 - t1 - 20;
t2 = 2 * a1;
Tens = t1 + t2 - 198;
t3 = 2 * a2;
Hund = a3 + t3 - 140;
t3 = t3 * 2; // now = 4 * a2
t2 = t2 * 2; // now = 4 * a1
Ones = Ones - t3 - t2;
t3 = t3 + a2 + a2; // now = 6 * a2
Tens = Tens + t3; // Tens done
t1 = t1 + a4; // now = 5 * a4
Hund = Hund + t1;
t3 = 4 * a3;
Thou = t1 + t3 - 144; // Thou done
Ones = Ones - t3; // Ones done
TenK = t1 + a4 + 16; // Tenk done

// "normalize" the digits - this asm code was
// copied directly from the original
#asm
movlw 0x0A
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
#endasm
}
 

bcd 10 digit display

upload the manual of scott mackenzie
 

bin2dec hunds tens ones

Maybe it helps you.


<pre>
unsigned chaê rez[3];


void Bin_BCD_18(int val)
{

rez[0] = (val%1000) / 100;
rez[1] = (val%100) / 10;
rez[2] = val % 10;

}

</pre>
 

int to bcd ccs

hi,

following program is for PIC16F877 for binary to 4 digit bcd


void bin2bcd1(void)
{
unsigned char a1,a2,a3,b=10;

digi1=value%b;
a1=value/10;

digi2=a1%b;
a2=a1/b;

digi3=a2%b;
a3=a2/b;

digi4=a3%b;


}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top