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

Example 10-10 (C version of Program 10-3)

Using TimerO and Timerl interrupts, write a program in which:


(a) PORTA counts up everytime Timerl overflows. It overflows once per second.
(b) A pulse is fed into TimerO where TimerO is used as counter and counts up. Whenever
the counter reaches 200, it will toggle the pin PORTB.6.
Solution:
#include "avr/io.h"
include "avr/interrupt.h"
i
nt
{

main ()
DDRA = OxFF;
DDRD = OxFF;
DDRB |= 0x40;
PORTB |= 0x01;
TCNTO = -200;
TCCRO = 0x06;
TCNT1H = (-31250) 8;
TCNT1L = (31250)&0xFF;
TCCR1A = 0x00;
TCCR1B = 0x04;

//make PORTA output


//make PORTD output
//PORTB.6 as an output
//activate pull-up
//load TimerO with -200
//Normal mode, falling edge, no
prescaler
//the high byte
//overflow after 31250 clocks
//Normal mode
//internal clock, prescaler 1:256

TIMSK = (lTOIE0) | (KCTOIEI) ; //enable Timers 0 & 1 int.


sei ( );
//enable interrupts
DDRC = 0x00;
DDRD = OxFF;

//make PORTC input


//make PORTD output

while (1)
PORTD = PINC;

//wait here

I
SR
{

(TIMERO _ 0 VF_ve ct)


TCNTO = -200;
PORTB A = 0x40;

//ISR for TimerO overflow


//TCNTO = -200
//toggle PORTB.6

I
SR
{
}

(TIMERl_OVF_vect)
TCNT1H = (-31250)> > 8;
TCNT1L = (31250)&0xFF;
PORTA
++;

//ISR for Timerl overflow


//the high byte
//overflow after 31250 clocks
//increment PORTA

Example 10-11 (C version of Example 10-4)


Using Timer 1, write a program that toggles pin PORTB.5 every second, while at the
same time transferring data from PORTC to PORTD. Assume XTAL = 8 MHz.
Solution:
include "avr/io.h"
include "avr/interrupt.h"
int main ()
{

DDRB [= 0x20;

//make DDRB.5 output

OCRO 40;
TCCRO = 0x09;
TIMSK = (lOCIE0) ;
sei ();

//CTC mode, internal elk, no prescaler


//enable TimerO compare match int.
//enable interrupts

DDRC = 0x00;
DDRD = GxFF;

while (1)
PORT
D

//make PORTC input


//make PORTD
output
//wait here
PINC
;

ISR (TIMERO_COMP_vect)

//ISR for TimerO compare match

PORTB A= 0x20;
}

//toggle PORTB.5

Example 10-12 (C version of Example 10-5)


Assume that the INTO pin is connected to a switch that is normally high. Write a program that toggles P0RTC.3, whenever INTO pin goes low. Use the external interrupt in
level-triggered mode.
Solution:

#include "avr/io.h"
frinclude
"avr/interrupt.h"
int main ()
{

DDRC = 13;
PORTD = 12;
GICR = (1INT0) ;
sei () ;

//PC3 as an output //pull-up


activated //enable external
interrupt 0 //enable
interrupts

while (1);

//wait here

ISR (INT0_vect)
{

PORTC A= (13) ;

//ISR for external interrupt 0


//toggle PORTC.3

Example 10-13 (C version of Example Example 10-7)


Rewrite Example 10-12 so that whenever INTO goes low, it toggles PORTC.3 only
once.
Solution:
#include "avr/io.h"
#include
"avr/interrupt.h"
int main ()
{
DDRC = 13;
PORTD = 12;
MCUCR = 0x02;

GICR = (1INT0);
sei ();
while (1);

}
ISR

(INTO vect)

{
PORTC A= (13);

//PC3 as an output
//pull-up activated
//make INTO falling edge triggered

//enable external interrupt 0 //enable


interrupts
//wait here
//ISR for external interrupt 0
//toggle P0RTC.3

SUMMARY
An interrupt is an external or internal event that interrupts the microcontroller to inform it that a device needs its service. Every interrupt has a program
associated with it called the I SR, or interrupt service routine. The AVR has many
sources of interrupts, depending on the family member. Some of the most widely
used interrupts are for the timers, external hardware interrupts, and serial communication. When an interrupt is activated, the IF (interrupt flag) bit is raised.
The AVR can be programmed to enable (unmask) or disable (mask) an
interrupt, which is done with the help of the I (global interrupt enable) and IE
(interrupt enable) bits. This chapter also showed how to program AVR interrupts
in both Assembly and C languages.

PROBLEMS
SECTION 10.1: AVR INTERRUPTS
1. Which technique, interrupt or polling, avoids tying down the microcontroller?
2. List some of the interrupt sources in the AVR.
3. In the ATmega32 what memory area is assigned to the interrupt vector table?
4. True or false. The AVR programmer cannot change the memory address location assigned to the interrupt vector table.
5. What memory address is assigned to the TimerO overflow interrupt in the
interrupt vector table?
6. What memory address is assigned to the Timerl overflow interrupt in the
interrupt vector table?
7. Do we have a memory address assigned to the TimeO compare match
interrupt in the interrupt vector table?
8. Do we have a memory address assigned to the external INTO interrupt in the
interrupt vector table?
9. To which register does the I bit belong?
10. Why do we put a JMP instruction at address 0?
11. What is the state of the I bit upon power-on reset, and what does it mean?
12. Show the instruction to enable the TimerO compare match interrupt.
13. Show the instruction to enable the Timerl overflow interrupt.
14. The TOIEO bit belongs to register_________.
15. True or false. The TIMSK register is not a bit-addressable register.
16. With a single instruction, show how to disable all the interrupts.
17. Show how to disable the INTO interrupt.
18. True or false. Upon reset, all interrupts are enabled by the AVR.
19. In the AVR, how many bytes of program memory are assigned to the reset?
SECTION 10.2: PROGRAMMING TIMER INTERRUPTS
20. True or false. For each of TimerO and Timerl, there is a unique address in the
interrupt vector table.

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