Вы находитесь на странице: 1из 5

MPI LAB

LAB 5
Handling Timers with interrupts on Atmega 32

Name : Burhan Ahmed Satti


Enrollment No. : 01-134172-065
Class : BSCS 4-A
Subject : MPI Lab
Lab Assistant : Muhammad Atif

Objectives
•Learning the use of timers
•Introduction interrupts of Atmega 32 processor
•Learning basics of Atmel studio
•Learning use of timers with interrrupts 32 processor

Tools Used
•Proteus 8.0
•Atmel Studio 7.0
BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 5.1


Write a C program to toggle only bit 8 of PORTB continuously every 500ms.Using timer zero
with interrupt.
Solution
#include <asf.h>
#include <avr/io.h>
#include <avr/interrupt.h>

int Counts = 0;

int main (void)


{
board_init();

DDRB = 1<<7;
TCCR0 = 1<<WGM01;
OCR0 = 25;
TIMSK = 1 << OCIE0;
sei();
TCCR0 = 1 << CS00;

while(1)
{

}
}

ISR(TIMER0_COMP_vect)
{
Counts++;
if(Counts >= 100)
{
Counts = 0;
PORTB ^= 1<<7;
TCNT0 = 0;
}
}

MPI Lab Page 2 of 5


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Result

Conclusion
The LED toggles every half second.

MPI Lab Page 3 of 5


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Task No. 5.2


Write a C program to toggle only bit 8 of PORTB continuously every 500ms.Using timer zero
with interrupt. and prescaler of 256 microseconds.
Solution
#include <asf.h>
#include <avr/io.h>
#include <avr/interrupt.h>

int Counts = 0;

int main (void)


{
board_init();

DDRB = 1<<7;
TCCR0 = 1<<WGM01;
OCR0 = 25;
TIMSK = 1 << OCIE0;
sei();
TCCR0 = 1 << CS02;

while(1)
{

}
}

ISR(TIMER0_COMP_vect)
{
Counts++;
if(Counts >= 39.0625)
{
Counts = 0;
PORTB ^= 1<<7;
TCNT0 = 0;
}
}}

MPI Lab Page 4 of 5


BSCS 4-A Burhan Ahmed Satti 01-134172-065

Result

Conclusion
LED is toggling successfully.

MPI Lab Page 5 of 5

Вам также может понравиться