ATmega103 Timer0 and Interrupts

Slides:



Advertisements
Similar presentations
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. A7: Bahasa Rakitan AVR Interrupt Handler 25 April 2003 Bobby Nazief Qonita Shahab.
Advertisements

More fun with Timer/Counters
68HC11 Polling and Interrupts
COMP3221: Microprocessors and Embedded Systems Lecture 16: Interrupts II Lecturer: Hui Wu Session 2, 2005.
Mark Neil - Microprocessor Course 1 Timers and Interrupts.
CSC Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The.
COMP3221: Microprocessors and Embedded Systems--Lecture 8 1 COMP3221: Microprocessors and Embedded Systems Lecture 8: Program Control Instructions
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
Pulse Width Modulation modes
External & internal Interrupts. Interrupt Sources There are 21 different interrupts and each one has its own vector located in a predefined location at.
The 8051 Microcontroller and Embedded Systems
Embedded System Design Laboratory September 27, 2002Stanford University - EE281 Lecture #1 Lecture #1 Outline Welcome Back AVR Hardware –Pins and Ports.
Introduction to AVR (Atmega 16/32)
16-Bit Timer/Counter 1 and 3 Counter/Timer 1,3 (TCNT1, TCNT3) are identical in function. Three separate comparison registers exist. Thus, three separate.
1 Timing System Timing System Applications. 2 Timing System components Counting mechanisms Input capture mechanisms Output capture mechanisms.
Embedded Systems 7763B Mt Druitt College of TAFE
7/23 Timers in Coldfire Processor Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee (480)
Slides created by: Professor Ian G. Harris Interrupts  Embedded systems often perform some tasks which are infrequent and possibly unpredictable Hang.
Timer/counter Chapter 12
A Few Words From Dilbert
MCU: Interrupts and Timers Ganesh Pitchiah. What’s an MCU ?
1. Registers Used in Timer/Counter  TH0, TL0, TH1, TL1  TMOD (Timer mode register)  TCON (Timer control register) 2.
Lecture 22 IO & data transfer Methodolodies –Polled, Interrupt Driven, DMA Interrupts –AVR Interrupts (RISC) –68HC11 Interrupts (CISC) IBM PC Boot Sequence.
ELE22MIC Lecture 18 The AVR Sleep Modes The ATMEGA128’s Timer System
Atmel Atmega128 Overview ALU Particulars RISC Architecture 133, Mostly single cycle instructions 2 Address instructions (opcode, Rs, Rd, offset) 32x8 Register.
ATtiny23131 A SEMINAR ON AVR MICROCONTROLLER ATtiny2313.
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
CS-280 Dr. Mark L. Hornick 1 Atmel Timer/Counter System Most microcontrollers include some type of timer system Facilitates real-time monitoring and control.
Interrupts  An interrupt is any service request that causes the CPU to stop its current execution stream and to execute an instruction stream that services.
Timers and Interrupts Mark Neil - Microprocessor Course.
Timers and Scheduled Interrupts
Interrupt On a very basic level, an interrupt is a signal that interrupts the current processor activity. It may be triggered by an external event (change.
Mark Neil - Microprocessor Course 1 Timers and Interrupts.
CS-280 Dr. Mark L. Hornick 1 Sequential Execution Normally, CPU sequentially executes instructions in a program Subroutine calls are synchronous to the.
Introduction to Exceptions 1 Introduction to Exceptions ARM Advanced RISC Machines.
Timers and Scheduled Interrupts
Embedded Systems Programming Interrupts and Handlers
Embedded Systems Programming
V.V.P. ENGINEERING COLLEGE,RAJKOT
ECE 3430 – Intro to Microcomputer Systems
68HC11 Interrupts & Resets.
Networked Embedded Systems Sachin Katti
Timer and Interrupts.
BITS EDU. CAMPUS , VARNAMA
Interrupts In 8085 and 8086.
AVR Addressing Modes Subject: Microcontoller & Interfacing
BVM Engineering College Electrical Engineering Department : Microprocessor and Microcontroller Interfacing Interrupts of 8051 Prepared by:
RX 8-Bit Timer (TMR) 4/20/2011 Rev. 1.00
UNIT 5 TIMRERS/COUNTERS
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
COMP2121: Microprocessors and Interfacing
Timer/Counter Modified from Dr. Lam Phung’s Slides.
16-bit Timer/Counter1 Features
8-bit Timer/Counter0 with PWM
Digital to Analog Converters
Tim Sumner, Imperial College, Rm: 1009, x47552
8-bit Timer/Counter2 with PWM and Asynchronous Operation
전자의료시스템 및 실습 System Configuration/Interrupt
Tim Sumner, Imperial College, Rm: 1009, x47552
COMPUTER PERIPHERALS AND INTERFACES
Md. Mojahidul Islam Lecturer Dept. of Computer Science & Engineering
Wave Generation and Input Capturing
Interrupt Chapter 10.
Wireless Embedded Systems
Device Drivers – Digital Voltmeter
Timer/Counter Timer/Counter 0 Timer/Counter 1 Timer/Counter 2 8 bit
COMP3221: Microprocessors and Embedded Systems
Presentation transcript:

ATmega103 Timer0 and Interrupts 30/10/2006 Tim Sumner, Imperial College, Rm: 1009, x47552

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 ! This is clearly not an efficient way of doing things ….. This is why interrupts were introduced as a means of getting the processor’s attention on demand 19/09/2018 Tim Sumner, Imperial College, Rm: 1009, x47552

Tim Sumner, Imperial College, Rm: 1009, x47552 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. 19/09/2018 Tim Sumner, Imperial College, Rm: 1009, x47552

The ATmega103 Timers and Interrupts  A device that counts time (Timer) could be one of the reasons for interrupt (internal) 24 different reasons to interrupt the CPU and execute an interrupt service routine. Eight external interrupt inputs 19/09/2018 Tim Sumner, Imperial College, Rm: 1009, x47552

Tim Sumner, Imperial College, Rm: 1009, x47552 The ATmega103 Memory Map The first 24 2-byte addresses in flash program memory are reserved for interrupts: your program jumps to one of these addresses if an interrupt occurs. 19/09/2018 Tim Sumner, Imperial College, Rm: 1009, x47552

Tim Sumner, Imperial College, Rm: 1009, x47552 Interrupts mapping ;Address;Labels;Code; Comments $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 19/09/2018 Tim Sumner, Imperial College, Rm: 1009, x47552

Tim Sumner, Imperial College, Rm: 1009, x47552 Interrupts mapping $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 19/09/2018 Tim Sumner, Imperial College, Rm: 1009, x47552

Tim Sumner, Imperial College, Rm: 1009, x47552 Using interrupts Global enable via the status register Masks to work at bit level within devices Control registers to select type of signal 19/09/2018 Tim Sumner, Imperial College, Rm: 1009, x47552

Global Level: Status Register D7 D6 D5 D4 D3 D2 D1 D0 I T H S V N Z C By now you should know what The V,N,Z,C bits are. In order to use any interrupts on ATmega103 you must set the ‘I’ bit using the command sei 19/09/2018 Tim Sumner, Imperial College, Rm: 1009, x47552

Tim Sumner, Imperial College, Rm: 1009, x47552 At device level: EIMSK – use to mask which external interrupts are used EICR – used to control how interrupts are recognised EIFR – flags to show which have been triggered 19/09/2018 Tim Sumner, Imperial College, Rm: 1009, x47552

Example: The Timer/Counter Mask Register TIMSK D7 D6 D5 D4 D3 D2 D1 D0 OCIE2 TOIE2 TICIE1 OCIE1A OCIE1B TOIE1 OCIE0 TOIE0 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 19/09/2018 Tim Sumner, Imperial College, Rm: 1009, x47552

Timer/Counter0 Control Register TCCR0: D7 Res D6 PWM0 D5 COM01 D4 COM00 D3 CTC0 D2 CS02 D1 CS01 D0 CS00 The timer pre-scale factor : CS02; CS01; CS00 CTC0: Clear Timer/Counter on Compare Match COM00 / COMM01:Compare Output Mode, Bits 1 and 0 PWM0: Pulse Width Modulator Enable 19/09/2018 Tim Sumner, Imperial College, Rm: 1009, x47552

Pre-scaling the Timer via TCCR0 f Counter TOSC1 ASO f f/8 f/32 f/64 f/128 f/256 f/1024 CS00 CS01 CS02 Multiplexor Clock Generator Timer0 Clock 19/09/2018 Tim Sumner, Imperial College, Rm: 1009, x47552

Pre-scaling the Timer via TCCR0 CS02 CS01 CS00 Frequency (PCK0 = 4 MHz) Timer/Counter0 is stopped 1 PCK0 PCK0/8 PCK0/32 PCK0/64 PCK0/128 PCK0/256 PCK0/1024 19/09/2018 Tim Sumner, Imperial College, Rm: 1009, x47552

Tim Sumner, Imperial College, Rm: 1009, x47552 Timer/Counter0 TCNT0 D7 D6 D5 D4 D3 D2 D1 D0 This is the counter preset …….These 8-bit registers 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/09/2018 Tim Sumner, Imperial College, Rm: 1009, x47552

Tim Sumner, Imperial College, Rm: 1009, x47552 Example Program In the example program the 4 Mhz clock of the ATmega103 is pre-scaled by 256 and the timer zero is loaded with $B2. So the counter overflows ($00) every 250 x 256 x ($FF-$B2 + 1) nsec (approx every 5 msec) and causes an interrupt. Every 200 interrupts a counter is incre- mented and the results is displayed on the PORTB LEDs. 19/09/2018 Tim Sumner, Imperial College, Rm: 1009, x47552

Interrupt Section At Program address 0 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_OVF ; Timer 0 Overflow interrupt Vector nop ; Vector Addresses are 2 words apart ;******************************************************* At Program address 0 your program jumps to the initialization Here it comes under hardware control when Timer0 overflows 19/09/2018 Tim Sumner, Imperial College, Rm: 1009, x47552

Interrupt initialization ; ; **** Timer0 Setup Code **** ldi r16,$06 ; Timer 0 Setup out TCCR0, r16 ; Timer - PRESCALE TCK0 BY 256 ; (devide the 4 Mhz clock by 256) ldi r16,$b2 ; load timer with n=178 out TCNT0,r16 ; The counter will go off ; in (256 - n)*256*250 nsec ; **** Interrupts Setup Code **** ldi r16, $01 ; Timer Interrupt Enables out TIMSK, r16 ; T0: Overflow 19/09/2018 Tim Sumner, Imperial College, Rm: 1009, x47552

Interrupt Service Routine When an interrupt occurs : D7 of SREG is set to ‘0’ and the program jumps to the interrupt service routine ;******************************************** 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 stuff to PORTB inc r18 ; Increment the portB number clr r17 ;clear cycle and start counting ; again 200 interrupts again: ldi r16,$B2 ;reload timer value out TCNT0,r16 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 ‘reti’ sets the interrupt bit, in the SREG, back to ‘1’ so the next interrupt can be serviced 19/09/2018 Tim Sumner, Imperial College, Rm: 1009, x47552

Tim Sumner, Imperial College, Rm: 1009, x47552 Main Program Main: ; waisting time ; nop rjmp main But 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 that it comes back in the loop where it was when the interrupt occurred……see STUDIO demo. 19/09/2018 Tim Sumner, Imperial College, Rm: 1009, x47552