Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Lab 5: Interrupts

I. Introduction:

This lab explores interrupt programming techniques for microcontrollers, specifically


targeting the AVR architecture. The main objective is to become proficient in using interrupts to
improve program functionality and responsiveness. Through a series of activities, we will learn
to program the AVR timer to generate square waves for sound output and manage external
interrupts triggered by events like key presses.

II. Apparatus:

To program interrupts in Atmega32, we will use AtmelStudio and to simulate our results we
will be using Proteus.

III. Experimental procedure & Results:


a. Activity 1:

In this activity, we generate a square function with a frequency of 1000Hz using timer
interrupts for Atmega32 in PORTC bit 2. To do that we have to determine the value of the timer
𝑻
counter for Timer 0: 1000Hz => 𝒅𝒆𝒍𝒂𝒚 = 𝟐 = 𝟓 ∗ 𝟏𝟎−𝟒𝒔 and 𝑻𝒙𝒕𝒂𝒍 = 𝟏𝟎𝟐𝟒/𝟖 ∗ 𝟏𝟎^𝟔 = 𝟏. 𝟐𝟖 ∗
𝟏𝟎^(−𝟒) 𝒔 𝒅𝒆𝒍𝒂𝒚 = (𝟐𝟓𝟔 − 𝑻𝑪𝑵𝑻𝟎) ∗ 𝑻𝒙𝒕𝒂𝒍 = 𝟎. 𝟎𝟎𝟎𝟓 => 𝑻𝑪𝑵𝑻𝟎 = −𝟐𝟓𝟐

We then set PORTC to output by setting it to FFH, set TCCR0 to 15H (1024prescaler, normal
mode), and TOIE0 in the timer interrupt mask register to 1 as shown below:
Using the sei() instruction, the interrupt instruction with the Timer0 normal mode vector name
will halt all tasks and toggle PORTC.2 once the timer flag is raised. For the simulation in
Proteus, to observe the square wave generated on PORTC.2 of the Atmega32 MCU, connect the
pin to Channel A of the oscilloscope, load the hex file of the application mentioned above, and
run the simulation:

In the oscilloscope we get the following waveform:


as observed, the count value of the period is 0.2s, however, our waveform has a frequency of 1ms,
so we modify to get the correct reading:

b. Activity 2:
In this activity, we will repeat the same procedure with a frequency of 500 Hz. Thus we
redo our calculation of the timer counter, knowing that the control register will have the same
value
𝑻 𝟏𝟎𝟐𝟒
since we keep the same prescaler:500Hz => 𝒅𝒆𝒍𝒂𝒚 = = 𝟏 ∗ 𝟏𝟎−𝟑𝒔 𝑻𝒙𝒕𝒂𝒍 = = 𝟏. 𝟐𝟖 ∗
𝟐 𝟖∗𝟏𝟎𝟔
𝟏𝟎−𝟒𝒔 𝒅𝒆𝒍𝒂𝒚 = (𝟐𝟓𝟔 − 𝑻𝑪𝑵𝑻𝟎) ∗ 𝑻𝒙𝒕𝒂𝒍 = 𝟎. 𝟎𝟎𝟏 => 𝑻𝑪𝑵𝑻𝟎 = −𝟐𝟒𝟖

We record on the oscilloscope:


We then regenerate the same waveform using CTCmode for Timer0. In this case, we have to
determine the value of OCR0:
𝑭 = 𝟓𝟎𝟎𝑯𝒛 => 𝑻 = 𝟎. 𝟎𝟎𝟐𝒔 𝒅𝒆𝒍𝒂𝒚 = 𝑻/𝟐 = 𝟎. 𝟎𝟎𝟏𝒔 and 𝑻𝒙𝒕𝒂𝒍 = 𝟔𝟒/(𝟖 ∗ 𝟏𝟎^𝟔)

𝑫𝒆𝒍𝒂𝒚 = (𝑶𝑪𝑹𝟎 + 𝟏) ∗ 𝑻𝒙𝒕𝒂𝒍 => 𝑶𝑪𝑹𝟎 = 𝟏𝟐𝟒

With a prescaler of 64, so the value TCCR0 will be 0BH. We also set the CTC mode flag in the
TIMSK register and changed the name vector of the interrupt instruction to
TIMER0_COMP_vect as shown below:

In Protus, we keep the same setup for the microcontroller and the oscilloscope as the previous
activity, load the new hex file, and run the simulation:
c. Activity 3:
In this activity, we will program our AVR to use external interrupts by monitoring the
INT0 pin, which is located at PORTD.2 on the Atmega31, and controlling PORTB.2. We'll
enable the pull-up resistor on the INT0 pin and connect it to a push button. Whenever INT0 goes
low, PORTB.2 will toggle. To achieve this with an interrupt, we need to set PORTD.2 to pull-up
and enable INT0 by setting it high in the General Interrupt Control Register (GICR). Finally, we
issue the sei() command and use the interrupt vector name (INT0_vect) to toggle PORTB.2:
In the simulation, we use an Atmega32 and connect INT0 (PORTD.2) to a pull-up resistor with a
push button to ground (5Ω). PORTB.2 is connected to an LED to monitor its state, with a 5kΩ
resistor added to protect the LED, which can only handle a maximum current of 10mA. We then
load the hex file and run the simulation:

IV. Questions:
1) The lowest frequency that can be generated with TIMER0 and for a crystal frequency of
16MHz is using a 1024 prescaler and setting TCNT0 to 0
𝟏
=> 𝑭𝒕𝒊𝒎𝒆𝒓 = 𝟏𝟔∗𝟏𝟎𝟔 = 𝟑𝟎. 𝟓𝟐𝑯𝒛.
𝟐∗𝒅𝒆𝒍𝒂𝒚 = 𝟐∗𝟏𝟎𝟐𝟒∗(𝟐𝟓𝟔−𝑻𝑪𝑵𝑻𝟎)

2) The lowest frequency that can be generated using TIMER1 with a frequency of 16MHz is
0.238Hz
=> 𝑭𝒕𝒊𝒎𝒆𝒓 = 𝟏 𝟏𝟔 ∗ 𝟏𝟎^𝟔
= = 𝟎. 𝟐𝟑𝟖𝑯𝒛
𝟐 ∗ 𝒅𝒆𝒍𝒂𝒚 𝟐 ∗ 𝟏𝟎𝟐𝟒 ∗ (𝟔𝟓𝟓𝟑𝟔 − 𝑻𝑪𝑵𝑻𝟏)
1. In normal mode, the TOVx flag is set high to indicate that the timer counter has reached its
maximum value, and the flag is cleared immediately after the counter resets.
2. In CTC mode, the OCRx flag is set high to indicate a compare match has occurred,
signaling that the counter needs to reset, and the flag is then cleared.

V. Conclusion:
This lab on interrupt programming for microcontrollers, with a focus on the AVR
architecture, aimed to enhance our understanding of using interrupts to improve program
functionality and responsiveness. Through hands-on activities, we learned to program the AVR
timer to generate square waves and manage external interrupts triggered by events like key
presses.

In Activity 1, we generated a 1000 Hz square wave using timer interrupts, carefully


calculating timer counter values and observing the waveforms through simulation. Activity 2
built on this by repeating the process for a 500 Hz frequency and exploring both Normal and
CTC modes of Timer0 operation. In Activity 3, we introduced external interrupts, using the
INT0 pin to monitor external events and toggle PORTB.2 accordingly.

These exercises provided us with insights into timer frequency calculations, interrupt
handling mechanisms, and practical applications in embedded systems design, establishing a
solid foundation for future work in the field.

You might also like