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.

How to quickly add and subtract in AVR

Status
Not open for further replies.

Noman Yousaf

Full Member level 4
Joined
Nov 19, 2003
Messages
208
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Lahore Pakistan
Activity points
1,763
in 8051 series there is very easy to add 2 digits in BCD. "DA" instruction is used for this. now in AVR i want to add 10 digits with 5 digits. how that is possible in very low time, just like using DA in 8051. but unluckily there is not any instruction in AVR instruction set. please help me.
i know 1 method.
by using "ADC", do all in "HEX" then convert it in BCD. but here is one more problem, there is not division in AVR so division takes long time by subtracting.
 

Re: AVR addition problem

You can use the Half Carry to do this. Here an simple code:

Code:
;R16 and R17 are bcd digits

	add	R16,R17		;Binary adition betwen R16 and R17
	brhc	No_HalfCarry
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;BCD Adjust
	andi	R16,0xF0	;Clear the low nibble
	ldi	R18,0x10	;
	add	R16,R18		;Add 1 to high nibble
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
No_HalfCarry:
	;code
 

Re: AVR addition problem

Hi projetos_afg
i m sorry but it doesn't work. i made a routine. and wrote "ret" after "no_halfcarry:" and loaded 2 bcd values in r16 and r17 and ran this routine, the result was still in HEX, not BCD. please check it yourself and give me the right solution. thanks
 

Re: AVR addition problem

I'm sorry, I forgot the addiction by 6:

Code:
;R16 and R17 are bcd digits

	add	R16,R17		;Binary adition betwen R16 and R17
	brhc	No_HalfCarry
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;BCD Adjust
	ldi	R18,0x06	;
	add	R16,R18		;Add 1 to high nibble
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
No_HalfCarry:
	;code
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top