Download presentation
Presentation is loading. Please wait.
Published byJasmin Clark Modified over 8 years ago
1
Mark Neil - Microprocessor Course 1 Timers and Interrupts
2
Example Product: Signal Generator Mark Neil - Microprocessor Course 2 A Signal Generator should be programmable. A user can use the the LCD display and the keyboard to change the: Frequency scale Amplitude scale Offset on/off etc. Waveform shape (Square, Sinusoidal, Triangle..) Analog control given by potentiometer Analog signal output by using a Digital to Analog Converter (DAC)
3
Ripple counter with D-type flip-flops Q* output is fed back to the D input on each D- type latch and to the clock input on the following latch. Each D-type output flip-flops from high to low, halving the frequency each step along the chain. Outputs Q0 to Q3 give a 4 bit counter from $0 to $F… … and then to $o 3 Mark Neil - Microprocessor Course
4
Timers and counters on ATmega128 ATmega128 has 2 8-bit counter/timers and 2 16-bit counter/timers. Each is programmable via control registers to perform a wide variety of different functions: Count-up, count-down, time external events (input capture), provide timed output waveforms (PWM), etc. And as a source of interrupts! 4 Mark Neil - Microprocessor Course
5
Processor Interrupts Mark Neil - Microprocessor Course 5 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.
6
The Need for Processor Interrupts Mark Neil - Microprocessor Course 6 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
7
ATmega128 Timers and Interrupts There are 24 different sources of interrupts There 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 7
8
The ATmega128 Memory Map The first 35 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 interrupt Mark Neil - Microprocessor Course 8
9
Interrupts mapping Mark Neil - Microprocessor Course 9 ;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 ;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
10
Interrupts mapping Mark Neil - Microprocessor Course 10 $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 ; UART0 RX Complete Handler $0026 jmp UART_DRE ; UART0/UDR Empty Handler $0028 jmp UART_TXC ; UART0 TX Complete Handler $002A jmp ADC ; ADC Conversion Complete Handler $002C jmp EE_RDY ; EEPROM Ready Handler $002E jmp ANA_COMP ; Analog Comparator Handler $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 ; UART0 RX Complete Handler $0026 jmp UART_DRE ; UART0/UDR Empty Handler $0028 jmp UART_TXC ; UART0 TX Complete Handler $002A jmp ADC ; ADC Conversion Complete Handler $002C jmp EE_RDY ; EEPROM Ready Handler $002E jmp ANA_COMP ; Analog Comparator Handler
11
Interrupts mapping Mark Neil - Microprocessor Course 11 $0030 jmp TIM1_COMPC ; Timer1 CompareC Handler $0032 jmp TIM3_CAPT ; Timer3 Capture Handler $0034 jmp TIM3_COMPA ; Timer3 CompareA Handler $0036 jmp TIM3_COMPB ; Timer3 CompareB Handler $0038 jmp TIM3_COMPC ; Timer3 CompareC Handler $003A jmp TIM3_OVF ; Timer3 Overflow Handler $003C jmp UART1_RXC ; UART1 RX Complete Handler $003C jmp UART1_DRE ; UART1/UDR Empty Handler $0040 jmp UART1_TXC ; UART1 TX Complete Handler $0042 jmp TWI ; ADC Conversion Complete Handler $0044 jmp SPM_RDY ; Programme Memory Ready Handler $0030 jmp TIM1_COMPC ; Timer1 CompareC Handler $0032 jmp TIM3_CAPT ; Timer3 Capture Handler $0034 jmp TIM3_COMPA ; Timer3 CompareA Handler $0036 jmp TIM3_COMPB ; Timer3 CompareB Handler $0038 jmp TIM3_COMPC ; Timer3 CompareC Handler $003A jmp TIM3_OVF ; Timer3 Overflow Handler $003C jmp UART1_RXC ; UART1 RX Complete Handler $003C jmp UART1_DRE ; UART1/UDR Empty Handler $0040 jmp UART1_TXC ; UART1 TX Complete Handler $0042 jmp TWI ; ADC Conversion Complete Handler $0044 jmp SPM_RDY ; Programme Memory Ready Handler
12
Using interrupts Mark Neil - Microprocessor Course 12 Global enable via the status register You must first tell the processor that it should use interrupts Masks to work at bit level within devices Control registers to select type of signal
13
Global Level: Status Register In order to use any interrupts on ATmega128 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 13 SREG
14
At device level: Mark Neil - Microprocessor Course 14 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
15
The Timer/Counter Mask Register OCIE2 :Timer/Counter2 Output Compare Interrupt Enable TOIE2 :Timer/Counter2 Overflow Interrupt Enable TICIE1 : Timer/Counter1 Input Capture Interrupt Enable OCIEA1, OCIEA2: Timer/Counter1 Output CompareA,B Match Interrupt Enable TOIE1 : Timer/Counter1 Overflow Interrupt Enable OCIE0 : Timer/Counter0 Output Compare Interrupt Enable TOIE0: Timer/Counter0 Overflow Interrupt Enable Mark Neil - Microprocessor Course 15 TIMSK Note: Timer3 settings in ETIMSK
16
Timer/Counter0 Control Register Mark Neil - Microprocessor Course 16 CTC0 : Clear Timer/Counter on Compare Match COM00 / COMM01 :Compare Output Mode, Bits 1 and 0 PWM0 : Pulse Width Modulator Enable The timer pre-scale factor : CS02; CS01; CS00 TCCR0
17
Pre-scaling the Timer via TCCR0 Mark Neil - Microprocessor Course 17 Counter Multiplexer CS00 CS01 CS02 f/128 f/64 f/8 f/32 f/1024 f/256 TOSC1 ASO f f f f Clock Generator Timer0 Clock
18
Pre-scaling the Timer via TCCR0 Mark Neil - Microprocessor Course 18
19
Timer/Counter0 TCNT0 Mark Neil - Microprocessor Course 19 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
20
Output compare register OCR0 Mark Neil - Microprocessor Course 20 8-bit register which contain a value which is compared against the current timer0 value at every increment. Various functions can be realised with the counter based on what is done for a compare match with the OCR registers
21
Example Program – timer0interrupt.asm Mark Neil - Microprocessor Course 21 Timer0 counts up until its value matches the value in compare register OCR0. This triggers the interrupt and resets Timer0 to $00. The 8 Mhz clock of the ATmega128 is pre-scaled by 256 and OCR0 is loaded with $4E. The counter compare match occurs every 125 x 256 x ($4E) 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.
22
Interrupt Jump table Mark Neil - Microprocessor Course 22 jmp Init;2 word instruction to set correct vector jmp xxx ;next interrupt nop ;use this two liner if no routine available reti. jmp TIM0_COMP; Timer 0 Compare Interrupt Vector nop; Vector Addresses are 2 words apart reti. At Program Reset your program jumps to the initialization Timer0 compare event trigger the interrupt controller to jump to this location
23
Interrupt initialization Mark Neil - Microprocessor Course 23 ; ; **** Timer0 Setup Code **** ; ldi r16,$0c; Timer 0 Setup out TCCR0, r16; Timer - PRESCALE TCK0 BY 256 ; (devide the 8 Mhz clock by 256) ; clear timer on OCR0 match ldi r16,$4e; load OCR0 with n=78 out OCR0,r16; The counter will go every ; n*256*125 nsec ; ; **** Interrupts Setup Code **** ; ldi r16, $02; OCIE0 out TIMSK, r16; T0: Output compare match... sei; Turn interrupts on ;
24
Interrupt Service Routine Mark Neil - Microprocessor Course 24 TIM0_OVF: in R4,SREG;save SREG inc r17;increment cycle nop cpi r17,$C8;compare cycle with 200 brne again;if <> jump to again out PORTB, r18;send bits to PORTB inc r18;Increment the portB number clr r17 ;clear cycle and start counting ;200 interrupts again: out SREG,r4;restore sreg reti It is a good idea to save the information stored on the status register and restore it at the end of the program 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
25
Main Program Mark Neil - Microprocessor Course 25 main:;wasting time ; nop rjmp main;loop The timer is counting in the background and when it matches a predetermined value 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 The timer is counting in the background and when it matches a predetermined value 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
26
Interrupt Service Routines Interrupts can occur at any time They are asynchronous to the operation of the rest of your program Your 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 26
27
Task Plan Signal generator with potentiometer control Use a timed interrupt to output your signal Square, triangle or sine wave Main programme reads in analog value on potentiometer And changes period of counter appropriately to control frequency Main programme can also use keyboard and LCD to change modes Eg amplitude control instead of frequency Different waveform shapes Mark Neil - Microprocessor Course 27
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.