Lab 08
Lab 08
Lab 08
Timer Programming
Submitted By:
Asna Maqsood
426990
BESE13 B
Embedded System
Lab 08
Timer Programming
Submitted By:
Muhammad Ahsan
406267
BESE13 B
Embedded System
Lab 08
Timer Programming
Submitted By:
Muhammad Owais Khan
404262
BESE13 B
Embedded System
Lab 08
Timer Programming
Submitted By:
Umar Farooq
406481
BESE13 B
Lab 8: Timer Programming
EE222: Microprocessor Systems
Contents
1 Acknowledgements..........................................................................................................................2
2 Administrivia.....................................................................................................................................2
2.1 Learning Outcomes..........................................................................................................................2
2.2 Deliverable........................................................................................................................................2
2.3 Hardware Resources........................................................................................................................2
3 Introduction.......................................................................................................................................3
3.1 AVR Timers.......................................................................................................................................3
3.2 AVR Timer 0 programming.............................................................................................................3
3.2.1 Timer Counter Zero (TCNT0)......................................................................................................3
3.2.2 Timer Counter Control Register Zero (TCCR0)..........................................................................3
3.2.3 Output Compare Register Zero (OCR0)......................................................................................4
3.2.4 Timer 0 Interrupt Flag Register (TIFR0)....................................................................................4
3.3 AVR Timer 1 programming.............................................................................................................4
3.3.1 Timer Counter One (TCNT1).......................................................................................................4
3.3.2 Timer Counter Control Register One (TCCR1)...........................................................................4
3.3.3 Output Compare Register One (OCR1).......................................................................................5
3.3.4 Timer 1 Interrupt Flag Register (TIFR1)....................................................................................5
3.3.5 Timer 1 CTC Mode (Clear Timer on Compare)...........................................................................5
3.3.6 Timer 1 CTC Mode Example.........................................................................................................6
4 Lab Tasks............................................................................................................................................7
4.1 Task A................................................................................................................................................7
4.2 Task B................................................................................................................................................7
4.3 Task C................................................................................................................................................7
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, focusing on the
ATmega16 microcontroller. Later on, the lab was revised for the ATmega328p Arduino Uno-
based microcontroller by Lab Engr. Shaiza. Reporting any errors or discrepancies found in the
text is appreciated.
2 Administrivia
2.1 Learning Outcomes
By the end of this lab you will be able to;
2.2 Deliverable
You are required to submit
• Issues in Developing the Solution and your Response in the beginning of next lab
• Resistance 47Ω x2
• Switches
3 Introduction
3.1 AVR Timers
Timers are special registers in microcontrollers/microprocessors whose value increment
with every clock tick. In AVR, there are three timers, Timer 0, Timer 1 and Timer 2.
For now only CS02:CS00 bits are of importance. These bits are used to apply prescalar to input
clock frequency
CS02:CS00 Function
000 Timer Counter Stopped
001 clk (No prescalar)
010 clk / 8
011 clk / 64
100 clk / 256
101 clk / 1024
WGM02:WGM00 Mode
000 Normal Mode
010 CTC Mode
3.2.3 Output Compare Register Zero (OCR0)
We can load some 8-bit value in this register, such that when the value in counter matches
it, the Output Compare Flag Zero (OCF0) is raised.
OCR0A: The output compare register A contains an 8-bit value that is continuously
compared with the counter value (TCNT0). A match can be used to generate a waveform
output on the OC0A pin.
OCR0B: The output compare register B contains an 8-bit value that is continuously
compared with the counter value (TCNT0). A match can be used to generate a waveform
output on the OC0B pin.
3.2.4 Timer 0 Interrupt Flag Register (TIFR0)
-- -- -- -- -- OCF0B OCF0A TOV0
TCNT1 is a 16-bit register divided into two 8-bit chunks TCNT1H and TCNT1L. To load a 16-
bit value into it we must separate it into two 8-bit chunks, load the lower 8-bits in TCNT1L
and higher 8-bits in TCNT1H separately.
WGM13:WGM10 Mode
The lower two bits of WGM control are in TCCR1A and the higher two bits are in TCCR1B.
For Timer 1, there are two output compare registers OCR1A and OCR1B, 16-bit each, with
compare match flag OCF1A and OCF1B in TIFR1.
{
if ( (TIFR1 & 0x02) != 0 ) // Check OCF1A flag bit
{
TIFR1 |= 0x02 ; // Clear
flag data = PINC; // Read
port C if ( ( data & 0x01)
== 0) PORTD = 'L' ;
else
PORTD= 'H' ;
}
}
}
In the example above, the time consumed by if-else statement and other instructions inside
while(1) loop will not affect the timer count because the timer has been restarted by
hardware after compare match and is running in parallel with these instructions.
Why have we used or-equals in line 16 and not simple equal? Find the answer.
4 Lab Tasks
4.1 Task A
Implement the code in example 2.3.6, just to get familiar with timer programming and paste
proper screenshots of output.
#include <avr/io.h> int
main( )
{
DDRC = 0x00 ; // Set port C as input
DDRD = 0xFF; // Set port D as output
TCNT1 = 0; //Initialize timer 1
OCR1A = 31250;// Set output compare register
TCCR1A = 0x00 ; // WGM13:WGM10 = 0100 , CS12:CS10 = 100
TCCR1B = 0x0C;
TCCR1C = 0x00;
unsigned char data ; while
(1)
{
if ( (TIFR1 & 0x02) != 0 ) // Check OCF1A flag bit
{
TIFR1 |= 0x02 ; // Clear flag
data = PINC; // Read port C
if ( ( data & 0x01) == 0)
PORTD = 0b00111000 ;
else
PORTD= 0b01110110 ;
}
}
}
Output:
4.2 Task B
Calculate and determine the value of compare match and prescalar for timer 1 such that it
produces a precise 1 second delay for 16MHz clock. Include your calculations in the report.
Code:
#include <avr/io.h>
int main() {
// Set Port D as output
DDRD = 0xFF;
// Initialize Timer 1
TCNT1 = 0; // Start count from 0
OCR1A = 62499; // Set compare match value for 1-second delay
TCCR1A = 0x00; // WGM13:WGM10 = 0100 (CTC mode)
TCCR1B = 0x0C; // WGM12 = 1, CS12:CS10 = 100 (prescalar = 256)
while (1) {
if (TIFR1 & 0x02) { // Check OCF1A flag
TIFR1 |= 0x02; // Clear flag by writing '1'
PORTD ^= 0xFF; // Toggle PORTD
}
}
return 0;
}
Calculations:
Given:
Clock Frequency (F_CPU) = 16 MHz = 16,000,000 Hz
Required Delay = 1 second
Prescaler = 256
Summary:
Timer Tick Period = 16 μs (16 microseconds)
Required Ticks = 62,500
OCR1A = 62,499
This configuration ensures that Timer 1 in CTC (Clear Timer on Compare Match) mode provides an
accurate delay of 1 second.
4.3 Task C
In this task you are required to create a “Digital Stop Watch” that records the time in
seconds precisely (use CTC mode).
1. Connect two 7-segment-Displays with your ATmega328p.
2. Connect two switches (say Sw1 and Sw2) with your ATmega328p.
3. If Sw1 is high, the Stop Watch must get reset to zero, no matter what is the state of Sw2.
4. If Sw2 is high and Sw1 is low, the Stop Watch must display the seconds passed.
5. If Sw2 is low and Sw1 is also low, the Stop Watch must pause its time and if Sw2 is
raised again, it should resume from where it was paused.
Hint: Using timer-1 is same as using timer-0. The difference is in size of the TCNTx. The example
provided in Section 2.3.6 serves as the code template for this task. You basically need to
enhance it with your functionality.
Code:
void configure_ports(void) {
DDRB = 0xFF;
DDRD = 0xFF;
PORTB = 0x00;
PORTD = 0x00;
DDRC = 0x00;
}
int main(void) {
uint8_t time_val = 0;
configure_ports();
TCNT1 = 0;
OCR1A = 62500;
TCCR1A = 0x00;
TCCR1B = 0x0C;
TCCR1C = 0x00;
while (1) {
if ((TIFR1 & 0x02) != 0) {
TIFR1 |= 0x02;
if (PINC & 0x01) {
time_val = 0;
} else if (PINC & 0x02) {
time_val++;
if (time_val > 59) {
time_val = 0;
}
}
show_value(time_val);
}
}
return 0;
}
Output: