2. Index
Introduction to COMPUTER SYSTEM
Memory
I/O Unit
Microprocessor
The internal structure of a microprocessor.
Central Processing Unit (CPU)
Arithmetic and logic unit (ALU)
Control unit
CPU Interactions
Clocks
Cache Memory (L1 and L2) Cache
2
3. Index 3
Special Purpose Registers
PC Program Counter Register
Instruction Register IR
Instruction Decoder ID
STACK Pointer SP Register
STACK Pointer SP Register
Status Register (SREG)
Status Register (SREG)
Index Register (X,Y)
Accumulator Registers (A,B)
General purpose Registers (R1,R2,R3,….)
4. Index 4
Microcontroller
Microcontroller Vs Microprocessor Vs SOC
von Neumann Architecture
Harvard Architecture
pipeline
CIS Vs RISC
AVR different groups
5. Index 5
GPIO/DIO/IO Module
Atmel-8155-8-bit-Microcontroller-AVR-
ATmega32A_Datasheet
DIO_LAB1
DIO_LAB2
DIO_Project1:
LED Matrix 8*8
quiz1: 7- segment “assembly Code”
DIO_LAB3:Two- digital Display Module
DIO_Project2
Seven Segment Digital Clock
References
6. Introduction to COMPUTER SYSTEM
A computer is a programmable machine that receives input, stores and manipulates
data//information, and provides output in a useful format.
6
7. BLOCK DIAGRAM OF A BASIC COMPUTER
SYSTEM
ROM RAM I/O
interface
I/O
devicesCPU
7
Basic computer system consist of a Central processing unit (CPU),
memory (RAM and ROM), input/output (I/O) unit.
Block diagram of a basic computer system
Address bus
Data bus Control
bus
8. Basic component of microcomputer
1. CPU - Central Processing Unit
the portion of a computer system that carries out the instructions of a computer
program
the primary element carrying out the computer's functions. It is the unit that reads
and executes program instructions.
The data in the instruction tells the processor what to do.
8
Pentium D dual core processors
9. physical devices used to store data or programs (sequences of
instructions) on a temporary or permanent basis for use in an electronic
digital computer.
Computer main memory comes in two principal varieties: random-access
memory (RAM) and read-only memory (ROM).
RAM can be read and written to anytime the CPU commands it, but
ROM is pre-loaded with data and software that never changes, so the
CPU can only read from it.
ROM is typically used to store the computer's initial start-up instructions.
In general, the contents of RAM are erased when the power to the
computer is turned off, but ROM retains its data indefinitely.
In a PC, the ROM contains a specialized program called the BIOS that
orchestrates loading the computer's operating system from the hard disk
drive into RAM whenever the computer is turned on or reset.
9
Memory
10. Input/output (I/O), refers to the communication between an information
processing system (such as a computer), and the outside world possibly a
human, or another information processing system.
Inputs are the signals or data received by the system, and outputs are
the signals or data sent from it
Devices that provide input or output to the computer are called
peripherals
On a typical personal computer, peripherals include input devices like
the keyboard and mouse, and output devices such as the display and
printer. Hard disk drives, floppy disk drives and optical disc drives serve
as both input and output devices. Computer networking is another form
of I/O.
10
I/O Unit
11. Microcontroller vs Microprocessor 11
A Microcontroller is basically a computer on a chip. It differs form normal desktop or laptop
computers in that a microcontroller is an application specific computer that usually runs
a single program performing dedicated task(s) while the the later two are general purpose
computers that can run numerous programs depending on a users needs. A microcontroller
contains on chip CPU, input/output interface, memory, clock, timer, and an assortment of of
other peripherals.
A Microprocessor on the other hand is just a CPU one
has to add externally memory, clock, input/output
interfaces, timer and all other needed peripheral. This is
the reason a microprocessor has so many pins.
12. Microprocessor
There are five important components in a microprocessor. They are Arithmetic and
Logic Unit (ALU), Control Unit, Registers, Instruction Decoder and Data Bus but the
first three are considered significant components. The block diagram of a
microprocessor with these basic components is shown below.
12
14. Central Processing Unit (CPU)
Arithmetic / Logic Unit
Registers
Control Unit
Small, fast
storage areas for
instructions and
data
Performs calculations
and decisions
Coordinates
processing steps
14
16. The Processor
The processor is the "brain" of the computer system.
Main processor is called the Central Processing Unit (CPU).
A particular computer will have a particular type of processor, such as a Pentium
or a SPARC chip.
Co-processors assist the CPU with some of the processing functions. Examples:
Math co-processors handle heavy duty math processing
Graphics coprocessors speed up the display of graphics onto the monitor
16
17. Internal structure and basic operation of
microprocessor
ALU Register
Section
Control and timing
section
Address bus
Data bus
Control bus
17
Block diagram of a microprocessor
18. Arithmetic and logic unit (ALU)
The component that performs the arithmetic and logical operations
the most important components in a microprocessor, and is typically the
part of the processor that is designed first.
able to perform the basic logical operations (AND, OR), including the
addition operation.
The inclusion of inverters on the inputs enables the same ALU hardware
to perform the subtraction operation (adding an inverted operand),
and the operations NAND and NOR.
18
20. Control unit
The circuitry that controls the flow of information through the
processor, and coordinates the activities of the other units within it.
In a way, it is the "brain within the brain", as it controls what happens
inside the processor, which in turn controls the rest of the PC.
On a regular processor, the control unit performs the tasks of
fetching, decoding, managing execution and then storing results.
20
22. Memory
Computer Memory - millions/billions of on/off charges
Divided into:
Bits 0 or 1
Bytes Groups of 8 bits A byte is the smallest unit of
storage. (Can hold one text character)
WordsGroups of bits/bytes (8, 16, 32, 64-bits)
22
23. Memory
Storage is usually too large to be expressed in bytes or
words. Instead we use:
Kilobyte (KB) = 1024 bytes(210 bytes)
Megabyte (MB) = 1024 x 1024 bytes or one
million bytes (220 bytes)
Gigabyte (GB) = 1024 x 1024 x 1024 bytes or
one trillion bytes (230 bytes)
Terabyte (TB) = 1024 x 1024 x 1024 x 1024 bytes
one quadrillion bytes (240 bytes)
23
24. Main Memory
Each computer has a specific word size
Word sizes vary from computer to computer.
Word size is an even multiple of a bytes.
Each word within memory can hold either
data or
program instructions
24
25. Main Memory
5248
5249
5250
5251
5252
5253
5254
5255
5256
A word is stored in
consecutive
memory bytes.
10011010
Each memory cell stores a
set number of bits (some
computers use 8 bits/one
byte, others use words)
Each memory cell
has a numeric
address, which
uniquely identifies
its location
25
26. CPU and Memory
CPU can interact with main memory in two ways:
It can write a byte/word to a given memory location.
The previous bits that were in that location are destroyed
The new bits are saved for future use.
It can read a byte/word from a given memory location.
The CPU copies the bits stored at that location and stores them in a CPU
register
The contents of the memory location are NOT changed.
26
27. Main Memory Characteristics
Very closely connected to the CPU.
Contents are quickly and easily changed.
Holds the programs and data that the
processor is actively working with.
Interacts with the processor millions of times
per second.
Nothing permanent is kept in main memory.
27
28. Secondary Storage Characteristics
Connected to main memory through a bus and a
device controller.
Contents are easily changed, but access is very
slow compared to main memory.
Only occasionally interacts with CPU.
Used for long-term storage of programs and data.
Much larger than main memory (GBs vs. MBs).
28
29. Program Instructions
Programs instructions are stored in secondary storage
(hard disks, CD-ROM, DVD).
To process data, the CPU requires a working area
Uses Main Memory
Also called: RAM (random access memory),
primary storage, and internal memory.
Before a program is run, instructions must first be copied
from the slow secondary storage into fast main memory
Provides the CPU with fast access to instructions to
execute.
29
30. Instructions
An instruction is a sequence of bits.
A simple instruction format may consist of an
operation code (op code) and an address or
operands.
Op Code Operands / Address
Instructions tell the computer’s CPU what to do.
30
31. Instructions
The operation code specifies the operation the
computer is to carry out (add, compare, etc)
The operand/address area can store an operand or an
address
An operand is a specific value or a register number
An address allows the instruction to refer to a
location in main memory
The CPU runs each instruction in the program,
starting with instruction 0, using the fetch-decode-
execute cycle.
31
32. Fetch-Decode-Execute Cycle
During the fetch part, the CPU fetches the next
instruction from the address contained in the
Program Counter and places the instruction in
the Instruction Register.
When a program starts, the program counter contains 0,
so the instruction at address 0 is fetched.
As soon as an instruction is fetched, the CPU
adds 1 word to the contents of the Program
Counter, so that it will contain the address of the
next sequential instruction.
32
33. Fetch-Decode-Execute Diagram
Program Counter (PC)
3023
Instruction Register
General Purpose Registers
Fetch
Get instruction and
increment PC
CPU
…
3020
3021
3022
3023
3024
3025
3026
3027
3028
…
Main Memory
add r3, r1, r2
33
45
add r3, r1, r2
r1
r2
r378
3024
Execute
In this case add r1 and
r2 and put result in r3.
Decode
Determine what the
instruction is (add)
Then begin again by Fetching the instruction in 3024….
33
34. Clocks
Computer systems have an internal clock, which is used to synchronize
their activities.
Processors run at a particular clock speed.
Clock speed is measured in Hertz
One Hertz is one clock tick per second.
MHz means mega Hertz
One MHz is one million clock ticks per second.
The clock speed determines how fast instructions can be run
34
35. Access to Instructions
The hard disk is too slow to provide instructions to the CPU.
So programs are first loaded into main memory, which is much faster.
The CPU can then access the instructions more quickly.
35
36. Cache Memory
But as CPU speeds became faster, the main
memory couldn’t provide the CPU with the
instructions at a fast enough rate.
So even faster memory ( cache memory) is
now placed between the CPU and main
memory to provide the instructions at an
quicker rate to the CPU.
36
37. Cache Memory
When an instruction or data is accessed from main memory, it is placed in
the cache. Second and subsequent use of the same instruction/data will
then be faster, since it is accessed directly from the cache.
37
38. Primary and Secondary
Cache Memory
Most modern CPUs now have a cache
memory (L1), on the same silicon wafer as
the CPU, to provide the CPU with
instructions at the same clock speed as the
CPU.
An additional off-the-chip secondary cache
(L2) may also interact with the CPU at a
slower speed.
38
40. User view of Computer Systems
Software that
controls the
hardware devices
Operating System –
- the user interface
Applications
e.g. Word, Netscape, etc
40
41. Register sets
The register section/array consists completely of circuitry used to
temporarily store data or program codes until they are sent to the
ALU or to the control section or to memory.
The number of registers are different for any particular CPU and the
more register a CPU have will result in easier programming tasks.
Registers are normally measured by the number of bits they can hold,
for example, an "8-bit register" or a "32-bit register".
41
42. 7-
42Registers
Use of Registers
Scratchpad for currently executing program
Holds data needed quickly or frequently
Stores information about status of CPU and currently executing program
Address of next program instruction
Signals from external devices
General Purpose Registers
User-visible registers
Hold intermediate results or data values, e.g., loop counters
Equivalent to LMC’s calculator
Typically several dozen in current CPUs
51. 51
ATMega32 Programmer Model: Registers (SR)
CPU
PC
ALU
registers
R1
R0
R15
R2 …
R16
R17
…
R30
R31
Instruction Register
Instruction decoder
SREG: I T H S V N CZ
$0000
$0001
$0020
General
purpose
RAM
(SRAM)
$001F
$005F
TWBR
TWSR
SPH
SREG
...
General
Purpose
Registers
Standard IO
Registers
$00
$01
$3E
$3F
$0060
......
Data Address
Space
IO Address
...
$FFFF
SREG:
Carry
Zero
Negative
oVerflow
Sign
N+VHalf carry
Temporary
Interrupt
H S V N CZTI
52. 52
ATMega32 Programmer Model: Registers (SR)
CPU
PC
ALU
registers
R1
R0
R15
R2 …
R16
R17
…
R30
R31
Instruction Register
Instruction decoder
SREG: I T H S V N CZ
$0000
$0001
$0020
General
purpose
RAM
(SRAM)
$001F
$005F
TWBR
TWSR
SPH
SREG
...
General
Purpose
Registers
Standard IO
Registers
$00
$01
$3E
$3F
$0060
......
Data Address
Space
IO Address
...
$FFFF
SREG:
Carry
Zero
Negative
oVerflow
Sign
N+VHalf carry
Temporary
Interrupt
H S V N CZTI
Example: Show the status of the C, H, and Z flags after the addition of 0x38
and 0x2F in the following instructions:
LDI R16, 0x38 ;R16 = 0x38
LDI R17, 0x2F ;R17 = 0x2F
ADD R16, R17 ;add R17 to R16
Solution: 1
$38 0011 1000
+ $2F 0010 1111
$67 0110 0111 R16 = 0x67
C = 0 because there is no carry beyond the D7 bit.
H = 1 because there is a carry from the D3 to the D4 bit.
Z = 0 because the R16 (the result) has a value other than 0 after the addition.
Example: Show the status of the C, H, and Z flags after the addition of
0x9C and 0x64 in the following instructions:
LDI R20, 0x9C
LDI R21, 0x64
ADD R20, R21 ;add R21 to R20
Solution: 1
$9C 1001 1100
+ $64 0110 0100
$100 1 0000 0000 R20 = 00
C = 1 because there is a carry beyond the D7 bit.
H = 1 because there is a carry from the D3 to the D4 bit.
Z = 1 because the R20 (the result) has a value 0 in it after the addition.
Example: Show the status of the C, H, and Z flags after the subtraction of
0x23 from 0xA5 in the following instructions:
LDI R20, 0xA5
LDI R21, 0x23
SUB R20, R21 ;subtract R21 from R20
Solution:
$A5 1010 0101
- $23 0010 0011
$82 1000 0010 R20 = $82
C = 0 because R21 is not bigger than R20 and there is no borrow from D8 bit.
Z = 0 because the R20 has a value other than 0 after the subtraction.
H = 0 because there is no borrow from D4 to D3.
Example: Show the status of the C, H, and Z flags after the subtraction of
0x73 from 0x52 in the following instructions:
LDI R20, 0x52
LDI R21, 0x73
SUB R20, R21 ;subtract R21 from R20
Solution:
$52 0101 0010
- $73 0111 0011
$DF 1101 1111 R20 = $DF
C = 1 because R21 is bigger than R20 and there is a borrow from D8 bit.
Z = 0 because the R20 has a value other than zero after the subtraction.
H = 1 because there is a borrow from D4 to D3.
Example: Show the status of the C, H, and Z flags after the subtraction of
0x9C from 0x9C in the following instructions:
LDI R20, 0x9C
LDI R21, 0x9C
SUB R20, R21 ;subtract R21 from R20
Solution:
$9C 1001 1100
- $9C 1001 1100
$00 0000 0000 R20 = $00
C = 0 because R21 is not bigger than R20 and there is no borrow from D8 bit.
Z = 1 because the R20 is zero after the subtraction.
H = 0 because there is no borrow from D4 to D3.
55. General purpose Registers (R1,R2,R3,….)
Most instructions have direct access to all registers ▫ LD, MUL
Some only operate on R16-R31
▫ ANDI, CPI, SUBI, MULS
A few operate only on R16:R23
▫ Special multiply operations
Double word operations operate on register pairs (R24-31)
Most single-byte register or register+immediate operations are single cycle
instructions
55
57. 57
AVR Assembler Statement
• The Assembler works on source files containing instruction mnemonics, labels and directives.
The instruction mnemonics and the directives often take operands.
• Code lines should be limited to 120 characters.
• Every input line can be preceded by a label, which is an alphanumeric string terminated by a
colon. Labels are used as targets for jump and branch instructions and as variable names in
Program memory and RAM.
• An input line may take one of the five following forms:
<label> <opcode> <operand> <;comments>
<label> <opcode> <operands> <;comments>
<label> <directive> <operands> <;comments>
Comments
Empty line
test: rjmp test ; Infinite loop (Instruction)
main: ldi R16, 9 ; store value 9 into register R16
label: .EQU var1=100 ; Set var1 to 100 (Directive)
.EQU var2=200 ; Set var2 to 200
; Pure comment line
58. 58
AVR Assembler Output FilesEDITOR
PROGRAM
ASSEMBLER
PROGRAM
DOWNLOAD TO
AVR ’s FLASH
DOWNLOAD TO
AVR’s EEPROM
myfile.asm
myfile.objmyfile.eep myfile.hex myfile.map myfile.lst
68. Microcontroller
Microcontrollers are basically used in embedded systems. Computerized or digital
control of devices is made plausible with the development of microcontrollers. The
development process of microcontroller is similar to that of a microprocessor.
Microcontrollers can be classified based on bus width, memory structure and
instruction set. Bus width indicates the size of the data bus. Microcontrollers can be
classified as 8 – bit, 16 – bit or 32 – bit based on the bus width. Higher bus widths
often result in better performance.
68
70. What is the difference between microprocessor, microcontroller and
microcomputer...?
70
71. MICROPROCESSOR MICROCONTROLLER
71
Microprocessor assimilates the
function of a central processing unit
(CPU) on to a single integrated circuit
(IC).
Microcontroller can be considered as
a small computer which has a
processor and some other
components in order to make it a
computer.
Microprocessors are mainly used in
designing general purpose systems
from small to large and complex
systems like super computers.
Microcontrollers are used in
automatically controlled devices.
72. MICROPROCESSOR MICROCONTROLLER
72
Microprocessors are basic
components of personal computers.
Microcontrollers are generally used in
embedded systems
Computational capacity of
microprocessor is very high. Hence
can perform complex tasks.
Less computational capacity when
compared to microprocessors. Usually
used for simpler tasks.
A microprocessor based system can
perform numerous tasks.
A microcontroller based system can
perform single or very few tasks.
73. MICROPROCESSOR MICROCONTROLLER
73
Microprocessors have integrated Math Coprocessor.
Complex mathematical calculations which involve
floating point can be performed with great ease.
Microcontrollers do not have math coprocessors. They
use software to perform floating point calculations
which slows down the device.
The main task of microprocessor is to perform the
instruction cycle repeatedly. This includes fetch,
decode and execute.
In addition to performing the tasks of fetch, decode
and execute, a microcontroller also controls its
environment based on the output of the instruction
cycle.
In order to build or design a system (computer), a
microprocessor has to be connected externally to
some other components like Memory (RAM and ROM)
and Input / Output ports.
The IC of a microcontroller has memory (both RAM
and ROM) integrated on it along with some other
components like I / O devices and timers.
74. MICROPROCESSOR MICROCONTROLLER
74
The overall cost of a system built
using a microprocessor is high. This is
because of the requirement of
external components.
Cost of a system built using a
microcontroller is less as all the
components are readily available.
Generally power consumption and
dissipation is high because of the
external devices. Hence it requires
external cooling system.
Power consumption is less.
The clock frequency is very high
usually in the order of Giga Hertz.
Clock frequency is less usually in the
order of Mega Hertz.
75. MICROPROCESSOR MICROCONTROLLER
75
Instruction throughput is given higher priority
than interrupt latency.
In contrast, microcontrollers are designed to
optimize interrupt latency.
Have few bit manipulation instructions Bit manipulation is powerful and widely used
feature in microcontrollers. They have
numerous bit manipulation instructions.
Generally microprocessors are not used in
real time systems as they are severely
dependent on several other components.
Microcontrollers are used to handle real time
tasks as they are single programmed, self
sufficient and task oriented devices.
77. Embedded System Classifications
There are two main families of embedded
system platforms:
MICROCONTROLLER Family
High Performance (System On Chip) SOC Platform Family
77
82. Conclusion
SOC is the full system, often the computer on a chip, or at least that is the goal.
They are very powerful compared to mere micro controllers. SOC may have large
amount of memory, peripheral interfaces, radio/wireless connection.May be
multicore.
Microcontrollers are processor chips with inbuilt peripheral components,
ADC,DAC and some memory. Designing a system is easier with these than with a
raw microprocessor.
Microprocessors are raw processors with minimal ALU+ registers without
any peripheral components. You need to connect other chips to make it useful.
82
83. Von Neumann Vs Harvard 83
Earlier microprocessors made use of Von Neumann
architecture where the data and instructions
(programs) are stored in the same memory. Even
though this architecture is simple, there are many draw
backs. One of the major drawbacks is that instruction and
data cannot be accessed at the same time as they share
a single data bus.
This often degrades the overall performance of the
device. Later, Harvard architecture is introduced which
makes use of separate program and data memories
with separate buses so that both data and
instructions can be accessed at the same time. Later
Modified Harvard Architecture is developed in which the
program memory is accessed as if it were data memory
84. von Neumann 84
• Same memory holds data, instructions.
• A single set of address/data buses
between CPU and memory
85. Harvard Architecture
Separate memories for data and instructions.
Two sets of address/data buses between CPU and memory
85
86. Harvard architecture
Address bus
Data bus
Control bus
Address bus
Data bus
Control bus
CPU Address bus
Data bus
Control bus
Data
Memory
Code
Memory
CPU
Code
Memory
Data
Memory
Von Neumann architecture
Von Neumann vs. Harvard architecture 86
88. Computer Systems
Not All Processors Are Created Equal
Newer machines use specialized processor chips that
have been developed to speed up the processing of
data.
CISC - Complex instruction set computer
Large instruction set, many formats
RISC - Reduced instruction set computer
Small instruction set, single or small variations in
format
MPP - Massive parallel processing
Many CPUs working in parallel
88
94. 94
ATMega32 Programmer Model: Memory
1. 2KB SRAM
– For temporary data storage
– Memory is lost when power is shut
off (volatile)
– Fast read and write
2. 1KB EEPROM
– For persistent data storage
– Memory contents are retained when
power is off (non-volatile)
– Fast read; slow write
– Can write individual bytes
3. 32KB Flash Program Memory
– Used to store program code
– Memory contents retained when
power is off (non-volatile)
– Fast to read; slow to write
– Can only write entire “blocks” of
memory at a time
– organized in 16-bit words
(16KWords)
95. 95
ATMega32 Programmer Model: Memory
Type Flash RAM EEPROM
F_END Size, kB RAMEND Size, kB E_END Size, kB
Atmega8 $0FFF 8 $045F 1 $1FF 0.5
Atmega32 $3FFF 32 $085F 2 $3FF 1
Atmega64 $7FFF 64 $10FF 4 $7FF 2
Atmega128 $FFFF 128 $10FF 4 $FFF 4
• AVR microcontrollers are Harvard architecture.
This means, that in this architecture are
separate memory types (program memory and
data memory) connected with distinct buses.
Such memory architecture allows processor to
access program memory and data memory at
the same time. This increases performance of
MCU comparing to CISC architecture, where
CPU uses same bus for accessing program
memory and data memory.
• Each memory type has its own address space:
96. 96ATMega32 Programmer Model: Data
Memory
• GPRs (general purpose
registers),
• Special Function Registers
(SFRs), and
• Internal data SRAM.
The data memory is composed
of three parts:
97. 97ATMega32 Programmer Model:I/O Registers
(SFRs)
• The I/O memory is dedicated to specific
functions such as status register, timers,
serial communication, I/O ports, ADC and
etc.
• Function of each I/O memory location is
fixed by the CPU designer at the time of
design. (because it is used for control of
the microcontroller and peripherals)
• AVR I/O memory is made of 8 bit
registers.
• All of the AVRs have at least 64 bytes of
I/O memory location. (This 64 bytes
section is called standard I/O memory)
• In other microcontrollers, the I/O registers
are called SFRs (Special Function
Registers)
NameAddress
I/O Mem.
$00 $20 TWBR
$01 $21 TWSR
$04 $24 ADCL
$05 $25 ADCH
$02 $22 TWAR
$03 $23 TWDR
$06 $26 ADCSRA
$07 $27 ADMUX
$08 $28 ACSR
$09 $29 UBRRL
$0A $2A UCSRB
$0B $2B UCSRA
$0C $2C UDR
$0D $2D SPCR
$0E $2E SPSR
$0F $2F
PIND$10 $30
DDRD$11 $31
PORTD$12 $32
PINC$13 $33
DDRC$14 $34
PORTC$15 $35
PINB$16 $36
DDRB$17 $37
PORTB$18 $38
PINA$19 $39
DDRA$1A $3A
PORTA$1B $3B
EECR$1C $3C
EEDR$1D $3D
EEARL$1E $3E
EEARH$1F $3F
SPDR
NameAddress
I/O Mem.
NameAddress
I/O Mem.
UBRRC
$20 $40
UBRRH
$21 $41 WDTCR
$22 $42 ASSR
$23 $43 OCR2
$24 $44 TCNT2
$25 $45 TCCR2
$26 $46 ICR1L
$27 $47 ICR1H
$28 $48 OCR1BL
$29 $49 OCR1BH
OCR1AH$2B $4B
SFIOR$30 $50
OCDR
$31 $51
OSCCAL
$32 $52
TCCR0$33 $53
MCUCSR$34 $54
MCUCR$35 $55
TWCR$36 $56
SPMCR$37 $57
TIFR$38 $58
TIMSK$39 $59
TCNT1L$2C $4C
TCNT1H$2D $4D
TCCR1B$2E $4E
TCCR1A$2F $4F
TCNT0
$3A $5A
GICR$3B $5B
OCR0$3C $5C
SPL$3D $5D
SPH$3E $5E
GIFR
OCR1AL$2A $4A SREG$3E $5E
99. 99
ATMega32 Pin out & Descriptions
These pins are used
to connect external
crystal or RC
oscillator
Provides supply
voltage to the chip.
It should be
connected to +5
Supply voltage for
ADC and portA.
Connect it to VCC
Clears all the
registers and
restart the
execution of
program
Reference voltage
for ADC
Port APort B
Port D
Port C
111. 111
ATMega32 Elementary I/O: LEDs
• LEDs must be connected the correct way round, the
diagram may be labeled a or + for anode and k
or - for cathode (yes, it really is k, not c, for
cathode!). The cathode is the short lead and there
may be a slight flat on the body of round LEDs. If
you can see inside the LED the cathode is the larger
electrode (but this is not an official identification
method).
• LEDs can be damaged by heat when soldering, but
the risk is small unless you are very slow. No special
precautions are needed for soldering most LEDs.
• LEDs are available in red, orange, amber, yellow,
green, blue and white. Blue and white LEDs are
much more expensive than the other colors. The
color of an LED is determined by the semiconductor
material, not by the coloring of the 'package' (the
plastic body).
An LED is a semiconductor device that converts electrical energy directly into a
discrete color of light
112. 112
ATMega32 Elementary I/O: LEDs
• Never connect an LED directly to a battery or power supply! It will be destroyed
almost instantly because too much current will pass through and burn it out.
• LEDs must have a resistor in series to limit the current to a safe value, for quick
testing purposes a 1k resistor is suitable for most LEDs if your supply voltage is 12V
or less.
• The resistor value, R is given by:
• Estimate 1.5 - 2 V voltage drop (VL)
• Typically draws 10-20 mA (I)
Unless you know
what are you doing!
113. 113ATMega32 Elementary I/O: LEDs
+5V
R
output pin
1
+3.2V
No current
Current
light
Estimate R =
voltage
current
=
5 – 1.8 – 0.4
13 x 10 -3 = 215 Ω ~ 220 Ω
easier to get
LED
no light
+5V
+5V
R
output pin
0
LED
+0.4V
Turning on an LED
Setting the pin to high will not
turn ON the LED
Setting the pin to low will turn
ON the LED
114. DIO_LAB1 114
Write A embedded C
Code
To toggle 3 Led on
sequential way
Then start a tick(pulse) by
Buzzer then
Rerun again
Port D pin 5,6,7 as output (LED)
Port D pin 4 (Buzzer)
step1
step2
step3
step4avr/io.h and util/delay.h
120. DIO_Project1
Seven Segment Digital Clock
Write a program for a 2-D Led array
(1*2 LCD) using all GPIO pins in
Atmeg32, that acts like a real screen.
The Screen is divided into two (8*8)
frames (characters), and write a two
functions that takes a character and
print it for each frame. And simulate
the project using proteus. the following
figure shows one frame
120
121. DIO_Project1
Seven Segment Digital Clock
Say if we want to turn on LED D10 in
the matrix, we need to power the
PIN14 of module and ground the
PIN3 on module. With this the D10 will
turn ON. This is shown in the figure
below. This should be first check for
MATRIX to know everything in order.
121
122. DIO_Project1
Seven Segment Digital Clock
Say if we want to turn on D1, we
need to power PIN9 of matrix and
ground the PIN13. With that LED D1
will glow. The current direction for this
case is shown in below figure.
122
123. DIO_Project1
Seven Segment Digital Clock
Now for the tricky part, consider we
want to turn on both D1 and D10 at a
time. So we power both PIN9, PIN14
and ground both PIN13, PIN3. With
that we will have D2 and D9 ON
along with D1 and D10. It’s because
they share common terminals. So if we
want to turn LEDs along the diagonal,
we will be forced to turn ON all the
LEDs along the way. This is shown in
below figure.
123
124. DIO_Project1
Seven Segment Digital Clock
So to eliminate this problem we will turn only one led on at a
time. Say at t=0m SEC, LED D1 is tuned ON. At t = 1m SEC, LED
D1 is tuned OFF and LED D2 is turned ON. Again at t=2 m SEC,
LED D2 is turned OFF and LED D1 is turned ON. This goes on.
Now the trick is, the human eye cannot capture a frequency
more than 30 HZ. That is if a LED goes ON and OFF
continuously at a rate of 30HZ or more. The eye sees the LED
as continuously ON. However this is not the case. The LED will
be constantly turning ON and OFF. This technique is called
multiplexing.
By using multiplexing, we will turn only one row at a time, and
there will be cycling around the 8 rows continuously. This
visualized as a completely turned ON matrix for a naked eye.
124
Multiplexing
With very speed
To render
Each row
125. 125
Led 2 1 0
write embedded C program
“up/down binary Counter”
By three leds and two push button
0
1
7
……
sw0sw1
DIO_Quiz1
126. 126
I/O: 7-Segment Display
There are applications where you need to display a decimal
digit using a seven segment LED display.
The display could represent e.g. the number of times a
switch was pressed.
Digits 0-9 and hex A-F can be displayed by giving the
proper 7-segment codes
q g f e d c b a q g f e d c b a
0 0 1 1 1 1 1 1 8 1 1 1 1 1 1 1
1 0 0 0 0 1 1 0 9 1 1 0 0 1 1 1
2 1 0 1 1 0 1 1 A 1 1 1 0 1 1 1
3 1 0 0 1 1 1 1 b 0 0 1 1 1 1 1
4 1 1 0 0 1 1 0 C 0 1 1 1 0 0 1
5 1 1 0 1 1 0 1 d 1 0 1 1 1 1 0
6 1 1 1 1 1 0 1 E 1 1 1 1 0 0 1
7 0 0 0 0 1 1 1 F 1 1 1 0 0 0 1
127. 127
I/O: 7-Segment Display
Common-anode :
requires VCC
LED ON when Output is LOW.
Common-cathode :
NO VCC ,
LED ON when Output is HIGH.
TTL and CMOS devices are
normally not used to drive the
common-cathode display directly
because of current (mA)
requirement. A buffer circuit is
used between the decoder chips
and common-cathode display
131. 131DIO_Lab2 :
A 7-segment is connected to PORTA.
Write an assembly/embedded C Code
to Display 1 on the 7-segment.
ATmega32
8
PORTC
0
1
2
3
5
6
4
132. 132DIO_Lab2 Solution: assembly
A 7-segment is connected to PORTA. Display 1 on the
7-segment.
ATmega32
8
PORTC
0
1
2
3
5
6
4
1 1 1 1 1 1 1 1
0 0 0 0 0 1 1 0
DDRC
:
PORTC:
;
; DIO_lab2.asm
;
; Author : kkhalil
;
; Replace with your application code
LDI R20,0x06 ;R20 = 00000110 (binary)
OUT PORTC,R20 ;PORTC = R20
LDI R20,0xFF ;R20 = 11111111 (binary)
OUT DDRC,R20 ;DDRC = R20 ;Delay 1
;cycle latency
L1: RJMP L1
133. 133
DIO_Lab3 :
Two- digital
Display Module
7 Segment Display
DATA LINES:
- 7SEG_APORTC .4
- 7SEG_BPORTC .5
- 7SEG_CPORTC .6
- 7SEG_DPORTC .7
DECIMAL POINT:
- 7SEG_DPPORTB .0
ENABLE LINES
- 7SEG_EN1PORTC .2
- 7SEG_EN2PORTC .3
The trick is, the human eye cannot capture a frequency
more than 30 HZ. That is if a LED goes ON and OFF
continuously at a rate of 30HZ or more. The eye sees the
LED as continuously ON. However this is not the case.
The LED will be constantly turning ON and OFF. This
technique is called multiplexing.
134. DIO_Lab3 :Two- digital Display Module
Write an embedded C Code to “up Counter” from 0 to 99.
134
135. DIO_Lab3 :Two- digital Display Module
Write an embedded C Code to “up Counter” from 0 to 99.
135
136. DIO_Lab3 :Two- digital Display Module
Write an embedded C Code to “up Counter” from 0 to 99.
136
139. DIO_Project2
Seven Segment Digital Clock
Our digital clock operates as follows. When the circuit is powered up the clock
starts at "00:00:00" for "HH:MM:SS". There are two push-button switches used to
set the time. Switch SW1 is for setting the minutes, when this button is pressed the
minutes in increase until it reaches 59 then reset and start counting from 0.
Switch SW2 is for setting the hours.
All seven segment displays are driven by the same port (PortB). The microcontroller
through controls from PortC indicate which seven segment display each digit is
displayed on. Note that these Seven Segment Displays are common
cathode display.
139