You are on page 1of 18

1

MODUL PERKULIAHAN

PERANCANGAN
BERBASIS
MIKROPROSESOR

Assembly

Abstrak Sub-CPMK (lihat di RPS)

Diisi dengan deskripsi pokok Sub-CPMK X.X


bahasan dari setiap modul Kemampuan akhir yang direncanakan
pertemuan (minggu) yang dari setiap modul pertemuan (minggu)
mengacu kepada capaian untuk memenuhi CPMK dan CPL
Sub-CPMK

Fakultas Program Studi Tatap Muka Disusun Oleh

05
Zendi Iklima, M.Sc
Fakultas Teknik Teknik Elektro
ATmega328
ATmega-328 is basically an Advanced Virtual RISC (AVR) micro-controller. It
supports the data up to eight (8) bits. ATmega-328 has 32KB internal builtin memory. This
micro-controller has a lot of other characteristics.
ATmega 328 has 1KB Electrically Erasable Programmable Read Only Memory
(EEPROM). This property shows if the electric supply supplied to the micro-controller is
removed, even then it can store the data and can provide results after providing it with the
electric supply. Moreover, ATmega-328 has 2KB Static Random Access Memory (SRAM).
ATmega 328 has several different features which make it the most popular device
in today’s market. These features consist of advanced RISC architecture, good
performance, low power consumption, real timer counter having separate oscillator, 6 PWM
pins, programmable Serial USART, programming lock for software security, throughput up
to 20 MIPS etc. ATmega-328 is mostly used in Arduino.
(please have a look ATMega328 Datasheet you’ve downloaded)
• ATmega328 is an 8-bit and 28 Pins AVR Microcontroller,
manufactured by Microchip, follows RISC Architecure and has a
flash type program memory of 32KB.
• It has an EEPROM memory of 1KB and its SRAM memory is of
2KB.
• It has 8 Pin for ADC operations, which all combines to form PortA
( PA0 – PA7 ).
• It has 3 builtin Timers, two of them are 8 Bit timers while the third
one is 16-Bit Timer.
• Operates ranging from 3.3V to 5.5V but normally we use 5V as a
standard.

2021 Pengantar Ilmu Bahan


2 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
ATmega328 Ports

2021 Pengantar Ilmu Bahan


3 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
ATmega328 Architecture

2021 Pengantar Ilmu Bahan


4 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
ATmega328 Memory

Flash Memory has 32KB capacity. It has an address of 15 bits. It is a Programmable Read
Only Memory (ROM). It is non volatile memory.
SRAM stands for Static Random Access Memory. It is a volatile memory i.e. data will be
removed after removing the power supply.
EEPROM stands for Electrically Erasable Programmable Read Only Memory. It has a long
term data.

ATmega328 Registers

• ATmega-328 has thirty two (32)


General Purpose (GP) registers.
• These all of the registers are the part of
Static Random Access Memory
(SRAM).

2021 Pengantar Ilmu Bahan


5 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
ATmega328 Port Registers

The ATmega328P needs to know how you are using its pins. Are they inputs or
outputs? That is the purpose of the data direction registers. You write a 1 to make a pin an
output and a 0 to make it an input. In our example, PD0 to PD3 will be outputs, while PD4
to PD7 will be inputs. PD0 corresponds to DDD0 in the DDRD register. The other pins
follow the same pattern as PD0. We now have enough information to write a line of code
to establish which pins are inputs or outputs:
DDRD = 0b00001111;

ATmega328 Internal Pull-Up Resistor


What's with the PULLUP notation? As shown in the schematic, without further effort,
the switches have no useful function. Whether a switch is pressed, or not, a logic 0 will be
seen at the switch's pin. There are two ways to make the switches useful. The first is to
connect a resistor between each of a switch's I/O pin and 5V. With this "pull-up" resistor
connected, a logic 1 is recorded at the pin if the switch is not pressed. Since the switch
connects the I/O pin to ground, a logic 0 will be recorded if the switch is pressed.
If you look in the data sheet at the description of the PORT registers, you will find if
a pin is declared an input, in the port's DDR register, programming a 1 in the pin's PORT
register will turn on the internal pull-up resistor. To make it clear, in our example, PD4 - PD7
have been declared inputs. Therefore, we will program PORTD4, PORTD5, PORTD6, and
PROTD7 as 1s. We will do this in one line of code:
PORTD = 0b11110000;

2021 Pengantar Ilmu Bahan


6 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
ATmega328 Port Registers
• We have already seen an example of using the port register for enabling the internal
pull-up resistors of input pins. The other obvious use of these registers is to turn on
and turn off external components like LEDs. In our example, we have four LEDs.
• In the line of code PORTD = 0b11110000;, where we connected internal pull-up
resistors, we have also turned the four LEDs off. A logic 0 provides, essentially,
ground voltage to the device. A logic 1 provides, essentially, +5V to the device. In
our case, 0 volts turns the LEDs off. To turn an LED on, program a 1 to the pin's
PORTD register. To turn LEDs D1 and D3 on, and to keep LEDs D2 and D4 off, we
can write the following line of code:
PORTD = 0b11110101;

ATmega328 & Arduino

• ATmega-328 is the most micro-


controller that is used while
designing.
• ATmega 328 is the most
important part of Arduino.
• The program is uploaded on the
AVR micro-controller attached
on Arduino
• The encircled section analog
pins consists of the Arduino pins
which are connected to the
corresponding AVR micro-
controller ATmega-328 pins.

ATmega328 Pinout

2021 Pengantar Ilmu Bahan


7 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
ATMEL STUDIO - ATmega328

Code Explanations
r16 is register shown in slide
2021 Pengantar Ilmu Bahan
8 Biro Bahan Ajar E-learning dan MKCU
Zendi Iklima, M.Sc
10.
http://pbael.mercubuana.ac.id/
We already simulated these code using proteus/ISIS.
You need to check the OpCode in the Datasheet you’ve downloaded in the previous
lecture (ATMega328 Datasheet page 12 section 6. Instruction Set).

In this line we use the assembler directive ".def" to


define the variable “reg_temp" as equal to the r16
"working register." We will use register r16 as the one
which stores the numbers that we want to copy to
various ports and registers (which can't be written to
directly).

A register contains a byte (8 bits) of information.


Essentially it is usually a collection of SR-Latches
each one is a "bit" and contains a 1 or a 0. We may
discuss this (and even build one!) later on in this
series. You may be wondering what is a "working
register" and why we chose r16. We will discuss that
in a future tutorial when we dive down into the
quagmire of the internals of the chip. For now I want
you to understand how to do things like write code and
program physical hardware.

2021 Pengantar Ilmu Bahan


9 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
This line is a "relative jump" to the label “init_"
and is not really necessary here since the next
command is already in Init but we include it for
future use.

Set all bits in reg_temp 1’s.

After the Init label we execute a "set register"


command. This sets all of the 8 bits in the
register "temp" (which you recall is r16) to 1's.
So temp now contains 0b11111111.

2021 Pengantar Ilmu Bahan


10 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Setting a bit as 0 on the Data Direction I/O.
Register for PortB, which is DDRB, sets that
pin as output, a 0 would set that pin as input
so here, all PortB pins are outputs (set to 1).

The register DDRB (Data Direction Register


for PortB) tells which pins on PortB (i.e. PB0
through PB7) are designated as input and
which are designated as output. Since we
have the pin PB0 connected to our LED and
the rest not connected to anything we will set
all the bits to 1 meaning they are all outputs.

Load the `immediate’ number to the


temp register, if it were just ldi then the
second argument would have to be a
memory location instead.

This line loads the binary number


0b11111110 into the temp register.

Exercise 1 change the binary number


0b11111110 into 0b01000100

2021 Pengantar Ilmu Bahan


11 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
mv/move temp to DDRD, result is that PD0
is input and the rest are outputs.

Now we set the Data Direction Register for


PortD from temp, since temp still contains
0b11111110 we see that PD0 will be
designated as an input pin (since there is a
0 in the far right spot) and the rest are
designated as outputs since there are 1's in
those spots.

clr reg_temp

First we "clear" the register temp which means


setting all of the bits to zero.

out PortB, reg_temp

Then we copy that to the PortB register which


sets 0V on all of those pins. A zero on a PortB
bit means that the processor will keep that pin
at 0V, a one on a bit will cause that pin to be
set to 5V.

2021 Pengantar Ilmu Bahan


12 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Exercise 2: Use a multimeter tools to check if all
of the pins on PortB are actually zero. Is
something weird going on with PB1? Any idea
why that might be?

Exercise 3: Remove the above two lines from


your code. Does the program still run correctly?
Why?

First we "load immediate" the number


0b00000001 to temp. The "immediate" part
is there since we are loading a straight up
number to temp rather than a pointer to a
memory location containing the number to
load.

In that case we would simply use "ld" rather


than "ldi". Then we send this number to
PortD which sets PD0 to 5V and the rest to
0V.

Exercise 4: Remove the above two lines


from your code. Does the program still
run correctly? Why? (This is different
from Exercise 3 above.

2021 Pengantar Ilmu Bahan


13 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Now we have set the pins as input or
output and we have set up their initial
states as either 0V or 5V (LOW or HIGH)
and so we now enter our program "loop".

It can be anything you like in this example


we define as ‘Main:’

PinD holds the state of PortD, copy this to temp


if the button is connected to PD0 this will be 0
when the button is pushed, 1 otherwise since
PD0 has a pull up resistor it’s normally at 5V.

The register PinD contains the current state of


the PortD pins. For example, if you attached a
5V wire to PD3, then at the next clock cycle
(which happens 16 million times per second
since we have the microcontroller hooked up to
a 16MHz clock signal) the PinD3 bit (from the
current state of PD3) would become a 1 instead
of a 0. So in this line we copy the current state
of the pins to reg_temp.

2021 Pengantar Ilmu Bahan


14 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Sends the 0’s and 1’s read above to PortB this
means we want the LED connected to PB0, when
PD0 is LOW, it sets PB0 to LOW and
turn on the LED (since the other side of the LED
is
connected to 5V and this will set PB0 to 0V so c
urrent will flow)

Now we send the state of the pins in PinD to the


PortB output. Effectively, this means that PD0 will
send a 1 to PortD0 unless the button is pressed.
In that case since the button is connected to
ground that pin will be at 0V and it will send a 0 to
PortB0. Now, if you look at the circuit diagram, 0V

on PB0 means the LED will glow


since the other side of it is at
5V.

Sends the 0’s and 1’s read above to PortB this


means we want the LED connected to PB0,
when PD0 is LOW, it sets PB0 to LOW and
turn on the LED (since the other side of the LED
is
connected to 5V and this will set PB0 to 0V so
current will flow)

If we are not pressing the button, so that a 1 is


sent to PB0, that would mean we have 5V on
PB0 and also 5V on the other side of the LED

and so there is no potential difference and no


current will flow and so the
LED will not glow (in this case
it is an LED which is a diode
and so current only flows one
2021
15
Pengantar Ilmu Bahan direction regardless
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
but
Zendi Iklima, M.Sc

whatever).
This relative jump loops us back to our Main:
label and we check PinD again and so on.
Checking every 16 millionth's of a second
whether the button is being pushed and setting
PB0 accordingly.

Exercise 5: Modify your code so that your LED


is connected to PB3 instead of PB0 and see
that it works.

Exercise 6: Please create the design of the


following code using Proteus and Simulate it. If
an error found you need to solve it by your self.

2021 Pengantar Ilmu Bahan


16 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Exercise 6: Please create the design of the following code using Proteus and Simulate it.
If an error found you need to solve it by your self.
Exercise 7: Run the code using your real environment that you’ve bought. You can use
AVRDude to upload the code into the ATMega328 (or using the software I’ve shared on
GDrive).

Conclusion
ser register sets all of the bits of a register to 1’s

clr register sets all of the bits of a register to 0’s

in register, i/o register copies the number from an i/o register to a working register

Submissions
Answer the following exercises that you found on the slides (exercises 1-7).

Submit it through POST and email with subject PBM05_YOURNAME_YOURNIM.

The file contains : PDF file (Source Code, Screen Shoot, etc) and MP4 (Screen Recorder)

Deadline: 10 April 2020 (23:59)

2021 Pengantar Ilmu Bahan


17 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Daftar Pustaka

• Charles M. Gilmore, “Microprocessor : Principles and Application”,


Glencoe/Mc.Graw-Hill, 2nd International Editions, 1998.
• John Crisp,(2004),Introduction Microprocessors and Microcontrollers (2nd
Edition),an imprint of Elsevier, ISBN: 0-7506-5989-0.
• Lance A. Leventhal, “Introduction to Microprocessors : Software, Programming”,
Prentice Hall, 1998
• Barry B. Brey, “Microprocessors and Peripherals : Hardware, Software, Interfacing,
and Applications”, Merrill Publishing Company 1998.
• Barry B. Brey, “The Intel Microprocessors 80386/80486, Pentium and Pentium
Processor”, Prentice Hall / Mac Millan 4/e, 1997
• David Calcutt, Fred Cowan and Hassan Parchizadeh,(2004),8051 Microcontrollers
–An Applications Based Introduction, an imprint of Elsevier, ISBN: 0-7506-5759-6
• Jack Purdum,(2012),Beginning C for Arduino, Published by by Springer Science,
ISBN:978-1-4302-4777-7
• Michael Margolis,(2011),Arduino Cookbook, Published by O’Reilly Media,
Inc.,ISBN: 978-0-596-80247-9

2021 Pengantar Ilmu Bahan


18 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/

You might also like