Advanced Arduino Assembly – Review Material
Quiz #1 Review
Note: Answers are listed after each question but, for the best study experience always answer them on your own first.
- With respect to the functional layout of data and program memory, which architecture, Charles Babbage’s mechanical Analytical Engine designed in 1833 or Dr. John Von Neumann’s electronic EDVAC computer designed over 100 years later in 1943, most closely resemble the AVR and why?
See Answer
The Analytical Engine designed in 1833 and the EDVAC designed in 1943 have the same five (5) principle organs that make up all modern day computers (Control, ALU, Memory, Input, and Output). Functionally, the main difference, other than the names given to each organ by Charles Babbage and Dr. Von Neumann, is the way data and instructions are stored. The Analytical Engine has a separate set of punched cards for data and the program. In contrast, both data and programs are stored together within the EDVAC’s ultrasonic delay line memory. Like the Analytical Engine, the AVR architecture separates Flash program and SRAM data memory and therefore is more like the Analytical Engine. Today this is known as the Harvard Memory Model.
2. What is the address of the last byte in memory of a computer system with an 18-bit address bus and an 8-bit data bus?
See Answer
Range of addresses is to
3. What is the maximum number of characters that can be represented by an 8-bit code?
See Answer
characters
4. What is the largest unsigned number, in decimal, that can be held in an 8-bit register?
See Answer
5. What is the range of signed 2’s complement numbers, in decimal, that can be held in an 8-bit register?
See Answer
to
6. Convert to binary and hex.
See Answer
x
x
7. Convert to decimal.
See Answer
8. Perform the hex subtraction 36Bh – F6h.
See Answer
9. Applying DeMorgan’s Theorem and the Basic Laws and Theorems of Boolean Algebra; simplify the following expressions:
See Answer
=
=
10. Complete the timing diagram for the following edge triggered D Flip-Flop. Q is initially at logic .
See Answer
11. Assuming a 64-bit computer, for example the Intel Itanium microprocessor; place the following terms in ascending order relative to the number of binary digits they represent (Nibble, Word, Bit and Byte).
See Answer
Bit, Nibble (4 bits), Byte (8 bits), Word (64 bit architecture)
12. Which register in a CPU always contains an address?
What address is contained in this register?
See Answer
Program Counter (PC).
The program counter holds the address of the next instruction to be executed.
13. During an opcode fetch, what is the information on the address and data buses?
What is the direction of information flow on these buses during an opcode fetch?
See Answer
14. The sole purpose in life of a computer is to do what?
See Answer
The sole purpose in life of a computer is to fetch and execute instructions.
15. The Arduino Uno Board uses the Atmel Atmega328P microcontroller. Use the web to complete the following table.
Feature | ATtiny24 | ATmega328P | ATmega644P |
Flash Program Memory | |||
SRAM Data Memory | |||
EEPROM | |||
Timers (8 and 16 bit) | |||
I/O pins | |||
Serial peropherals (UART, USART, USI, SPI) | |||
10-bit A/D channels |
See Answer
Feature | ATtiny24 | ATmega328P | ATmega644P |
Flash Program Memory | 2KBytes | 32KBytes | 64Kbytes |
SRAM Data Memory | 128 Bytes | 2 KBytes | 4 KBytes |
EEPROM | 128 | 1 KBytes | 2 KBytes |
Timers (8 and 16 bit) | 2 | 3 | 3 |
I/O pins | 12 | 23 | 32 |
Serial peropherals (UART, USART, USI, SPI) | 2 | 2 | 3 |
10-bit A/D channels | 8 | 8 | 8 |
AVR Assembly Language Programming
16. List all AVR ISA (Instruction Set Architecture) registers.
See Answer
Program Counter (PC)
General Purpose Registers R0 thru R31 Status Register (SREG)
Stack Pointer (SP)
17. For the three addressing modes listed, indicate where the data is located and at what address.
Addressing Mode | Data is located here | ATmega328P Address Space |
Register | ||
Immediate | ||
Direct |
See Answer
Addressing Mode | Data is located here | ATmega328P Address Space |
Register | CPU | 0016 – 1F16 (R0 – R31) |
Immediate | Flash Program Memory | 000016 – 03FF16 |
Direct | SRAM Data Memory I/O | 000016 to 08FF16
000016 to 003F16 |
EE346A Quiz #2 Name ___________________
Key ______
Frequency _____ Hz
Clock Frequency: 8MHz, 10MHz, 16MHz, 20MHz
Timer: T0, T1, T2, Custom ____ (default 12 bits)
IMPOTANT: Please read before you begin:
- Unless otherwise directed (see question 8), please provide base 10 answers using decimal notation (no powers of 2 or fractions 1/3). If not a whole number, you can limit written answers to three places past the decimal point. Please round up or down as needed (round 5 up). For example 62.4875 would be written as 62.488.
- Prefix hexadecimal digits using 0x notation.
- To avoid round-off errors later in the quiz, use the memory function on your calculator to save the answer to earlier (dependent) question.
- Always check your answers. When possible turn your answer into a question. For example, take your answer to question 9 and convert to a decimal number, the answer to question 8, to verify that conversion is correct. Using this approach you can also verify your answers to questions 6, 4, and 7.
Question | Answer | Units | ||
1 | How many bits is the Timer/Counter | bits | ||
2 | Assuming the counter is cleared (equal to 0), How many tics of the clock have occurred at the moment the overflow bit is set (for example for Timer 1 overflow bit is TOV1)? | tics | ||
3 | What is the period of the waveform? | msec | ||
4 | Assuming a 50% duty cycle, what delay should be programmed into the Timer/Counter? | msec | ||
5 | What clock prescalar should be selected (1, 8, 64, 256, and 1024)? | |||
6 | Assuming this prescalar value, how many microseconds does it take for 1 tic of the clock? | µsec | ||
7 | What is the maximum delay that can be generated by this delay? | msec | ||
8 | What whole number (positive interger) should be preloaded into the Timer/Counter register(s) to generate the desired delay in decimal? | Base 10 | ||
9 | What value should be preloaded into the Timer/Counter register(s) to generate the desired delay in hexadecimal? | Base 16 | ||
10 | What assembly instruction opcode mnemonic (do not include operands) would you use to load this constant into register r16? | |||
Show all your work here and on the back of this page.
Quiz 3 Study Guide
While I always hope to cover all the material and examples in the lecture material, I typically run out of time before material. Here are some of the lectures where you can find problems and examples you can work on at home.
- Lecture 01 to 09 Programming and Lab Basics (Load-Store, SREG, Branching and Looping, Subroutines, GPIO)
- Lectures 10 to 12 Interrupts , for example lecture 11 page 15 “Practice Problems”
- Lecture 13 AVR Indirect Addressing Modes – pages 11 and 16 Program Examples
- Lecture 14 AVR Logic and Shift – Page 5 Knight Rider, examples in slides, and Questions page 14
- Lecture 15 AVR Stack Operations – Review example on page 6
You can find review question in the AVR Final Review document under the corresponding lecture headings.
Lecture 01 to 09 Programming and Lab Basics
- Given two numbers, calculate the difference, signed and unsigned relationship.
- For a given arithmetic operation (add or subtract) define the state of SREG bits H, S, V, N, Z, C.
- Know how to simulate a call to ReadSwitches.
- Know how to save and restore the Status Register (SREG)
- Know the methods for sending information to and from a subroutine.
- Be prepared to write a program to send data to and receive data from a subroutine. Specifically, in a register or one of the SREG bits. Your program will not be required to implement a stack frame.
- Be able to identify code which violates one or more of the rules for working with a subroutine or an interrupt service routine. For example the code jumps out of a subroutine, a push is not matched to a pop instruction, or a ret instruction is used to end an ISR.
Lecture 10 to 12 – Interrupts
- Understand how an ISR is different from and similar to a subroutine.
- Be able to locate interrupts within the Interrupt Vector Table (IVT) and the priority of each.
- Know how to configure an interrupt to be triggered based on the nature of the input signal (low logic level, logic change, falling or rising edge.
- Know how to enable a given external interrupt(s). This external interrupt(s) might be one of our two dedicated external interrupt lines or one or more of our pin-change interrupt lines.
- Know what happens when an interrupt is triggered and what if any registers are placed on the stack.
- Given our ground bounce (low-pass filter) circuit be able to generate the output for a given clock a button input condition.
Lecture 13 – Indirect Addressing Mode
- What does defining a table actually do? Does it give addresses for constants that already exist in program memory or does it do something else?
- What is an index and why is it used?
- If the least significant bit selects whether ZL or ZH is used, is one of them (ZL or ZH) a 7 bit register?
- Do the mnemonics “low” and “high” automatically correspond to the low and high bytes of the Z-register?
- Specifically for lab 10, why is the index equal to 20*row+col?
- What register pair is found in the source operand address field of an lpm instruction?
- What register numbers correspond to pre-defined mnemonics ZH:ZL?
Lecture 14 – Indirect Addressing Mode
- Be able to clear, set, and toggle bits, including setting a bit pattern (see page 12)
- Know how to test if one or more bits are set or cleared.
- Understand multiplication and division by 2 using shift instructions.
Lecture 15 – AVR Stack Operations
- Understand the difference between the difference between a LIFO and a FIFO stack
- Understand the difference between an implicit and explicit stack operation.
- Given the address of an rcall instruction, the address where the called subroutine begins, and the value of the stack pointer before the call; be prepared to calculate what the stack pointer will be equal to after the call instruction; at the beginning of the subroutine and the contents of the stack.
- Be able to identify the code within a subroutine that will result in stack incoherence or a poorly formed series of pushes and pops.
Load-Store Worksheet
As you work through the following questions, please have at hand your reference pages provided along with Midterm 1.
Question #1:
Computers at the most fundamental level manipulate data. This may be as simple as transferring the data from one place to another or executing an add operation. In a RISC architecture the data to be manipulated is contained in one or more general purpose registers within the central processing unit (CPU) of the microcontroller. These registers go by a number of names including temporary and scratch-pad. How many general purpose registers does our AVR processor have and how many bits wide is each?
Answer #1:
The AVR processor has 32 8-bit registers.
Loading Data
Question #2:
Another way of saying this is that all operations on the data are register-register. To get the data into the CPU we will need to “Load” it from somewhere. That somewhere can be from SRAM Data memory, Flash Program memory, an Input/Output register, or Extended Input/Output register.
I want to multiply a number in one of our registers by the constant 3310. Where would you expect to find the number 3310?
a) SRAM Data b) I/O Register c) Extended I/O Register d) Flash Program Memory
Answer #2:
Constants are encoded with the program instruction, so the answer is Flash Program memory.
Question #3:
Looking at the “Register Summary” you can see that each of our three general purpose I/O ports (B, C and D) are defined by three registers (PORT, DD, and PIN). Notice that all of these registers have two addresses. The first number in the “Address” column, is the I/O address, the second (in parenthesis) is its duplicate address within the data memory address space. For now we will simply use the I/O address.
I want to read a temperature sensor, whose digital value is wired to one of our three general purpose digital I/O Ports. In what “address space” can you find this data?
a) SRAM Data b) I/O Register c) Extended I/O Register d) Flash Program Memory
Hint: Look at the “Register Summary” pages in the handout.
Answer #3:
All of our general purpose digital I/O ports are located in the I/O register address space. Specifically, I/O address 0x00 to 0x3F.
Question #4:
When the AVR processor was originally designed, the architects assumed that 64 I/O addresses (0x00 to 0x3F) would be sufficient for accessing all the peripheral devices co-located with the processor. When Atmel adopted the AVR processor as its core for the ATmega line of microcontrollers, they quickly discovered that they would need more than 64 I/O addresses. To accommodate the additional space they mapped these new “Extended” I/O registers into the SRAM address space of the ATmega and named it the Extended I/O address space. Looking at the register summary you can find these extended I/O registers starting at memory location 0x60 to 0xFF. This means that our actual SRAM data memory begins at address 0x0100.
With respect to the “Register Summary” table where would you find the registers used by the ATmega Analog-to-Digital Converter (ADC Data Registers).
a) SRAM Data b) I/O Register c) Extended I/O Register d) Flash Program Memory
Answer #4:
The result of an analog-to-digital conversion are located in registers ADC Data Register Low byte and ADC Data Register High Byte. These registers are located at Extended I/O address 0x78 and 0x79 respectively.
Question #5:
I want to load a variable named X, where would you expect to find this variable?
a) SRAM Data b) I/O Register c) Extended I/O Register d) Flash Program Memory
Answer #5:
Variables which are located in SRAM data memory.
Question #6:
Where would I find an Array?
a) SRAM Data b) I/O Register c) Extended I/O Register d) Flash Program Memory
Answer #6:
An array is simply an indexed set of variables, so they would be located in SRAM data memory.
Question #7:
I want to add an offset of 10010 to each value in an array. Where would you expect to find the number 10010 in my program?
a) SRAM Data b) I/O Register c) Extended I/O Register d) Flash Program Memory
Answer #7:
Because I know I always want to add 100 – this is a constant that I would save in Flash Program memory. This number would be encoded with the instruction.
Question #8:
I want to load the value of the Watchdog Timer Control Register (WDTCSR). Where can I find this register?
a) SRAM Data b) I/O Register c) Extended I/O Register d) Flash Program Memory
Answer #8:
Looking at the Register Summary, you will find WDTCSR in the Extended I/O address space of the ATmega. In this example, extended I/O address 0x60.
Question #9:
When the AVR processor executes an arithmetic instruction, like add, we often want to know more than simply the answer. For example; is the answer correct? Is the answer zero? These are some of the answers located in the Status Register (SREG). Where would you find this data?a) SRAM Data b) I/O Register c) Extended I/O Register d) Flash Program Memory
Answer #9:
The status register is located in the I/O address space of the AVR processor at location 0x3F. Once again, if we are presented with both an I/O address 0x3F and a memory address (0x5F) we will classify the register as being within the I/O address space of the processor.
Question #10:
I always want to multiply a variable by ninety seven (9710). Where would you find the number 9710?
a) SRAM Data b) I/O Register c) Extended I/O Register d) Flash Program Memory
Answer #10:
I always want to multiply by 9710, so the number would be encoded with the load instruction. Consequently, the data would be saved in Flash Program memory.
Question #11:
Once we know where the data is located we also know the addressing mode. In fact, “addressing mode” is just technical speak for saying where is the data?
If the data is located in Flash Program Memory you would use the immediate addressing mode to load it into one of our general purpose registers. Specifically, you would use the “load immediate” ldi Rd, K assembly instruction. Where ldi is a short hand way “mnemonic” of saying LoaD Immediate. The source operand is constant K (upper case K) and destination operand address Rd is the register we want to contain the constant after the instruction executes. The word “operand” is again just technical speak and based on the context may be read as the “address of the data” or simply the “data” itself. For our load immediate instruction would our constant K operand be read as “the address of the data” or simply as “data”?
Answer #11:
The constant is encoded with the instruction so the operand is the “data.”
Question #12:
If the data is located in SRAM data memory, you would use the direct addressing mode. You would also use the direct addressing mode if the data is located in the extended I/O address space. There are other addressing modes that can access, these locations within the memory address space; However, we will leave them for now. The instruction you use to load data “directly” from SRAM data memory is lds Rd, k. Where lds is a short hand way of saying LoaD from SRAM data memory. The source operand is the address k (lower case K) in data memory containing the data, and destination operand addres Rd is the register we want to contain the data after the instruction executes. For our load from SRAM data memory instruction would our constant k operand be read as “the address of the data” or simply as “data”?
Answer #12:
The source operand field encoded with the instruction contains the “address of the data.”
Question #13:
You might be surprised to learn that you use the same instruction to access SRAM data and Extended I/O registers. The only thing that is different is the address. What assembly instruction would I use to load the contents of the low order byte of the ADC register (ADCL) into general purpose register r20, and at what Extended I/O address would I find this ADC register?
Answer #13:
You would use the lds r20, ADCL assembly instruction, where the assembler would replace ADCL with the address 0x78.
Question #14:
If the data is located in our I/O address space, not the extended I/O address space, you would again use the direct addressing mode; however, this time you would use the “in” instruction. The “in” instruction is the only instruction used to directly load data from the I/O address space. Consequently, if you know the data is located in the I/O address space, you know the addressing mode (direct) and the instruction to use.
What assembly instruction would I use to read the contents of PORT D’s data direction register DDRD into general purpose register r12?
Answer #14:
Looking at the Register Summary, we find register DDRD is located at address 0x0A, so we would use the in instruction.
Question #15:
What assembly instruction would I use to load the contents of variable A into general purpose register r20, and within what SRAM address range would variable A be located? Hint: the top of SRAM data memory is located at address 0x8FF.
Answer #15:
You would use the lds r20, A assembly instruction, where the assembler would replace A with the SRAM address assigned to variable A by the assembler. Because the assembler will assign variable A when the program is assembled, we only know that it will be somewhere in our SRAM memory; address space 0x0100 to 0x08FF. See Figure 7-2 Program Memory Map ATmega88PA, ATmega168PA, and ATmega328P to see why the top of memory is 0x08FF.
Question #16:
How much real SRAM data memory does the ATmega328P contain?
Answer #16:
SRAM memory is located from address 0x8FF down to 0x0100. If our memory had a base address of 0x0000 then the last address would be o0x08FF – 0x0100 = 0x07FF. Which corrsponds to a 2 K byte (where K = 210 = 1024) or 204810 byte SRAM memory address space.
Question #17:
What assembly instruction would I use to load constant 0x80 into general purpose register r24?
Answer #17:
ldi r24, 0x80
Question #18:
As mentioned earlier, constants are encoded with the instruction itself. Because immediate instruction are encoded into one 16-bit Flash Program word there are some restrictions. First, the constant must fit within a byte (8-bits). If you are working with a number; unsigned numbers must be between 0 to 255 and signed numbers between -128 to 127. Second, to fit the 8 bit constant into the 16-bit instruction, Atmel had to limit the registers which could be used to r16 to r31. How can you work with a 16-bit constant? For example; how would you load the 16-bit constant 123416 into the r17:r16 register pair?
Answer #18:
Break the constant into a high-byte and a low-byte, assigning each to its own general purpose register. For example;
ldi r16, 0x34
Question #19:
Looking at the Instruction Set Summary and specifically Data Transfer Instructions, how could you load constant 0x33 into register r8?
Answer #19:
Break the constant into a high-byte and a low-byte, assigning each to its own general purpose register. For example;
Temporarily Load the constant into one of the registers from r16 to r31 and then use the move instruction mov to copu it into the desired register. For example;
mov r8, r16
Question #20:
What assembly instruction would I use to move Status Register SREG into general purpose register r27?
Answer #20:
Looking at the Register Summary, we find that SREG is located at address 0x3F, so we would use the in instruction.
Question #21:
What assembly instruction would I use to read the PINs on PORT B into general purpose register r12?
Answer #21:
Looking at the Register Summary, we find that PINB is located at address 0x03, so we would use the in instruction.
Register – Register Instructions
Question #22:
After we have loaded the date into the general purpose registers of the AVR processor, technically known as the register file, we manipulate the data as required by the problem to be solved. This often is the easiest part of the problem within the context of a load-store architecture. This is because, RISC machines offer a wide selection of register-register instructions for solving most any problem. In addition, the names of the instructions typically provide you enough information to know which one(s) to employ to solve your problem. Later in the semester we will solve more complex problems; but, for now lets look at a few simple examples.
Variables A and B have been loaded into register r2 and r3 respectively. How would you add these two numbers together putting the sum into register r2.
Answer #22:
add r2, r3
Question #23:
Some modern RISC computers have register-register instructions containing three operand fields, with the third operand field providing the processor with the address of where the answer is to be saved. Our RISC architecture only supports up to two operand fields. This means for arithmetic or logical operations requiring two operands (A plus B), the destination operand also provides one of the numbers to be operated on. Consequently, all instructions of this type destroy one of the source registers (by writing the result to it). In the problem above the source/destination register was r2. How can you perform an arithmetic or logincal operation without destroying the value in one of your source registers?
Answer #23:
Use the mov instruction to make a copy of one of your variables.
Question #24:
Write the code needed to add r2 and r3 placing the answer in register r4.
Answer #24:
mov r4, r2
add r4, r3
Question #25:
Write the code needed to take the logical or of register r2 and r3 placing the answer in register r4.
Answer #25:
mov r4, r2
Question #26:
Write the code needed to take the logical and of register r2 and r3 placing the answer in register r2.
Answer #26:
and r2, r3
Question #27:
As you can see often it is simply a matter of looking up the required arithmetic or logical instruction in the “Instruction Set Summary” and plugging it into your program.
We have a 16-bit unsigned number. The most significant 8-bits are located in register r5 and the least significant 8-bits in register r4. Another way of saying the same thing is that the register pair r5:r4 contains a 16-bit unsigned number. Given that the register pair r7:r6 holds a second 16-bit number, write the code needed to add these two 16-bit numbers together placing the result back into the register pair r5:r4. Hint: do not forget the carry from the least significant byte.
Answer #27:
add r4, r6
Storing the Answer
To be continued...
Looping Example
Design Objective
When the user presses the button, read first 3 switches (least significant), if the number is less than or equal to 5 then calculate factorial. If greater than 5 turn on decimal point. Display the least significant 4 bits of the answer.
My Design Steps
Step 1: Initialized Ports
; Disable interrupts and configure stack pointer for 328P
cli
; Initialize Switches with Pull-up resistors and Test LEDs
in r16,DDRC // input Port C Data Direction Register (0x07) for switches 5 to 0
cbr r16,0b00111111 // define bits 5 to 0 as input (clear bit register)
out DDRC,r16 // output
in r16,PORTC // input Port C Register (0x08) for switches 5 to 0
sbr r16,0b00111111 // add pull-up resistors (PUR)
out PORTC,r16 // output
in r16,DDRD // input Port D Data Direction Register (0x0A) for switches 7 to 6
cbr r16,0b11000000 // define bits 7 to 6 as input (clear)
out DDRD,r16 // output
in r16,PORTD // input Port D Register (0x0B) for switches 7 to 6
sbr r16,0b11000000 // add pull-up resistors (PUR)
out PORTD,r16 // output
; Initialize SPI Port and Test LEDs
in r16,DDRB // Input from Port B Data Direction Register (DDRB) at i/o address 0x04
sbr r16,0b00101111 // Set PB5, PB3, PB2 (SCK, MOSI, SS) and PB1, PB0 (TEST LEDs) as outputs
out DDRB,r16 // Output to Port B Data Direction Register (DDRB) at i/o address 0x04
in r16,PORTB // input Port B Register (0x05) bit 2 (SS) at i/o address 0x05
cbr r16,0b00000111 // bit 1 (TEST LED1), bit 0 (TEST LED0)
out PORTB,r16 // output
ldi r16,0b01010001 // Set SPCR Enable (SPE) bit 6, Master (MSTR) bit 4,
// clock rate fck/16 (SPR1 = 0,SPR0 = 1)
out SPCR,r16 // Output to SPI Control Register (SPCR) at i/o address 0x2c
Step 2: Turned on LED 0 to indicate initialization complete
sbi PORTB, 0 // Turn on LED 0
Step 3: Wrote code to pulse the clock
start:
cbi PORTD, 5
sbi PORTD, 5
Step 4: Read in pin waiting for the button to be pressed (Loop Example 1)
// check button
sbic PIND, 2
rjmp start
Step 5: Need to filter out Bounce (Loop Example 2)
delay_50:
ldi r16, 0 // 256
wait:
dec r16 // 1 clock cycle
brne wait // + 2 cycle if true, 1 cycles if false
// 3 cycles x 256 - 1 = 599 x 1/16 MHz = 48 usec
Maximum delay that could be generated was only 48 usec
Step 6: Added a NOP instruction, max delay was now 64 usec
Set delay for nice even number of 50 usec
delay_50:
ldi r16, 200 // 200 = 0xC8
wait:
nop // 1 clock cycle
dec r16 // 1 clock cycle
brne wait // + 2 cycle if true, 1 cycles if true
// 4 cycles x 200 - 1 = 799 x 1/16 MHz = 50 usec
Step 7: Made an outside loop of 10 (Loop Example 3)
delay_500:
ldi r17, 10
delay_50:
ldi r16, 200 // 200 = 0xC8
wait:
nop // 1 clock cycle
dec r16 // 1 clock cycle
brne wait // + 2 cycle if true, 1 cycles if true
// 4 cycles x 200 - 1 = 799 x 1/16 MHz = 50 usec
dec r17
brne delay_50 // 10 x 50 usec = 500 us (approx)
Step 8: Converted loop to a subroutine so I could change condition to button release.
; --------------------------
Delay500:
push r16
push r17
ldi r17, 10 // was 10
delay_50:
ldi r16, 200 // 200 = 0xC8
wait:
nop // 1 clock cycle
dec r16 // 1 clock cycle
brne wait // + 2 cycle if true, 1 cycles if true
// 4 cycles x 200 - 1 = 799 x 1/16 MHz = 50 usec
dec r17
brne delay_50 // 10 x 50 usec = 500 us (approx)
dec r18
brne delay_500 // 10 x 50 usec = 500 us (approx)
pop r17
pop r16
ret
Step 9: Check for button pressed and then released
start:
cbi PORTD, 5
sbi PORTD, 5
// check button down
sbic PIND, 2
rjmp start
rcall Delay500 // remove bounce
check_button:
cbi PORTD, 5
sbi PORTD, 5
// check button up
sbis PIND, 2
rjmp check_button
rcall Delay500 // remove bounce
Step 10: Read Switch and check if less than or equal to 5
in r16, PINC
cbr r16, 0b11110000 // clear undefined bits
cpi r16, 6 // no unsigned less than or equal to 5
brlo factorial
// error condition
ldi r16, 0x80 // decimal point
mov r8, r16
rcall writeDisplay
rjmp start
Step 11: Calculate Factorial (Loop Example 4)
factorial:
ldi r17, 1
mov r0, r17
calculate:
mul r0, r16 // r1:r0 = r0 x r16
dec r16
brne calculate
Step 12: Convert least significant nibble to 7-segment display (Flash Program Indirect Addressing Mode)
display_answer:
ldi r16, 0b00001111 // limit to least significant nibble
and r0, r16
ldi ZL,low(table<<1) // load address of look-up
ldi ZH,high(table<<1)
clr r1
add ZL, r0
adc ZH, r1
lpm spi7SEG, Z
rcall
writeDisplay
rjmp start
// gfedcba gfedcba gfedcba gfedcba gfedcba gfedcba
table: .DB 0b00111111, 0b00000110, 0b01011011, 0b01001111, 0b01100110, 0b01101101
// 0 1 2 3 4 5
.DB 0b01111101, 0b00000111, 0b01111111, 0b01100111, 0b01110111, 0b01111100
// 6 7 8 9 A B
.DB 0b00111001, 0b01011110, 0b01111001, 0b01110001
// C D E F
Midterm 1 Review
The midterm has not been written; however, I envision a mix of multiple choice (» 15), short answer, and Programming problems.
Material Covered in Midterm 1 (Lecture 01 to 08)
Lecture | Worksheets | |
• Introduction to Assembly – Simple Programming | 01 | |
• Introduction to Microcontrollers | 02 | |
• AVR Peripherals – including AVR Studio and Simulation | From lab material | |
• Load-Store Programming | 03 | Questions on Page 9, Load-Store Worksheet |
• AVR ALU and SREG | 04 and | Questions on Page 11 of lecture 04 |
• Bit and Bit-Test Instructions | From lab material | |
• AVR Branching and Looping | 05 and 06 | AVR Branching Solutions and AVR Looping Solutions |
• Quiz 1 | Quiz 1 Review – Solutions | |
• AVR Subroutine Basics | 07 | |
• ATmega GPIO | 08 and Lab material | Labs 1 and 2 |
• Labs up to and including Lab 2 | ||
• | Practice Programming Problems |
Sample Topics
AVR Assembly: Number Systems, Instruction Types, MCU Instructions, AVR Assembly Files
AVR Microcontroller: Flip-flops and Registers, Microcontroller Architecture, CPU Operation, ISA Registers, Pipeline, Memory Mapped I/O
Load-Store Programming (plus Addressing Modes): Data Transfer Instructions, Addressing Modes, Data Transfer Instruction encoding.
ALU and SREG: ALU Instructions, SREG Bits
Bit and Bit-Test Instructions: Bit and Bit-Test Instructions, Lab
AVR Jumping, Branching, and Looping: Compare and Test Instructions, Control Transfer Instructions, Modification of ISA Registers, Instruction call, rcall, jmp, rjmp encoding, Implementation in assembly of high level programming control transfer and looping instructions, Software Timing Loops, Pipeline Operation
AVR Peripherals – including AVR Studio Assembly and Simulation: From Lab Material
You will be provided with the following
- Arduino Proto‐Shield Schematic
- Four (4) pages from the ATmega328P Summary (Atmel Document 8161S)
- Section 5. Register Summary Pages 10 and 11 (not 8 and 9)
- Section 6. Instruction Set Summary Pages 12 and 13 (not 14)
You can bring…
- One half (½) of the front side of notes. See Syllabus for rules on what may and may not be on your sheet of notes.
- 4-function calculator
Midterm II Review Questions
In addition to the following questions, I would recommend reviewing the Midterm 1 Load-Store Worksheet
Warning:
Your one-sided 8 ½ x 11 page of notes may not contain questions and answers or copy-pasted bullet-points. It may only contain technical information relevant to the ATmega processor and other coursework related material. The following questions are provided to help you prepare for the second midterm only.
ATmega328P Timers and Interrupts
1. A computer spends its whole life _________ and _________ instructions.
Answer
Fetch and Execute
2. Where can you find the Interrupt Vector Table (IVT)?
Answer
Flash Program Memory
3. How many words (16-bits) are reserved for each entry in the IVT?
Answer
2
4. While the global interrupt SREG bit I is cleared, both the Timer/Counter 1 Overflow bit is set (IVT Address IVT 0x001A) Timer1 OVF and an external interrupt is received (IVT Address 0x0002) INT0. Assuming the interrupt enable bit for both interrupts is set, what will happen when the global interrupt bit is set (enabled)?
Answer
The External Interrupt Request 0 will be run.
5. In which register can you find the global interrupt enable bit?
Answer
SREG
6. How many bits need to be set for a Timer/Counter 1 Overflow interrupt to be triggered?
Answer
3 (SREG I, TOIE1, TOV1)
7. When an interrupt is triggered, what register is placed on the stack?
Answer
PC
8. Why is this register saved?
Answer
So the currently running process can continue execution when the processor is completes running the ISR.
9. Why is the first instruction executed, at address 0x0000, always a jump?
Answer
So the AVR processor does not accidentally execute an ISR.
10. If the instruction at address 0x0000 is not a jump what ISR will be executed?
Answer
External Interrupt Request 0 INT0 at address 0x0002.
11. What triggers a Timer/Counter 1 Overflow Interrupt?
Answer
Counter goes from 0xFFFF to 0x0000
12. What immediately happens when an interrupt occurs?
Answer
The microcontroller completes the current instruction, stores the address of the next instruction on the stack, and clears the SREG I-bit.
13. Just before a Timer/Counter Overflow Interrupt is run, what IVT address needs to be placed in the program counter (PC)?
Answer
0x001A
14. What is instruction is required to return from an Interrupt?
Answer
reti
15. In what way is a ret instruction different from an reti instruction?
Answer
An reti instruction sets the global interrupt enable flag bit I in SREG.
16. What is one of the last things and ISR does before it returns control to the interrupted program?
Answer
Restores the Program Counter and Enables the global interrupt flag bit I.
17. What is the first and second to last thing your ISR should do?
Answer
Save and restore the SREG register.
18. What is the last thing your ISR should do?
Answer
Execute an reti instruction.
19. What is wrong with this instruction push SREG?
Answer
You can only push a general purpose register onto the stack.
20. Where should your ISR save the SREG register?
Answer
One of the 32 general purpose registers.
21. Where should your ISR save general purpose registers modified by the ISR?
Answer
Registers modified by the ISR should be temporarily placed on the stack.
22. Upon return from an ISR and enabling the global interrupt flag ; the AVR processor finds another interrupt waiting to be executed. What will happen next?
Answer
The main program will execute one more instruction before any pending interrupt is run.
23. How many bits would be required to encode the register direct addressing mode?
Answer
5 because 25 = 32
24. The immediate addressing mode can only access 16 of the 32 general purpose registers. How many bits would be required to encode the register used in this type of instruction?
Answer
4 because 24 = 16
25. The immediate addressing mode encodes the data (operand) with the instruction. How many bits would be required to encode the data?
Answer
8
AVR Addressing Modes including Indirect
1. You can find the operand for the immediate addressing mode in what type of memory?
Answer
Flash Program Memory
2. You can find the operand for the direct addressing mode of an lds and sts instruction in what type of memory?
Answer
SRAM Data Memory
3. You can find the operand for the direct addressing mode of an in and out instruction in which address space(s)?
Answer
SRAM Data Memory and I/O Register Memory
4. The address space of which two addressing modes overlap?
Answer
SRAM Data Direct and I/O Register Memory Direct
5. What addressing mode should you use if you want to look up a pre-defined value in a table (data is known at assembly time)?
Answer
Flash Program Indirect
6. What addressing mode should you use if you want to look up a value in a table (data is known at run time)?
Answer
SRAM Data Indirect
7. What addressing mode is used for the source operand of an lpm instruction?
Answer
Flash Program Indirect
8. What register pair is found in the source operand address field of an lpm instruction?
Answer
Z
9. What register numbers correspond to pre-defined mnemonics ZH:ZL?
Answer
R31:r30
10. What two addressing mode should you use if you want to work with a table of data located in SRAM (data is known at run time)?
Answer
1. SRAM Data Indirect and
2. SRAM Data Indirect with Displacement
11. What addressing mode is used for the source operand of an ld instruction?
Answer
SRAM Data Indirect
12. Which three register pairs may be found in the source operand address field of an ld instruction?
Answer
X, Y, and Z
13. What two 8-bit register mnemonics are used to define the X register pair?
Answer
XH:XL and R27:r26
14. What addressing mode is used for the destination operand address field of the instruction lpm r16, Z?
Answer
The key to this question is in italics “destination.” The destination operand uses the Register Direct addressing mode, the source operand is register indirect.
15. What register is both a source and a destination for the instruction add r16, r17?
Answer
R16
16. Write a code snip-it to load the 3rd byte (index = 2) of data from a table (label = TABLE) located in Flash Program memory. Answer left up to the student.
17. What is wrong with this instruction lds r16, low(Table << 1)? The source operand address field is of the immediate addressing mode type.
Answer
The ldi instruction should have been used in place of the lds instruction.
18. I want to load the number 3316 into register 16. Why can I not use the instruction lds r16, 0x33 to do this?
Answer
The source operand address field should be immediate , not SRAM Data direct. The lds instruction would load r16 with the contents of SRAM memory at location 0x33. The ldi instruction should have been used in place of the lds instruction.
19. What addressing mode is used for the destination operand address field of the instruction lds r16, 0x33?
Answer
Register Direct
20. The AVR processor saves bytes of data in Flash Program Memory using what memory byte ordering?
Answer
Little Endian
21. Big Endian saves what half of a 2 byte (16-bit) word in the first byte (lowest address)?
Answer
The most significant (Big) byte.
22. What addressing mode is great for implementing look-up tables in Flash Program Memory?
Answer
Program Memory Indirect
23. Build a program to convert a 4-bit gray-code number into binary. Solution left to the student
24. What instruction is used to divide a register by two?
Answer
lsr
25. Write a program to set a 32 byte buffer located in SRAM to the blank ‘’ ASCII character. Solution left to the student.
26. To modify the seven segment display on the proto-shield you must write to register 8 and do what?
Answer
call spiTx
AVR Branching and Looping
1. ISA is an abbreviation for what?
Answer
Instruction Set Architecture
2. How many general purpose registers does the AVR processor have?
Answer
32
3. What is the mnemonic for the last AVR general purpose register?
Answer
r31
4. All unconditional jump instructions using the relative addressing mode, utilize 12 bits to encode the distance the program is to jump relative to the program counter (PC). Given that this 12 bit number is saved using 2’s complement notation, what is the range in words (16-bits) that the AVR processor can jump for this type of instruction?
Answer
Branch relative to PC + (– 2k-1 a 2k-1– 1, where k = 12) + 1 aPC-2048 to PC+2047, within 16 K word address space of ATmega328P
5. All conditional branch instructions using the relative addressing mode, utilize 7 bits to encode the distance the program is to branch relative to the program counter (PC). Given that this 7 bit number is saved using 2’s complement notation, what is the range in words (16-bits) that the AVR processor can branch for this type of instruction?
Answer
All branch relative to PC + (– 2k-1 a 2k-1– 1, where k = 7) + 1 aPC-64 to PC+63, within 16 K word address space of ATmega328P
6. Which instructions do you typically find before a relative branch instruction? Compare and test instructions?
Answer
cp, cpc, cpi, tst, bst
7. Compare and test instructions like cp, cpc, cpi, tst, bst do not modify any of the 32 general purpose registers. What register(s) do they modify?
Answer
SREG
8. What type of instruction typically follows a compare and test instruction, like cp, cpc, cpi, tst, bst?
Answer
A conditional control transfer instruction.
9. Why does a compare or test instruction, typically precede a conditional branch instruction?
Answer
The compare or test instruction sets or clears flag bits within the status register (SREG) used by the conditional branch instruction to make a decision.
10. What is wrong with the following two sequential instructions? cpi r16, 0x33,followed by rjmp there
Answer
The first instruction is setting and clearing SREG bits to be used by a conditional branch instruction. The second instruction is an unconditional jump instruction which doesn’t need to make any decision (i.e., it is always going to jump).
11. Why is the following two sequential instructions silly?
Answer
clr r16,followed by ldi r16, 0x33 The second instruction will write 3316 to r16, so there is no reason to clear it.
12. What bit(s) within SREG are never modified by a compare or test instruction?
Answer
I (global interrupt enable), T (bit copy storage).
13. What is wrong with the following two sequential instructions? cpi r16, 0x33,followed by brts there
Answer
The first instruction is setting and clearing SREG bits associated with an ALU instruction. The second instruction is a conditional branch instruction which tests to see if the T-bit is set. The T-bit is not modified by a compare instruction.
14. Assuming that r16 contains the value 0x33. What value would be in r16 after the instruction cpi r16, 0x33 is executed?
Answer
R16 = 0x33
15. Assuming that r16 contains the value 0x33. What value would be in r16 after the instruction subi r16, 0x33 is executed?
Answer
R16 = 0x00
16. Assuming that r16 contains the value 0x33. What 1-bit value would be contained in the Z-bit in SREG after the instruction cpi r16, 0x33 is executed?
Answer
Z = 1
17. What type of instruction never modify bits within the SREG register?
Answer
Data Transfer
18. Write the code to implement the following flow-chart
19. Write the code to implement the following flow-chart
Solution left to the student
AVR Bits and Bytes
1. What instruction(s) could you use to clear the carry bit?
Answer
clc, bclr
SREG_C, bclr 0
2. What instruction would you use to clear PORT D bit 3?
Answer
cbi
PORTD,3 or cbi 0x0B,3
3. What is wrong with this the instruction cbi TIMSK1, 0?
Answer
TIMSKI is located in the extended I/O address space of the ATmega328P microcontroller and is therefore not accessible.
4. Write
an instruction to clear bit 4, 2, and 0 in register 16.
Answer
cbr r16,
0b0001 0101
5. Write an instruction to clear bit 4, 2, and 0 in register 16 without using the cbr instruction.
Answer
andi r16, 0b1110 1010 or and r16, 0xEA
6. Write an instruction to set bit 4, 2, and 0 in register 16.
Answer
sbr r16, 0b0001 0101
7. Write an instruction to set bit 4, 2, and 0 in register 16 without using the sbr instruction.
Answer
ori r16, 0b0001 0101 or and r16, 0x15
8. What is wrong with this instruction to toggle bit 4, 2, and 0 in register 16?
Answer
eor r16, 0b0001 0101
The eor instruction works with two registers.
9. Write an instruction sequence to toggle bit 4, 2, and 0 in register 16.
Answer
ldi r17, 0b0001 0101
eor r16, r17
10. Write an instruction sequence to set bits 4, 2, and 0 to 1012 in register 16, without modifying any other bits.
Answer
cbr r16, 0b0000 0100
sbr r16, 0b0001 0001
11. Write an instruction to set bits 7, 6, 5, 4, 3, 2, 1, 0 in register 16, without using the sbr or or instruction.
Answer
ser r16
12. Write a program to wire switch 0 to the decimal point of the 7-segment display on the proto-shield.
Solution left to the student
13. Pulse Clock input of Proto-Shield Debounce D Flip-flop (PORTD bit 5). Assume currently at logic 0.
Answer
sbi PORTD, 5
cbi PORTD, 5
14. What logical instruction is implemented by the tst instruction?
Answer
and
15. What is the difference between the and and the tst instruction?
Answer
The tst instruction performs the and operation without modifying the destination operand.
16. What does the cp instruction have in common with the tst instruction?
Answer
Both do not modify the register(s) in the operand field. Both are used to set flag bits in SREG.
17. Assuming register 16 contains 0x00. What would the Z-bit be set/cleared to after executing the instruction tst r16?
Answer
Z = 1
18. What instruction is used to disable all interrupts?
Answer
cli
19. How many operand(s ) does the tst instruction have?
Answer
1
20. Complete the following Boolean expression representing the operation of a the tst r16 instruction.
r16 ___ r16
Answer
r16 ∙r16
21. What instruction would be used to implement the logic operation represented by the question mark (?) in Figure 1 (Shown below)?
Answer
eor
22. What phrase best describes the operation shown in Figure 1 box A?
Answer
Don’t Change
23. What word best
describes the operation shown in Figure 1 box B?
Figure 1
Answer
Toggle
24.
Write the code needed to implement the following circuit.
Midterm 2 Design Example
In this design example, we are going to design a Slot Machine
Assume Port D is wired as shown in the table below.
Direction
|
DDRD bit(s)
|
Type / Initialization
|
PORTD
|
||
PD bits 3-0
|
switches
|
Input
|
000
|
Passive Input
|
1
|
PD bit 4
|
Win light
|
Output
|
1
|
Initially Off
|
0
|
PD bit 5
|
new Account button
|
Input
|
0
|
Active output of a DFF
|
0
|
PD bit 6
|
add Account button
|
Input
|
0
|
Active output of a DFF
|
0
|
PD bit 7
|
Lose light
|
Output
|
1
|
Initially Off
|
0
|
We begin by defining SRAM variable account and initializing Port D as defined in the table.
Now let’s generate a clock to the two D flip-flops so we can read our new and add account button.
Given clock is 20 Mhz. I want to clock the DFF at a Frequency is 20Khz.
Alternative wording: I want to cycle and test if the button is pressed every 50 microseconds.
If I want to use timer 2, what divide frequency will I need to do that? What would you need to load into Timer 2 to generate this delay?
Calculate Max delay given the following information. You are using Timer 2, a clock frequency of fclk = 20 MHz, and a clock divider of ÷8. Timer 2 is an 8-bit timer so the maximum number of tics is 28 = 256 tics. To convert to time we need to equate tics to time.
f1/0 = fclk /8 = 2.5MHz t1/0=1/f1/0 =0.4 µsec/tic
tmax = 0.4µsec/tic × 256 tics = 102.4 µsec
So our timer with given conditions can generate a 25 microseconds delay, now let’s look at what we need to preload our counter with to get a delay of 25 microseconds.
25 µsec ÷ 0.4 µsec/tics = 62.5 tics (Rounding down we leave it at 62). So we would need to preload timer 2 with a value of 256 – 62 = 19410 = 0xC2
We can now use polling or an interrupt service routine to generate our clock.
Now let’s generate the subroutines to be called when the user sets a dollar amount into 4-switches and presses the new account or add account button.
The Slot Machine Wheel
The strange characters to be generated by our slot machine are shown at left. In the table are the segments to be turned on/off to create the strange symbols.
Converting this table into bytes to be saved in Flash.
0x0123 wheel: .DB 0x47, 0x56, …..0x2A
EE346 Final Exam – Practice Problems
WARNING No questions contained in this document, or any other worksheet, may be reproduced on your page of notes. Use the page of notes to record data about the ATmega Microcontroller and other class-related material. |
In addition to the material presented here, another great source of practice problems can be found in the Programming Problems folder.
Table of Contents
The following questions cover the following worksheets.
ATmega328P Subsystems
- ATmega328P Peripherals
- ATmega328P Serial Communications
- ATmega328P Timers and Interrupts
- ATmega328P External Interrupts
AVR Microprocessor
- AVR Bits and Bytes
- AVR SREG
- AVR Load-Store Programming
- AVR Addressing Modes II
- Indirect Addressing Mode Questions.
- AVR Branching
Lecture material not explicitly covered by this document include the following.
- AVR Assembly I Fundamental material required to answer many if not all of the questions.
- AVR Microcontroller Fundamental material required to answer many of the questions.
- AVR Subroutines Required to answer many of the questions.
ATmega328P Subsystems
The following questions are based on the Arduino Proto-Shield and the following table.
GPIO Ports
- Using two single bit instructions, configure Port D bit 2 as an input without a pull-up resistor.
- Using two single bit instructions, configure and set to one (1) Port D bit 5.
- Using six byte instructions, configure Port C bits 5 to 0 as inputs with pull-up resistors. Do not modify bits 7 to 6. Use register 16 and 17 as working (temporary) registers.
- Using six byte instructions, configure Port D bits 7 and 6 as inputs with pull-up resistors. Do not modify bits 5 to 0. Use register 16 and 17 as working (temporary) registers.
SPI Interface
Review ATmega328P SPI Serial Communications lecture notes for help in answering these questions.
- Assume the SPI subsystem of the ATmega328P is configured as the master, and outputting to an 8-bit Shift Register with Output Latches (74HC595) as shown in the Arduino Proto-Shield schematic. How many pins are needed to implement this interface?
3
2. Assume the SPI subsystem of the ATmega328P is configured as the master, and outputting to an 8-bit Shift Register with Output Latches (74HC595) as shown in the Arduino Proto-Shield schematic. Plus, I add an additional 8-bit Parallel In Serial Out Shift Register to read the 8 switches. I wire the output of this register to the Master In Serial Out (MISO) pin of the Arduino. How many pins are needed to implement this interface?
Answer4
3. The SPI interface is implemented with a maximum of four(4) pins (MISO, MOSI, SCK, and SS). On which of these pins would you see the serial data output?
AnswerMOSI
4. The SPI interface is implemented with a maximum of four(4) pins (MISO, MOSI, SCK, and SS). On which of these pins would you see the serial clock?
AnswerSCK
5. Using three byte instructions, configure Port B bits 5, 3, and 2 (SCK, MOSI, SS) as outputs. Do not modify bits 5 to 0. Use register 16 as a working (temporary) register.
Answer- Using two byte instructions, set SPCR Enable (SPE) bit 6, Master (MSTR) bit 4, clock rate fck/16 (SPR1 = 0, SPR0 = 1)of the configure the SPI Control Register (SPCR). Set all other bits to zero (0).
- Using a single I/O instruction, output register r8 to the SPI by writing its contents to the SPI Data Register (SPDR).
- After a byte is written to the SPI Data Register, we can poll the SPI interrupt flag (bit ) in the SPI status register (SPSR) to find out when the byte has been transmitted. Write a subroutine to wait for the end of transmission by implementing the following comments.
Timer Subsystem
Review ATmega328P Timers lecture notes for help in answering these questions.
- A sinusoid signal repeats itself 60 times each second. Waht is the period of this signal?
16.67 ms
- What is the frequency, period, and duty cycle of the following waveform.
F = 2 Hz, T = 500 ms, duty cycle = 50%
- What is the maximum delay that can generated by our 16-bit Timer 1 with a prescale value of 64 and a system clock frequency of 16 MHz.
262.14 msec
- A prescaler of 1024 (clk/1024) is applied to our 8-bit time 0, while a prescaler of 256 (clk/256) is applied to our 8-bit time 2. Finally, a prescaler of 1 (clk) is applied to our 16-bit time 1. Assuming a system clock frequency of 16 MHz, which timer(s) will generate a delay of 096 msec.
Timer 0 = 16.384 msec, Timer 1 = 4.096 msec, Timer 2 = 4.096 msec
Answer is Timer 1 and Timer 2.
- What value would you load into the TCNT1H and TCNT1L register pair to generate a delay of 250 msec.
TCNT1H = 0x0B and TCNT1L = 0xDC
Interrupts
Review Timer Interrupts lecture notes for help in answering these questions.
- In which register can you find the global interrupt enable bit?
SREG
- Where can you find the Interrupt Vector Table (IVT)?
Flash Program Memory
- How many words (16-bits) are reserved for each entry in the IVT?
2
- When an interrupt is triggered, what register is placed on the stack?
PC
- Why is this register saved?
So the currently running process can continue execution when the processor is completes running the ISR.
- Why is the first instruction executed, at address 0x0000, always a jump?
So the AVR processor does not accidently execute an ISR.
- If the instruction at address 0x0000 is not a jump what ISR will be executed?
External Interrupt Request 0 INT0 at address 0x0002.
- What immediately happens when an interrupt occurs?
The microcontroller completes the current instruction, stores the address of the next instruction on the stack, and clears the SREG I-bit.
- What is instruction is required to return from an Interrupt?
reti
- In what way is a ret instruction different from an reti instruction?
An reti instruction sets the global interrupt enable flag bit I in SREG.
- What is one of the last things and ISR does before it returns control to the interrupted program?
Restores the Program Counter and Enables the global interrupt flag bit I.
- What is the first and second to last thing your ISR should do?
Save and restore the SREG register.
- What is the last thing your ISR should do?
Execute an reti instruction.
- Where should your ISR save the SREG register?
One of the 32 general purpose registers.
- Where should your ISR save general purpose registers modified by the ISR?
Registers modified by the ISR should be temporarily placed on the stack.
- Upon return from an ISR and enabling the global interrupt flag ; the AVR processor finds another interrupt waiting to be executed. What will happen next?
The main program will execute one more instruction before any pending interrupt is run.
Timer Interrupts
Review Timer Interrupts lecture notes for help in answering these questions.
- Just before a Timer/Counter Overflow Interrupt is run, what IVT address needs to be placed in the program counter (PC)?
0x001A
- How many bits need to be set for a Timer/Counter 1 Overflow interrupt to be triggered?
3 (SREG I, TOIE1, TOV1)
- What triggers a Timer/Counter 1 Overflow Interrupt?
Counter goes from 0xFFFF to 0x0000
External Interrupts
Review ATmega328P External Interrupts lecture notes for help in answering these questions.
- While the global interrupt SREG bit I is cleared, both the Timer/Counter 1 Overflow bit is set (IVT Address IVT 0x000D) Timer1 OVF and an external interrupt is received (IVT Address 0x0001) INT0. Assuming the interrupt enable bit for both interrupts is set, what will happen when the global interrupt bit is set (enabled)?
The External Interrupt Request 0 will be run.
AVR Microprocessor
Introduction to Microcontrollers including History
See Lecture Notes and Quiz 1 Review Material
- What memory model is used by the EDVAC?
Princeton
- ISA is an abbreviation for what?
Instruction Set Architecture
- How many general purpose registers does the AVR processor have?
32
- What is the mnemonic for the last AVR general purpose register?
r31
Status Register (SREG)
Review AVR SREG lecture notes for help in answering these questions.
- Using a single bit instructions, disable all interrupts.
cli
- What is wrong with this instruction push SREG?
You can only push a general purpose register onto the stack.
- Assume the subtract instruction sub r16, r17 has just been run by the AVR microprocessor. Complete the table provided. The “difference” column should reflect the contents of register r16 after the subtraction operation (leave the answer in 2’s complement form) and not the actual difference (i.e., if done using your calculator). Use the AVR Studio simulator to verify your answers.
signed
|
unsigned
|
|||||||||
r16
|
r17
|
difference
|
relationship
|
relationship
|
H
|
S
|
V
|
N
|
Z
|
C
|
3B
|
3B
|
00
|
+ = +
|
=
|
0
|
0
|
0
|
0
|
1
|
0
|
3B
|
15
|
26
|
+ > +
|
>
|
0
|
0
|
0
|
0
|
0
|
0
|
15
|
3B
|
|||||||||
F9
|
F6
|
|||||||||
F6
|
F9
|
|||||||||
15
|
F6
|
|||||||||
F6
|
15
|
|||||||||
68
|
A5
|
|||||||||
A5
|
68
|
Addressing Modes
Direct and Immediate Addressing Modes
Review “AVR Load-Store Programming”.
- You can find the operand for the immediate addressing mode in what type of memory?
Flash Program Memory
- You can find the operand for the direct addressing mode of an lds and sts instruction in what type of memory?
SRAM Data Memory
- You can find the operand for the direct addressing mode of an in and out instruction in which address space(s)?
SRAM Data Memory and I/O Register Memory
- The address space of which two addressing modes overlap?
SRAM Data Direct and I/O Register Memory Direct
- What register is both a source and a destination for the instruction add r16, r17?
R16
- What addressing mode is used for the destination operand address field of the instruction lds r16, 0x33?
Register Direct
Indirect Addressing Modes
Review “AVR Indirect Addressing”, “Application of the Indirect Addressing Mode”, and “Indirect Addressing Mode Questions” lecture notes for help in answering these questions.
- What addressing mode should you use if you want to look up a pre-defined value in a table (data is known at assembly time)?
Flash Program Indirect
- What addressing mode should you use if you want to look up a value in a table (data is known at run time)?
SRAM Data Indirect
- What addressing mode is used for the source operand of an lpm instruction?
Flash Program Indirect
- What register pair is found in the source operand address field of an lpm instruction?
Z
- What register numbers correspond to pre-defined mnemonics ZH:ZL?
R31:r30
- What two addressing mode should you use if you want to work with a table of data located in SRAM (data is known at run time)?
- SRAM Data Indirect
- SRAM Data Indirect with Displacement
7. What addressing mode is used for the source operand of an ld instruction?
AnswerSRAM Data Indirect
- Which three register pairs may be found in the source operand address field of an ld instruction?
X, Y, and Z
- What two 8-bit register mnemonics are used to define the X register pair?
XH:XL and R27:r26
- What addressing mode is used for the destination operand address field of the instruction lpm r16, Z?
The key to this question is in italics “destination.” The destination operand uses the Register Direct addressing mode, the source operand is register indirect.
- Write a code snip-it to load the 3rd byte (index = 2) of data from a table (label = TABLE) located in Flash Program memory.
Answer left up to the student.
- What is wrong with this instruction lds r16, low(Table << 1)?
The source operand address field is of the immediate addressing mode type. The ldi instruction should have been used in place of the lds instruction.
- I want to load the number 3316 into register 16. Why can I not use the instruction lds r16, 0x33 to do this?
The source operand address field should be immediate , not SRAM Data direct. The lds instruction would load r16 with the contents of SRAM memory at location 0x33. The ldi instruction should have been used in place of the lds instruction.
- The AVR processor saves bytes of data in Flash Program Memory using what memory byte ordering?
Little Endian
- Big Endian saves what half of a 2 byte (16-bit) word in the first byte (lowest address)?
The most significant (Big) byte.
- Each entry (.DW) in the following table contains two bytes (1 16-bit word). These two bytes provide the row and column of a room containing bees. For example with respect to the maze, the room in row 00 column 04 contains 1 bee. If we look at the first entry we see it contains 0x0400. Comparing this with the corresponding Program Memory Window in AVR Studio the least significant byte is saved in the lowest order byte; so 0x0400 would be save as bytes 0x00 and 0x40. What form of Byte ordering (Big or Little Endian) does this represent?
Little Endian
- What addressing mode is great for implementing look-up tables in Flash Program Memory?
Program Memory Indirect
- Build a program to convert a 4-bit gray-code number into binary.
Solution left to the student
- What instruction is used to divide a register by two?
lsr
- Write a program to set a 32 byte buffer located in SRAM to the blank ‘’ ASCII character.
Solution left to the student.
- To modify the seven segment display on the proto-shield you must write to register 8 and do what?
call spiTx
Instructions Encoding
Be able to encode instructions.
- ldi
Load Sore Programming and AVR Addressing Indirect Lectures
- jmp and call
AVR Branching and AVR Stack Operations
- rjmp and rcall
AVR Branching and AVR Stack Operations
- ret
AVR Stack Operations
Programming, Labs, and Using the AVR Studio Simulator
Assembly Directives
Load-Store Programming
See Addressing Modes.
Arithmetic and Logic Instructions
ALU Instructions
To be generated…
Bits and Bit-test Instructions
Review AVR Bits and Bytes lecture notes for help in answering these questions.
- What instruction(s) could you use to clear the carry bit?
clc, bclr SREG_C, bclr 0
- What instruction would you use to clear PORT D bit 3?
cbi PORTD,3 or cbi 0x0B,3
- What is wrong with this the instruction cbi TIMSK1, 0?
TIMSKI is located in the extended I/O address space of the ATmega328P microcontroller and is therefore not accessible.
- Write an instruction to clear bit 4, 2, and 0 in register r16.
cbr r16, 0b0001 0101
- Write an instruction to clear bit 4, 2, and 0 in register r16 without using the cbr instruction.
andi r16, 0b1110 1010 or andi r16, 0xEA
- Write an instruction to set bit 4, 2, and 0 in register r16.
sbr r16, 0b0001 0101
- Write an instruction to set bit 4, 2, and 0 in register r16 without using the sbr instruction
ori r16, 0b0001 0101 or and r16, 0x15
- What is wrong with this instruction to toggle bit 4, 2, and 0 in register 16? eor r16, 0b0001 0101
The eor instruction works with two registers.
- Write an instruction sequence to toggle bit 4, 2, and 0 in register 16.
ldi..r17, 0b0001 0101
eor r16, r17
- Write an instruction sequence to set bits 4, 2, and 0 to 1012 in register 16, without modifying any other bits.
cbr r16, 0b0000 0100
sbr r16, 0b0001 0001
- Write an instruction to set bits 7, 6, 5, 4, 3, 2, 1, 0 in register 16, without using the sbr or or instruction
ser r16
- Write a program to create a “software wire” between switch 0 to the decimal point of the 7-segment display on the proto-shield.
Solution left to the student
- Pulse Clock input of Proto-Shield Debounce D Flip-flop (PORTD bit 5). Assume currently at logic 0.
sbi PORTD, 5
cbi PORTD, 5
- What logical instruction is implemented by the tst instruction?
and
- What is the difference between the and and the tst instruction?
The tst instruction performs the and operation without modifying the destination operand.
- What does the cp instruction have in common with the tst instruction?
Both do not modify the register(s) in the operand field. Both are used to set flag bits in SREG.
- Assuming register 16 contains 0x00. What would the Z-bit be set/cleared to after executing the instruction tst r16?
Z = 1
- What instruction is used to disable all interrupts?
cli
- How many operand(s ) does the tst instruction have?
1
- Write the Boolean instrucion that would be functionally equivalent to the tst r16
and r16, r16
- What instruction would be used to implement the logic operation represented by the question mark (?) in Figure 1?
eor
- What phrase best describes the operation shown in Figure 1 box A?
Don’t Change
- What word best describes the operation shown in Figure 1 box B?
Toggle
Figure 1 Truth Table of the Exclusive OR operator
- Write the code needed to implement the following circuit.
25. Using just two instructions, test the contents of register16, if all the bits are cleared (equal to zero) branch to the label is_zero.
Answertst r16
breq is_zero
- Write a subroutine to test if a bit set in byte variable imageD corresponds to a bit set in byte variable imageR. If the corresponding bit is set return a non-zero value in register r24.
hitWall:
lds r24,imageD
lds r16,imageR
and r24, r16
ret
- Given that only one bit is set in register r16, write a subroutine to if this bit is at either edge of the register (bits 7 and 0). If it isn’t then branch to label contScan.
ldi r19, 0b100000001
and r19,r16 // test if LED hit is at an edge
breq contScan // continue scan if z = 0
- Using the exclusive or instruction, write a subroutine to test if byte variable row contains -1 (0xFF). If it does return zero in register r24.
lds r24, row
ldi r16, 0xFF
eor r24, r16
ret
- In ASCII, the character ‘H’ is encoded as 0x48 and the character A is 0x41. Assume that register r16 contains ‘H’ or ‘I.’ We want to write a subroutine that when called will convert ‘H’ to ‘I’ and ‘I’ to ‘H.’ One way to accomplish this to toggle bits 3 and 0 in register r16 each time the subroutine is called. Can you write the subroutine?
ldi r17, 0b00001001
eor r16, r17
ret
- Write an instruction to divide a signed number in register r16 by 2?
asr r16
- Write an instruction to multiply an unsigned number in register r16 by 2?
lsl r16
- Each entry in a table contains 16 bits (2 bytes). The address of this table is saved in the register pair ZH:ZL. In my program I need to access bytes within the table. To do this I need to change the word address in Z into a byte address. This is done by shifting register Z to the left by one bit. Write the code needed to shift the register pair ZH:ZL to the left.
lsl ZL // carry-in is zero
rol ZH // shift in the carry-out from ZL
Control Transfer
Branching
Review AVR Branching lecture notes for help in answering these questions.
- All unconditional jump instructions using the relative addressing mode, utilize 12 bits to encode the distance the program is to jump relative to the program counter (PC). Given that this 12 bit number is saved using 2’s complement notation, what is the range in words (16-bits) that the AVR processor can jump for this type of instruction?
Branch relative to PC + (– 2k-1 a 2k-1– 1, where k = 12) + 1 aPC-2048 to PC+2047, within 16 K word address space of ATmega328P
- All conditional branch instructions using the relative addressing mode, utilize 7 bits to encode the distance the program is to branch relative to the program counter (PC). Given that this 7 bit number is saved using 2’s complement notation, what is the range in words (16-bits) that the AVR processor can branch for this type of instruction?
All branch relative to PC + (– 2k-1 a 2k-1– 1, where k = 7) + 1 aPC-64 to PC+63, within 16 K word address space of ATmega328P
- Which instructions do you typically find before a relative branch instruction? Compare and test instructions?
cp, cpc, cpi, tst, bst
- Compare and test instructions like cp, cpc, cpi, tst, bst do not modify any of the 32 general purpose registers. What register(s) do they modify?
SREG
- What type of instruction typically follows a compare and test instruction, like cp, cpc, cpi, tst, bst?
A conditional control transfer instruction.
- Why does a compare or test instruction, typically precede a conditional branch instruction?
The compare or test instruction sets or clears flag bits within the status register (SREG) used by the conditional branch instruction to make a decision.
- What is wrong with the following two sequential instructions? cpi r16, 0x33, followed by rjmp there
The first instruction is setting and clearing SREG bits to be used by a conditional branch instruction. The second instruction is an unconditional jump instruction which doesn’t need to make any decision (i.e., it is always going to jump).
- Why is the following two sequential instructions silly? clr r16, followed by ldi r16, 0x33
The second instruction will write 3316 to r16, so there is no reason to clear it.
- What bit(s) within SREG are never modified by a compare or test instruction?
I (global interrupt enable), T (bit copy storage).
- What is wrong with the following two sequential instructions? cpi r16, 0x33, followed by brts there
The first instruction is setting and clearing SREG bits associated with an ALU instruction. The second instruction is a conditional branch instruction which tests to see if the T-bit is set. The T-bit is not modified by a compare instruction.
- Assuming that r16 contains the value 0x33. What value would be in r16 after the instruction cpi r16, 0x33 is executed?
R16 = 0x33
- Assuming that r16 contains the value 0x33. What value would be in r16 after the instruction subi r16, 0x33 is executed?
R16 = 0x00
- Assuming that r16 contains the value 0x33. What 1-bit value would be contained in the Z-bit in SREG after the instruction cpi r16, 0x33 is executed?
Z = 1
- What type of instruction never modify bits within the SREG register?
Data Transfer
- What hex value encodes the opcode (rcall).
D
- Write the machine code instruction that encodes the assembly instruction rjmp loop. The instruction is located at address 0x215. The label loop is located at address 0x01E2.
Solution left to the student
- The instruction rjmp end_switch is located at address 0x01ED and is encoded as 0xC020. At what address would you find the label end_switch?
Solution left to the student
- Write the code to implement the following flow-chart
Solution left to the student
- Write the code to implement the following flow-chart
Solution left to the student
Branching
Review AVR Looping lecture notes for help in answering these questions.
Subroutines
The Basics
Review AVR Subroutine Basics lecture notes for help in answering these questions.
Stack Operations
Review AVR Subroutine Basics lecture notes for help in answering these questions.
Introduction to C++
Review C++ Introduction plus “Test Your Knowledge” questions.