.

INCLUDES RFID interfacing with avr GPS interfacing with avr RF Module interfacing with avr Stepper Motor With AVR

Tuesday, December 20, 2011

Numbers

As you read through these pages you might want to have a look at an ascii table. You can find one in the Banner frame ("quick links").
Conversions are very important for user interaction: If a register has the value 0x30 its corresponding ascii character is '0'. But if you want it to be displayed as '48' (0x30 is 48), you need to convert the number. In the case of '0' this is not that important, but 0xFF is displayed as a block. And '48' is better than '0' if you're displaying a temperature...
Some protocols, such as Ymodem, also use strings of values we have to convert first before we can perform calculations on them: Ymodem sends a file size of 512 bytes as '512'. An AVR has to convert this from ascii coded decimal to 16bit int first before it knows what '512' means.
Some number formats you should have in mind when doing calculations:
128
'128'

0x30

0b11001010
'11001010'
; normal decimal value
; ascii coded decimal. In this case you need three bytes ('1', '2' and
; '8') to store that number.
; hex value
;
; binary value
; ascii coded binary
It's up to you which number format you use for a specific task. Ascii coded hex is quite often used for debugging purposes, because the numbers are all of the same size (number of characters needed) and becase the conversion always takes the same number of cpu cycles and doesn't require much space. Ascii coded decimal is better for things like temperatures or rpm of a motor. Ascii coded binary is good for displaying flag registers (SREG, Interrupt flag registers and so on).
I'll show you ways to convert numbers in both directions: From int to something you can display and back.

Commonly Used Number Formats
The ALU of an AVR only knows the integer number, unsigned as well as signed, and only 8 bits wide. The 8 bit limit is not as bad, as we can still use the carry bit to make 16-, 24- and 32 bit operations possible.
Converting numbers from one format to another is not as easy and requires the person writing the code to understand the number formats first.
All conversions explained on these pages have the integer as one "end" (source or result). This is the number the AVR actually deals with.
Other formats use ASCII characters or the fact that a digit (which has a range of 0 to 9) only uses one nibble of a byte.
HEX format:
In AVR Assembler (and on this site) HEX numbers are written with the "$"-sign or "0x" at the beginning:
$10 is equal to 16 and 0x20 is equal to 32.
The Hex format splits the 8 bits of a byte into "nibbles" of 4 bits (the high nibble and the low nibble) and displays them with a number or character:

Nibble value:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Hex:
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
If an 8-bit number is sent or printed as ASCII Coded Hex, the number is split into high nibble and low nibble (in the case of ox20 these are 2 and 0). Then the nibbles are converted to their ASCII representative: 0x32 for 2 and 0x30 for 0. These values can be printed on screen. The values from the table above can not be preinted on the screen: In the ASCII table these are either not defined or control characters. A won't be displayed as 'A'.
Binary format:
The binary format should be quite clear: 0b00001000 is equal to 8, 0b00011000 is equal to 24. Easy. When a number comes as ASCII coded binary, the 1s and 0s are sent as their ASCII representative, 0x30 and 0x31, and thus have to be converted before they are "real" bits. The binary format also requires bit shifting for the conversion.
Binary Coded Decimal (BCD) format:
Binary coded decimal is very handy for storing two digits (0..9) in one byte without much coding. The digits are directly written to a byte nibble.
0x22 means that the low nibble contains the number 2 and the high nibble contains the number 2 as well. A consequence of this is that a byte can only hold value in the range of 0 to 99: The values 10 to 15 (A to F in Hex format) are not allowed in BCD format.
This format can for example be written to a port which has a 7447 connected to it. This IC is a 7-segment LED driver which converts this format so that the segments of the LED display show the number of the nibble.
The Ascii Table
Very often you'll need to convert ascii to hex or decimal numbers and back. An ascii table is THE tool you'll need for that. Here is one.
If you need the hex value of 'H', look for H. It's in column $4x and row $x8. 'H' = $48 or 0x48. Other way around: You need to know what 0x69 is when shown as a character. Column $6x, row $x9: 'i'
The "ctrl" column contains the control characters in short form. The real name can be found further down on this page in a seperate table.
We're working on a printable version of this... Most probably we'll have to divide the table by three or so and fill three pages. One won't be enough for all this...


$0x
$1x
$2x
$3x
$4x
$5x
$6x
$7x
$8x
$9x
$Ax
$Bx
$Cx
$Dx
$Ex
$Fx
dec
char
ctrl
dec
char
ctrl
dec
char
dec
char
dec
char
dec
char
dec
char
dec
char
dec
char
dec
char
dec
char
dec
char
dec
char
dec
char
dec
char
dec
char
$x0
000

NUL
016

DLE
032
spc
048
0
064
@
080
P
096
`
112
p
128
144

160

176
°
192
À
208
Ð
224
à
240
ð
$x1
001

SOH
017

DC1
033
!
049
1
065
A
081
Q
097
a
113
q
129

145
161
¡
177
±
193
Á
209
Ñ
225
á
241
ñ
$x2
002

STX
018

DC2
034
"
050
2
066
B
082
R
098
b
114
r
130
146
162
¢
178
²
194
Â
210
Ò
226
â
242
ò
$x3
003

ETX
019

DC3
035
#
051
3
067
C
083
S
099
c
115
s
131
ƒ
147
163
£
179
³
195
Ã
211
Ó
227
ã
243
ó
$x4
004

EOT
020

DC4
036
$
052
4
068
D
084
T
100
d
116
t
132
148
164
¤
180
´
196
Ä
212
Ô
228
ä
244
ô
$x5
005

ENQ
021

NAK
037
%
053
5
069
E
085
U
101
e
117
u
133
149
165
¥
181
µ
197
Å
213
Õ
229
å
245
õ
$x6
006

ACK
022

SYN
038
&
054
6
070
F
086
V
102
f
118
v
134
150
166
¦
182
198
Æ
214
Ö
230
æ
246
ö
$x7
007

BEL
023

ETB
039
'
055
7
071
G
087
W
103
g
119
w
135
151
167
§
183
·
199
Ç
215
×
231
ç
247
÷
$x8
008

BS
024

CAN
040
(
056
8
072
H
088
X
104
h
120
x
136
ˆ
152
˜
168
¨
184
¸
200
È
216
Ø
232
è
248
ø
$x9
009

HT
025

EM
041
)
057
9
073
I
089
Y
105
i
121
y
137
153
169
©
185
¹
201
É
217
Ù
233
é
249
ù
$xA
010

LF
026

SUB
042
*
058
:
074
J
090
Z
106
j
122
z
138
Š
154
š
170
ª
186
º
202
Ê
218
Ú
234
ê
250
ú
$xB
011

VT
027

ESC
043
+
059
;
075
K
091
[
107
k
123
{
139
155
171
«
187
»
203
Ë
219
Û
235
ë
251
û
$xC
012

FF
028

FS
044
,
060
<
076
L
092
\
108
l
124
|
140
Œ
156
œ
172
¬
188
¼
204
Ì
220
Ü
236
ì
252
ü
$xD
013

CR
029

GS
045
-
061
=
077
M
093
]
109
m
125
}
141

157

173
­

189
½
205
Í
221
Ý
237
í
253
ý
$xE
014

SO
030

RS
046
.
062
>
078
N
094
^
110
n
126
~
142
Ž
158
ž
174
®
190
¾
206
Î
222
Þ
238
î
254
þ
$xF
015

SI
031

US
047
/
063
?
079
O
095
_
111
o
127

143

159
Ÿ
175
¯
191
¿
207
Ï
223
ß
239
ï
255
ÿ
0x20 ('spc") means space, of course.
Here is the control character table.
SOH - Start Of Header DLE - Data Link Escape
STX - Start Of teXt DC1 - Device Control 1
ETX - End Of teXt DC2 - Device Control 2
EOT - End Of Transmission DC3 - Device Control 3
ENQ - ENQuiry DC4 - Device Control 4
ACK - ACKnowledge NAK - Negative AcKnowledge
BEL - BELl SYN - SYNchronous idle
BS - BackSpace ETB - End of Transmission Block
HT - Horizontal Tabulation CAN - CANcel
LF - Line Feed EM - End of Medium
VT - Vertical Tabulation SUB - SUBstitute
FF - Form Feed ESC - ESCape
CR - Carriage Return FS - File Separator
SO - Shift Out GS - MainForm.Group Separator
SI - Shift In RS - Record Separator
US - Unit Separator



No comments:

Post a Comment