Lab 03
Lab 03
Contents
1 Acknowledgements 2
2 Administrivia 2
2.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Deliverable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.3 Hardware Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3 Introduction 3
3.1 AVR subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.2 AVR Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.3 ATmega16A Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.4 Delay Calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.5 Digital I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.6 Bit Addressing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4 Lab Tasks 6
4.1 Controlling LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.1.1 Task A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.2 Delay Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.2.1 Task B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.3 Subroutines and Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.3.1 Task C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1
1 Acknowledgements
This lab exercise is prepared by Mohammad Azfar Tariq and Muhammad Usman under the
supervision of Dr. Rehan Ahmed for the course EE-222 Microprocessor Systems. Reporting
any error or discrepancy found in the text is appreciated.
2 Administrivia
2.1 Objective
By the end of this lab you will be able to
2.2 Deliverable
You are required to submit
• Universal Programmer
• Resistance 47Ω
2
3 Introduction
3.1 AVR subroutines
When a task is to be performed repetitively, programmers usually create a separate code
block to write the respective code only once in it and jump to that block as many times, the
task is to be performed. Such programming practice saves the cost of writing the same piece
of code again and again. In the context of assembly language, it is called “subroutine”.
In AVR special instructions are present to jump and return back from a subroutine which
are CALL, RCALL, RET,, etc. Details of these instructions can be read from Microchip AVR
ISA Manual1 . For example a subroutine that adds 4 to R16 can be written as,
1 .ORG 0 x00
2 LDI R16 , 0 x11
3 CALL PlusFour
4
5 plusFour :
6 SUBI R16 , −4
7 RET
Whenever a function is called, the address of instruction after the call is pushed on the stack
and when RET instruction is executed, the address is popped back to program counter so
“for a safe function call, you must set the stack pointer to RAMEND in the start
of program”.
1
https://www.microchip.com/webdoc/avrassembler/avrassembler.wbi nstructionl ist.html
3
3.3 ATmega16A Clock
ATmega16A has many options for clock. We can provide clock to it through external crystal
or RC oscillator. However, an internal RC oscillator is also present which can be calibrated
to provide clock of 1,2,4 or 8 MHz. The factory default configurations have already set the
internal RC oscillator to provide a “1MHz” clock. In this lab we will be using ATmega16A
with factory default configurations. For more information see chapter 8 in data sheet2 .
DDRx register decides wether the port pins will act as input or output. If any bit of DDRx is
loaded with 1, the corresponding pin of the port will be activated in output mode and vice
versa for input mode. For example to activate pins of Port B as output pins, we have to
load 0xFF in DDRB.
To send some binary value out at the pins (which are set as output by DDRx), we have to
store it in PORTx register. For example to send 0xAA out at the pins of port B, we have to
store it in PORTB.
To read the status of pins (high or low) which are set as input by DDRx, we have to read
register PINx. For example, if we have to read what logic values are present at the pins of
port B, we must read the values in PINB register, they will be same.
Under normal condition a pin in a port cannot perform dual operations (–that is input and
output at the same time).
Use specific instructions “IN and OUT” to read and deliver data to I/O registers.
2
http://ww1.microchip.com/downloads/en/devicedoc/atmel-8154-8-bit-avr-atmega16ad atasheet.pdf
4
3.6 Bit Addressing
AVR ISA provides a set of special instructions to manipulate individual bits in the registers.
A few of these instructions are,
Instruction Description
SBI R, n Set bit number n in the register R to 1
CBI R, n Clear bit number n in the register R.
BST R, n Store bit number n from register R to T-Flag
BLD R, n Load bit number n of register R with the value of T-Flag
For further explanation of these instructions see Microchip AVR ISA Manual3
3
https://www.microchip.com/webdoc/avrassembler/avrassembler.wbi nstructionl ist.html
5
4 Lab Tasks
4.1 Controlling LED
4.1.1 Task A
Turn an LED on or off by sensing a switch connected to a pin as schematic suggests.
Logic low at pin should turn off the LED while logic high should immediately turn it on.
6
6 LDI R16 , ’C’
7 STS 0 x202 , R16
8 LDI R16 , ’E ’
9 STS 0 x203 , R16
10 LDI R16 , ’C’
11 STS 0 x204 , R16
12 LDI R16 , ’A’
13 STS 0 x205 , R16
14 LDI R16 , ’R’
15 STS 0 x206 , R16
16
17 CALL CHECK PALINDROME
18 Here : RJMP Here
19
20 .ORG 0 x50
21 ;− − − − − Your Code Here− − − − − −
22 CHECK PALINDROME:
23
24 ;− − − − − − − − − − − − − − − − − −