where : ibrtses embedded
AVR small stuff
ASM
jump table
assumed the CPU supports the required commands
all in CSEG :
;executes indexed (R15) procedure
execproc:
PushY
PushZ
Push R16
SetZPtr (jumptable*2) ; word adressing to byte adressing
lsl R15 ; word adressing to byte adressing
add ZL ; add base and offset
clr R16
adc ZH,R16
lpm
mov YL,R0 ; the lsb of the adress to YL
adiw ZL,1
lpm
mov YH,R0 ; the msb of the adress to YH
CopyYtoZ ; icall works through Z
icall ; execute the selected procedure
pop R16
PopZ
PopY
ret
jumptable:
proc0: .dw proc0
proc1: .dw proc1
proc2: .dw proc2
..
proc0: ; the proc0
..
ret
proc1: ; the proc1
..
ret
proc2: ; the proc2
..
ret
less than 10bit to BCD
assume U1..U3 as R16..R31
; word U2,U3 msb to BCD at Z^ - 2 decimals
; 366 -> 36.60
; 127 -> 12.70
; 37 -> 03.70
; 15 -> 01.50
w2BCD3A:
PushY
Push U1
push u2 ; preserve input
push u3
mov YL,U2
mov YH,U3
clr U1 ; loop counter
ldi u2,0x64 ; 100 per digit
ldi u3,0
_w2l1: inc u1
sub YL,u2
sbc YH,u3
brcc _w2l1 ; subtract until below zero
add YL,u2
adc YH,u3 ; reverse to above zero
dec u1
ori u1,0x30 ; make ASCII
st Z+,u1 ; store
clr U1 ; loop counter
_w2l2: inc u1
sbiw Yl,10 ; 10 per digit
brcc _w2l2 ; subtract until below zero
adiw YL,10 ; reverse to above zero
dec u1
ori u1,0x30 ; make ASCII
st Z+,u1 ; store
ldi u1,'.'
st Z+,u1 ; decimal dot
mov u1,yl ; reminder as ones
ori u1,0x30
st Z+,u1
ldi u1,0x30 ; append ASCII zero
st Z+,u1
pop u3
pop u2
pop U1
popY
ret
send byte - no irq, queueable
Sendbyte: ; u1 is byte
out UDR,u1
nop
nop
sb2: sbis USR,UDRE ; wait till out
rjmp sb2
sb3: sbis USR,TXC ;wait for last bit out
rjmp sb3
ret
compares as from p14-7
assumes the compare as cp, cpi, cp16, cpi16, the last two to be seen
on my macro page, or Appnote 202
cpXX(Rd,Rr)
BRXX as below
signed
compare mnemo cp - para
Rd > Rr BRLT Rr,Rd (swap)
Rd >= Rr BRGE Rd,Rr
Rd = Rr BREQ Rd,Rr
Rd <= Rr BRGE Rr,Rd (swap)
Rd < Rr BRLT Rd,Rr
unsigned
compare mnemo cp - para
Rd > Rr BRLO Rr,Rd (swap)
Rd >= Rr BRCC Rd,Rr
Rd = Rr BREQ Rd,Rr
Rd <= Rr BRSH Rr,Rd (swap)
Rd < Rr BRLO Rd,Rr
eg 'skip if (U1 >= 0x10)' becomes when unsigned byte
cpi U1,0x10
brcc _next
eg 'skip if (U1 <= 0x10)' becomes when unsigned byte
ldi U2,0x10 ; cannot be constant compare - use reg
cp U2,U1
brsh _next
other tricks
addi(byte) = subi(0xFF-byte+1) ; addi does not exist, but subi does
disclaimer
Questions ?
Suggestions?
Feedback ?
sponsored links
AVR
embedded software pages
home
last updated: 1.jan.00
Copyright (99,2001) Ing.Büro R.Tschaggelar