Gast
#7812
Hallo!
Manchmal möchte man eine 10bittige Zahl ins BCD format umwandeln (z.B.
vom AD Wandler).
hier mein code (musste leider ins statement, weil upload nicht geht).
Ich hab in Englisch kommentiert... aber das is ja kein problem.
.include "../avr/includes/4433def.inc"
.DEVICE at90s4433
.def mpr = r16
.def numberL = XL ;number stores the hex value
.def numberH = XH
.def bcd1000 = r1 ;these registers will contain
.def bcd100 = r2 ;the binary coded decimals.
.def bcd10 = r3
.def bcd1 = r4
.org 0x000;
reset:
;set up stack
ldi r16,RAMEND
out SPL, r16 ;SPL=Stack Pointer Low
; we don't need to set SPH,
; because 4433 only has 128bytes of sram
ldi mpr,0x00
mov r1,mpr
mov r2,mpr
mov r3,mpr
mov r4,mpr
ldi numberL, LOW(1012)
ldi numberH, HIGH(1012)
rcall c10bit2bcd
end: rjmp end
;**********************************
;Convert 8bit number in mpr to bcd
;bcd is stored in 3 registers
;this will destroy the number in mpr
;***********************************
c8bit2bcd:
gt100:
cpi mpr,100
brlo gt10 ;jump if less than 100
subi mpr,100
inc bcd100
rjmp gt100;
gt10:
cpi mpr,10
brlo lt10 ;jump if less than 10
subi mpr,10
inc bcd10
rjmp gt10
lt10:
mov bcd1,mpr ;the number is now less than 10
ret
; Add a number to BCDn, with carry
; this macro could be made a routine
; if we use indirect register adressing (0x00-0x1f)
.MACRO addtobcd
ldi mpr,@1
add bcd@0,mpr
mov mpr,bcd@0 ;if bcd<10 then break
cpi mpr,10
brlo lt10_m
mov mpr,bcd@0 ;bcd -= 10
subi mpr,10
mov bcd@0,mpr
inc bcd@00
lt10_m:
.ENDMACRO
c10bit2bcd:
mov mpr,numberL ;convert the lower byte to bcd
rcall c8bit2bcd;
clc ;clear the carry
lsr numberH ;LSB to carry
;if Carry set, add 256 to the number
brcc dont_add_256
addtobcd 1,6
addtobcd 10,5
addtobcd 100,2
dont_add_256:
clc ;clear the carry
lsr numberH ;LSB to carry
;if Carry set, add 512 to the number
brcc dont_add_512
addtobcd 1,2
addtobcd 10,1
addtobcd 100,5
dont_add_512:
ret