generated from Tarang74/QUT-Notes-Template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCAB202 Lecture Notes.tex
4487 lines (4261 loc) · 188 KB
/
CAB202 Lecture Notes.tex
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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%!TEX TS-program = xelatex
%!TEX options = -aux-directory=Debug -shell-escape -file-line-error -interaction=nonstopmode -halt-on-error -synctex=1 "%DOC%"
\documentclass{article}
\input{LaTeX-Submodule/template.tex}
% Additional packages & macros
\usepackage{xcolor}
\newcommand{\keyword}[1]{\textcolor[rgb]{0.00,0.50,0.00}{\textbf{#1}}}
\newcommand{\keywordinline}[1]{\textcolor[rgb]{0.00,0.50,0.00}{\textbf{\mintinline{ca65}{#1}}}}
\setminted{
escapeinside=||,
frame=lines,
breaklines
}
\usepackage{subcaption}
\usepackage{multicol}
\usepackage{multirow}
% Header and footer
\newcommand{\unitName}{Microprocessors and Digital Systems}
\newcommand{\unitTime}{Semester 2, 2022}
\newcommand{\unitCoordinator}{Dr Mark Broadmeadow}
\newcommand{\documentAuthors}{Tarang Janawalkar}
\fancyhead[L]{\unitName}
\fancyhead[R]{\parbox[t]{0.6\textwidth}{\raggedleft\leftmark\strut}}
\fancyfoot[C]{\thepage}
% Copyright
\usepackage[
type={CC},
modifier={by-nc-sa},
version={4.0},
imagewidth={5em},
hyphenation={raggedright}
]{doclicense}
\date{}
\begin{document}
%
\begin{titlepage}
\vspace*{\fill}
\begin{center}
\LARGE{\textbf{\unitName}} \\[0.1in]
\normalsize{\unitTime} \\[0.2in]
\normalsize\textit{\unitCoordinator} \\[0.2in]
\documentAuthors
\end{center}
\vspace*{\fill}
\doclicenseThis
\thispagestyle{empty}
\end{titlepage}
\newpage
%
\tableofcontents
\newpage
%
\part{Foundations of Microcontrollers}
\section{Computer Systems Architecture}
\subsection{Introduction to Computers}
\begin{definition}[Computer]
A computer is a digital electronic machine that can be programmed to
carry out sequences of arithmetic or logical operations
(computations) automatically.
\end{definition}
\begin{definition}[Central Processing Unit]
The central processing unit (CPU) is a system of components that
processes instructions, performs calculations, and manages the flow
of data through a computer. It consists of several components such
as the control unit, arithmetic logic unit, and processing core(s).
\end{definition}
\begin{definition}[Control unit]
The control unit is a component of the CPU responsible for managing
the flow of instructions and data within the processor. It
interprets program instructions, coordinates the activities of other
CPU components, and ensures that operations are carried out in the
correct sequence.
\end{definition}
\begin{definition}[Arithmetic logic unit]
The arithmetic logic unit (ALU) is a subsystem of the CPU that
performs mathematical operations, such as addition and subtraction,
as well as logical comparisons, such as equality or inequality
checks. It is a critical component for executing calculations and
decision-making tasks within a computer.
\end{definition}
\begin{definition}[Processing core]
A core is an independent processing unit within the CPU, capable of
executing its own set of instructions. Modern CPUs may contain
multiple cores, enabling parallel execution of tasks to improve
efficiency and performance. Each core operates as a self-contained
processor within the larger CPU system.
\end{definition}
\subsection{Microprocessors \& Microcontrollers}
While a microcontroller puts the CPU and all peripherals onto the same
chip, a microprocessor houses a more powerful CPU on a single chip that
connects to external peripherals. The peripherals include memory, I/O,
and control units. The QUTy board used in this unit houses an AVR
ATtiny1626 microcontroller. Some of the key features of this
microcontroller are provided in the next section.
\subsection{The AVR ATtiny1626 Microcontroller}
The ATtiny1626 microcontroller has the following features:
\begin{itemize}
\item CPU:\@ AVR Core (AVRxt variant)
\item Memory:
\begin{itemize}
\item Flash memory (\qty{16}{K.B}) used to store program
instructions in memory
\item SRAM (\qty{2}{K.B}) used to store data in memory
\item EEPROM (\qty{256}{B})
\end{itemize}
\item Peripherals:\@ Implemented in hardware (part of the chip) in
order to offload complexity
\end{itemize}
\subsubsection{Flash Memory}
\begin{itemize}
\item Non-volatile --- memory is not lost when power is removed
\item Inexpensive
\item Slower than SRAM
\item Can only be erased in large chunks
\item Typically used to store program data
\item Generally read-only. Programmed via an external tool, which
is loaded once and remains static during the lifetime of the
program
\item Writes are slow
\end{itemize}
\subsubsection{SRAM}
\begin{itemize}
\item Volatile --- memory is lost when power is removed
\item Expensive
\item Faster than flash memory and is used to store variables and
temporary data
\item Can access individual bytes (large chunk erases are not
required)
\end{itemize}
\subsubsection{EEPROM}
\begin{itemize}
\item Older technology
\item Expensive
\item Non-volatile
\item Can erase individual bytes
\end{itemize}
\subsubsection{The AVR Core}
\begin{itemize}
\item 8-bit Reduced Instruction Set Computer (RISC)
\item 32 working registers (R0 to R31)
\item Program Counter (PC) --- location in memory of the next
instruction to execute
\item Status Register (SREG) --- stores key information from
calculations performed by the ALU (i.e., whether a result is
negative)
\item Stack Pointer --- location in memory of the top of the stack
\item 8-bit core --- all data, registers, and operations, operate within 8-bits
\end{itemize}
\subsubsection{Status Register}
The status register is an 8-bit register that stores the result of the
last operation performed by the ALU. It has the following flags:
\begin{itemize}
\item[\textbf{C}] Carry Flag
\item[\textbf{Z}] Zero Flag
\item[\textbf{N}] Negative Flag
\item[\textbf{V}] Two's Complement Overflow Flag
\item[\textbf{S}] Sign Flag
\item[\textbf{H}] Half Carry Flag
\item[\textbf{T}] Transfer Bit
\item[\textbf{I}] Global Interrupt Enable Bit
\end{itemize}
\subsection{Computer Programming Basics}
A computer program is a set of instructions written to perform a
specific task or solve a problem. These instructions are processed by
the CPU, which executes them step by step to produce the desired
outcome. Programs are typically written in high-level programming
languages, which are then translated into machine-readable formats for
execution.
\begin{definition}[Machine code]
Machine code is the lowest-level representation of a program,
consisting of binary instructions (sequences of 0s and 1s) that the
CPU can execute directly. Each instruction corresponds to a specific
operation, such as arithmetic, memory access, or control flow,
defined by the CPU's architecture.
\end{definition}
\begin{definition}[Assembly language]
Assembly language is a low-level programming language that provides
a human-readable representation of machine code. It uses symbolic
names (called operational codes) for operations and memory
addresses, making it easier for programmers to write and understand
code that is closely tied to the CPU's architecture. Assembly
language must be translated into machine code by an assembler for
execution.
\end{definition}
\subsection{Program Execution}
At the time of reset, PC = 0 and the following steps are performed:
\begin{enumerate}
\item Fetch instruction (from memory)
\item Decode instruction (decode binary instruction)
\item Execute instruction:
\begin{itemize}
\item Execute an operation
\item Store data in data memory, the ALU, a register, or
update the stack pointer
\end{itemize}
\item Store result
\item Update PC: increment once if the instruction is one word,
otherwise increment twice. Control flow instructions may move
the program to another location and, as a result, set the PC
to a specific address.
\end{enumerate}
This is illustrated in the following figure:
\begin{figure}[H]
\centering
\includegraphics[height = 12cm, keepaspectratio = true]{figures/AVR_CPU.pdf}
\caption{Program execution on the ATtiny1626.} % \label{}
\end{figure}
\subsubsection{Instructions}
\begin{itemize}
\item The CPU understands and can execute a limited set of
instructions --- \textasciitilde88 unique instructions for
the ATtiny1626
\item Instructions are encoded in program memory as opcodes. Most
instructions are two bytes long, but some instructions are
four bytes long
\item The AVR Instruction Set Manual describes all the available
instructions, and how they are translated into opcodes
\item Instructions fall into five categories:
\begin{itemize}
\item Arithmetic and logic --- arithmetic and logical
operations performed by the ALU
\item Change of flow --- jumping to specific locations of
the program unconditionally, or conditionally, by
testing bits in the status register
\item Data transfer --- moving data in/out of registers,
into the data space, or into RAM
\item Bit and bit-test --- inspecting data in registers
(specifically for bit-level operations)
\item Control --- Special microcontroller instructions
\end{itemize}
\end{itemize}
\subsubsection{Memory and Peripherals}
The CPU interacts with both memory and peripherals via the data space.
From the perspective of the CPU, the data space is a large array of
locations that can be read from, or written to, using an address.
Peripherals can be controlled by reading from, and writing to, their
registers which have a unique address in the data space. When
peripherals are accessed in this manner we refer to them as being
memory mapped. Different devices, peripherals, and memory can be
included in a memory map (and sometimes a device can be accessed at
multiple different addresses).
\section{Digital Representations and Operations}
\subsection{Bits, Bytes, and Nibbles}
A \textbf{bit}\footnote{The term \textit{bit} comes from
\textbf{b}inary dig\textbf{it}.} is the most basic unit of information
in a digital system. A bit encodes a logical state with one of two
possible values. These states can represent a variety of concepts:
\begin{itemize}
\item true, false (Boolean states)
\item high, low (voltage states)
\item on, off (switch states)
\item set, reset (memory states)
\item 1, 0 (binary states)
\end{itemize}
A sequence of \textit{eight} bits is known as a \textbf{byte}, and it is the most
common representation of data in digital systems.
A sequence of \textit{four} bits is known as a \textbf{nibble}.
A sequence of \(n\) bits can represent up to \(2^n\) states.
\subsection{Number Representations}
\subsubsection{Binary}
The \textbf{binary system} is a base-2 system that uses a sequence of
bits to represent a number. Bits are written right-to-left from
\textbf{least significant} to \textbf{most significant} bit. The
left-most bit is the ``most significant'' bit because it is associated
with the highest value in the sequence (coefficient of the highest
power of two).
\begin{itemize}
\item The \textbf{least significant bit} (LSB) is at bit index 0.
\item The \textbf{most significant bit} (MSB) is at bit index \(n -
1\) in an \(n\)-bit sequence.
\end{itemize}
\begin{align*}
0000_2 & = 0 & 0100_2 & = 4 & 1000_2 & = 8 & 1100_2 = 12 \\
0001_2 & = 1 & 0101_2 & = 5 & 1001_2 & = 9 & 1101_2 = 13 \\
0010_2 & = 2 & 0110_2 & = 6 & 1010_2 & = 10 & 1110_2 = 14 \\
0011_2 & = 3 & 0111_2 & = 7 & 1011_2 & = 11 & 1111_2 = 15
\end{align*}
The subscript 2 indicates that the number is represented using a base-2
system. As with the familiar decimal system, left-padded zeros do not
change the value of a number, but are included here for formatting
purposes.
\subsubsection{Octal}
The \textbf{octal system} is a base-8 system. It is most notably used
as a shorthand for representing file permissions on UNIX systems, where
three bits are used to represent read, write, execute permissions for
the owner of a file, the groups the owner is part of, and other users.
\begin{align*}
0_8 & = 000_2 & 4_8 & = 100_2 \\
1_8 & = 001_2 & 5_8 & = 101_2 \\
2_8 & = 010_2 & 6_8 & = 110_2 \\
3_8 & = 011_2 & 7_8 & = 111_2
\end{align*}
As each octal digit maps to three bits, it is not very convenient for
systems with byte-sized data. Despite this, it is still available in
many programming languages for historical reasons.
\subsubsection{Hexadecimal}
The \textbf{hexadecimal system} (hex) is a base-16 system. As we need
more than 10 digits in this system, we use the letters A-F to represent
digits 10 to 15. Hex is a convenient notation when working with digital
systems as each hexadecimal digit maps to a nibble.
\begin{align*}
0_{16} & = 0000_2 & 4_{16} & = 0100_2 & 8_{16} & = 1000_2 & C_{16} = 1100_2 \\
1_{16} & = 0001_2 & 5_{16} & = 0101_2 & 9_{16} & = 1001_2 & D_{16} = 1101_2 \\
2_{16} & = 0010_2 & 6_{16} & = 0110_2 & A_{16} & = 1010_2 & E_{16} = 1110_2 \\
3_{16} & = 0011_2 & 7_{16} & = 0111_2 & B_{16} & = 1011_2 & F_{16} = 1111_2
\end{align*}
\subsubsection{Numeric Literals}
When a fixed value is declared directly in a program, it is referred to
as a \textbf{literal}. Here we must use prefixes to denote the base of
the number:
\begin{itemize}
\item \textbf{Binary} notation requires the prefix \mintinline{ca65}{0b}
\item \textbf{Decimal} notation does not require prefixes
\item \textbf{Octal} notation requires the prefix \mintinline{ca65}{0o}
\item \textbf{Hexadecimal} notation requires the prefix \mintinline{ca65}{0x}
\end{itemize}
For example, \mintinline{ca65}{0x80 |=| 0o200 |=| 0b10000000 |=| 128}.
\subsection{Unsigned Integers}
The \textbf{unsigned integers} represent the set of counting (natural)
numbers, starting at 0. In the \textbf{decimal system} (base-10), the
unsigned integers are encoded using a sequence of decimal digits
(0--9). The decimal system is a \textbf{positional numeral system},
where the contribution of each digit is determined by its position. For
example,
\begin{align*}
278_{10} & = 2 \times 10^2 & & + 7 \times 10^1 & & + 8 \times 10^0 \\
& = 2 \times 100 & & + 7 \times 10 & & + 8 \times 1 \\
& = 200 & & + 70 & & + 8 \\
\end{align*}
In the \textbf{binary system} (base-2) the unsigned integers are encoded using a sequence of binary digits (0--1)
in the same manner. For example,
\begin{align*}
10101_2 & = 1 \times 2^4 & & + 0 \times 2^3 & & + 1 \times 2^2 & & + 0 \times 2^1 & & + 1 \times 2^0 \\
& = 1 \times 16 & & + 0 \times 8 & & + 1 \times 4 & & + 0 \times 2 & & + 1 \times 1 \\
& = 16 & & + 0 & & + 4 & & + 0 & & + 1 \\
& = 21_{10}
\end{align*}
The range of values an \(n\)-bit binary number can hold when encoding an unsigned integer is 0 to \(2^n - 1\).
\begin{table}[H]
\centering
\begin{tabular}{c c}
\toprule
\textbf{No.\ of Bits} & \textbf{Range} \\
\midrule
8 & \(0\)--\(255\) \\
16 & \(0\)--\(\num{65535}\) \\
32 & \(0\)--\(\num{4294967295}\) \\
64 & \(0\)--\(\num{18446744073709551615}\) \\
\bottomrule
\end{tabular}
\caption{Range of available values in binary representations.} % \label{}
\end{table}
\subsection{Signed Integers}
Signed integers are used to represent integers that can be positive or
negative. The following representations allow us to encode negative
integers using a sequence of binary bits:
\begin{itemize}
\item Sign-magnitude
\item One's complement
\item Two's complement (most common)
\end{itemize}
\subsubsection{Sign-Magnitude}
In sign-magnitude representation, the most significant bit encodes the
sign of the integer. In an 8-bit sequence, the remaining 7-bits are
used to encode the value of the bit.
\begin{itemize}
\item If the sign bit is 0, the remaining bits represent a positive
value,
\item If the sign bit is 1, the remaining bits represent a negative
value.
\end{itemize}
As the sign bit consumes one bit from the sequence, the range of values that can be
represented by an \(n\)-bit sign-magnitude encoded bit sequence is:
\begin{equation*}
-\left( 2^{n - 1} - 1 \right) \text{ to } 2^{n - 1} - 1.
\end{equation*}
For 8-bit sequences, this range is: \(-127\) to \(127\).
However, this presents several issues:
\begin{enumerate}
\item There are two ways to represent zero:
\mintinline{ca65}{0b10000000 |=| 0}, or
\mintinline{ca65}{0b00000000 |=| -0}.
\item Arithmetic and comparison requires inspecting the sign bit
\item The range is reduced by 1 (due to the redundant zero
representation)
\end{enumerate}
\subsubsection{One's Complement}
In one's complement representation, a negative number is represented by
inverting the bits of a positive number (i.e., \(0 \to 1\) and \(1 \to
0\)). While the range of representable values are still the same:
\begin{equation*}
-\left( 2^{n - 1} - 1 \right) \text{ to } 2^{n - 1} - 1
\end{equation*}
this representation tackles the second problem in the previous representation as
addition is performed via standard binary addition with \textit{end-around carry} (carry bit is added onto result).
\begin{equation*}
a - b = a + \left( \text{\textasciitilde} b \right) + C.
\end{equation*}
\subsubsection{Two's Complement}
In two's complement representation, the most significant bit encodes a
negative weighting of \(2^{n - 1}\). For example, in 8-bit sequences,
index-7 represents a value of \(-128\). It can be shown that the two's
complement is calculated by adding 1 to the one's complement. The range
of representable values is then:
\begin{equation*}
-2^{n - 1} \text{ to } 2^{n - 1} - 1.
\end{equation*}
This representation is more efficient than the previous because \mintinline{ca65}{0} has a single representation
and subtraction is performed by adding the two's complement of the subtrahend.
\begin{equation*}
a - b = a + \left( \text{\textasciitilde} b + 1 \right).
\end{equation*}
\subsection{Logical Operators}
\subsubsection{Boolean Functions}
A Boolean function is a function whose arguments and results assume
values from a two-element set, (usually \(\left\{ 0,\: 1 \right\}\) or
\mintinline{text}{{false, true}}). These functions are also referred to
as \textit{logical functions} when they operate on bits. The most
common logical functions available to microprocessors and most
programming languages are:
\begin{itemize}
\item Negation: \keywordinline{NOT} \(a\), \textasciitilde\(a\),
\(\overline{a}\)
\item Conjunction: \(a\) \mintinline{ca65}{AND} \(b\), \(a\)
\mintinline{ca65}{&} \(b\), \(a \cdot b\), \(a \land b\)
\item Disjunction: \(a\) \keywordinline{OR} \(b\), \(a\)
\mintinline{ca65}{|\vert|} \(b\), \(a + b\), \(a \lor b\)
\item Exclusive disjunction: \(a\) \keywordinline{XOR} \(b\), \(a\)
\mintinline{ca65}{^} \(b\), \(a \oplus b\)
\end{itemize}
By convention, we map a bit value of \mintinline{ca65}{0} to \mintinline{ca65}{false}, and a bit value of \mintinline{ca65}{1} to \mintinline{ca65}{true}.
\subsubsection{Negation}
\keywordinline{NOT} is a unary operator used to \textbf{invert} a bit.
\begin{table}[H]
\centering
\begin{tabular}{c c}
\toprule
\textbf{\(a\)} & \keywordinline{NOT} \(a\) \\
\midrule
0 & 1 \\
1 & 0 \\
\bottomrule
\end{tabular}
\end{table}
\subsubsection{Conjunction}
\mintinline{ca65}{AND} is a binary operator whose output is true if \textbf{both} inputs are \textbf{true}.
\begin{table}[H]
\centering
\begin{tabular}{c c c}
\toprule
\textbf{\(a\)} & \textbf{\(b\)} & \textbf{\(a\) \mintinline{ca65}{AND} \(b\)} \\
\midrule
0 & 0 & 0 \\
0 & 1 & 0 \\
1 & 0 & 0 \\
1 & 1 & 1 \\
\bottomrule
\end{tabular}
\end{table}
\subsubsection{Disjunction}
\keywordinline{OR} is a binary operator whose output is true if \textbf{either} input is \textbf{true}.
\begin{table}[H]
\centering
\begin{tabular}{c c c}
\toprule
\textbf{\(a\)} & \textbf{\(b\)} & \(a\) \keywordinline{OR} \(b\) \\
\midrule
0 & 0 & 0 \\
0 & 1 & 1 \\
1 & 0 & 1 \\
1 & 1 & 1 \\
\bottomrule
\end{tabular}
\end{table}
\subsubsection{Exclusive Disjunction}
\keywordinline{XOR} (Exclusive \keywordinline{OR}) is a binary operator whose output is true if \textbf{only one} input is \textbf{true}.
\begin{table}[H]
\centering
\begin{tabular}{c c c}
\toprule
\textbf{\(a\)} & \textbf{\(b\)} & \(a\) \keywordinline{XOR} \(b\) \\
\midrule
0 & 0 & 0 \\
0 & 1 & 1 \\
1 & 0 & 1 \\
1 & 1 & 0 \\
\bottomrule
\end{tabular}
\end{table}
\subsubsection{Bitwise Operations}
When applying logical operators to a sequence of bits, the operation is
performed in a \textbf{bitwise} manner. The result of each operation is
stored in the corresponding bit index also.
\subsection{Bit Manipulation}
Often we need to modify individual bits within a byte, \textbf{without}
modifying other bits. This is accomplished by performing a bitwise
operation on the byte using a \textbf{bit mask} or \textbf{bit field}.
These operations can:
\begin{itemize}
\item \textbf{Set} specific bits (change value to \mintinline{ca65}{1})
\item \textbf{Clear} specific bits (change value to \mintinline{ca65}{0})
\item \textbf{Toggle} specific bits (change values from \(0 \to 1\), or \(1 \to 0\))
\end{itemize}
\subsubsection{Setting Bits}
To \textbf{set} a bit, we take the bitwise \keywordinline{OR} of the
byte, with a bit mask that has a \textbf{1} in each position where the
bit should be set.
\begin{figure}[H]
\centering
\includegraphics[height = 4cm, keepaspectratio = true]{figures/bit_set.pdf}
\caption{Setting bits using the logical or.} % \label{}
\end{figure}
\subsubsection{Clearing Bits}
To \textbf{clear} a bit, we take the bitwise \mintinline{ca65}{AND} of
the byte, with a bit mask that has a \textbf{0} in each position where
the bit should be cleared.
\begin{figure}[H]
\centering
\includegraphics[height = 4cm, keepaspectratio = true]{figures/bit_clear.pdf}
\caption{Clearing bits using the logical and.} % \label{}
\end{figure}
\subsubsection{Toggling Bits}
To \textbf{toggle} a bit, we take the bitwise \keywordinline{XOR} of
the byte, with a bit mask that has a \textbf{1} in each position where
the bit should be toggled.
\begin{figure}[H]
\centering
\includegraphics[height = 4cm, keepaspectratio = true]{figures/bit_toggle.pdf}
\caption{Toggling bits using the logical exclusive or.} % \label{}
\end{figure}
Other bitwise operations act on the entire byte.
\begin{itemize}
\item One's complement (bitwise \keywordinline{NOT})
\item Two's complement (bitwise \keywordinline{NOT} + 1)
\item Shifts
\begin{itemize}
\item Logical
\item Arithmetic (for signed integers)
\end{itemize}
\item Rotations
\end{itemize}
\subsubsection{One's Complement}
The one's complement of a byte inverts every bit in the operand. This
is done by taking the bitwise \keywordinline{NOT} of the byte.
Similarly, we can subtract the byte from \mintinline{ca65}{0xFF} to get
the one's complement.
\subsubsection{Two's Complement}
The two's complement of a byte is the one's complement of the byte plus
one. Therefore, we can take the bitwise \keywordinline{NOT} of the
byte, and then add one to it.
\subsubsection{Shifts}
Shifts are used to move bits within a byte. In many programming
languages this is represented by two greater than \mintinline{ca65}{>>}
or two less than \mintinline{ca65}{<<} characters.
\begin{equation*}
a \gg s
\end{equation*}
shifts the bits in \(a\) by \(s\) places to the right while adding \mintinline{ca65}{0}'s to the MSB.\
\begin{figure}[H]
\centering
\includegraphics[height = 2cm, keepaspectratio = true]{figures/logical_right_shift.pdf}
\caption{Right shift using \mintinline{ca65}{lsr} in AVR Assembly.} % \label{}
\end{figure}
Similarly,
\begin{equation*}
a \ll s
\end{equation*}
shifts the bits in \(a\) by \(s\) places to the left while adding \mintinline{ca65}{0}'s to the LSB.\
\begin{figure}[H]
\centering
\includegraphics[height = 2cm, keepaspectratio = true]{figures/logical_left_shift.pdf}
\caption{Left shift using \keyword{\ttfamily{lsl}} in AVR Assembly.} % \label{}
\end{figure}
When using signed integers, the arithmetic shift is used to preserve the value of the sign bit when shifting.
\begin{figure}[H]
\centering
\includegraphics[height = 2cm, keepaspectratio = true]{figures/arithmetic_right_shift.pdf}
\caption{Arithmetic right shift using \keyword{\ttfamily{asr}} in AVR Assembly.} % \label{}
\end{figure}
Left shifts are used to multiply numbers by 2, whereas right shifts are used to divide numbers by 2 (with truncation).
\subsubsection{Rotations}
Rotations are used to shift bits with a carry from the previous
instruction. To understand why, calculate the decimal value of the
resulting byte after a shift.
\begin{figure}[H]
\centering
\includegraphics[height = 2cm, keepaspectratio = true]{figures/rotate_left.pdf}
\caption{Rotate left using \mintinline{ca65}{rol} in AVR Assembly.} % \label{}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[height = 2cm, keepaspectratio = true]{figures/rotate_right.pdf}
\caption{Rotate right using \mintinline{ca65}{ror} in AVR Assembly.} % \label{}
\end{figure}
Here the blue bit is carried from the previous instruction, and the carry bit is updated
to the value of the bit that was shifted out.
Rotations are used to perform multibyte shifts and arithmetic operations.
\subsection{Arithmetic Operations}
\subsubsection{Addition}
Addition is performed using the same process as decimal addition except
we only use two digits, 0 and 1.
\begin{enumerate}
\item \mintinline{ca65}{0b0 + 0b0 = 0b0}
\item \mintinline{ca65}{0b0 + 0b1 = 0b1}
\item \mintinline{ca65}{0b1 + 0b1 = 0b10}
\end{enumerate}
When adding two 1's, we carry the result into the next bit position as we would with a 10 in decimal addition.
In AVR Assembly, we can use the \keywordinline{add} instruction to add two bytes. The following
example adds two bytes.
\begin{minted}{ca65}
; Accumulator
|\keyword{ldi}| r16, 0
; First number
|\keyword{ldi}| r17, 29
|\keyword{add}| r16, r17 ; R16 <- R16 + R17 = 0 + 29 = 29
; Second number
|\keyword{ldi}| r17, 118
|\keyword{add}| r16, r17 ; R16 <- R16 + R17 = 29 + 118 = 147
\end{minted}
Below is a graphical illustration of the above code.
\begin{figure}[H]
\centering
\includegraphics[height = 3cm, keepaspectratio = true]{figures/add.pdf}
\caption{Overflow addition using \keyword{\ttfamily{add}} in AVR Assembly.} % \label{}
\end{figure}
\subsubsection{Overflows}
When the sum of two 8-bit numbers is greater than 8-bit (255), an
\textbf{overflow} occurs. Here we must utilise a second register to
store the high byte so that the result is represented as a 16-bit
number. To avoid loss of information, a \textbf{carry bit} is used to
indicate when an overflow has occurred. This carry bit can be added to
the high byte in the event that an overflow occurs. The following
example shows how to use the \mintinline{ca65}{adc} instruction to
carry the carry bit when an overflow occurs.
\begin{minted}{ca65}
; Low byte
|\keyword{ldi}| r30, 0
; High byte
|\keyword{ldi}| r31, 0
; Empty byte for adding carry bit
|\keyword{ldi}| r29, 0
; First number
|\keyword{ldi}| r16, 0b11111111
; Add to low byte
|\keyword{add}| r30, r16 ; R30 <- R30 + R16 = 0 + 255 = 255, C <- 0
; Add to high byte
adc r31, r29 ; R31 <- R31 + R29 + C = 0 + 0 + 0 = 0
; Second number
|\keyword{ldi}| r16, 0b00000001
; Add to low byte
|\keyword{add}| r30, r16 ; R30 <- R30 + R16 = 255 + 1 = 0, C <- 1
; Add to high byte
adc r31, r29 ; R31 <- R31 + R29 + C = 0 + 0 + 1 = 1
\end{minted}
Therefore, the final result is:
% latexignore
\mintinline{ca65}{R31|:|R30 |=| 0b00000001|:|0b00000001 |=| 256}.
Below is a graphical representation
of the above code.
\begin{figure}[H]
\centering
\includegraphics[height = 6cm, keepaspectratio = true]{figures/adc.pdf}
\caption{Overflow addition using \mintinline{ca65}{adc} in AVR Assembly.} % \label{}
\end{figure}
\subsubsection{Subtraction}
Subtraction is performed using the same process as binary addition,
with the subtrahend in two's complement form. In the case of overflows,
the carry bit is discarded.
\subsubsection{Multiplication}
Multiplication is understood as the sum of a set of partial products,
similar to the process used in decimal multiplication. Here each digit
of the multiplier is multiplied to the multiplicand and each partial
product is added to the result.
Given an \(m\)-bit and an \(n\)-bit number, the product is at most
\((m+n)\)-bits wide.
\begin{align*}
13 \times 43 & = 00001101_2 \times 00101011_2 \\
& =
\begin{aligned}[t]
& & 00001101_2 & & \times & & 1_2 \\
& + & 00001101_2 & & \times & & 10_2 \\
& + & 00001101_2 & & \times & & 1000_2 \\
& + & 00001101_2 & & \times & & 100000_2
\end{aligned}
\\
& =
\begin{aligned}[t]
& & 00001101_2 \\
& + & 00011010_2 \\
& + & 01101000_2 \\
& + & 110100000_2
\end{aligned}
\\
& = 1000101111
\end{align*}
Using AVR assembly, we can use the \keywordinline{mul} instruction to perform multiplication.
\begin{minted}{ca65}
; First number
|\keyword{ldi}| r16, 13
; Second number
|\keyword{ldi}| r17, 43
; Multiply
|\keyword{mul}| r16, r17 ; R1:R0 <- 0b00000010:0b00101111 = 559
\end{minted}
The result is stored in the register pair \mintinline{text}{R1:R0}.
\subsubsection{Division}
Division, square roots and many other functions are very expensive to
implement in hardware, and thus are typically not found in conventional
ALUs, but rather implemented in software. However, there are other
techniques that can be used to implement division in hardware. By
representing the divisor in reciprocal form, we can try to represent
the number as the sum of powers of 2. For example, the divisor \(6.4\)
can be represented as:
\begin{equation*}
\frac{1}{6.4} = \frac{10}{64} = 10 \times 2^{-6}
\end{equation*}
so that dividing an integer \(n\) by \(6.4\) is approximately equivalent to:
\begin{equation*}
\frac{n}{6.4} \approx \left( n \times 10 \right) \gg 6
\end{equation*}
When the divisor is not exactly representable as a power of 2 we can use fractional
exponents to represent the divisor, however this requires a floating point
system implementation which is not provided on the AVR\@.
\part{Microcontroller Fundamentals}
\section{Microcontroller Interfacing}
\subsection{Logic Levels}
\subsubsection{Discretisation}
The process of discretisation translates a continuous signal into a
discrete signal (bits). As an example, we can translate \textbf{voltage
levels} on microcontroller pins into digital \textbf{logic levels}.
\subsubsection{Logic Levels}
For digital input/output (IO), conventionally:
\begin{itemize}
\item The voltage level of the positive power supply represents a
\textbf{logical 1}, or the \textbf{high state}, and
\item \qty{0}{V} (ground) represents a \textbf{logical 0}, or the \textbf{low state}.
\end{itemize}
The QUTy is supplied \qty{3.3}{V} so that when a digital output is high,
the voltage present on the corresponding pin will be around \qty{3.3}{V}.
Because voltage is a continuous quantity, we must discretise the full range of voltages into logical levels using \textbf{thresholds}.
\begin{itemize}
\item A voltage \textbf{above} the input \textbf{high threshold}
\(t_H\) is considered \textbf{high}.
\item A voltage \textbf{below} the input \textbf{low threshold}
\(t_L\) is considered \textbf{low}.
\end{itemize}
The interpretation of a voltage between these states is determined by \textbf{hysteresis}.
\subsubsection{Hysteresis}
Hysteresis refers to the property of a system whose state is
\textbf{dependent} on its \textbf{history}. In electronic circuits,
this avoids ambiguity in determining the state of an input as it
switches between voltage levels.
\begin{figure}[H]
\centering
\includegraphics[height = 5cm, keepaspectratio = true]{figures/hysteresis.pdf}
\caption{Example of hysteresis.} % \label{}
\end{figure}
Given a transition:
\begin{itemize}
\item If an input is currently in the \textbf{low state}, it has
not transitioned to the \textbf{high state} until the voltage
crosses the \textbf{high input voltage} threshold.
\item If an input is currently in the \textbf{high state}, it has
not transitioned to the \textbf{low state} until the voltage
crosses the \textbf{low input voltage} threshold.
\end{itemize}
It is therefore always preferable to drive a digital input to an unambiguous voltage level.
\subsection{Electrical Quantities}
\subsubsection{Voltage}
\textbf{Voltage} \(v\) is the electrical \textit{potential difference} between two points in a circuit, measured in \textbf{Volts (\unit{V})}.
\begin{itemize}
\item Voltage is measured across a circuit element, or between two
points in a circuit, commonly with respect to a \qty{0}{V}
reference (ground).
\item It represents the \textbf{potential} of the electrical system
to do \textbf{work}.
\end{itemize}
\subsubsection{Current}
\textbf{Current} \(i\) is the \textit{rate of flow of electrical charge} through a circuit, measured in \textbf{Amperes (\unit{A})}.
\begin{itemize}
\item Current is measured through a circuit element.
\end{itemize}
\subsubsection{Power}
\textbf{Power} \(p\) is the rate of energy transferred per unit time, measured in \textbf{Watts (\unit{W})}.
Power can be determined through the equation
\begin{equation*}
p = v i.
\end{equation*}
\subsubsection{Resistance}
\textbf{Resistance} \(R\) is a property of a material to \textit{resist the flow of current}, measured in \textbf{Ohms (\unit{\ohm})}.
Ohm's law states that the voltage across a component is proportional to the current that flows through it:
\begin{equation*}
v = i R.
\end{equation*}
Note that not all circuit elements are resistive (or Ohmic), as they
do not follow Ohm's law; this can be seen in diodes.
\begin{figure}[H]
\centering
\begin{subfigure}{0.47\linewidth}
\centering
\includegraphics[height=4.5cm]{figures/vi_ohmic.pdf}
\caption{VI curve for Ohmic components.}
\end{subfigure}
\begin{subfigure}{0.47\linewidth}
\centering
\includegraphics[height=4.5cm]{figures/vi_diode.pdf}
\caption{VI curve for diodes.}
\end{subfigure}
\caption{Voltage-current characteristic curves for various components.}
\end{figure}
Although the wires used to connect a circuit are resistive, we usually assume that they are ideal, that is,
they have zero resistance.
\subsection{Common Electrical Components}
\subsubsection{Resistors}
A \textbf{resistor} is a circuit element that is designed to have a
specific resistance \(R\).
\begin{figure}[H]
\centering
\includegraphics[height = 2.5cm, keepaspectratio = true]{figures/resistor.pdf}
\caption{Resistor circuit symbol.} % \label{}
\end{figure}
\subsubsection{Switches}
A \textbf{switch} is used to connect and disconnect different elements
in a circuit. It can be \textbf{open} or \textbf{closed}.
\begin{itemize}
\item In the \textbf{open} state, the switch \textbf{will not
conduct}\footnote{Conductance is a measure of the ability for
electric charge to flow in a certain path.} current
\item In the \textbf{closed} state, the switch \textbf{will
conduct} current
\end{itemize}
Switches can take a variety of forms:
\begin{itemize}
\item \textbf{Poles} --- the number of circuits the switch can control.
\item \textbf{Throw} --- the number of output connections each pole can connect its input to.
\item Momentary or toggle action
\item Different form factors, e.g., push button, slide, toggle,
etc.
\end{itemize}
Switches are typically for user input.
\begin{figure}[H]
\centering
\begin{subfigure}{0.47\linewidth}
\centering
\includegraphics[width=2.5cm]{figures/spst.pdf}
\caption{Single pole single throw switch.}
\end{subfigure}
\begin{subfigure}{0.47\linewidth}
\centering
\includegraphics[width=2.5cm]{figures/spdt.pdf}
\caption{Single pole double throw switch.}
\end{subfigure}
\vspace*{5ex}
\begin{subfigure}{0.47\linewidth}
\centering
\includegraphics[width=2.5cm]{figures/dpst.pdf}
\caption{Double pole single throw switch.}
\end{subfigure}
\begin{subfigure}{0.47\linewidth}
\centering
\includegraphics[width=2.5cm]{figures/dpdt.pdf}
\caption{Double pole double throw switch.}
\end{subfigure}
\caption{Various types of switches.}
\end{figure}
\subsubsection{Diodes}
A \textbf{diode} is a semiconductor device that conducts current in
only one direction: from the \textbf{anode} to the \textbf{cathode}.
\begin{figure}[H]
\centering
\includegraphics[height = 2cm, keepaspectratio = true]{figures/diode.pdf}
\caption{Diode symbol.} % \label{}
\end{figure}
Diodes are a non-Ohmic device:
\begin{itemize}
\item When \textbf{forward biased}, a diode \textbf{does} conduct
current, and the anode-cathode voltage is equal to the diodes
\textbf{forward voltage}.
\item When \textbf{reverse biased}, a diode \textbf{does not}
conduct current, and the cathode-anode voltage is equal to
the \textbf{applied voltage}.
\end{itemize}
\begin{figure}[H]
\centering
\begin{subfigure}{0.47\linewidth}
\centering
\includegraphics[height=3.5cm]{figures/diode_forward_bias.pdf}
\caption{Forward biased diode. \\\(v_{AK} = v_f\) and \(i > 0\).}
\end{subfigure}
\begin{subfigure}{0.47\linewidth}
\centering
\includegraphics[height=3.5cm]{figures/diode_reverse_bias.pdf}
\caption{Reverse biased diode. \\\(v_{KA} > 0\) and \(i = 0\).}
\end{subfigure}
\caption{Diodes in forward and reverse bias.}
\end{figure}
A diode is only forward biased when the applied anode-cathode voltage \textbf{exceeds} the forward voltage \(v_f\).
A typical forward voltage \(v_f\) for a silicon diode is in the range \qtyrange{0.6}{0.7}{V}, whereas for
Light Emitting Diodes (LEDs), \(v_f\) ranges between \qtyrange{2}{3}{V}.
\subsubsection{Integrated Circuit}
An \textbf{integrated circuit} (IC) is a set of electronic circuits
(typically) implemented on a single piece of semiconductor material,
usually silicon. ICs comprise hundreds to many thousands of
transistors, resistors and capacitors; all implemented on silicon. ICs
are \textbf{packaged}, and connections to the internal circuitry are
exposed via \textbf{pins}. In general, the specific implementation of
the IC is not important, but rather the \textbf{function of the device}
and how it \textbf{interfaces} with the rest of the circuit. Hence, ICs
can be treated as a functional \textbf{black box}. For digital ICs:
\begin{itemize}
\item \textbf{Input pins} are typically \textbf{high-impedance}, and they appear as an open circuit.
\item \textbf{Output pins} are typically \textbf{low-impedance}, and will actively drive the voltage
on a pin and any connected circuitry to a \textbf{high} or \textbf{low} state. They can also
drive connected loads.
\end{itemize}
\subsection{Digital Outputs}
Digital output interfaces are designed to be able to drive connected
circuitry to a logical high, or logical low; however, the appropriate
technique is \textbf{context specific}. When referring to digital
outputs, we will refer to the states of a net. A \textbf{net} is
defined as the common point of connection of multiple circuit
components. In this section we will consider:
\begin{itemize}
\item What kind of load the output drives
\item Could more than one device be attempting to actively drive
the net to a specific logic level?
\end{itemize}
\subsubsection{Push-Pull Outputs}
A push-pull digital output is the most common form of output used in
digital outputs. The \textbf{output driver} \(A\) \textit{drives} the
\textbf{output state} \(Y\) to:
\begin{itemize}
\item \textbf{HIGH} by connecting the output net to the supply voltage \(+\unit{V}\).
\item \textbf{LOW} by connecting the output net to the ground voltage GND (\qty{0}{V}).
\end{itemize}
\begin{figure}[H]
\centering
\includegraphics[height = 4cm, keepaspectratio = true]{figures/push_pull.pdf}
\caption{Push-pull output.} % \label{}
\end{figure}
Hence, the output state \(Y\) is determined by the logic level of the output driver \(A\).
\begin{equation*}
Y = A.
\end{equation*}
\begin{table}[H]
\centering
\begin{tabular}{c | c}
\toprule
\textbf{\(A\)} & \textbf{\(Y\)} \\
\midrule
LOW & LOW \\
HIGH & HIGH \\
\bottomrule
\end{tabular}
\caption{Truth table for a push-pull digital output.} % \label{}
\end{table}
The push-pull output \(Y\) can both source and sink current from the connected net.
\subsubsection{High-Impedance Outputs}
In many instances, a digital output is required to be placed in a
high-impedance (HiZ) state. This is accomplished by using an
\textbf{output enable} (OE) signal.
\begin{figure}[H]
\centering
\includegraphics[height = 3cm, keepaspectratio = true]{figures/HiZ.pdf}
\caption{High-impedance output.} % \label{}
\end{figure}
\begin{itemize}
\item When the OE signal is \textbf{HIGH}, the output state \(Y\)
is determined by the output driver \(A\).
\item When the OE signal is \textbf{LOW}, the output state \(Y\) is
in a \textbf{high-impedance} state.
\end{itemize}
\begin{table}[H]
\centering
\begin{tabular}{c c | c}
\toprule
\textbf{\(A\)} & \textbf{OE} & \textbf{\(Y\)} \\
\midrule
LOW & LOW & HiZ \\
HIGH & LOW & HiZ \\
LOW & HIGH & LOW \\
HIGH & HIGH & HIGH \\
\bottomrule
\end{tabular}