COMP2121: Microprocessors and Interfacing

Similar documents
Microprocessors & Interfacing

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

Timers and Interrupts. Mark Neil - Microprocessor Course

EE 308: Microcontrollers

Microprocessors & Interfacing

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

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

Embedded Systems and Software

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

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

Interrupts and timers

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

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

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

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

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

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

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

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

[TUT] Newbie's Guide to AVR Interrupts

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

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

AGH University of Science and Technology Cracow Department of Electronics

COMP2121: Microprocessors and Interfacing

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

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

Atmel AVR Timers and Interrupts

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

8-bit Microcontroller with 1K Byte Flash. ATtiny15L

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

AVR Microcontrollers Architecture

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

AVR ISA & AVR Programming (I)

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

AGH University of Science and Technology Cracow Department of Electronics

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

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

Processor and compiler dependent

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

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

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

MICROPROCESSOR BASED SYSTEM DESIGN

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

Topic 11: Interrupts ISMAIL ARIFFIN FKE UTM SKUDAI JOHOR

Embedded Systems and Software

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

Lecture 14. Ali Karimpour Associate Professor Ferdowsi University of Mashhad

AVR Timers TIMER0. Based on:

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

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

Introduction to Embedded Systems

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

Assembly Programming (III) Lecturer: Sri Parameswaran Notes by: Annie Guo Dr. Hui Wu

EE 308: Microcontrollers

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

Physics 120B: Lecture 11. Timers and Scheduled Interrupts

Assembly Programming (III)

UNIVERSITY OF MANITOBA Final Exam

Chapter 2. Overview of Architecture and Microcontroller-Resources

INTERRUPTS in microprocessor systems

8-bit Microcontroller with 4K Bytes In-System Programmable Flash. ATtiny40. Preliminary

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

Topic 11: Timer ISMAIL ARIFFIN FKE UTM SKUDAI JOHOR

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

M68HC08 Microcontroller The MC68HC908GP32. General Description. MCU Block Diagram CPU08 1

Watchdog Timer. The Basics. The watchdog timer operates independent of the CPU, peripheral subsystems, and even the clock of the MCU.

Mechatronics and Microcomputers. Stipendium Hungaricum 2018/2019 Autumn Semester Szilárd Aradi, PhD

CN310 Microprocessor Systems Design

ECE 372 Microcontroller Design Parallel IO Ports - Interrupts. ECE 372 Microcontroller Design Parallel IO Ports - Interrupts

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

Microprocessor Fundamentals. Topic 7 Timing: the Timer/Counter

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

Arduino Uno R3 INTRODUCTION

Interrupt/Timer/DMA 1

Microcontroller basics

Interrupts & Interrupt Service Routines (ISRs)

VCC PB2 (SCK/ADC1/T0/PCINT2) PB1 (MISO/AIN1/OC0B/INT0/PCINT1) PB0 (MOSI/AIN0/OC0A/PCINT0)

AVR Subroutine Basics

AVR MICROCONTROLLER ARCHITECTURTE

Grundlagen Microcontroller Interrupts. Günther Gridling Bettina Weiss

Newbie s Guide to AVR Interrupts

ARDUINO MEGA INTRODUCTION

Context Switching & Task Scheduling

COMP3221: Microprocessors and. and Embedded Systems. Overview. Variable Types and Memory Sections. Types of Variables in C

Department of Electronics and Instrumentation Engineering Question Bank

Course Introduction. Purpose: Objectives: Content: 27 pages 4 questions. Learning Time: 20 minutes

EE 308: Microcontrollers

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

Introduction to the MC9S12 Hardware Subsystems

Laboratory 4 Usage of timers

8-Bit Microcontroller with 2K Bytes of In-System Programmable Flash. AT90S2323 AT90LS2323 AT90S2343 AT90LS2343 Preliminary AT90S/LS2323.

ATmega8A. Introduction. Features. 8-bit AVR Microcontroller DATASHEET COMPLETE

HC12 Built-In Hardware

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

Design with Microprocessors

ECED3204: Microprocessor Part I--Introduction

The Atmel ATmega328P Microcontroller

CHAPTER 11 INTERRUPTS PROGRAMMING

8-bit Microcontroller with 2/4/8K Bytes In-System Programmable Flash. ATtiny261/V ATtiny461/V ATtiny861/V. Preliminary

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

Transcription:

Lecture 19: Interrupts II http://www.cse.unsw.edu.au/~cs2121 Lecturer: Hui Wu Session 1, 2006 Overview AVR Interrupts Interrupt Vector Table System Reset Watchdog Timer Timer/Counter0 Interrupt Service Routines 2 AVR MCU Architecture Interrupts in AVR 3 The number of interrupts varies with specific AVR device. Two types of interrupts: Internal interrupts and external interrupts. Internal interrupts: Generated by on-chip I/O devices. External interrupts: Generated by external I/O devices. For most internal interrupts, they don t have an individual enable/disable bit. Program cannot enable/disable these interrupts. External interrupts have an individual enable/disable bit. Program can enable/disable these interrupts. An external interrupt can be rising edge-triggered, or falling edgetriggered or low level-triggered). Special I/O registers (External Interrupt Control Registers EICRA and EICRB in Mega64) to specify how each external interrupt is triggered.. 4

Interrupts in AVR (Cont.) Interrupts in AVR (Cont.) There is a global interrupt enable/disable bit, the I-bit, in Program Status Register SREG. Setting the I-bit will enable all interrupts except those with individual enable/disable bit. Those interrupts are enabled only if both I and their own enable/disable bit are set. The I-bit is cleared after an interrupt has occurred and is set by the instruction RETI. Programmers can use SEI and CLI to set and clear the I-bit. If the I-bit is enabled in the interrupt service routine, nested interrupts are allowed. SREG is not automatically saved by hardware when entering an interrupt service routine. An interrupt service routine needs to save it and other conflict registers on the stack at the beginning and restore them at the end. 5 Reset is handled as a nonmaskable interrupt. Each interrupt has a 4-byte interrupt vector, containing an instruction to be executed after MCU has accepted the interrupt. Each interrupt vector has a vector number, an integer from 1 to n, the maximum number of interrupts. The priority of each interrupt is determined by its vector number. The lower the vector number, the higher priority. All interrupt vectors, called Interrupt Vector Table, are stored in a contiguous section in flash memory. Starts from address 0 by default. Can be relocated. 6 Interrupt Vectors in Mega64 Interrupt Vectors in Mega64 (Cont.) 7 8

Interrupt Vectors in Mega64 (Cont.) Initialization of Interrupt Vector Table in Mega64 Typically an interrupt vector contains a branch instruction (JMP) that branches to the first instruction of the interrupt handler. 9 10 IVT Initialization in Mega64 (Cont.) IVT Initialization in Mega64 (Cont.).include "m64def.inc" jmp TIM2_COMP ; Timer2 Compare Handler.cseg jmp TIM2_OVF ; Timer2 Overflow Handler jmp RESET ; Reset Handler jmp TIM1_CAPT ; Timer1 Capture Handler jmp EXT_INT0 ; IRQ0 Handler jmp TIM1_COMPA ; Timer1 CompareA Handler jmp EXT_INT1 ; IRQ1 Handler jmp TIM1_COMPB ; Timer1 CompareB Handler jmp EXT_INT2 ; IRQ2 Handler jmp TIM1_OVF ; Timer1 Overflow Handler jmp EXT_INT3 ; IRQ3 Handler jmp TIM0_COMP ; Timer0 Compare Handler jmp EXT_INT4 ; IRQ4 Handler jmp TIM0_OVF ; Timer0 Overflow Handler jmp EXT_INT5 ; IRQ5 Handler jmp SPI_STC ; SPI Transfer Complete Handler jmp EXT_INT6 ; IRQ6 Handler jmp USART0_RXC ; USART0 RX Complete Handler jmp EXT_INT7 ; IRQ7 Handler 11 jmp USART0_DRE ; USART0,UDR Empty Handler 12

IVT Initialization in Mega64 (Cont.) IVT Initialization in Mega64 (Cont.) jmp USART0_TXC ; USART0 TX Complete Handler jmp ADC ; ADC Conversion Complete Handler jmp EE_RDY ; EEPROM Ready Handler jmp ANA_COMP ; Analog Comparator Handler jmp TIM1_COMPC ; Timer1 CompareC Handler jmp TIM3_CAPT ; Timer3 Capture Handler jmp TIM3_COMPA ; Timer3 CompareA Handler jmp TIM3_COMPB ; Timer3 CompareB Handler jmp USART1_RXC ; USART1 RX Complete Handler jmp USART1_DRE ; USART1,UDR Empty Handler jmp USART1_TXC ; USART1 TX Complete Handler jmp TWI ; Two-wire Serial Interface Handler Jmp SPM_RDY ; SPM Ready Handler ; End of Interrupt Vector Table jmp TIM3_COMPC ; Timer3 CompareC Handler jmp ANA_COMP ; Analog Comparator Handler jmp TIM3_OVF ; Timer3 Overflow Handler 13 14 IVT Initialization in Mega64 (Cont.) RESET in Mega64 RESET: ldi r16, high(ramend) ; Interrupt handler for RESET out SPH, r16 ; Set Stack Pointer to top of RAM ldi r16, low(ramend) out SPL,r16 sei ; Enable interrupts rjmp main; main: ; Main program starts here The ATmega64 has five sources of reset: Power-on Reset. The MCU is reset when the supply voltage is below the Power-on Reset threshold (VPOT). External Reset. The MCU is reset when a low level is present on the RESET pin for longer than the minimum pulse length. Watchdog Reset. The MCU is reset when the Watchdog Timer period expires and the Watchdog is enabled. 15 16

RESET in Mega64 (Cont.) RESET Logic in Mega64 Brown-out Reset. The MCU is reset when the supply voltage VCC is below the Brown-out Reset threshold (VBOT) and the Brown-out Detector is enabled. JTAG AVR Reset. The MCU is reset as long as there is a logic one in the Reset Register, one of the scan chains of the JTAG system. For each reset, there is a flag (bit) in MCU Control Register MCUCSR. These bits are used to determine the source of the RESET interrupt. 17 18 Used to detect software crash. Watchdog Timer Can be enabled or disabled by properly updating WDCE bit and WDE bit in Watchdog Timer Control Register WDTCR. 8 different periods determined by WDP2, WDP1 and WDP0 bits in WDTCR. If enabled, it generates a Watchdog Reset interrupt when its period expires. So program needs to reset it before its period expires by executing instruction WDR. When its period expires, Watchdog Reset Flag WDRF in MCU Control Register MCUCSR is set. This flag is used to determine if the watchdog timer has generated a RESET interrupt. 19 Watchdog Timer Logic 20

Timer Interrupt Timer Interrupt (Cont.) Timer interrupt has many applications: Used to schedule (real-time) tasks Round-Robin scheduling All tasks take turn to execute for some fixed period. Real-time scheduling Some tasks must be started at a particular time and finished by a deadline. Some tasks must be periodically executed. Used to implement a clock How much time has passed since the system started? Used to synchronize tasks. Task A can be started only if a certain amount of time has passed since the completion of task B. Can be coupled with wave-form generator to support Pulse- Width Modulation (PWM). Details to be covered later. 21 22 Timer0 in AVR Timer0 In AVR Block Diagram 8-bit timer with the following features: Clear Timer on Compare Match (Auto Reload) Glitch-free, Phase Correct Pulse Width Modulator (PWM) Frequency Generator 10-bit Clock Prescaler Overflow and Compare Match Interrupt Sources (TOV0 and OCF0) It generates a Timer0 Overflow Interrupt Timer0OVF when it overflows. It generates a Timer/Counter0 Output Match Interrupt Timer0COMP when the timer/counter value matches the value in Output Compare Register OCR0. Timer0OVF and Timer0COMP can be individually enabled/disabled. Allows Clocking from External 32 khz Watch Crystal Independent of the I/O Clock 23 24

Prescaler for Timer0 Timer0 Registers 25 Seven I/O registers for Timer0: Timer/Counter Register TCNT0. Contains the current timer/counter value. Output Compare Register OCR0. Contains an 8-bit value that is continuously compared with the counter value (TCNT0). Timer/Counter Control Register TCCR. Contains control bits. Timer/Counter Interrupt Mask Register TIMSK (shared with other timers). Contains enable/disable bits. 26 Timer0 Registers (Cont.) Timer Control Register Timer/Counter Interrupt Flag Register TIFR (shared with other timers). Contains interrupt flags. Asynchronous Status Register ASSR. Contains control bits for asynchronous operations. Special Function I/O Register SFIOR. Contains synchronization mode bit and prescaler reset bit. 27 28

Timer Control Register (Cont.) Timer/Counter Interrupt Mask Register The mode of operation, i.e., the behavior of the Timer/Counter and the Output Compare pins, is defined by the combination of the Waveform Generation mode (WGM01:0) and Compare Output mode (COM01:0) bits. The simplest mode of operation is the Normal Mode (WGM01:0 = 0). In this mode the counting direction is always up (incrementing), and no counter clear is performed. The counter simply overruns when it passes its maximum 8-bit value (TOP = 0xFF) and then restarts from the bottom (0x00). Refer to Mega64 Data Sheet (pages 96~100) for details. Bit 1 OCIE0: Timer/Counter0 Output Compare Match Interrupt Enable. 1: Enabled 0: Disabled Bit 0 TOIE0: Timer/Counter0 Overflow Interrupt Enable. 1: Enabled 0: Disabled 29 30 ISR Example ISR Example.include "m64def.inc" ; This program implements a second counter.def temp r16.macro Clear. ; using Timer0 interrupt. ldi r28, low(@0) ; Load the low byte of label @0 ldi r29, high(@0) ; Load the high byte of label @0 clr temp st y, temp st y, temp ; Initialize the two-byte integer at @0 to 0.ENDMACRO 31.dseg SecondCounter:.byte 2 ; Two-byte second counter. TempCounter:.byte 2 ; Temporary counter. Used to determine ; if one second has passed.cseg.org 0 jmp RESET jmp DEFAULT ; No handling for IRQ0. jmp DEFAULT ; No handling for IRQ1. 32

ISR Example (Cont.) Timer0 ISR jmp Timer0 ; Jump to the interrupt handler for Timer 0 overflow. jmp DEFAULT ; No handling for all other interrupts. DEFAULT: reti ; No handling for this interrupt RESET: ldi temp, high(ramend) ; Initialize stack pointer out SPH, temp ldi temp, low(ramend) out SPL, temp ; Insert further initialization code here Timer0: ; Prologue starts. push r29 ; Save all conflict registers in the prologue. push r28 push r25 push r24 in r24, SREG push r24 ; Prologue ends. ldi r28, low(tempcounter) ; Load the address of the temporary ldi r29, high(tempcounter) ; counter. ld r24, y+ ; Load the value of the temporary counter. rjmp main 33 34 ld r25, y adiw r25:r24, 1 ; Increase the temporary counter by one. Timer0 ISR (Cont.) Timer0 ISR (Cont.) cpi r24, low(3597) ; Check if (r25:r24)=3597 ldi temp, high(3597) ; 3597= 10 6 /278 cpc r25, temp brne NotSecond clr temp ; One second has passed since last interrupt st z, r25 ; Store the value of the second counter. st z, r24 NotSecond: st y, r25 ; Store the value of the temporary counter. st y, r24 st y, temp ; Reset the temporary counter. st y, temp ldi r30, low(secondcounter) ; Load the address of the second ldi r31, high(secondcounter) ; counter. ld r24, z+ ; Load the value of the second counter. ld r25, z pop r24 out SREG, r24 pop r24 pop r25 pop r28 pop r29 ; Epilogue starts; ; Restore all conflict registers from the stack. adiw r25:r24, 1 ; Increase the second counter by one. 35 36 reti ; Return from the interrupt.

ISR Example (Cont.) main: Clear TempCounter ; Initialize the temporary counter to 0 Clear SecondCounter ; Initialize the second counter to 0 ldi temp, 0b00000010 out TCCR0, temp ; Prescaling value=8 256*8/7.3728 ldi temp, 1<<TOIE0 ; =278 microseconds out TIMSK, temp ; T/C0 interrupt enable sei ; Enable global interrupt loop: rjmp loop ; loop forever Comments: 1: The frequency of Timer clock in Mega64 is 7.3728Mhz. 2: Prescaling value is set to 8. Since the maximum value of a 8-bit counter is 255, Timer0 Overflow Interrupt occurs every 256*8/7.3728 37 =278 microseconds. Non-Nested Interrupts Interrupt Service Routines cannot be interrupted by another interrupt. Main program Interrupt service routine 38 Nested Interrupts Interrupt Service Routines can be interrupted by another interrupt. Main program ISR1 ISR2 ISR3 Reading Material Read the following sections in Mega64 Data Sheet. 1. Overview 2. System Control and Reset. 3. Watchdog Timer. 4. Interrupts. 5. External Interrupts. 6. 8-bit Time/Counter0 with PWM and Asynchronous Operation. 39 40