Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

converting binary and hexa to decimal for display

Status
Not open for further replies.

n ranjan

New Member
hi , i am a novoice to micro controllers. could someone suggest the logic and the way to convert 8 bit positive binary number to a BCD for display in 7 segment dispaly. how to implent the same in micro controlers. i know how to convert it in paper but do not know how to implement in micro controller
 
n ranjan said:
hi , i am a novoice to micro controllers. could someone suggest the logic and the way to convert 8 bit positive binary number to a BCD for display in 7 segment dispaly. how to implent the same in micro controlers. i know how to convert it in paper but do not know how to implement in micro controller

Probably the easiest (and fastest) way to display HEX is to use a lookup table, it only has to be 16 words long. Treat the byte as two separate nibbles, and use the value in each nibble to access the lookup table. For the low nibble you simply AND the value with 0x0F to remove the high nibble, for the high nibble either shift four times, or swap the high and low nibbles (which a PIC lets you do), then AND with 0x0F as before.

There's PIC code in my tutorials, and I've actually posted the code section in another thread as well.
 
hello mr godwin

thanks for yr reply. from yr reply i understand the assumption is each byte has 2 digit hex numbers.the logic is to get the lower digit by ANDing 0xOF and shifting right by 4 positions to get higher hex digit. by doing this each digit will hv 16 patterns which has to be translated to a 7 segment display. yes i understood. my querry is if the number is in binary form to the base 2 how do u convert it to a decimal digit.

for eg i can easily write programme in C to to seperate 108 in to 3 digits i.e i can output first 8, then 0, then 1. suppose if this 108 is represented in binary equivalent as 01101100 in a byte how do i get 8,0,1 in BCD so that i can multiplex and show it in LED.
 
Do you mean a binary number stored as an 8 digit string?, or just a single byte you want to display as decimal?. Presumably, if I'm understanding you correctly, you want to convert a single byte to decimal (not BCD) to display on 7 segment LED's.

What sort of processor are you using?.
 
I cut this out of a voltmeter project i made recently. It converts the binairy value in BinDisplay to BCD in Val1, Val2, Val3. Easy to modify
Don't mind the comments it's in dutch, i don't have time to translate

Code:
Bin2BCD

		CLRF    Val1 
        SWAPF   BinDisplay, W			;w  = A0*16+A1
        ADDWF   BinDisplay, W 			;w  = A0+A1
        ANDLW   b'00001111' 			;w  = A0+A1 % 16

        SKPNDC 							;if A0+A1 > 16
        ADDLW  	0x16 					;  w  += 16

        SKPNDC 							;if w % 16 > 10
        ADDLW 	0x06 					;  w  += 6

        ADDLW   0x06 					;w  += 6

        SKPDC 							;if w < 10
        ADDLW  	-0x06 					;  w  -= 6

        BTFSC   BinDisplay,4 
        ADDLW   0x16 - 1 + 0x6 

        SKPDC 
        ADDLW  -0x06 

        BTFSC   BinDisplay, 5 
        ADDLW  	0x30 

        BTFSC   BinDisplay, 6 
        ADDLW  	0x60 

        BTFSC   BinDisplay, 7 
        ADDLW  	0x20 

        ADDLW   0x60 
        RLF     Val1, F 

        BTFSS   Val1, 0 
        ADDLW   -0x60 

        MOVWF   Val3 
        BTFSC   BinDisplay, 7 
        INCF   	Val1, F

		;Tientallen EN eenheden zitten nu in Val3 - omzetten en tientallen in Val2 - eenheden in Val3
		SWAPF	Val3, W					;swap nibbles naar W - tientallen zitten nu aan LSB zijde in W
		ANDLW	b'00001111'				;verwijder de eenheden
		MOVWF	Val2					;en sla op in Val2
		
		MOVLW	b'00001111'
		ANDWF	Val3, F					;en verwijder de tientallen uit Val3
		RETURN
 
converting binary to decimal

dear mr godwin

yes i want to convert binary to decimal. to be precise suppose if i hv
00011111 i must be able to dispaly 31 in 7segment LED.

i am using pic16f877

regards

n ranjan
 
Re: converting binary to decimal

n ranjan said:
dear mr godwin

yes i want to convert binary to decimal. to be precise suppose if i hv
00011111 i must be able to dispaly 31 in 7segment LED.

I've sent you a reply to your PM, basically the page you need on the PICLIST.
 
This is how I did it in a programme I wrote some years ago.

In the actual programme I inserted the binary number to be converted into "units" not d'217' of course.

Code:
	call	bin2bcd		;convert binary to BCD

;sub routine

bin2bcd		;convert binary to BCD
	movlw	d'217'	;value to be converted for test purposes
	movwf	units
	clrf	tens
	clrf	hundreds

uaa	movlw	d'100'
	subwf	units, w
	btfss	c_bit		;STATUS, C is defined as c_bit
	goto	uab		;w < 0

	movwf	units	;w >= 0
	incf	hundreds	;increment the hundreds counter
	goto	uaa

uab	movlw	d'10'
	subwf	units, w
	btfss	c_bit
	return			;w < 0. The remainder in units is the units digit,

	movwf	units	;w >= 0
	incf	tens	;increment the tens counter
	goto	uab
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top