PIC elado
VFX
info at vfx.hu
Mon Apr 28 17:50:50 CEST 2003
Hali!
> Kezd itt atmenni anyazasba a dolog. Fiuk probaljuk meg kulturalt keretek kozott
> megvitatni ezt a kerdest.
Jaja csillapitsuk egy kicsit a vitat :)
> Es vegul, hogy ne csak kisarkitott peldakat hozzunk, lassunk egy eleg
> gyakori muveletet egy 16 bites binaris szam BCD-ve alakitasat:
> Ez PIC-en igy nez ki (32 word a programmemoriaban, 6 fileregiszter,
> es 406 orajel hosszu):
>
> cblock
> AARG8:0, AARG16:0, AARG24:0
> AARGB0, AARGB1, AARGB2
>
> BARG8:0, BARG16:0
> BARGB0, BARGB1
>
> LOOPCOUNT
> endc
>
> ; Input : BARG16 ; 16 bit binary number
> ; Output: AARGB0:AARGB1:AARGB2 ; (AARG24) converted number (5 digit)
>
> bin2bcd16
> movlw 16
> movwf LOOPCOUNT
> clrf AARGB0
> clrf AARGB1
> clrf AARGB2
> goto _startb16
>
> _adjdec16
> movlw 0x33
> addwf AARGB0,f
> addwf AARGB1,f
> addwf AARGB2,f
>
> movlw 0x03
> btfss AARGB0,3
> subwf AARGB0,f
> btfss AARGB1,3
> subwf AARGB1,f
> btfss AARGB2,3
> subwf AARGB2,f
>
> movlw 0x30
> btfss AARGB0,7
> subwf AARGB0,f
> btfss AARGB1,7
> subwf AARGB1,f
> btfss AARGB2,7
> subwf AARGB2,f
>
> _startb16
> rlf BARGB1,f
> rlf BARGB0,f
> rlf AARGB2,f
> rlf AARGB1,f
> rlf AARGB0,f
> decfsz LOOPCOUNT,f
> goto _adjdec16
> return
>
Ez AVR-re 32 bites szam bin to ascii konverter, de meg nem mondom hany
csiklus :))
Sose szamoltam ki. Debuggert meg nem hasznalok igy nem tudom
kiszamoltatni.
;*******************************************************************
;** Fairly fast, & code eff. ltoa((long)num) and itoa((int)num) **
;** Blanks Leading Zeros **
;** Acc1 is destroyed, you must preserv it yourself **
;* enter 'T' == 0, then UNSIGNED Print else, SIGNED Decimal Print *
;* R14 = 0 nincs bevezeto nulla
ITOA: clr R18 ; assume 16 bit Pos num
clr R19
tst R17 ; test bit 15
brpl SLTOA
ser R18 ; set the MSB to 0xff
ser R19
;* Fall through to 32 bit Signed LTOA *
SLTOA: set ; Signed Print Decimal
rjmp LTOA
ULTOA: clt ; Un-Signed Print Decimal
LTOA: ldi XL,low(ASCIIBuf) ;Ascii Buffer Ptr
ldi XH,high(ASCIIBuf)
brtc LTOA1 ; signed/unsigned flag from caller
tst R19 ; test Sign bit 31
brpl LTOA1
rcall NEGMANT1 ; 2's comp to Pos num
ldi temp,'-'
st X+,temp ; store the Sign, Buf pointer++
LTOA1: clr count
clr temp
or temp,R16
or temp,R17
or temp,R18
or temp,R19
; breq DECDIGX ;!!!!!!! Full 9 karakteres kiiras kell
ldi count,9 ; 9 rows of 4 columns ea.
ldi ZL,low(I1E9*2) ; Decimal Table below
ldi ZH,high(I1E9*2)
clt ; leading zero suppression flag
LTOAloop: ; load each sequential table group
in R0,RAMPZ
push R0
ldi R20,1
out RAMPZ,R20
elpm R20,Z+
elpm R21,Z+
elpm R22,Z+
elpm R23,Z+
clr temp ; now test Acc1 w/table in Acc2.
pop R0
out RAMPZ,R0
DECDIG1: cp R16,R20
cpc R17,R21
cpc R18,R22
cpc R19,R23
brlo DECDIG2 ; can we subtract without borrow?
sub R16,R20 ; yes
sbc R17,R21
sbc R18,R22
sbc R19,R23
inc temp ; inc this each time we can subtract.
rjmp DECDIG1 ; keep subtracting until underflow!
DECDIG2:
ori temp,'0' ; convert to Ascii char
; brts DECDIG3 ; Blank leading Zeros
; cpi temp,'0' ; does temp == '0'?
; breq DECDIG4 ; yes, don't Print it until firs non-zero
char.
; set ; signal first non-zero digit.
;ezeket kivettem mert kell a leading 0
DECDIG3: st X+,temp ; store the dig in the SRAM Buffer, and inc
'y' reg
DECDIG4: dec count ; have we processed all tables yet?
brne LTOAloop
DECDIGX: ori R16,'0' ; print the last '0-9' residual digit!
st X+,R16 ; save it.
; * count = 0 here, use it to add ending 'NULL' char. *
st X+,count
ret ; return to caller!
;** Number to ASCII convertion Table **
I1E9: .db 0x00,0xca,0x9a,0x3b ; 1,000,000,000
I1E8: .db 0x00,0xe1,0xf5,0x05 ; 100,000,000
I1E7: .db 0x80,0x96,0x98,0 ; 10,000,000
I1E6: .db 0x40,0x42,0x0f,0 ; 1,000,000
I1E5: .db 0xa0,0x86,0x01,0 ; 100,000
I1E4: .db 0x10,0x27,0x00,0 ; 10,000
I1E3: .db 0xe8,0x03,0x00,0 ; 1,000
I1E2: .db 0x64,0x00,0x00,0 ; 100
I1E1: .db 0x0a,0x00,0x00,0 ; 10
More information about the Elektro
mailing list