Code Converters

Download Report

Transcript Code Converters

Codes and
Code Converters
Lecture L6.12
Section 6.5
Codes and Code Converters
•
•
•
•
ASCII
Binary-to-BCD Converters
Gray Code
Bar Codes
Standard ASCII Codes
Table 9.2
Standard ASCII codes
Dec
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
0
16
32
48
64
80
96
112
0
1
2
3
4
5
6
7
NUL
SOH
STX
ETX
EOT
ENQ
ACK
BEL
BS
HT
LF
VT
FF
CR
SO
SI
DLE
DC1
DC2
DC3
DC4
NAK
SYN
ETB
CAN
EM
SUB
ESC
FS
GS
RS
US
blank
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?
@
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
[
\
]
^
_
!
"
#
$
%
&
'
(
)
*
+
,
.
/
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
{
|
}
~
DEL
ASCII Codes
Dec
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Hx
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
10
11
12
13
14
15
16
17
18
19
1A
1B
1C
1D
1E
1F
Oct
000
001
002
003
004
005
006
007
010
011
012
013
014
015
016
017
020
021
022
023
024
025
026
027
030
031
032
033
034
035
036
037
Char
NUL
SOH
STX
ETX
EOT
ENQ
ACK
BEL
BS
TAB
LF
VT
FF
CR
SO
SI
DLE
DC1
DC2
DC3
DC4
NAK
SYN
ETB
CAN
EM
SUB
ESC
FS
GS
RS
US
Dec
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
Hx
20
21
22
23
24
25
26
27
28
29
2A
2B
2C
2D
2E
2F
30
31
32
33
34
35
36
37
38
39
3A
3B
3C
3D
3E
3F
Oct
040
041
042
043
044
045
046
047
050
051
052
053
054
055
056
057
060
061
062
063
064
065
066
067
070
071
072
073
074
075
076
077
Html Chr Dec Hx Oct Html Chr Dec Hx Oct Html Chr
&#32; Space 64 40 100 &#64; @ 96 60 140 &#96;
`
&#33;
!
65 41 101 &#65; A 97 61 141 &#97;
a
&#34;
“
66 42 102 &#66; B 98 62 142 &#98;
b
&#35;
#
67 43 103 &#67; C 99 63 143 &#99;
c
&#36;
$
68 44 104 &#68; D 100 64 144 &#100; d
&#37;
%
69 45 105 &#69; E 101 65 145 &#101; e
&#38;
&
70 46 106 &#70; F 102 66 146 &#102; f
&#39;
‘
71 47 107 &#71; G 103 67 147 &#103; g
&#40;
(
72 48 110 &#72; H 104 68 150 &#104; h
&#41;
)
73 49 111 &#73; I 105 69 151 &#105; i
&#42;
*
74 4A 112 &#74; J 106 6A 152 &#106; j
&#43;
+
75 4B 113 &#75; K 107 6B 153 &#107; k
&#44;
,
76 4C 114 &#76; L 108 6C 154 &#108; l
&#45;
77 4D 115 &#77; M 109 6D 155 &#109; m
&#46;
.
78 4E 116 &#78; N 110 6E 156 &#110; n
&#47;
/
79 4F 117 &#79; O 111 6F 157 &#111; o
&#48;
0
80 50 120 &#80; P 112 70 160 &#112; p
&#49;
1
81 51 121 &#81; Q 113 71 161 &#113; q
&#50;
2
82 52 122 &#82; R 114 72 162 &#114; r
&#51;
3
83 53 123 &#83; S 115 73 163 &#115; s
&#52;
4
84 54 124 &#84; T 116 74 164 &#116; t
&#53;
5
85 55 125 &#85; U 117 75 165 &#117; u
&#54;
6
86 56 126 &#86; V 118 76 166 &#118; v
&#55;
7
87 57 127 &#87; W 119 77 167 &#119; w
&#56;
8
88 58 130 &#88; X 120 78 170 &#120; x
&#57;
9
89 59 131 &#89; Y 121 79 171 &#121; y
&#58;
:
90 5A 132 &#90; Z 122 7A 172 &#122; z
&#59;
;
91 5B 133 &#91; [ 123 7B 173 &#123; {
&#60;
<
92 5C 134 &#92; \ 124 7C 174 &#124; |
&#61;
=
93 5D 135 &#93; ] 125 7D 175 &#125; }
&#62;
>
94 5E 136 &#94; ^ 126 7E 176 &#126; ~
&#63;
?
95 5F 137 &#95; _ 127 7F 177 &#127; DEL
Definitions of ASCII
Control Characters
NUL
SOH
STX
ETX
EOT
ENQ
ACK
BEL
BS
TAB
LF
VT
FF
CR
SO
SI
(null)
(start of heading)
(start of text)
(end of text)
(end of transmission)
(enquiry)
(acknowledge)
(bell)
(backspace)
(horizontal tab)
(line feed, new line)
(vertical tab)
(form feed, new page)
(carriage return)
(shift out)
(shift in)
DLE
DC1
DC2
DC3
DC4
NAK
SYN
ETB
CAN
EM
SUB
ESC
FS
GS
RS
US
(data link escape)
(device control 1)
(device control 2)
(device control 3)
(device control 4)
(neg. acknowledge)
(synchronous idle)
(end of trans. block)
(cancel)
(end of medium)
(substitute)
(escape)
(file separator)
(group separator)
(record separator)
(unit separator)
Table 9.4
Keyboard Scan/ASCII Hex Codes
Function Keys
Unshifted Shifted
Ctrl
Alt
F1
3B/00
54/00
5E/00
68/00
F2
3C/00
55/00
5F/00
69/00
F3
3D/00
56/00
60/00
6A/00
F4
3E/00
57/00
61/00
6B/00
F5
3F/00
58/00
62/00
6C/00
F6
40/00
59/00
63/00
6D/00
F7
41/00
5A/00
64/00
6E/00
F8
42/00
5B/00
65/00
6F/00
F9
43/00
5C/00
66/00
70/00
F10
44/00
5D/00
67/00
71/00
Table 9.4
Keyboard Scan/ASCII Hex Codes
Numeric
Keypad
Ins
0
Unshifted Shifted
52/00
52/30
End
1
4F/00
4F/31
PgDn
2
3
50/00
51/00
50/32
51/33

4
5
4B/00
4B/34
4C/35
73/00
00/04
00/05

Home
6
7
4D/00
47/00
4D/36
47/37
74/00
77/00
00/06
00/07
PgUp
8
9
48/00
49/00
48/38
49/39
84/00
00/08
00/09
-
4A/2D
4A/2D
+
4E/2B
4E/2B
53/00
53/2E
Del
.
Ctrl
Alt
75/00
00/01
76/00
00/02
00/03
Table 9.4 (cont.)
Keyboard Scan/ASCII Hex Codes
Key
Unshifted
Shifted
Ctrl
Alt
A
1E/61
1E/41
1E/01
1E/00
B
30/62
30/42
30/02
30/00
C
2E/63
2E/43
2E/03
2E/00
D
20/64
20/44
20/04
20/00
E
12/65
12/45
12/05
12/00
F
21/66
21/46
21/06
21/00
G
22/67
22/47
22/07
22/00
H
23/68
23/48
23/08
23/00
I
17/69
17/49
17/09
17/00
J
24/6A
24/4A
24/0A
24/00
K
25/6B
25/4B
25/0B
25/00
L
26/6C
26/4C
26/0C
26/00
M
N
32/6D
31/6E
32/4D
31/4E
32/0D
31/0E
32/00
31/00
O
18/6F
18/4F
18/0F
18/00
P
19/70
19/50
19/10
19/00
Q
10/71
10/51
10/11
10/00
R
13/72
13/52
13/12
13/00
S
1F/73
1F/53
1F/13
1F/00
T
14/74
14/54
14/14
14/00
U
16/75
16/55
16/15
16/00
V
2F/76
2F/56
2F/16
2F/00
W
11/77
11/57
11/17
11/00
X
2D/78
2D/58
2D/18
2D/00
Y
15/79
15/59
15/19
15/00
Z
2C/7A
2C/5A
2C/1A
2C/00
Space
ESC
39/20
01/1B
39/20
01/1B
39/20
01/1B
39/20
1 !
02/31
02/21
2 @
03/32
03/40
3 #
04/33
04/23
7A/00
4 $
05/34
05/24
7B/00
5 %
06/35
06/25
7C/00
6 ^
07/36
07/5E
7 &
08/37
08/26
7E/00
8 *
09/38
09/2A
7F/00
9 (
0A/39
0A/38
80/00
0 )
0B/30
0B/29
81/00
- _
0C/2D
0C/5F
= +
0D/3D
0D/2B
78/00
03/00
07/1E
0C/1F
79/00
7D/00
82/00
83/00
[ {
] }
1A/5B
1B/5D
1A/7B
1B/7D
1A/1B
1B/1D
backspac 0E/08
e
0E/08
0E/7F
; :
27/3B
27/3A
' "
28/27
28/22
` ~
29/60
29/7E
, <
33/2C
33/3C
. >
34/2E
34/3E
/ ?
35/2F
35/3F
Enter
1C/0D
1C/0D
Tab
0F/09
0F/00
\ |
2B/5C
2B/7C
1C/0A
2B/1C
Codes and Code Converters
•
•
•
•
ASCII
Binary-to-BCD Converters
Gray Code
Bar Codes
Binary-to-BCD Converter
HEX
O
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
Binary
C3 C2
0 0
0 0
0 0
0 0
0 1
0 1
0 1
0 1
1 0
1 0
1 0
1 0
1 1
1 1
1 1
1 1
C1 C0
0 0
0 1
1 0
1 1
0 0
0 1
1 0
1 1
0 0
0 1
1 0
1 1
0 0
0 1
1 0
1 1
Binary Coded Decimal (BCD)
E
D C B A
BCD
0
0 0 0 0
00
0
0 0 0 1
01
0
0 0 1 0
02
0
0 0 1 1
03
0
0 1 0 0
04
0
0 1 0 1
05
0
0 1 1 0
06
0
0 1 1 1
07
0
1 0 0 0
08
0
1 0 0 1
09
1
0 0 0 0
10
1
0 0 0 1
11
1
0 0 1 0
12
1
0 0 1 1
13
1
0 1 0 0
14
1
0 1 0 1
15
bin2bcd.abl
I = 1110
MODULE bin2bcd
TITLE 'Binary to binary coded decimal'
E
DCBA
1
4
DECLARATIONS
" INPUT PINS "
I3..I0 PIN 11,7,6,5;" Switches S6 - 1..4
I = [I3..I0];
" 4-bit input vector
" OUTPUT PINS "
[E,D,C,B,A] PIN 5,4,3,2,1 ISTYPE 'com'; " LEDs 12..16
BCD = [E,D,C,B,A];
EQUATIONS
@radix 16;
truth_table ( I -> BCD )
0 -> 00;
1 -> 01;
2 -> 02;
3 -> 03;
4 -> 04;
5 -> 05;
6 -> 06;
7 -> 07;
8 -> 08;
9 -> 09;
0A -> 10;
0B -> 11;
0C -> 12;
0D -> 13;
0E -> 14;
0F -> 15;
END bin2bcd
" Identify the Hex Base
" Binary to BCD Code Converter
bin2bcd.abl (cont’d)
HEX
O
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
Binary
C3 C2
0 0
0 0
0 0
0 0
0 1
0 1
0 1
0 1
1 0
1 0
1 0
1 0
1 1
1 1
1 1
1 1
C1 C0
0 0
0 1
1 0
1 1
0 0
0 1
1 0
1 1
0 0
0 1
1 0
1 1
0 0
0 1
1 0
1 1
Binary Coded Decimal (BCD)
E
D C B A
BCD
0
0 0 0 0
00
0
0 0 0 1
01
0
0 0 1 0
02
0
0 0 1 1
03
0
0 1 0 0
04
0
0 1 0 1
05
0
0 1 1 0
06
0
0 1 1 1
07
0
1 0 0 0
08
0
1 0 0 1
09
1
0 0 0 0
10
1
0 0 0 1
11
1
0 0 1 0
12
1
0 0 1 1
13
1
0 1 0 0
14
1
0 1 0 1
15
Shift and Add-3 Algorithm
1.
2.
3.
4.
Shift the binary number left one bit.
If 8 shifts have taken place, the BCD number is in the
Hundreds, Tens, and Units column.
If the binary value in any of the BCD columns is 5 or greater,
add 3 to that value in that BCD column.
Go to 1.
Operation
HEX
Start
Hundreds
Tens
Units
Binary
F
1 1 1 1
F
1 1 1 1
Steps to convert an 8-bit binary number to BCD
Operation
HEX
Start
Shift 1
Shift 2
Shift 3
Add 3
Shift 4
Add 3
Shift 5
Shift 6
Add 3
Shift 7
Add 3
Shift 8
BCD
Hundreds
1
1
1 0
2
Tens
1
0
0
0
1
1 1
0 0
0 1
0 1
1 0
5
Units
1
1
1
0
1
0
0
1
1
0
1
0
0
0
0
1
0
1
1 1
0 1
1 0
0 0
0 0
0 1
0 1
1 1
0 1
1 0
5
Binary
1
1
1
0
1
0
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
F
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1
1
1
1
1
1
1
1
1
F
1 1 1 1
1 1 1
1 1
1
1
Example of converting hex E to BCD
Operation
HEX
Start
Shift 1
Shift 2
Shift 3
Shift 4
6
Add 6
BCD
Tens
Units
1
1
1
1 1
1 1 1
0 1 1
0 1 0
4
Binary
1
1
1
0
0
0
E
1 1 1 0
1 1 0
1 0
0
Truth table for Add-3 Module
A3 A2 A1 A0
C
S3 S2 S1 S0
A3
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
A2
0
0
0
0
1
1
1
1
0
0
0
0
1
1
1
1
A1
0
0
1
1
0
0
1
1
0
0
1
1
0
0
1
1
A0
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
S3
0
0
0
0
0
1
1
1
1
1
X
X
X
X
X
X
S2
0
0
0
0
1
0
0
0
0
1
X
X
X
X
X
X
S1
0
0
1
1
0
0
0
1
1
0
X
X
X
X
X
X
S0
0
1
0
1
0
0
1
0
1
0
X
X
X
X
X
X
Binary-to-BCD
Converter
Binary-to-BCD
Converter
Other Binary Coded Decimal
Representations
Decimal Digit
Excess-3
2421 BCD
2-out-of-5
0
1
2
3
4
5
6
7
8
9
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
0000
0001
0010
0011
0100
1011
1100
1101
1110
1111
11000
00011
00101
00110
01001
01010
01100
10001
10010
10100
Codes and Code Converters
•
•
•
•
ASCII
Binary-to-BCD Converters
Gray Code
Bar Codes
Gray Code
Note that the least significant bit
that can be changed without
repeating a value is the bit
that is changed
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0000
0001
0011
0010
0110
0111
0101
0100
1100
1101
1111
1110
1010
1011
1001
1000
Gray codes eliminate errors
0
1
000
0
1
000
001
7
2
001
7
2
111
010
100
011
110
011
101
010
3
6
101
5
100
3
6
111
4
Binary Code
Reading Region
Region 3: B3B3B3
Transition: B3B3B4
Transition: B3B4B4
Region 4: B4B4B4
5
110
4
Gray Code
Binary Counter
011
R3
010
R2
000
R0
100
R4
Gray Code
010
R3
010
R3
010
R3
110
R4
Gray Code Conversion
Gray Code Decimal equivalent
000
001
011
010
110
111
101
100
0
1
2
3
4
5
6
7
G_in = [G2..0];
G_out = [B2..0];
Truth_table (G_in => G_Out)
Fill in this TRUTH_TABLE
Command so as to convert
the Gray code input, [G2..0],
to the binary output, [B2..0]
Gray Code Conversion
Gray Code Decimal equivalent
000
001
011
010
110
111
101
100
0
1
2
3
4
5
6
7
G_in = [G2..0];
G_out = [B2..0];
Truth_table
0 -> 0; 1
3 -> 2; 2
6 -> 4; 7
5 -> 6; 4
(G_in => G_Out)
-> 1;
-> 3;
-> 5;
-> 7;
Fill in this TRUTH_TABLE
Command so as to convert
the Gray code input, [G2..0],
to the binary output, [B2..0]
Codes and Code Converters
•
•
•
•
ASCII
Binary-to-BCD Converters
Gray Code
Bar Codes
Code 39 Bar Code
A
B
C
D
3-of-9 bars are WIDE bars
(Bars can be black or white)