Sie sind auf Seite 1von 45

Timer Interrupts

ECET 209 – Lecture 21


Introduction to Microcontrollers
Interrupts
• Interrupts are actions that pull us away from
our normal activities
• For instances:
– Phone Ringing
– Door Bell
– Smoke Alarm
– Clothes Dryer
– Etc.
• These devices signal us when they they
need attention
ECET 209 Purdue University 2
Interrupts

• Microcontrollers have Interrupts that signal


us when they need attention
– Timer Interrupt
– Serial Interrupt
– External Interrupts
– ETC

ECET 209 Purdue University 3


Interrupts

• Interrupts are characterized as events that:


– Require immediate attention
– Happen at irregular intervals
– Happen infrequently

– * Background tasks

ECET 209 Purdue University 4


Interrupt Service Routines

• Interrupt Service Routines (ISR) are


functions that are executed in response to a
hardware event rather than a function call
embedded in the program

ECET 209 Purdue University 5


Interrupt Service Routines

• “Normal” function calls occur in the natural flow of


a program…

do something
do something else
make a function call
do that other thing
and so on
ECET 209 Purdue University 6
Interrupt Service Routines

• ISR occur whenever they are triggered…

do something
do something else
do that other thing ISR
still doing stuff
and so on

ECET 209 Purdue University 7


Interrupt Service Routines

• ISR are almost identical to normal functions


with a few executions
– They CANNOT receive any parameters
– They CANNOT return any values
– WHY?
• We do not know when they are going to occur!!
• Therefore, any values that they need or that they are
going to provide must be declared GLOBALLY!

ECET 209 Purdue University 8


Interrupt Service Routines

• Interrupt Service Routines must be


configured before they can be used
– The individual interrupt must be enabled
– AND the Global Interrupt Flag must be set

ECET 209 Purdue University 9


Timer Review

ECET 209 Purdue University 10


Timers
• ATmega16
– Two 8-Bit Timers
– One 16-Bit Timer

ECET 209 Purdue University 11


Microcontroller Timer

ECET 209 Purdue University 12


Microcontroller Timer

ECET 209 Purdue University 13


Microcontroller Timer

ECET 209 Purdue University 14


Clock Select

GND (no signal )


System Clock

CK/8

TCCR1B
System Clock
Prescale

CK/64
Timer/Counter
CK/256
Register (TCNT1)
CK/1024

External Pin T1

CS10
CS11
CS12
ECET 209 Purdue University 15
Clock Select
Timer / Counter Control Register - TCCR1B

7 6 5 4 3 2 1 0
- - - - - CS12 CS11 CS10

GND (no input) 0 0 0


System Clock 0 0 1
System Clock / 8 0 1 0
System Clock / 64 0 1 1
System Clock / 256 1 0 0
System Clock / 1024 1 0 1
External Pin T1 - Falling Edge 1 1 0
External Pin T1 - Rising Edge 1 1 1

ECET 209 Purdue University 16


Timer/Counter Register

TCNT1

16-Bit Counter

ECET 209 Purdue University 17


Timer Implementation

• PreLoad the Timer/Counter


• Select the clock frequency
• Wait for the overflow flag
• Stop the counter
• Clear the Overflow Flag

ECET 209 Purdue University 18


TCNT1 = 64786; // set for 250 counts
TCCR1B = 2; // select CLK / 8
while ( (TIFR & 0x04) == 0) // wait for flag
{
}
TCCR1B = 0; // stop the clock
TIFR = TIFR | 0x04; // clear the flag

ECET 209 Purdue University 19


Timer Example

• Timer – The timer overflow flag is set high


when an overflow occurs.
– This is actually an interrupt flag that is being
set
– The interrupt wasn’t executed because we
didn’t enable it to.
– Instead, we just “polled” for the flag

ECET 209 Purdue University 20


TIFR – Timer Interrupt Flag
Register
• When:
– The Timer Overflow Interrupt is Enabled
– AND the Global Interrupt Flag is Enabled
– AND the Timer Overflow flag is set
– The Timer ISR is executed!!

ECET 209 Purdue University 21


Configuring the Interrupt
• Solution Steps to use a Timer Interrupt
– Setup the timer for the desired delay
– Write the code for the ISR
– Enable the actual timer interrupt in the Timer
Interrupt Mask Register – TIMSK
– Enable the global interrupt enable bit located in
the Status Register – SREG

– Wait for the interrupt


ECET 209 Purdue University 22
Timers
• ATmega16
– Two 8-Bit Timers
– One 16-Bit Timer

ECET 209 Purdue University 23


Timer0

ECET 209 Purdue University 24


Block Diagram

GND (no signal )


System Clock

CK/8
System Clock

TCCR0
Prescale

CK/64
Timer/Counter
CK/256
Register (TCNT0)
CK/1024

External Pin T0

CS00
CS01
CS02
ECET 209 Purdue University 25
Microcontroller Timer

ECET 209 Purdue University 26


Microcontroller Timer

ECET 209 Purdue University 27


Microcontroller Timer

ECET 209 Purdue University 28


Microcontroller Timer

ECET 209 Purdue University 29


ECET 209 Purdue University 30
Interrupt Example

• Create a Timer Interrupt Service Routine:


– Toggles the value on Port C every 4mSec
– Utilize Timer0

ECET 209 Purdue University 31


Creating a 4mS Delay

Oscillator Frequency 6.0000 MHz

Desired Time Delay 4 Milliseconds

Calculated Number of Ticks


Clock 24000
Clock / 8 3000
Clock / 64 375
Clock / 256 93.75
Clock / 1024 23.4375

ECET 209 Purdue University 32


Example

• Setup the timer for the desired clock


void main (void)
{
TCNT0 = 232; // set for 24 ticks
TCCR0 = 5; // select CLK/1024

ECET 209 Purdue University 33


Example

• Write the ISR code

interrupt [TIM0_OVF] void timer0_ovf_isr(void)


{
PORTC = ~PORTC; // toggle the port
}

ECET 209 Purdue University 34


Example
• Enable the individual timer interrupt flag in the TIMSK
register
void main(void)
{
DDRC = 0xFF; // PORTC = output
TCNT0 = 232; // set for 24 ticks
TCCR0 = 5; // select CLK/1024
TIMSK = 0x01; // enable timer 0 ISR

ECET 209 Purdue University 35


Example
• Enable the global interrupt bit in the Status Register
SREG
void main(void)
{
DDRC = 0xFF; // PORTC = output
TCNT0 = 232; // set for 24 ticks
TCCR0 = 5; // select CLK/1024
TIMSK = 0x01; // enable timer 0 ISR
SREG = SREG | 0x80; // enable global int

ECET 209 Purdue University 36


Example
• Do nothing and wait for the interrupt to expire.

while (1) // do nothing


{

ECET 209 Purdue University 37


Example

• You can still preload the timer with values to


adjust the number of rollover ticks by writing data
into the TCNT0 register.

interrupt [TIM0_OVF] void timer0_ovf_isr(void)


{
TCNT0 = 232; // 4mSec delay
PORTC = ~PORTC; // toggle the port
}
ECET 209 Purdue University 38
Timer Interrupt Flags

• Timer/Counter Overflow Flag is set when


an overflow occurs.
– Flag is cleared by writing a ‘1’ to the flag

– Alternatively, the flag is cleared automatically


be the microcontroller when executing the ISR

ECET 209 Purdue University 39


Lab
• You will need to create a timer interrupt that
is 4mSec long (calculate just as before)
– In the event that 4mS cannot be accurately
obtained, pick something that is close.

• Ultimately you will use the timer interrupt to


output BCD data to the 7-segment display
board!!

ECET 209 Purdue University 40


Displaying Data

• Very similar to the techniques we’ve


already used with a few differences…

– Cannot waste time in an interrupt

ECET 209 Purdue University 41


Displaying Data

• Very similar to the techniques we’ve


already used with a few differences…

– Cannot waste time in an interrupt


– Must perform the desired task and move on
(general rule is that the flowchart flowlines are
not allowed to go “up”, they must always go
“down”)
ECET 209 Purdue University 42
Flowcharts

ECET 209 Purdue University 43


Flowcharts

Display Data

Boundary
Check

Reset Index

ECET 209 Purdue University 44


Questions?

ECET 209 Purdue University 45

Das könnte Ihnen auch gefallen