Assembly Robot Lab 4 – Handling the Intersections with Delays and Interrupts

Introduction

This lab is designed to help you understand how timers, interrupts, and finite state machines work by using them to address the issue of changing directions from Lab 3. You will learn how to configure the timers for a specified delay, how the interrupts are handled, and how to get the robot to turn at the right positions in the maze.

What Is New

The following instructions and assembly directives are used in Lab 4. If you have any questions on any instructions or assembly directives a nice source of information, in addition to your textbook, are AVR Instruction Set Manual and the Atmel AVR Assembler User Guide.

AVR Assembly Instructions

Data Transfer

lds r24, variable      // Load value from variable(or location in SRAM) to register 24
sts variable, r16      // Store value from register 16 to variable (or location in SRAM)
push r17               // Save the value from register 17 to the stack temporarily
pop r19                // Copy the last value from the stack to register 19.

Arithmetic and Logic

cbr r24, 0b01101101     // Clear the bits in register 24 designated with a value of 1. Locations with a value of 0 are left the same.
tst r16                 // Check to see if the value in register 16 is equal to zero or negative (does the AND operation with itself).

Control Transfer

cpi r24, 0x3C         // Compares the value in register 24 to the constant 0x3C
reti                  // Return from Interrupt Service Routine (will re-enable global interrupts)
sei                   // Set the I bit in SREG (enable global interrupts)

AVR Studio Assembly

Directives

.DSEG                       // Indicates the start of the Data Segment (SRAM)

example: .BYTE X      // Defines an SRAM variable called example that is in terms of bytes and takes up X bytes. (IE X=3 means it takes up 3 bytes)

Breaking the Objective into Parts

At the end of Lab 3, you should have gotten your robot to continuously follow a line and be able to cross over intersections at a slower speed instead of stopping. This allows us to use some creative solutions for handling the position of the robot when it needs to make turns rather than having to perform some precise control. As it is, the program from Lab 3 is suited for the parts of your path that are straight lines but is not capable of changing directions. We will be adding in code that will form the new structure of the main loop for future labs and taking care of the turning without extra readings from the IR sensors to determine the robot’s position. The end goal of Lab 4 is to have the robot follow a line to an intersection, cross it and move to the center of the room, and then spin in place. The spinning in place is a temporary output to show that the robot has switched from the line following algorithm to making a decision after it has entered a room in the maze.

In order to implement this, the lab can be broken down into four parts that are explained further below. The first part is setting the timer up properly for a specific delay. This will be necessary for capturing the time between hitting the intersection and when the robot is in the center of the room. The second part is to configure and enable the timer overflow interrupt. This allows the CPU to not have to be constantly checking that status of the timer and it can perform the line following algorithm until it is time to switch to spinning in place. The third part is to modify the main loop in order to implement the finite state machine. We are doing this in order to prepare for future labs and understand how they work. The fourth part is to change the conditional speed setting from Lab 3 to a more compact version that separates it into the different cases. This is in order to activate the timer overflow interrupt each time the intersection is hit, not at the very beginning of the program.

Setting up the Timer

As discussed in Prelab 4, there are several steps involved with setting up the timer to achieve a specified delay. We will review these steps and go over what is needed for Lab 4.

It is important to note that the timer registers are not part of the general purpose registers that are in the AVR CPU. They are located within the Extended I/O registers which are a part of the SRAM address space, not the I/O address space. We are only concerned with the normal mode which will increment by 1 until it reaches the maximum value 0xFFFF and then restarts at 0x0000 when it overflows. You may notice that there are terms for TOP, BOTTOM, and MAX. These can be changed depending on the operating mode but you should consider MAX to be 0xFFFF and BOTTOM to be 0x0000 for our course.

Figure 1: ATmega32u4 Operating Modes

As we are only working in normal mode, you will not need to modify any of the configuration registers besides TCCR1B which stands for Timer/Counter 1 Control Register B. This is because the default setting for the other registers is to start in normal mode.

Figure 2: TCCR1B Description

The main bits that we are concerned about in TCCR1B are the clock select or CS bits. These bits allow us to define how quickly the timer will increment and ultimately how long it takes to overflow. The options for this are all based on the system clock signal coming from the microcontroller or an external clock source as indicated in Figure 3. You will need to select the appropriate combination for the bits in order to configure the timer for your desired delay. For example, if the pre-scale value of 256 is chosen with a system clock frequency of 16 MHz, the clock frequency for the timer will effectively be 62.5 KHz and result in a maximum delay of 1.0486 seconds.

Figure 3: Pre-scale Value Selection

The last of the registers that you will need to become familiar with are the timer counter registers (TCNT1H and TCNT1L) and the timer interrupt flag register (TIFR1). They are relatively self explanatory, with the timer counter registers holding the current value before the next increment and the timer interrupt flag register holds the status of the flags. We are mainly concerned with the TOV1 bit which indicates if the timer has overflowed (becomes 1) or not. One important thing to keep in mind is that the timer does not automatically tell the microcontroller that the overflow has occurred unless the corresponding interrupt was configured. If that has not been set, the microcontroller will only know that the timer is finished when it is programmed to check the value of the TOV1 bit.

Figure 4: Timer 1 Counter Registers


Figure 5: Timer 1 Interrupt Flag Register

The delay that you will need to implement will be dependent on your robot. As each robot has different maximum and minimum speeds, they will each get to the center of the room at different times after hitting the intersection. Since you will not know what is the right value at the start, we will begin with a delay of 500 milliseconds. This will be changed after the rest of the code has been completed as you will need to test the robot and make sure it does not go beyond the center of the room. If it crosses two or more intersections, you will need to lower the delay.

Understanding how the Timer 1 Overflow Interrupt works

Go over how interrupts operate, talk about how this works with the timer 1 overflow interrupt, and walk through the steps to set this up.

IVT

ISR

Configuration

Global Enable

Finite State Machine

Discuss how this works, go over the implementation here with state 0 being the regular line following and state 1 being the time to decide which way to turn. Show how they cycle through each other. Go over how to define SRAM variables and add in the code for where they should be updated.

Putting it all Together

Cover the final changes to the code with how the main program is structured. Tell them that they will need to keep testing until the robot is accurately going over just one intersection.

Design Challenge – Staying in a Square (5 Points)

Modify the code so that the robot will go back into line following after it has turned right or left. Will probably suggest using a timed delay for this one as well. Have it so the robot will move in a square continuously.

Lab 4 Deliverable(s)

All labs should represent your own work – DO NOT COPY.

Submit your list file as defined below. Make sure that the code compiles without any errors. Do not forget to comment your code.

Lab 4 Demonstration

At sign-off,