ATmega103 Timer0 and Interrupts

21
Mark Neil - Microprocessor Course 1 Timers and Interrupts

description

ATmega103 Timer0 and Interrupts. The Need for Processor Interrupts. Up to now if you wanted to do something in a program as soon as a bit was set (key pressed, bit set in a register, voltage exceeded a given threshold,…) you had to keep reading the bit until it changed ! - PowerPoint PPT Presentation

Transcript of ATmega103 Timer0 and Interrupts

Page 1: ATmega103 Timer0 and Interrupts

1

Mark Neil - Microprocessor Course

Timers and Interrupts

Page 2: ATmega103 Timer0 and Interrupts

2

The Need for Processor Interrupts

Mark Neil - Microprocessor Course

Up to now if you wanted to do something in a program as soon as a bit was set (key pressed, bit set in a register, voltage exceeded a given threshold,…) you had to keep reading the bit until it changed ! This is called Polling

This is clearly not an efficient way of doing things CPU time is wasted watching for something to happen

This is why interrupts were introduced as a means of getting the processor’s attention on demand Use the CPU for other things until whatever we were

waiting for has happened

Page 3: ATmega103 Timer0 and Interrupts

3

Processor Interrupts

Mark Neil - Microprocessor Course

Interrupts are subroutine calls initiated not by an rcall command but by hardware signals. These hardware signals cause the processor to jump to interrupt service routines. At the end they return control to your program just where it was just before the interrupt occurred.

Page 4: ATmega103 Timer0 and Interrupts

4

ATmega128 Timers and InterruptsThere are 24 different

sources of interruptsThere are timers on the

ATMega128 that can be used to trigger an interrupt at fixed intervals

Interrupts can be triggered when certain hardware tasks are completed

Eight external inputs can be used to request an interrupt

Mark Neil - Microprocessor Course

Page 5: ATmega103 Timer0 and Interrupts

5

The ATmega103 Memory Map

The first 24 2-word addresses in flash program memory are reserved for interrupts: your program jumps to one of these addresses if an interrupt occurs.

A table of jump instructions is used to let the processor know where to execute code in case of an interruptMark Neil - Microprocessor Course

Page 6: ATmega103 Timer0 and Interrupts

6

Interrupts mapping

Mark Neil - Microprocessor Course

;Address;$0000 jmp RESET ; Reset Handler$0002 jmp EXT_INT0 ; IRQ0 Handler - PortD$0004 jmp EXT_INT1 ; IRQ1 Handler - PortD$0006 jmp EXT_INT2 ; IRQ2 Handler - PortD$0008 jmp EXT_INT3 ; IRQ3 Handler - PortD$000A jmp EXT_INT4 ; IRQ4 Handler - PortE$000C jmp EXT_INT5 ; IRQ5 Handler - PortE$000E jmp EXT_INT6 ; IRQ6 Handler - PortE$0010 jmp EXT_INT7 ; IRQ7 Handler - PortE$0012 jmp TIM2_COMP ; Timer2 Compare Handler$0014 jmp TIM2_OVF ; Timer2 Overflow Handler$0016 jmp TIM1_CAPT ; Timer1 Capture Handler

External interrupts

Page 7: ATmega103 Timer0 and Interrupts

7

Interrupts mapping

Mark Neil - Microprocessor Course

$0018 jmp TIM1_COMPA ; Timer1 CompareA Handler$001A jmp TIM1_COMPB ; Timer1 CompareB Handler$001C jmp TIM1_OVF ; Timer1 Overflow Handler$001E jmp TIM0_COMP ; Timer0 Compare Handler$0020 jmp TIM0_OVF ; Timer0 Overflow Handler$0022 jmp SPI_STC ; SPI Transfer Complete Handler$0024 jmp UART_RXC ; UART RX Complete Handler$0026 jmp UART_DRE ; UDR Empty Handler$0028 jmp UART_TXC ; UART TX Complete Handler$002A jmp ADC ; ADC Conversion Complete Handler$002C jmp EE_RDY ; EEPROM Ready Handler$002E jmp ANA_COMP ; Analog Comparator Handler

Page 8: ATmega103 Timer0 and Interrupts

8

Using interrupts

Mark Neil - Microprocessor Course

Global enable via the status register You must first tell the processor that it should use

interruptsMasks to work at bit level within devicesControl registers to select type of signal

Page 9: ATmega103 Timer0 and Interrupts

9

Global Level: Status Register

In order to use any interrupts on ATmega103 you must set the ‘I’ bit in the status register (SREG) using the command sei

By now you should know what The V,N,Z,C bits are.

Mark Neil - Microprocessor Course

D7 D6 D5 D4 D3 D2 D1 D0I T H S V N Z C

SREG

Page 10: ATmega103 Timer0 and Interrupts

10

At device level:

Mark Neil - Microprocessor Course

EIMSK – use to mask which external interrupts are used

EICR – used to control how external interrupts are recognised

EIFR – flags to show which have been triggered

Page 11: ATmega103 Timer0 and Interrupts

11

The Timer/Counter Mask Register

OCIE2:Timer/Counter2 Output Compare Interrupt EnableTOIE2:Timer/Counter2 Overflow Interrupt EnableTICIE1: Timer/Counter1 Input Capture Interrupt EnableOCIEA1, OCIEA2:Timer/Counter1 Output CompareA,B

Match Interrupt EnableTOIE1: Timer/Counter1 Overflow Interrupt EnableOCIE0: Timer/Counter0 Output Compare Interrupt EnableTOIE0:Timer/Counter0 Overflow Interrupt Enable

Mark Neil - Microprocessor Course

D7 D6 D5 D4 D3 D2 D1 D0OCIE2 TOIE2 TICIE1 OCIE1A OCIE1B TOIE1 OCIE0 TOIE0

TIMSK

Page 12: ATmega103 Timer0 and Interrupts

12

Timer/Counter0 Control Register

Mark Neil - Microprocessor Course

CTC0: Clear Timer/Counter on Compare MatchCOM00 / COMM01:Compare Output Mode, Bits 1

and 0PWM0: Pulse Width Modulator EnableThe timer pre-scale factor : CS02; CS01; CS00

D7 D6 D5 D4 D3 D2 D1 D0

Res PWM0 COM01 COM00 CTC0 CS02 CS01 CS00

TCCR0

Page 13: ATmega103 Timer0 and Interrupts

13

Pre-scaling the Timer via TCCR0

Mark Neil - Microprocessor Course

Counter

MultiplexerCS00CS01CS02

f/128

f/64

f/8 f/32

f/1024

f/256

TOSC1

ASO f

f

Clock Generator Timer0

Clock

Page 14: ATmega103 Timer0 and Interrupts

14

Pre-scaling the Timer via TCCR0

Mark Neil - Microprocessor Course

CS02 CS01 CS00 Frequency (PCK0 = 8 MHz)

0 0 0 Timer/Counter0 is stopped0 0 1 PCK0

0 1 0 PCK0/80 1 1 PCK0/321 0 0 PCK0/641 0 1 PCK0/1281 0 0 PCK0/2561 1 1 PCK0/1024

Page 15: ATmega103 Timer0 and Interrupts

15

Timer/Counter0 TCNT0

Mark Neil - Microprocessor Course

8-bit registers which contain the value of the Timer/Counters.

Both Timer/Counters are realized as up or up/down (in PWM mode) counters with read and write access.

If the Timer/Counter is written to and a clock source is selected, it continues counting in the timer clock cycle after it is preset with the written value

D7 D6 D5 D4 D3 D2 D1 D0

Page 16: ATmega103 Timer0 and Interrupts

16

Example Program

Mark Neil - Microprocessor Course

In the example program the 8 Mhz clock of the ATmega128 is pre-scaled by 256 and the timer zero is loaded with $B2.

The counter overflows ($00) every 125 x 256 x ($FF-$B2 + 1) nsec (approx every 2.5 msec) and causes an interrupt.

Every 200 interrupts a counter is incremented and the result is displayed on the PORTB LEDs.

Page 17: ATmega103 Timer0 and Interrupts

17

Interrupt Jump table

Mark Neil - Microprocessor Course

jmp Init ;2 word instruction to set correct vectorjmp xxx ;next interrupt nop ;use this two liner if no routine available reti ..jmp TIM0_OVF ; Timer 0 Overflow interrupt Vector nop ; Vector Addresses are 2 words apartreti.

At Program Reset your program jumps to the initialization

when Timer0 overflows the interrupt controller jumps to this location

Page 18: ATmega103 Timer0 and Interrupts

18

Interrupt initialization

Mark Neil - Microprocessor Course

;; **** Timer0 Setup Code **** ;

ldi r16,$06 ; Timer 0 Setup out TCCR0, r16 ; Timer - PRESCALE TCK0 BY 256

; (devide the 8 Mhz clock by 256)ldi r16,$b2 ; load timer with n=178out TCNT0,r16 ; The counter will go off

; in (256 - n)*256*125 nsec;; **** Interrupts Setup Code **** ;

ldi r16, $01 ; Timer Interrupt Enables out TIMSK, r16 ; T0: Overflow

;

Page 19: ATmega103 Timer0 and Interrupts

19

Interrupt Service Routine

Mark Neil - Microprocessor Course

TIM0_OVF:in R4,SREG ;save SREGinc r17 ;increment cyclenopcpi r17,$C8 ;compare cycle with 200brne again ;if <> jump to againout PORTB, r18 ;send bits to PORTBinc r18 ;Increment the portB numberclr r17 ;clear cycle and start counting

;200 interruptsagain:

ldi r16,$B2 ;reload timer valueout TCNT0,r16out SREG,r4 ;restore sregreti

It is a good idea to save the information stored on the status

register and restore it at the end of the program

‘reti’ sets the interrupt bit, in the SREG, back to ‘1’ so the next interrupt

can be serviced

When an interrupt occurs : D7 of SREG is set to ‘0’ and the program jumps to the interrupt service routine

Page 20: ATmega103 Timer0 and Interrupts

20

Main Program

Mark Neil - Microprocessor Course

main: ;wasting time;

nopnopnopnoprjmp main ;loop

• The timer is counting in the background and when it overflows it causes an interrupt forcing the program to execute the interrupt service routine.

• After the interrupt is handled we call reti and the program comes back in the loop where it was when the interrupt occurred

Page 21: ATmega103 Timer0 and Interrupts

21

Interrupt Service Routines

Interrupts can occur at any time They are asynchronous to the operation of the rest of your

programYour program may be doing something else important

The interrupt service routine should leave the program in a state so that in can continue running after the reti

General Hints Keep the service routines short – don’t do any heavy

computation, don’t do long and involved I/O if you can avoid it Better to simply flag that the interrupt has happened so that

elsewhere in your program you can deal with it Make sure to save and restore any registers that you use –

including the status register Only if you can guarantee that you are not using a register

elsewhere in your program can you avoid saving and restoring Very unpredictable effects can happen if you don’t do this

Mark Neil - Microprocessor Course