SlideShare a Scribd company logo
1 of 35
By/Ramadan Ahmed
What’s Interrupts ?
Basically events that require immediate attention by the
microcontroller.
An interrupt is a signal to the processor emitted by
hardware or software indicating an event that needs
immediate attention.
When an interrupt event occurs, the microcontroller pause
its current task and attend to the interrupt by executing
its corresponding Interrupt Service Routine (ISR) then
microcontroller returns to the task it had pause and
continue its normal operations.
Such an event may be:
 An peripheral device has finished the last task it was given
and is now ready for another. For example Timer may
generate interrupt after reaching the maximum possible
value.
 External Interrupt event on I/O Pin.
 An error from a peripheral.
You can think of it as a hardware-generated function
call.
ISR (Interrupt service routine)
 Piece of code that should be execute when an interrupt is triggered
 Should be deterministic and short as possible, as it usually set a flag
to a specific task indicating a certain event is happened, or save
small data in buffer.
REMEMBER
Global interrupt enabled bit MUST be enabled for
interrupts to be activated in the µC.
To Set global interrupt :
 sei();
To clear global interrupt :
 cli();
Interrupt Flags and Enabled bits in AVR µC
 Each interrupt is associated with two (2) bits, an Interrupt
Flag Bit and an Interrupt Enabled Bit. These bits are
located in the I/O registers associated with the specific
interrupt:
The interrupt enabled bit(IE): is used to enable or
disable a specific interrupt. Basically is tells the µC
whether or not it should respond to the interrupt if it is
triggered.
The interrupt flag bit (IF):is set whenever the
interrupt event occur, whether or not the interrupt is
enabled. And The flag is cleared when the ISR is
executed. So it is also called “Automatic Flag”.
Global interrupt
Interrupt flag
Interrupt enabled
Interrupt Request
RESET interrupt
 It active low
interrupt which
means it triggered
when low voltage (0
volt) is applied on
reset pin (number 9).
Timer is special register that can be 8-
16 bit so capable of holding from 0 to
255-65536
What differ it from any register that it
count up / down automatically at
predefined rate and this is Timer
Clock that doesn’t need CPU
intervention.
Timer Clock could be internal /
external
One of basic condition when Timer overflow( it means
timer counted up to its maximum value and return back to
zero).
In this situation it causes interrupt and if global interrupt
mask is enabled and timer interrupt enable so you have to
write ISR to handle the event of the overflow
Resolution:
 is the smallest period taken by timer to take one count we can
calculate it through formula
Resolution = (1/Frequency)
 If you using 1 MHZ for CPU Clock timer takes to count up till it
overflow 256 us
 So if microcontroller is clocked with 1MHz source, then 8 bit timer
will run with resolution:
Resolution=1/1MHz=1µs
In case you want to increase the time needed for specific
operation we use timer prescalar
Prescalar divide the clock frequency and produce timer
clock
Prescalar modes are
 No prescalar (Timer Clock).
 Clock/8.
 Clock/64.
 Clock/256.
 Clock/512.
 Clock/1024.
 NO clock.
Note
CPU Clock doesn’t affected by
reduction using prescalar.
It remain the same
Example for using prescalar :
 If you using 1 MHZ for CPU Clock without using prescalar timer
takes to count up till it overflow 256 µs
 But when you use prescalar 1024 timer takes to count up till it
overflow 0.26 sec
 It’s easy to know the time timer need to overflow if you know
Resolution.
Resolution = (1/Frequency)
 So if microcontroller is clocked with 1MHz source, then 8 bit timer
without prescaler will run with resolution:
Resolution=1/1MHz=1µs
So if timer take 256 counts to overflow then it takes :
T= Resolution * 256 = 1µs *256 = 256 µs
 And when we use 1024 as prescalar:
Resolution = 1024 / 1 MHZ = 1024µs
T = Resolution * 256 = 1024 * 256 = 0.256s
ATmega16/32 have 3 Timer/counter we have three
Timers/Counters each one with Separate Prescaler.
 Timer 0 - 8 bit timer.
 Timer 1 -16 bit timer.
 Timer 2 - 8 bit timer.
Timer also can be operated in 3 modes :
 Normal mode (Overflow).
 Pulse Width Modulation mode(PWM).
 Clear Timer Compare mode(CTC ).
Timer0
 Timer1 is an 16 bit timer/counter which can count from 0 to
0xFF (256).
 In the timer mode this peripheral uses an internal clock
signal.
 In the counter mode an external signal on PIN0 in PORTB.
 The timer can be operated either in the polling mode or in
the interrupt mode.
 Timer/Counter Control Register – TCCR0
 Bit 7 – FOC0: Force Output Compare
 The FOC0 bit is only active when the WGM00 bit specifies a non-
PWM mode.
 Bit 3, 6 – WGM01:0: Waveform Generation Mode
 Bit 5:4 – COM01:00 Compare Match Output Mode
Bit 2:0 – CS02:0: Clock Select
Timer/Counter Register – TCNT0
 This is where the 8-bit counter of the timer resides. The
value of the counter is stored here and it increases
automatically each clock cycle. Data can be both read/written
from this register. The initial value of the counter is set by
writing it.
Output Compare Register – OCR0
 The Output Compare Register contains an 8-bit value that is
continuously compared with the counter value (TCNT0). A match
can be used to generate an output compare interrupt, or to
generate a waveform output on the OC0 pin.
Timer/Counter Interrupt Mask Register –TIMSK
 TOIE0: Timer Overflow Interrupt Enable
 OCIE0: Output Compare Match Interrupt Enable
 Timer/Counter Interrupt Flag Register – TIFR
 TOV0:
 The bit TOV0 is set (one) when an overflow occurs in Timer/Counter0. TOV0 is
cleared by hardware when executing the corresponding interrupt handling
vector. When the SREG I-bit, TOIE0 (Timer/Counter0 Overflow Interrupt
Enable), and TOV0 are set (one), the Timer/Counter0 Overflow interrupt is
executed.
 OCF0
 The OCF0 bit is set (one) when a compare match occurs between the
Timer/Counter0 and the data in OCR0 – Output Compare Register0. OCF0 is
cleared by hardware when executing the corresponding interrupt handling
vector. When the I-bit in SREG, OCIE0 (Timer/Counter0 Compare Match
Interrupt Enable), and OCF0 are set (one), the Timer/Counter0 Compare Match
Interrupt is executed.
Normal Mode (Overflow)
 A timer overflow means that the counter(TCTNx) has counted up
to its maximum value and is reset to zero in the next timer clock
cycle.
 The resolution of the timer determines the maximum value of that
timer.
 The timer overflow event causes the Timer Overflow Flag (TOVx)
to be set in the Timer Interrupt Flag Register (TIFR).
Pulse Width Modulation
 is the technique used to generate analogue signals from a
digital device like a MCU. Almost all modern MCUs have
dedicated hardware for PWM signal generation.
 A digital device, like a microcontroller can only generate two
levels on its output lines, HIGH=5v and LOW=0V.
 But what if we want to generate 2.5v or 3.1v or any voltage
between 0-5 volt output ?
 For these kinds of requirement, instead of generating a
constant DC voltage output we generate a square wave, which
has high = 5V and Low = 0v
 We will use the simplest timer, TIMER0 for PWM generation.
 Pulse Width Modulation
 The period (frequency) depends upon the prescaler settings. Now for PWM
generation from this count sequence we have a OCR0 (Output Compare Register
Zero Register). We can store any value between 0-255 in OCR0, say we store 64 in
OCR0 then it would appear in the graph as follows (the RED line).
 When the TIMER0 is configured for fast PWM mode, while up counting whenever
the value of TIMER0 counter (TCNT0 register) matches OCR0 register an output
PIN (OC0 PIN) is pulled low (0) and when counting sequence begin again from 0
it is SET again (pulled high=VCC).
 Pulse Width Modulation
Note:
to be able to generate
an output from the
PWM in Timer0, the
Output Compare Pin of
Timer0 (OC0) must
be set as output pin.
DDRB |= PB3
Timer & Interrupt Atmega16

More Related Content

What's hot

Basic functions & types of RTOS ES
Basic functions & types of  RTOS ESBasic functions & types of  RTOS ES
Basic functions & types of RTOS ESJOLLUSUDARSHANREDDY
 
3.programmable interrupt controller 8259
3.programmable interrupt controller 82593.programmable interrupt controller 8259
3.programmable interrupt controller 8259MdFazleRabbi18
 
Memory organization of 8051
Memory organization of 8051Memory organization of 8051
Memory organization of 8051Muthu Manickam
 
Microcontoller and Embedded System
Microcontoller and Embedded SystemMicrocontoller and Embedded System
Microcontoller and Embedded SystemKaran Thakkar
 
Presentation on 8086 microprocessor
Presentation on 8086 microprocessorPresentation on 8086 microprocessor
Presentation on 8086 microprocessorDiponkor Bala
 
Microprocessor Interfacing and 8155 Features
Microprocessor Interfacing and 8155 FeaturesMicroprocessor Interfacing and 8155 Features
Microprocessor Interfacing and 8155 FeaturesSrikrishna Thota
 
Microprocessor based Temperature Controller
Microprocessor based Temperature ControllerMicroprocessor based Temperature Controller
Microprocessor based Temperature ControllerRevanth Reddy
 
Double data rate (ddr)
Double data rate (ddr)Double data rate (ddr)
Double data rate (ddr)Anderson Huang
 
8086 modes
8086 modes8086 modes
8086 modesPDFSHARE
 
8051 Microcontroller Tutorial and Architecture with Applications
8051 Microcontroller Tutorial and Architecture with Applications8051 Microcontroller Tutorial and Architecture with Applications
8051 Microcontroller Tutorial and Architecture with Applicationselprocus
 
microcontroller basics
microcontroller basicsmicrocontroller basics
microcontroller basicssagar Ramdev
 

What's hot (20)

Timing diagram 8085 microprocessor
Timing diagram 8085 microprocessorTiming diagram 8085 microprocessor
Timing diagram 8085 microprocessor
 
Basic functions & types of RTOS ES
Basic functions & types of  RTOS ESBasic functions & types of  RTOS ES
Basic functions & types of RTOS ES
 
3.programmable interrupt controller 8259
3.programmable interrupt controller 82593.programmable interrupt controller 8259
3.programmable interrupt controller 8259
 
Lpc2148 i2c
Lpc2148 i2cLpc2148 i2c
Lpc2148 i2c
 
SPI Bus Protocol
SPI Bus ProtocolSPI Bus Protocol
SPI Bus Protocol
 
8051 ch9-950217
8051 ch9-9502178051 ch9-950217
8051 ch9-950217
 
Embedded system
Embedded systemEmbedded system
Embedded system
 
Memory organization of 8051
Memory organization of 8051Memory organization of 8051
Memory organization of 8051
 
Microcontoller and Embedded System
Microcontoller and Embedded SystemMicrocontoller and Embedded System
Microcontoller and Embedded System
 
Presentation on 8086 microprocessor
Presentation on 8086 microprocessorPresentation on 8086 microprocessor
Presentation on 8086 microprocessor
 
Microprocessor Interfacing and 8155 Features
Microprocessor Interfacing and 8155 FeaturesMicroprocessor Interfacing and 8155 Features
Microprocessor Interfacing and 8155 Features
 
Microprocessor based Temperature Controller
Microprocessor based Temperature ControllerMicroprocessor based Temperature Controller
Microprocessor based Temperature Controller
 
Double data rate (ddr)
Double data rate (ddr)Double data rate (ddr)
Double data rate (ddr)
 
Serial Communication in 8051
Serial Communication in 8051Serial Communication in 8051
Serial Communication in 8051
 
3 jump, loop and call instructions
3 jump, loop and call instructions3 jump, loop and call instructions
3 jump, loop and call instructions
 
I2C Protocol
I2C ProtocolI2C Protocol
I2C Protocol
 
UART
UARTUART
UART
 
8086 modes
8086 modes8086 modes
8086 modes
 
8051 Microcontroller Tutorial and Architecture with Applications
8051 Microcontroller Tutorial and Architecture with Applications8051 Microcontroller Tutorial and Architecture with Applications
8051 Microcontroller Tutorial and Architecture with Applications
 
microcontroller basics
microcontroller basicsmicrocontroller basics
microcontroller basics
 

Viewers also liked

AVR_Course_Day7 timers counters and interrupt programming
AVR_Course_Day7 timers counters and  interrupt programmingAVR_Course_Day7 timers counters and  interrupt programming
AVR_Course_Day7 timers counters and interrupt programmingMohamed Ali
 
Timers in Unix/Linux
Timers in Unix/LinuxTimers in Unix/Linux
Timers in Unix/Linuxgeeksrik
 
Developing an avr microcontroller system
Developing an avr microcontroller systemDeveloping an avr microcontroller system
Developing an avr microcontroller systemnugnugmacmac
 
OpenPump: open-source hardware for medical devices
OpenPump: open-source hardware  for medical devicesOpenPump: open-source hardware  for medical devices
OpenPump: open-source hardware for medical devicesSwansea University
 
project report on embedded system
project report on embedded systemproject report on embedded system
project report on embedded systemram avtar
 
Mp3 player project presentation
Mp3 player project presentationMp3 player project presentation
Mp3 player project presentationAntonio Mondragon
 
Persistence of Vision Display
Persistence of Vision DisplayPersistence of Vision Display
Persistence of Vision DisplayUday Wankar
 
Điều khiển động cơ sử dụng atmega16
Điều khiển động cơ sử dụng atmega16Điều khiển động cơ sử dụng atmega16
Điều khiển động cơ sử dụng atmega16Pham Hoang
 
Designing of media player
Designing of media playerDesigning of media player
Designing of media playerNur Islam
 
L12 c-language-programming-of-atmega328 p
L12 c-language-programming-of-atmega328 pL12 c-language-programming-of-atmega328 p
L12 c-language-programming-of-atmega328 prsamurti
 
L15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 pL15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 prsamurti
 
Rfid based toll tax collection system 3 (repaired)
Rfid based toll tax collection system 3 (repaired)Rfid based toll tax collection system 3 (repaired)
Rfid based toll tax collection system 3 (repaired)KrishNa Patel
 

Viewers also liked (20)

Timer
TimerTimer
Timer
 
AVR_Course_Day7 timers counters and interrupt programming
AVR_Course_Day7 timers counters and  interrupt programmingAVR_Course_Day7 timers counters and  interrupt programming
AVR_Course_Day7 timers counters and interrupt programming
 
Timers in Unix/Linux
Timers in Unix/LinuxTimers in Unix/Linux
Timers in Unix/Linux
 
Developing an avr microcontroller system
Developing an avr microcontroller systemDeveloping an avr microcontroller system
Developing an avr microcontroller system
 
OpenPump: open-source hardware for medical devices
OpenPump: open-source hardware  for medical devicesOpenPump: open-source hardware  for medical devices
OpenPump: open-source hardware for medical devices
 
digital clock atmega16
digital clock atmega16digital clock atmega16
digital clock atmega16
 
project report on embedded system
project report on embedded systemproject report on embedded system
project report on embedded system
 
JAVA Media Player
JAVA Media PlayerJAVA Media Player
JAVA Media Player
 
Mp3 player project presentation
Mp3 player project presentationMp3 player project presentation
Mp3 player project presentation
 
Persistence of Vision Display
Persistence of Vision DisplayPersistence of Vision Display
Persistence of Vision Display
 
Android mp3 player
Android mp3 playerAndroid mp3 player
Android mp3 player
 
Điều khiển động cơ sử dụng atmega16
Điều khiển động cơ sử dụng atmega16Điều khiển động cơ sử dụng atmega16
Điều khiển động cơ sử dụng atmega16
 
Android Media player
Android Media playerAndroid Media player
Android Media player
 
Timers
TimersTimers
Timers
 
Designing of media player
Designing of media playerDesigning of media player
Designing of media player
 
L12 c-language-programming-of-atmega328 p
L12 c-language-programming-of-atmega328 pL12 c-language-programming-of-atmega328 p
L12 c-language-programming-of-atmega328 p
 
Arduino introduction
Arduino introductionArduino introduction
Arduino introduction
 
L15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 pL15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 p
 
final report
final reportfinal report
final report
 
Rfid based toll tax collection system 3 (repaired)
Rfid based toll tax collection system 3 (repaired)Rfid based toll tax collection system 3 (repaired)
Rfid based toll tax collection system 3 (repaired)
 

Similar to Timer & Interrupt Atmega16

timer counter (1).pptx
timer counter (1).pptxtimer counter (1).pptx
timer counter (1).pptxSujalKumar73
 
Unit 3 timer and counter and there application .pptx
Unit 3 timer and counter and there application .pptxUnit 3 timer and counter and there application .pptx
Unit 3 timer and counter and there application .pptxnaveen088888
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Aarav Soni
 
unit 2 lect 6 AND 7 8254.pptx
unit 2 lect 6 AND 7 8254.pptxunit 2 lect 6 AND 7 8254.pptx
unit 2 lect 6 AND 7 8254.pptxDrVikasMahor
 
8051 training an interactive tutorial
8051 training an interactive tutorial8051 training an interactive tutorial
8051 training an interactive tutorialFutura infotech
 
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...Waqas Afzal
 
Best-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbaiBest-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbaiUnmesh Baile
 
Microprocessor & Interfacing (Part-2) By Er. Swapnil V. Kaware
Microprocessor & Interfacing (Part-2) By Er. Swapnil V. KawareMicroprocessor & Interfacing (Part-2) By Er. Swapnil V. Kaware
Microprocessor & Interfacing (Part-2) By Er. Swapnil V. KawareProf. Swapnil V. Kaware
 
MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50
MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50
MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50Ruthvik Vaila
 

Similar to Timer & Interrupt Atmega16 (20)

timer counter (1).pptx
timer counter (1).pptxtimer counter (1).pptx
timer counter (1).pptx
 
Unit 3 timer and counter and there application .pptx
Unit 3 timer and counter and there application .pptxUnit 3 timer and counter and there application .pptx
Unit 3 timer and counter and there application .pptx
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)
 
Timers
TimersTimers
Timers
 
unit 2 lect 6 AND 7 8254.pptx
unit 2 lect 6 AND 7 8254.pptxunit 2 lect 6 AND 7 8254.pptx
unit 2 lect 6 AND 7 8254.pptx
 
8051 training an interactive tutorial
8051 training an interactive tutorial8051 training an interactive tutorial
8051 training an interactive tutorial
 
Lpc 1768 timers
Lpc 1768 timersLpc 1768 timers
Lpc 1768 timers
 
Timers in Arduino
Timers in ArduinoTimers in Arduino
Timers in Arduino
 
Microcontroller part 5
Microcontroller part 5Microcontroller part 5
Microcontroller part 5
 
AVRTIMER.pptx
AVRTIMER.pptxAVRTIMER.pptx
AVRTIMER.pptx
 
Microcontroller part 3
Microcontroller part 3Microcontroller part 3
Microcontroller part 3
 
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
 
8051 Timers
8051 Timers8051 Timers
8051 Timers
 
Arduino 101
Arduino 101Arduino 101
Arduino 101
 
Best-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbaiBest-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbai
 
Timers
TimersTimers
Timers
 
Peripheral 8245,16550&8237 dma controller
Peripheral 8245,16550&8237 dma controllerPeripheral 8245,16550&8237 dma controller
Peripheral 8245,16550&8237 dma controller
 
Microprocessor & Interfacing (Part-2) By Er. Swapnil V. Kaware
Microprocessor & Interfacing (Part-2) By Er. Swapnil V. KawareMicroprocessor & Interfacing (Part-2) By Er. Swapnil V. Kaware
Microprocessor & Interfacing (Part-2) By Er. Swapnil V. Kaware
 
Lecture7
Lecture7Lecture7
Lecture7
 
MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50
MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50
MEASUREMENT AND DISPLAY OF THE MAINS FREQUENCY USING PIC18F4520/50
 

Recently uploaded

VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfrs7054576148
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfRagavanV2
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 

Recently uploaded (20)

VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 

Timer & Interrupt Atmega16

  • 2.
  • 4.
  • 5. Basically events that require immediate attention by the microcontroller. An interrupt is a signal to the processor emitted by hardware or software indicating an event that needs immediate attention. When an interrupt event occurs, the microcontroller pause its current task and attend to the interrupt by executing its corresponding Interrupt Service Routine (ISR) then microcontroller returns to the task it had pause and continue its normal operations.
  • 6. Such an event may be:  An peripheral device has finished the last task it was given and is now ready for another. For example Timer may generate interrupt after reaching the maximum possible value.  External Interrupt event on I/O Pin.  An error from a peripheral. You can think of it as a hardware-generated function call.
  • 7. ISR (Interrupt service routine)  Piece of code that should be execute when an interrupt is triggered  Should be deterministic and short as possible, as it usually set a flag to a specific task indicating a certain event is happened, or save small data in buffer.
  • 8.
  • 9. REMEMBER Global interrupt enabled bit MUST be enabled for interrupts to be activated in the µC. To Set global interrupt :  sei(); To clear global interrupt :  cli();
  • 10. Interrupt Flags and Enabled bits in AVR µC  Each interrupt is associated with two (2) bits, an Interrupt Flag Bit and an Interrupt Enabled Bit. These bits are located in the I/O registers associated with the specific interrupt: The interrupt enabled bit(IE): is used to enable or disable a specific interrupt. Basically is tells the µC whether or not it should respond to the interrupt if it is triggered. The interrupt flag bit (IF):is set whenever the interrupt event occur, whether or not the interrupt is enabled. And The flag is cleared when the ISR is executed. So it is also called “Automatic Flag”.
  • 11. Global interrupt Interrupt flag Interrupt enabled Interrupt Request
  • 12. RESET interrupt  It active low interrupt which means it triggered when low voltage (0 volt) is applied on reset pin (number 9).
  • 13.
  • 14. Timer is special register that can be 8- 16 bit so capable of holding from 0 to 255-65536 What differ it from any register that it count up / down automatically at predefined rate and this is Timer Clock that doesn’t need CPU intervention. Timer Clock could be internal / external
  • 15.
  • 16. One of basic condition when Timer overflow( it means timer counted up to its maximum value and return back to zero). In this situation it causes interrupt and if global interrupt mask is enabled and timer interrupt enable so you have to write ISR to handle the event of the overflow
  • 17. Resolution:  is the smallest period taken by timer to take one count we can calculate it through formula Resolution = (1/Frequency)  If you using 1 MHZ for CPU Clock timer takes to count up till it overflow 256 us  So if microcontroller is clocked with 1MHz source, then 8 bit timer will run with resolution: Resolution=1/1MHz=1µs
  • 18. In case you want to increase the time needed for specific operation we use timer prescalar Prescalar divide the clock frequency and produce timer clock Prescalar modes are  No prescalar (Timer Clock).  Clock/8.  Clock/64.  Clock/256.  Clock/512.  Clock/1024.  NO clock. Note CPU Clock doesn’t affected by reduction using prescalar. It remain the same
  • 19. Example for using prescalar :  If you using 1 MHZ for CPU Clock without using prescalar timer takes to count up till it overflow 256 µs  But when you use prescalar 1024 timer takes to count up till it overflow 0.26 sec  It’s easy to know the time timer need to overflow if you know Resolution. Resolution = (1/Frequency)  So if microcontroller is clocked with 1MHz source, then 8 bit timer without prescaler will run with resolution: Resolution=1/1MHz=1µs
  • 20. So if timer take 256 counts to overflow then it takes : T= Resolution * 256 = 1µs *256 = 256 µs  And when we use 1024 as prescalar: Resolution = 1024 / 1 MHZ = 1024µs T = Resolution * 256 = 1024 * 256 = 0.256s
  • 21. ATmega16/32 have 3 Timer/counter we have three Timers/Counters each one with Separate Prescaler.  Timer 0 - 8 bit timer.  Timer 1 -16 bit timer.  Timer 2 - 8 bit timer. Timer also can be operated in 3 modes :  Normal mode (Overflow).  Pulse Width Modulation mode(PWM).  Clear Timer Compare mode(CTC ).
  • 22. Timer0  Timer1 is an 16 bit timer/counter which can count from 0 to 0xFF (256).  In the timer mode this peripheral uses an internal clock signal.  In the counter mode an external signal on PIN0 in PORTB.  The timer can be operated either in the polling mode or in the interrupt mode.
  • 23.  Timer/Counter Control Register – TCCR0  Bit 7 – FOC0: Force Output Compare  The FOC0 bit is only active when the WGM00 bit specifies a non- PWM mode.  Bit 3, 6 – WGM01:0: Waveform Generation Mode
  • 24.  Bit 5:4 – COM01:00 Compare Match Output Mode
  • 25. Bit 2:0 – CS02:0: Clock Select
  • 26. Timer/Counter Register – TCNT0  This is where the 8-bit counter of the timer resides. The value of the counter is stored here and it increases automatically each clock cycle. Data can be both read/written from this register. The initial value of the counter is set by writing it.
  • 27. Output Compare Register – OCR0  The Output Compare Register contains an 8-bit value that is continuously compared with the counter value (TCNT0). A match can be used to generate an output compare interrupt, or to generate a waveform output on the OC0 pin.
  • 28. Timer/Counter Interrupt Mask Register –TIMSK  TOIE0: Timer Overflow Interrupt Enable  OCIE0: Output Compare Match Interrupt Enable
  • 29.  Timer/Counter Interrupt Flag Register – TIFR  TOV0:  The bit TOV0 is set (one) when an overflow occurs in Timer/Counter0. TOV0 is cleared by hardware when executing the corresponding interrupt handling vector. When the SREG I-bit, TOIE0 (Timer/Counter0 Overflow Interrupt Enable), and TOV0 are set (one), the Timer/Counter0 Overflow interrupt is executed.  OCF0  The OCF0 bit is set (one) when a compare match occurs between the Timer/Counter0 and the data in OCR0 – Output Compare Register0. OCF0 is cleared by hardware when executing the corresponding interrupt handling vector. When the I-bit in SREG, OCIE0 (Timer/Counter0 Compare Match Interrupt Enable), and OCF0 are set (one), the Timer/Counter0 Compare Match Interrupt is executed.
  • 30. Normal Mode (Overflow)  A timer overflow means that the counter(TCTNx) has counted up to its maximum value and is reset to zero in the next timer clock cycle.  The resolution of the timer determines the maximum value of that timer.  The timer overflow event causes the Timer Overflow Flag (TOVx) to be set in the Timer Interrupt Flag Register (TIFR).
  • 31. Pulse Width Modulation  is the technique used to generate analogue signals from a digital device like a MCU. Almost all modern MCUs have dedicated hardware for PWM signal generation.  A digital device, like a microcontroller can only generate two levels on its output lines, HIGH=5v and LOW=0V.  But what if we want to generate 2.5v or 3.1v or any voltage between 0-5 volt output ?  For these kinds of requirement, instead of generating a constant DC voltage output we generate a square wave, which has high = 5V and Low = 0v  We will use the simplest timer, TIMER0 for PWM generation.
  • 32.
  • 33.  Pulse Width Modulation  The period (frequency) depends upon the prescaler settings. Now for PWM generation from this count sequence we have a OCR0 (Output Compare Register Zero Register). We can store any value between 0-255 in OCR0, say we store 64 in OCR0 then it would appear in the graph as follows (the RED line).  When the TIMER0 is configured for fast PWM mode, while up counting whenever the value of TIMER0 counter (TCNT0 register) matches OCR0 register an output PIN (OC0 PIN) is pulled low (0) and when counting sequence begin again from 0 it is SET again (pulled high=VCC).
  • 34.  Pulse Width Modulation Note: to be able to generate an output from the PWM in Timer0, the Output Compare Pin of Timer0 (OC0) must be set as output pin. DDRB |= PB3

Editor's Notes

  1. What happens when an interrupt occurs in AVR Microcontrollers? 1. The microcontroller completes the execution of the current instruction, clears the I bit(Global interrupt enable bit in the SREG status register) and stores the address of the next instruction that should have been executed (the content of the PC), processor status register, And all the CPU registers are pushed onto the stack. 2. The interrupt vector of the triggered interrupt (ISR start address of this interrupt) is then loaded in the PC(program counter) from the interrupt vector table and the microcontroller starts execution from that point up until reaches the end of the ISR. 3. The address that was stored on the stack in step 1 is reloaded in the PC register, Status register and all the CPU registers are popped from the stack and the I bit is re-enabled. 4. The micro-controller then continue executing the program.