辅导案例-ENGN3213

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
ANU College of Engineering and Computer Science





Lab Six

Breadboarding and programming the
ATmega328 microcontroller

ENGN3213
Digital Systems and Microprocessors





Semester 1, 2020


Copyright © 2013-20, The Australian National University



















ENGN 3213/6213 Digital Systems and Microprocessors

ANU College of Engineering and Computer Science Page 2/11

TABLE OF CONTENTS

1. INTRODUCTION ............................................................................................................................... 3
2. AIMS OF THE LAB ............................................................................................................................ 3
3. THE ATMEL ATMEGA328 MICROCONTROLLER ........................................................................... 3
3.1. Appearance of the ATmega328 .................................................................................................. 3
3.2. TinkerCAD ................................................................................................................................... 4
Creating a design:........................................................................................................................... 4
Sharing your design ........................................................................................................................ 5
3.3. Arduino with ATmega328P ......................................................................................................... 5
4. YOUR FIRST MICROCONTROLLER PROGRAM [7 marks] ............................................................ 6
4.2. Coding ......................................................................................................................................... 8
4.3. Simulating .................................................................................................................................... 8
5. YOUR FIRST MICROCONTROLLER PROGRAM (in ASSEMBLY) ................................................. 9
5.1. The program ................................................................................................................................ 9
5.2. Your exercise ............................................................................................................................ 10
5.2.1. Interpreting the code [3 marks] ........................................................................................... 10
5.2.1. Making changes [3 marks] .................................................................................................. 10
6. SOME INDEPENDENT WORK ....................................................................................................... 11
6.2. Your exercise [7 marks] ............................................................................................................ 11









ENGN 3213/6213 Digital Systems and Microprocessors

ANU College of Engineering and Computer Science Page 3/11
1.INTRODUCTION
During this lab you will have the opportunity to work with a microprocessor, the AVR ATmega328 by
Atmel. This chip (this is also true of its siblings with similar architecture) is fairly inexpensive (at less
than $10) and widely used in industrial applications (automotive and robotics mainly, but hey, you could
come up with some new ones). It is also the heart of the popular Arduino hardware products.

Social distancing provisions in place as a result of the current COVID-19 pandemic are preventing us
from running labs in person, so 2020 labs will be done on a virtual circuit simulator called Autodesk
TinkerCAD Circuits. It is an online platform which can be used to build and simulate basic circuits with
Arduino. While there are other simulation products available, some of which with greater complexity
and functionality (e.g., Virtual Breadboard), TinkerCAD Circuits was chosen as it is browser-based and
therefore OS-independent.

During the first part of this Lab, you will use TinkerCAD to create the virtual circuit using breadboard,
the microcontroller and other components, while in the second part of the lab we will be running some
pre-written simple program in C. Finally, you will have the time to run something of your own.

2.AIMS OF THE LAB
• To gain hands-on skills about breadboarding a microcontroller
• To learn how to program a microcontroller

3. THE ATMEL ATMEGA328 MICROCONTROLLER
You have learned about the architecture of ATmega8 microcontroller in lectures. The Arduino hardware
available for simulation in TinkerCAD, however, is based on an ATmega328P microcontroller. This
means that the lab work presented here will be done on an ATmega328 chip instead of an Atmega8.
This is not a major hurdle, as the ATmega328P microcontroller is extremely similar to the Atmega8 in
most aspects. To assist you with adjusting your lecture knowledge to this platform, some general
Atmega328P-specific information is provided in the next subsections. Also, be sure to look out for
paragraphs with the header Atmega328P VS Atmega8 throughout the lab documents as these will
highlight differences that might be relevant for a specific exercise. The datasheet for Atmega328P
contains detailed specifications about the chip, its features and associated registers and will be an
indispensable aid for you to tackle this and future labs. It is available here and a copy is also uploaded
in Wattle for your convenience.
3.1. Appearance of the ATmega328
Just like the Atmega8, ATmega328P comes in a 28-pin, plastic dual in-line package (PDIP) as shown
in Figure 1. PDIPs have the largest pin spacings of all packages, which facilitates for manual wiring up
of circuits (e.g., through breadboarding). Other packages with much smaller footprints do exist.
The device features up to three 8-bit signal ports on pins PBx, PCx, PDx (for x=0-7), a UART interface
(pins 2 and 3), six analog-to-digital converter inputs (ADC) on pins 23-28, two external interrupts (4 and
5) and many others. Note the half-moon shape at the top of the chip, which assists with determining
orientation of the microcontroller and locating the correct pins.
Six of the ports are used for programming the MCU via the Serial Peripheral Interface (SPI) protocol.
SPI has data and clock lines: MOSI (pin 17), MISO (pin 18) and the clock SCK (pin 19). The power
ENGN 3213/6213 Digital Systems and Microprocessors

ANU College of Engineering and Computer Science Page 4/11
supply AVCC (pin 19) and ground (pin 22) are also sent to the SPI capable programming device.
Note: SPI programmer connections are listed here for interest but will not be necessary for TinkerCAD
work as program compilation and deployment to the virtual microcontroller are provided by the
simulation environment.
A reset pin is also present (Pin 1). When asserted, a reset input causes the microcontroller to restart
execution of the embedded program from the beginning.


Figure 1: ATmega328 pinout and image of the chip

Finally, note that many of the pins on the ATmega328 PDIP package are multiplexed. This means that
a given pin can be assigned to serve multiple purposes (thus reducing the overall footprint for the
device), however, this also means that it may not be possible to use certain features at the same time
in a program (for example, it may not be possible to use pin #23 as digital output for portC-bit0 and
input for ADC0 at the same time).

3.2. TinkerCAD
You have to create a free account in Autodesk in order to use this tool. After creating and logging in to
the account, you will be able to create circuits on a virtual breadboard. This tool being a simulator, tries
to mimic the behavior of hardware to an extend but may not necessarily reflect the real-world
performance of the designs.

Creating a design:
1. Sign up into TinkerCAD at https://www.tinkercad.com/join and create an Autodesk personal
account if you haven’t already.
2. Log in to TinkerCAD and head to https://www.tinkercad.com/dashboard.
3. Select Circuits tab option from list at the left-hand side of the dashboard.
ENGN 3213/6213 Digital Systems and Microprocessors

ANU College of Engineering and Computer Science Page 5/11
1. Click on Create New Circuit button. This will take you to the Circuit view (Figure 2) screen.
Figure 2: Circuit view

2. Name your design by editing the text above the Edit tool bar.
3. In the Circuit view you can design your circuits by selecting the components from the
Component picker. Type the component name in the Search box to find it and drag it on to
the Design editor area to start using it.
4. After bringing components to the Design editor area you can rotate and delete using Edit
tool bar.
5. The properties or values of each component can be modified by clicking on it and changing it
in the Component editor popup that appears near the Component list drop down (Figure
3).
6. You can go back to your Dashboard by clicking on the TinkerCAD icon.
7. To open and edit a design, click on the design’s thumbnail in the Dashboard and click on
Tinker this.
Sharing your design
In the Dashboard, you can preview, open, share and delete your designs.
1. To share your design, click on the cog icon on your design’s thumb nail and click on
Properties.
2. Change privacy to Public. You can update other details if you wish.
3. Close the Design Properties screen and click on the thumbnail again. Copy and send the
link to anyone to share the design.
3.3. Arduino with ATmega328P
TinkerCAD does not have an option to place a stand-alone ATmega328P microcontroller on a
breadboard. Instead, it offers an Arduino UNO board package (with associated ATmega328P
microcontroller). This can be found as “Arduino UNO R3” in the components search. By knowing the
mapping between ATmega328P pins and Arduino output ports (see Figure 4), we can ultimately
connect our circuit with the microcontroller features we require. The Arduino platform neatly hides or

Figure 3: Component editor
ENGN 3213/6213 Digital Systems and Microprocessors

ANU College of Engineering and Computer Science Page 6/11
simplifies some features such as clock, programming interface, power, etc. This will help us complete
designs more quickly, but it also means that we will not be able to engage with those (typically low-
level) microcontroller features.

Figure 4: ATmega328P-Arduino pin mapping


4.YOUR FIRST MICROCONTROLLER PROGRAM [7 marks]
After a brief introduction on how to use TinkerCAD software, you will be able to compile your first
program and finally bring your chip to life! (virtually). You will blink an LED using ATmega328P.

1. Create a new design with steps explained in Section 3.2. Name this
Lab6_Blinky__.

2. Select Breadboard small from the Component picker on the right side of the screen and drag it
on the design editor. You can also use the Search option to find components. Drag the component
to the design space. Add the following components and arrange neatly on and around the
breadboard
1. LED
2. Resistor
3. Arduino UNO
ENGN 3213/6213 Digital Systems and Microprocessors

ANU College of Engineering and Computer Science Page 7/11
3. Connect the components as shown in the Figure 6. Click on the points to start and finish drawing
the wires. Virtual breadboard is wired similar to real bread board (Figure 6), i.e rows marked a, b, c,
d and e are connected column wise. Similarly, f, g, h, i and j points are connected column wise.
Positive and ground rails are connected row-wise. Gutter in the middle isolates a-e and f-g. Notch
on the LED is cathode and should be connected to ground.
4. We need a 220-ohm resistor for this circuit. Click on the resistor in the circuit and a pop up
(Figure 3) will appear on the top-right of the screen. Change the value to 220 and unit to ohms.
5. Now the wiring is complete. It is time to program the ATmega328 to blink the LED.








Figure 5: Blinky circuit
Figure 6: Breadboard internal connections
ENGN 3213/6213 Digital Systems and Microprocessors

ANU College of Engineering and Computer Science Page 8/11
4.2. Coding
The next thing to do is to write the program. The example below takes you through the editing for
program, designed to light up a LED intermittently.
1. To add the program, click on the Code button on the right-top of the screen.
2. From the new code slider window, Select Text. Click Continue in the new pop-up window.
3. Replace the text with the following program shown in Code 1.
---------------------------------------------------------------------------------------------
/*
* Blinky1.c
*/

#include
#include

int main(void)
{
DDRB=0x2; //set port PB1 to be an output (equivalent to 0b00000010)
while(1)
{
PORTB=~PORTB; //change sign of port output
_delay_ms(250); //this function is in library delay.h and can force a
//maximum delay of 263ms
}
}
---------------------------------------------------------------------------------------------
Code 1: program code for Blinky1.c
4.3. Simulating
Once the code is written, you are ready to simulate.
1. Click on to hide the code slider window.
2. Click on Click on Start simulation button. The system will run the simulator if no errors
are detected.
3. The LED changes state every 250ms as per the program. You can also interact with the circuit while
simulating. Try pressing the reset button on the Arduino and observe what happens to the LED.
4. Click on to stop the simulation and return to design mode.
5. To verify the time period, connect an Oscilloscope from the Component picker across the resistor.
Then, change the Timer Per Division of the Oscilloscope to 250ms in the Component editor.
6. Run the simulation to ensure the on time and off time are 250ms.

Figure 7: Verification circuit
ENGN 3213/6213 Digital Systems and Microprocessors

ANU College of Engineering and Computer Science Page 9/11
5.YOUR FIRST MICROCONTROLLER PROGRAM (in ASSEMBLY)
In this section you will work on a program similar to Blinky, but you will do so using the Assembly
language. Although we have not worked with AVR Assembly, you should be able to interpret this code
in light of the AVR architecture knowledge which we have discussed in the lectures (particularly the
use of registers), your previous experience with simple MU0 Assembly, and, if all else fails, using the
AVR Instruction Set reference document (on Wattle). This exercise is about interpreting a short and
simple low-level program written in AVR Assembly. This section should take you 45 minutes at the
most.

5.1. The program
Below you find the assembly code for the program. TinkerCAD does not support assembly code and
thus cannot use it to program the ATmega328P directly. This is the program as would be supported in
AVR studio. An AVR-GCC compiler-friendly version of the code (which exploits a technique called inline
assembly, which allows us to compile assembly code using a c compiler) is provided in the lab
resources.
---------------------------------------------------------------------------------------------
/**************
* Blinky2.asm
*************/
.INCLUDE "m328def.inc" ;don't worry about these lines
.org 0x0000 ;don't worry about these lines

ldi r16, low(RAMEND) ;stack definition in this block
out SPL, r16
ldi r16, high(RAMEND)
out SPH, r16 ;end of stack definition

ldi r19, 0b00000010
ldi r20, 0b00000000
out 0x04, r19 ; Note 0x04 is the DDRB register address

;NOTE words followed by a colon (such as loop: below) represent a line
;label which can be used as a target for a jump or call instruction

loop: out 0x05, r19 ; Note 0x05 is the PortB register address
call delay_05
out 0x05, r20
call delay_05
rjmp loop

delay_05: ldi r16, 255
outer_loop: ldi r24, lo8(58535) ;lo8/hi8 extracts lowest/highest 8 bits of argument
ldi r25, hi8(58535)
delay_loop: adiw r24, 1
brne delay_loop
dec r16
brne outer_loop
ret
---------------------------------------------------------------------------------------------
Code 2: program code for Blinky2.asm



ENGN 3213/6213 Digital Systems and Microprocessors

ANU College of Engineering and Computer Science Page 10/11
5.2. Your exercise
5.2.1. Interpreting the code [3 marks]
As you may have noticed, in the code there are some, but not too many comments. The first task of
your exercise is to figure out what the code is doing. As a hint, we can tell you that it is blinking the LED
by using delay loops (i.e., using up processor time by counting and only switching output ports every
so many counts). Go through the program and using the AVR assembly reference document provided
in Wattle, comment the rest of the code by writing what each instruction is doing and how many clock
cycles it takes to execute. Through calculation estimate the expected frequency of the blinking. For
your reference, the system clock frequency of the Arduino UNO is 16MHz.
Once you are confident with your analysis, copy-paste the provided resource file into the TinkerCAD
code pane and simulate your design. Verify the blinking period with the oscilloscope tool.
Some hints:
- the assembly reference document has a big reference table where you can see how many clock cycles
are needed to execute each instruction.
- The TinkerCAD simulation of the circuit may not run in real time (depending on your browser and
computer performance), so do not be surprised if the LED appears to blink slower than expected. Keep
an eye for the simulation time shown above your design while the simulation is running.
- The oscilloscope tool is quite basic and does not always give an amazing rendering of your signal. Try
and play with the timescale settings until you get a good visualization. Ideally you want to have a timescale
that would capture 1 or 2 full periods of your blinky signal wave.
5.2.1. Making changes [3 marks]
Now comment out the following lines in the code. In the assembly source this would be done by
inserting semicolons as follows

; delay_loop: adiw r24, 1
; brne delay_loop

In the inline assembly version used in TinkerCAD, comments have to be introduced C-language-style
with the double forward slash, i.e.,

// " delay_loop: "
// " adiw r24, 1 \n "
// " brne delay_loop \n"


Now run the simulation again. What is the new period of the square wave? Can you explain what has
changed? Review your calculations so that you can predict the new blinking period. Confirm this by
oscilloscope observations.

ENGN 3213/6213 Digital Systems and Microprocessors

ANU College of Engineering and Computer Science Page 11/11
6.SOME INDEPENDENT WORK
The final part of this lab will see you work independently to create a new system, program it and
simulate it.

6.2. Your exercise [7 marks]
For this exercise you should (indicative time about 1 hour):

• Add another two LEDs to the breadboard, connected to another two of the port B pins (I suggest
PB0 and PB2, but ultimately it is up to you). You can add more than two if you would like to
have more fun, but in any case, two is a minimum.

Hint: LEDs have very low resistance, so if you do not want to burn your microcontroller by
drawing too much current, you should make sure that you are using your series resistor(s)
adequately. This is not going to pose an issue in simulation work, but if you are interested do
add a current meter to your port. Atmega328 ports are rated at max 20mA current output and
max 40mA current sinking. Anything greater than that may burn up a real-world chip!

• Modify your C program from part 4 so that the LEDs can flash in a desired sequence. If you
have only three LEDs all up, they should light one at a time in sequence, and stay lit for at least
200ms (a bit like the Blinky program). If you have more than that (>3), you can choose your
own pattern as long as:
o No LED stays on for more than 500ms
o The LEDs are never all on at the same time
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468