Timers and Interrupts. Mark Neil - Microprocessor Course

Similar documents
COMP2121: Microprocessors and Interfacing

EE 308: Microcontrollers

INTERRUPT, TIMER/COUNTER. KONKUK UNIV. VLSI Design Lab. LSI Design Lab

8-bit Microcontroller with 64K/128K Bytes In-System Programmable Flash. ATmega603 ATmega603L ATmega103 ATmega103L. Preliminary.

Microprocessors & Interfacing

8-bit Microcontroller with 2K Bytes of In-System Programmable Flash AT90S2313

8-bit Microcontroller with 4K/8K bytes In-System Programmable Flash AT90S4414 AT90S8515. Features. Pin Configurations

8-bit Microcontroller with 8K Bytes In-System Programmable Flash AT90S8515

Interrupts (II) Lecturer: Sri Parameswaran Notes by: Annie Guo

8-bit Microcontroller with 16K Bytes In-System Programmable Flash. ATmega163 ATmega163L

8-bit Microcontroller with 16K Bytes In-System Programmable Flash. ATmega163 ATmega163L. Advance Information

Interrupts and timers

8-bit Microcontroller with 128K Bytes In-System Programmable Flash. ATmega103 ATmega103L

Embedded Systems and Software

8-bit Microcontroller with 4K Bytes of In-System Programmable Flash AT90S4433 AT90LS4433. Features. Not Recommend for New Designs. Use ATmega8.

Fundamental concept in computation Interrupt execution of a program to handle an event

8-bit Microcontroller with 1K Byte Flash. ATtiny15L

ATmega Interrupts. Reading. The AVR Microcontroller and Embedded Systems using Assembly and C) by Muhammad Ali Mazidi, Sarmad Naimi, and Sepehr Naimi

Laboratory 4 Usage of timers

8-bit Microcontroller with 1K Byte of In-System Programmable Flash AT90S1200

AGH University of Science and Technology Cracow Department of Electronics

Microprocessors & Interfacing

Interrupts (I) Lecturer: Sri Notes by Annie Guo. Week8 1

Processor and compiler dependent

8-bit Microcontroller with 2K Bytes of In-System Programmable Flash. ATtiny22 ATtiny22L. Preliminary. Features. Description

8-bit Microcontroller with 8K Bytes Programmable Flash AT90C8534. Preliminary

8-bit Microcontroller with 1K Bytes Flash. ATtiny10 ATtiny11 ATtiny12. Preliminary. Features. Pin Configuration

Unit 14 Timers and Counters 14.1

INTERRUPTS in microprocessor systems

Introduction to Embedded Systems

8-bit Microcontroller with 2K Bytes of In-System Programmable Flash AT90S2323 AT90LS2323 AT90S2343 AT90S/LS2323. Features.

Atmel AVR Timers and Interrupts

Unit 13 Timers and Counters

AVR134: Real Time Clock (RTC) Using the Asynchronous Timer. Features. Introduction. AVR 8-bit Microcontrollers APPLICATION NOTE

8-bit Microcontroller. Application Note. AVR134: Real-Time Clock (RTC) using the Asynchronous Timer. Features. Theory of Operation.

School of Electrical, Computer and Telecommunications Engineering University of Wollongong Australia

TIMSK=0b ; /* enables the T/C0 overflow interrupt in the T/C interrupt mask register for */

8-bit Microcontroller with 2K Bytes of Flash. ATtiny28L ATtiny28V

HC12 Built-In Hardware

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

AVR Timers TIMER0. Based on:

Microprocessor Fundamentals. Topic 7 Timing: the Timer/Counter

EE 308: Microcontrollers

8-bit Microcontroller with 1K Byte Flash. ATtiny11. ATtiny12

MCU: Interrupts and Timers. Ganesh Pitchiah

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

AT90S Bit Microcontroller with 1K bytes Downloadable Flash AT90S1200. Features. Description. Pin Configuration

AVR ISA & AVR Programming (I)

FIFTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLOGY-MARCH 2014 EMBEDDED SYSTEMS (Common for CT,CM) [Time: 3 hours] (Maximum marks : 100)

8-bit Microcontroller with 2K Bytes Flash. ATtiny26 ATtiny26L. Preliminary. Features

AGH University of Science and Technology Cracow Department of Electronics

12.1. Unit 12. Exceptions & Interrupts

Interrupts Arduino, AVR, and deep dark programming secrets. What is an Interrupt?

Topic 11: Interrupts ISMAIL ARIFFIN FKE UTM SKUDAI JOHOR

Physics 120B: Lecture 11. Timers and Scheduled Interrupts

Topic 11: Timer ISMAIL ARIFFIN FKE UTM SKUDAI JOHOR

[TUT] Newbie's Guide to AVR Interrupts

CENG-336 Introduction to Embedded Systems Development. Timers

CN310 Microprocessor Systems Design

Microcontroller basics

COMP2121: Microprocessors and Interfacing

Question Bank Microprocessor and Microcontroller

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

Introduction to the MC9S12 Hardware Subsystems

8-bit Microcontroller. Application Note. AVR134: Real-Time Clock (RTC) using the Asynchronous Timer. Features. Theory of Operation.

8-Bit Microcontroller with 1K bytes In-System Programmable Flash AT90S1200. Features. Description. Pin Configuration

Chapter 2. Overview of Architecture and Microcontroller-Resources

Physics 124: Lecture 12. Timer Basics. Timers and Scheduled Interrupts

Motion Sensing with the Pyroeletric Sensor

AVR XMEGA Product Line Introduction AVR XMEGA TM. Product Introduction.

The Atmel ATmega168A Microcontroller

Marten van Dijk, Syed Kamran Haider

Newbie s Guide to AVR Timers

The Atmel ATmega328P Microcontroller

AVR XMEGA TM. A New Reference for 8/16-bit Microcontrollers. Ingar Fredriksen AVR Product Marketing Director

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Interrupt Handling Module No: CS/ES/13 Quadrant 1 e-text

CHAPTER 11 INTERRUPTS PROGRAMMING

EE 308: Microcontrollers

Fredrick M. Cady. Assembly and С Programming forthefreescalehcs12 Microcontroller. шт.

Department of Electronics and Instrumentation Engineering Question Bank

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

Embedded Systems and Software

Clock and Fuses. Prof. Prabhat Ranjan Dhirubhai Ambani Institute of Information and Communication Technology, Gandhinagar

I Introduction to Real-time Applications By Prawat Nagvajara

University of Technology

Lecture 14. Ali Karimpour Associate Professor Ferdowsi University of Mashhad

IAS0430 MICROPROCESSOR SYSTEMS

EE 308 Spring A software delay. To enter a software delay, put in a nested loop, just like in assembly.

Chapter 5 Embedded Soft Core Processors

AVR Microcontrollers Architecture

Interrupt/Timer/DMA 1

Grundlagen Microcontroller Interrupts. Günther Gridling Bettina Weiss

Infineon C167CR microcontroller, 256 kb external. RAM and 256 kb external (Flash) EEPROM. - Small single-board computer (SBC) with an

These three counters can be programmed for either binary or BCD count.

Registers Format. 4.1 I/O Port Address

Microprocessors B (17.384) Spring Lecture Outline

EEL 4744C: Microprocessor Applications. Lecture 7. Part 1. Interrupt. Dr. Tao Li 1

Reading Assignment. Interrupt. Interrupt. Interrupt. EEL 4744C: Microprocessor Applications. Lecture 7. Part 1

Introduction to Mechatronics. Fall Instructor: Professor Charles Ume. Interrupts and Resets

Design with Microprocessors

Transcription:

Timers and Interrupts 1

Example Product: Signal Generator 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) 2

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

4 Timers and counters on ATmega128 ATmega128 has 2 8-bit counter/timers and 2 16-bit counter/timers. Timer/Counter TCNTx = Count Clear Direction Control Logic TCLK TOP BOTTOM = = 0 TOVx (Int.Req.) Clock Select Edge Detector ( From Prescaler ) OCFxA (Int.Req.) Waveform Generation Tx OCxA 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! DATABUS OCRxA = OCRxB = OCRxC ICRx Fixed TOP Values ICFx (Int.Req.) Edge Detector OCFxB (Int.Req.) Waveform Generation OCFxC (Int.Req.) Waveform Generation Noise Canceler TCCRxA TCCRxB TCCRxC OCxB OCxC ( From Analog Comparator Ouput ) ICPx

Processor Interrupts 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. 5

The Need for Processor Interrupts 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

ATmega128 Timers and Interrupts 7 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

The ATmega128 Memory Map 8 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

Interrupts mapping 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 External interrupts

Interrupts mapping 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

Interrupts mapping 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

Using interrupts 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

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. 13 D7 D6 D5 D4 D3 D2 D1 D0 I T H S V N Z C SREG

At device level: 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 14

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 15 D7 D6 D5 D4 D3 D2 D1 D0 OCIE2 TOIE2 TICIE1 OCIE1A OCIE1B TOIE1 OCIE0 TOIE0 TIMSK Note: Timer3 settings in ETIMSK

Timer/Counter0 Control Register 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 16 D7 D6 D5 D4 D3 D2 D1 D0 Res PWM0 COM01 COM00 CTC0 CS02 CS01 CS00 TCCR0

Pre-scaling the Timer via TCCR0 17 f TOSC1 Counter ASO f f/8 f/32 f/64 f/128 f/256 f/1024 CS00 CS01 CS02 Clock Generator Multiplexer Timer0 Clock

Pre-scaling the Timer via TCCR0 18 CS02 CS01 CS00 Frequency (PCK0 = 8 MHz) 0 0 0 Timer/Counter0 is stopped 0 0 1 PCK0 0 1 0 PCK0/8 0 1 1 PCK0/32 1 0 0 PCK0/64 1 0 1 PCK0/128 1 1 0 PCK0/256 1 1 1 PCK0/1024

Timer/Counter0 TCNT0 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 19 D7 D6 D5 D4 D3 D2 D1 D0

Output compare register OCR0 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 For instance an interrupt. D7 D6 D5 D4 D3 D2 D1 D0

Example Program timer0interrupt.asm 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.

Interrupt Jump table 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. 22 At Program Reset your program jumps to the initialization Timer0 compare event trigger the interrupt controller to jump to this location

Interrupt initialization 23 ; ; **** Timer0 Setup Code **** ; ldi r16,$0e ; 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 ; ; **** Interrupts Setup Code **** ;... ; ; The counter will go every ; n*256*125 nsec ldi r16, $02 ; OCIE0 out TIMSK, r16 ; T0: Output compare match sei ; Turn interrupts on

Interrupt Service Routine TIM0_COMP: 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 reti 24 ;restore sreg When an interrupt occurs : D7 of SREG is set to 0 and the program jumps to the interrupt service routine It is a good idea to save the information stored on the status register and restore it at the end of the routine reti sets the interrupt bit, in the SREG, back to 1 so the next interrupt can be serviced

Main Program 25 main: nop nop nop nop rjmp main ;wasting time ; ;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

Interrupt Service Routines 26 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

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 27