$30
Computer Science 230
Assignment 2
Objectives
More practice writing assembly language programs
Introduction to peripherals: LEDs and buttons
Introduction to subroutines
AVR Studio Project
As in assignment 1, you should create a new assembly language project in AVR Studio for this
assignment. Download the sample code and add the files to the project.
Please note that this assignment comes in two parts. Part 1 is a problem
set on conneX. You fill your answers directly on conneX. Part 2 is a set of
programming assignments that are given below.
Assignment 2 Part 1: Problem set on conneX
Complete the Assignment 2 Part1 problem set on conneX. This is available in Tests & Quizzes section.
Assignment 2 Part II – Assembly Programming Assignments
Question 1: Lighting the LEDs
As you've seen in the lab, the boards include a 6 LED strip, where each LED can be controlled
independently from the others. Although the LEDs are wired to adjacent Arduino pins, they are
controlled using two AVR ports: PORTB and PORTL. The exact mapping is shown in the table below:
Pin PORT Bit #
42 L 7
44 L 5
46 L 3
48 L 1
50 B 3
52 B 1
You should modify the code in a2q1.asm so that it displays the binary value in R0 of bits 5-0 in binary
using the 6 LEDs ignoring bits 6 and 7. You should consider the LED on Pin 42 to be the leastsignificant bit and the LED on Pin 52 to be the most-significant bit.
For example, if R0 contains the value 0x13, the LEDs should look like:
Pin 52 50 48 46 44 42
Value OFF ON OFF OFF ON ON
Be sure to test your code on several different values. Your instructor's solution used a combination of
ANDI and BRNE instructions to test the individual bits in R0 and then used ORI to set individual bits in
two output registers: one that is output to PORTL and another that is output to PORTB.
Question 2 – Subroutines and timing
Using the code you wrote in Question 1 above and the delay code provided to you in Lab 4, using
a2q2.asm write a program that does the following:
counter = 0
start:
display counter in binary on the LEDs
counter = counter + 1
delay between 100 and 200 milliseconds
goto start
First you should turn the code you wrote in Question 1 above into a subroutine called display. Then
write a main program that implements the algorithm shown above. It will be easiest if you use R0 for
your counter.
Question 3 – Button press counting
Using the button code and delay code provided to you in Lab 4, using a2q3.asm that is provided to
you write a program that does the following:
counter = 0
start:
if button pressed
counter = counter + 1
delay between 100 and 200 milliseconds
display counter in binary on the LEDs
goto start
Question 4 – Improved button code
Improve the button code so that it correctly determines which button has been pressed. Study the
comments in a2q4.asm for details on the values returned from the analog to digital conversion when
the buttons are pressed.
You've been provided with a main program – you'll need to fix the button subroutine and include your
LED display subroutine. Once you do that, a different light should come on for each button you press.
Your instructor's solution used the BRSH instruction.
Submission
Submit your a2q1.asm, a2q2.asm, a2q3.asm and a2q4.asm using connex. Do NOT submit your
project file – just the .asm files.
Grading
If you submit a program that does not assemble you will receive 0 for that part of the assignment.
Question 1 5 marks
Question 2 5 marks
Question 3 6 marks
Question 4 4 marks
Total 20 marks