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.

HC11 convert BCD to binary

Status
Not open for further replies.

windozeuser

Member
I have an array (4 byte) with each byte being a possible BCD digit 0 to 9

How can I convert this entire byte BCD array into a 16 bit binary number?

I'm treating the entire array as one number, so array+3 = thousands, array+2 = hundreds, array+1 = tens, array = units
 
Yeah I've been reading it over and over, I can't figure out how to write it for the HC11 in assembly tho

The problem is that that chip is over 25 years old (and based on the older 6800) and, I guess, no one here has used it. I did program the 6809 at one time but it's too long ago to remember.

Mike.
 
Code:
******************************************************************
; Set up the Pincode buffer to store the code the user enteres from keypad

  ORG 0 ; Store Pincode in RAM
Pincode   RMB   4   ; Pincode 4 byte buffer

******************************************************************
*	<<< MAIN PROGRAM LOOP >>>
******************************************************************
	ORG	PROG
START

	; Load the Pincode array with the value 1234, with each digit being in it's own byte of the array  
  ldaa #1
  staa Pincode
  ldaa #2
  staa Pincode+1
  ldaa #3
  staa Pincode+2
  ldaa #4
  staa Pincode+3




*******************************************************************
*CONVERT BCD TO A the 16bit binary decimal NUMBER
*******************************************************************

BCDBIN 	  

        
  clra
  clrb ; clears D
  std BINVAL ; Clear BINVAL 
  
thousands
  
  tst Pincode
  beq hundrends
  addd #1000
  dec Pincode
  bne thousands
  
hundrends
  
  tst Pincode+1
  beq tens
  addd #100
  dec Pincode+1
  bne hundrends

tens
  
  tst Pincode+2
  beq processpin
  addd #10
  dec Pincode+2
  bne tens

processpin

; take care of units
  
  addd Pincode+3
  
  std BINVAL ; BINVAL now equal to 16bit binary equiv

	ldx BINVAL
	
	cpx SECRET
	
	beq SUCCESS
	
	
	swi		;end program 

SUCCESS            
	
	ldx	#msg11	;point X to  message ; Display you got the Pin correct!!!

	jsr	OUTSTRG	;output it

This is the code I took from my larger program, basically I am polling a 4x4 keypad and storing a 4 digit "Pin number" the user enters into an array. The problem is each digit is in a separate byte of the array, and I need to convert the entire array into the 16 bit BCD equivalent.

This converts 1234 into 0x08D2, which is wrong, it should be 0x02D2, I can't figure out whats wrong!

Any help would be greatly appreciated!
 
I can't see anything wrong with your code but I did notice that 1234 is 0x04D2.

Mike.
 
I found the problem with the help of the trace command and selecting 0 for all values,

; take care of units

addd Pincode+3


This is adding the address to the D register rather than the value at the address, I tried doing intermediate #Pincode+3 but it's still loading the address instead
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top