Starting from:

$30

Lab 4 Simple Function Call and Input Instructions

CSC 230 
1
Lab 4 Simple Function Call and Input Instructions
Submit lab4Button.asm at the end of your lab, not your project.
I. Simple Function Call
In the first year programming language courses such as CSc 110, CSc 111, we wrote many
functions. In assembly language, the term procedure, function, or subroutine can be used
interchangeably. A function call implies a transfer (branch, jump) to an address representing the
entry point of the function, causing execution of the body of the function. When the function is
finished, another transfer occurs to resume execution at the statement following the call. The first
transfer is the function call (for invocation), the second transfer is the return (to get back to the
calling program). Together, this constitutes the processor’s call-return mechanism.
In today’s lab, we are going to write a simple function – no parameter passing, no return value.
Create a new project named lab4. Type the following code:
Observe how pc (program counter) is changed. How program is transferred to the function “delay”
and is returned to the statement (inc r16) after the call. The value in r16 is changed in the
function call.
II. Exercises1: download lab4Blink.asm, change the code to a function call.
CSC 230 Fall 2016
2
III. Analog Inputs from Push Buttons
The six push buttons on the board are connected to one analog pin on the board. Analog means
continuous time signal and we need to use the built-in Analog to Digital Convertor (ADC) to
convert these continuous electronic analog signal to digital signal first. When a signal is sampled,
the ADC converts the analog value to a 10-bit digital value as shown below:
The following is the I/O View of the ADC:
CSC 230 Fall 2016
3
The memory addresses in SRAM are shown at the diagram below:
ADC has multiple registers just like other ports we have used and they need to be setup properly.
These registers are: ADCSRA (Status Register), ADMUX (selects a channel, as ADC can handle up
to 8 channels) and ADC (10-bit Data register). ADC is split into two 8-bit registers ADCL (8-bits)
and ADCH (only 2-bits are used)
Expand the “+” sign for details. Give them a symbolic name using the following code:
Set the proper values for those I/O registers:
CSC 230 Fall 2016
4
A brief description of the registers:
To initialize the ADC, several I/O registers should be set up properly. These are: ADEN bit in
ADCSRA, reference signal bits REFS1:S0 in ADMUX, the channel to be selected. If ADLAR is
enabled, then the conversion is just 8-bits and provided in ADCH.
ADCSRA: 0x87 = 0b 1000 0111 (means enable the ADC and slow down the ADC clock from
16mHz to ~125kHz, 16mHz/128).
ADMUX: 0x40 = 0b 0100 0000 (means use the AVCC with external capacitor at AREF pin and
use the right adjustment since ADCLAR is 0, ADC0 channel is used.) The reason for the right
adjustment is that the analog signal is digitized to a 10 bits value. ADCH:ADCL is 2 bytes (16bits). 
CSC 230 Fall 2016
5
The digitized value is either left-adjusted or right-adjusted. You can find more information about
these registers in the AVR Datasheet.
In order to use the ADC, first initialize it and then enable conversion when needed. The bit ADSC
in ADSRA is used for this purpose. When this is set to 1, ADC will start converting the analog
signal to digital format. This bit will continue to remain as 1 as long as the conversion is in progress.
It will be reset to 0 after the conversion is done. After the conversion is done, the 10-bit digital
value is available in ADCH:ADCL. That is what the following code is doing.
IV. Exercises: download lab4Button.asm and write the delay function you did in the previous
exercise. Please see LCD Shield Buttons.pdf document for range of values to be used.
V. Other Exercises: Modify the code so that one can detect press of any button. Use the LED lights
to display which button is pressed.
Submit lab4Button.asm at the end of your lab.
This lab is derived from the Chapter 6 of your textbook (Some Assembly Required by Timothy S.
Margush) and section 26 (ADC – Analog to Digital Converter) of the datasheet of ATMEGA 2560
(See AVR Resources on connex)
In Single Conversion mode, turn on the
ADC start conversion bit (ADSC) to start.
In Single Conversion mode, ADSC bit is 1
if the conversion is in progress, 0 if the
conversion is completed.
CSC 230 Fall 2016
6
LCD Shield Buttons
The LCD keypad shield we use in the lab is made by DF Robot. It looks like there are two versions
of this board (v1.0 and v1.1). As the boards are not marked with version number, there is a high
chance that both versions of these boards may be present in the lab. This lab assumed it is v1.0
board. So please use the following table and test your code in case of any problems. The limits are
decimal values and corresponding hex values are provided here. Thanks.
if (adc_key_in 1000) return btnNONE; (1000 = 0x3E8)
// For V1.1 use this threshold
if (adc_key_in < 50) return btnRIGHT; (50 = 0x032)
if (adc_key_in < 250) return btnUP; (250 = 0x0FA)
if (adc_key_in < 450) return btnDOWN; (450 = 0x1C2)
if (adc_key_in < 650) return btnLEFT; (650 = 0x28A)
if (adc_key_in < 850) return btnSELECT; (850 = 0x352)
// For V1.0 use the one below:
if (adc_key_in < 50) return btnRIGHT; (50 = 0x032)
if (adc_key_in < 195) return btnUP; (195 = 0x0C3)
if (adc_key_in < 380) return btnDOWN; (380 = 0x17C)
if (adc_key_in < 555) return btnLEFT; (555 = 0x22B)
if (adc_key_in < 790) return btnSELECT; (790 = 0x316)

The schematic on the LCD shield is posted below:
http://www.dfrobot.com/image/data/DFR0009/LCDKeypad%20Shield%20V1.0%20SCH.pdf
It’s also available on ConneX AVR Resources.

More products