CSEP567 PreLab 3: “Programming in C”
Objectives
The goal of this lab is to introduce various features and
modules of the ATmega16 while introducing how to program the microcontroller
using C.
Reading / Helpful Hints
-
The following sections of the ATmega16 datasheet might be helpful for this lab
-
Interrupts
-
8-bit Timer2
-
Analog to Digital Converter
-
Here is a reference on bypassing, a technique to reduce noise in your circuit:
http://www.seattlerobotics.org/encoder/jun97/basics.html.
-
Even though the ATMega16 is an 8-bit processor, an int is 16 bits.
-
Debouncing a button is essentially an averaging or smoothing of the input
value. This is often referred to
as low-pass filtering because only the long-term variations (at low frequency)
are used (passed) and rapid changes (at high frequency) are discarded. A
good way to do this in software is to sample a button several times and take
the average or mode (the most common value across all the samples – this is
like voting for Boolean values).
Resources
Tasks
-
Read
the link about bypass capacitors.
-
Implement a counter in C that counts at 1Hz from '00' to '59'
using Timer 1's output compare interrupt for timing.
Documentation for the avr-gcc compiler is available at
http://www.nongnu.org/avr-libc/user-manual/index.html From
that link, go to the modules section and then the Interrupts and Signals entry.
Remember, when writing the interrupt handler routine, you'll want to minimize
the amount of code in it. Interrupt handlers are meant to be very fast, and do
only the work that is critical to do right at that instant. There is no way
that anyone will be able to notice if your LEDs update even a few milliseconds
late, so the LED update code should not be in the handler.
-
Implement the stop-start button (pin 18) so that pressing the button will
cause the counter to toggle between counting or halting. The left (tens digit)
decimal point should indicate whether the counter is running by being on when
the counter is running and off when the counter is stopped. We have provided
some sample code for debouncing a button. You should setup the timer to sample
the button at 5ms intervals. Remember that if you push the button and hold it
for a long time it should only register as a single push.
-
Implement the up-down button (pin 19) so pressing the button will change
the direction of counting. The decimal point on the right (ones digit) should
be on when counting up and be off when counting down. Note that even when the
counter is stopped, the system should still process an up-down button press by
immediately updating the direction and decimal point state. \
Code
Question
Question 1: What is the purpose of the
bypass capacitors? Why might they be useful for this lab?
Question 2: When and why should you use the type qualifier "volatile"?
(Hint: it related to the optimization of a compiler)
Turn-in
Please complete the prelab individually. Please turn-in your commented C
code along with the answers to the two questions at the beginning of next weeks
lab.