where : ibrtses embedded
AVR turbo pascal strings
The string type introduced by turbo pascal offers too many advantages
to be overlooked. Definition :
- array of byte/character with 0th byte as length
- up to 255 bytes following
Knowing the length in advance makes it simple to copy them.
A separate length byte allows having the 0x00 being part of the string.
Therefore it is also useable as buffer for communication
Define them as :
.DSEG
mystring .byte 16 ; preset a length including the size
copy string part
; Z^ to Y^, R17 bytes
copystr:
pushY
pushZ
cy1: ld R16,Z+
st Y+,R16
dec R17
brne cy1
popZ
popY
ret
send string to UART - no IRQ
; sends a string Z^ without the length byte, no interrupt
SendString:
push R17
push R18
ld R17,Z+
ss1: ld R18,Z+
out UDR,R18
nop
nop
ss2: sbis USR,UDRE ; wait till out
rjmp ss2
dec R17
brne ss1
ss3: sbis USR,TXC ;wait for last bit out
rjmp ss3
pop R18
pop R17
ret
append string
; string Z^ gets string X^appended Z^:=Z^+X^
appendstr:
pushX
pushY
pushZ
copyZtoY
push R17
push R18
push R19
push R20
ld R17,Z ;R17:=length(Z)
mov R20,R17
inc R17
add YL,R17 ; Y:next after last
ld R17,X+ ;R17:=length(X)
mov R19,R17
tst R17
breq fienf ; exit if none
fi4: ld R18,X+
st Y+,R18
inc R20
cpi R20,LCD_Width
brne fi5 ;test overlength
ldi R17,LCD_Width
st Z,R17
rjmp fienf
fi5:
dec R17
brne fi4
;adj size
ld R17,Z
add R17,R19
st Z,R17
fienf:
pop R20
pop R19
pop R18
pop R17
popZ
popY
popX
ret
byte to hexstring
; byte R17 to hexstring Z^
b2hexstr:
pushY
copyZtoY
push R18
push R19
ldi R18,2
st Y+,R18
mov R18,R17
andi R18,0x0F
ori R18,0x30
cpi R18,0x3A
brlo b2hs1
ldi R19,7
add R18,R19
b2hs1: st Y+,R18
mov R18,R17
swap R18
andi R18,0x0F
ori R18,0x30
cpi R18,0x3A
brlo b2hs2
ldi U3,7
add R18,R19
b2hs2: st Y+,R18
pop R19
pop R18
popY
ret
word to hexstring
; word (lsb,msb) R18,R19(msb) to hex string Z^
w2hexstr:
pushY
copyZtoY
push R20
push R21
ldi R20,4
st Y+,R20
mov R20,R19
swap R20
andi R20,0x0F
ori R20,0x30
cpi R20,0x3A
brlo w2hs1
ldi R21,7
add R20,R21
w2hs1: st Y+,R20
mov R20,R19
andi R20,0x0F
ori R20,0x30
cpi R20,0x3A
brlo w2hs2
ldi R21,7
add R20,R21
w2hs2: st Y+,R20
mov R20,R18
swap R20
andi R20,0x0F
ori R20,0x30
cpi R20,0x3A
brlo w2hs3
ldi R21,7
add R20,R21
w2hs3: st Y+,R20
mov R20,R18
andi R20,0x0F
ori R20,0x30
cpi R20,0x3A
brlo w2hs4
ldi R21,7
add R20,R21
w2hs4: st Y+,R20
pop R21
pop R20
popY
ret
notes
the macros used are described here
usage:
SetZPtr mystring
ldi R17,0x33
rcall b2hexstr
rcall Sendstring
sends '33' = 0x33 0x33 to the UART
disclaimer
Questions ?
Suggestions?
Feedback ?
sponsored links
AVR
embedded software pages
home
last updated: 25.dec.99
Copyright (99,2001) Ing.Büro R.Tschaggelar