timers and interrupts - students.iitk.ac.in · interrupts are special events that can...
TRANSCRIPT
![Page 1: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/1.jpg)
Anurag Dwivedi
![Page 2: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/2.jpg)
![Page 3: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/3.jpg)
MCU
![Page 4: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/4.jpg)
MCU
A small computer integrated in a
single IC
![Page 5: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/5.jpg)
MCU
A small computer integrated in a
single IC
Has I/O pins, RAM and Memory
![Page 6: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/6.jpg)
Software Used
![Page 7: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/7.jpg)
CVAvr Software Used
![Page 8: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/8.jpg)
CVAvr Software Used
Editor
![Page 9: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/9.jpg)
CVAvr Software Used
![Page 10: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/10.jpg)
CVAvr Software Used
Compiler
![Page 11: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/11.jpg)
Software Used
![Page 12: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/12.jpg)
Avr-Studio Software Used
![Page 13: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/13.jpg)
Avr-Studio Software Used To program the code into the MCU
![Page 14: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/14.jpg)
MCU Coding
![Page 15: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/15.jpg)
MCU Coding
The data direction is set through DDR
Register
![Page 16: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/16.jpg)
MCU Coding
The data direction is set through DDR
Register
![Page 17: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/17.jpg)
MCU Coding
![Page 18: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/18.jpg)
MCU Coding
I/O ports are accessed by PORT and PIN Registers
![Page 19: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/19.jpg)
MCU Coding
I/O ports are accessed by PORT and PIN Registers
.
.
. While(1){ PORTA.1 = 1; //sets the pin to 5V PORTA.1 = 0; // sets the pin to 0V X = PINA.0; //reads the value of pin // and copies it to X } . . .
![Page 20: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/20.jpg)
Registers are actual hardware memory locations inside the μC.
What do we mean by this??
Consider a 8-bit long register. Each bit of the register can be realized as a flip-flop.
Ex. PORTX is a register.
When you set the value of PORTA = 0X01, you physically set the corresponding flip-flop a value of +5 Volts.
![Page 21: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/21.jpg)
A Timer is usually a 8-bit register.
It starts with
0
.
.
.
.
255
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
![Page 22: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/22.jpg)
8-bit register. Values starts from 0 and goes up to 255.
![Page 23: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/23.jpg)
8-bit register. Values starts from 0 and goes up to 255. Timer value increases by 1,after each period.
t = 0 T 0 0 0 0 0 0 0 0
![Page 24: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/24.jpg)
8-bit register. Values starts from 0 and goes up to 255. Timer value increases by 1,after each period.
t = 0 T 0 0 0 0 0 0 0 0 t = 1 T 0 0 0 0 0 0 0 1
![Page 25: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/25.jpg)
8-bit register. Values starts from 0 and goes up to 255. Timer value increases by 1,after each period.
t = 0 T 0 0 0 0 0 0 0 0 t = 1 T 0 0 0 0 0 0 0 1 t = 2 T 0 0 0 0 0 0 1 0
![Page 26: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/26.jpg)
8-bit register. Values starts from 0 and goes up to 255. Timer value increases by 1,after each period.
t = 0 T 0 0 0 0 0 0 0 0 t = 1 T 0 0 0 0 0 0 0 1 t = 2 T 0 0 0 0 0 0 1 0 t = 255 T 1 1 1 1 1 1 1 1
![Page 27: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/27.jpg)
8-bit register. Values starts from 0 and goes up to 255. Timer value increases by 1,after each period.
When the timer reaches its maximum value, in the next cycle, its value becomes 0 again and the process repeats itself.
t = 0 T 0 0 0 0 0 0 0 0 t = 1 T 0 0 0 0 0 0 0 1 t = 2 T 0 0 0 0 0 0 1 0 t = 255 T 1 1 1 1 1 1 1 1
![Page 28: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/28.jpg)
8-bit register. Values starts from 0 and goes up to 255. Timer value increases by 1,after each period.
When the timer reaches its maximum value, in the next cycle, its value becomes 0 again and the process repeats itself.
t = 0 T 0 0 0 0 0 0 0 0 t = 1 T 0 0 0 0 0 0 0 1 t = 2 T 0 0 0 0 0 0 1 0 t = 255 T 1 1 1 1 1 1 1 1 t = 256 T 0 0 0 0 0 0 0 0
![Page 29: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/29.jpg)
8-bit register. Values starts from 0 and goes up to 255. Timer value increases by 1,after each period.
When the timer reaches its maximum value, in the next cycle, its value becomes 0 again and the process repeats itself.
The timer frequency can be factors of the base frequency of the MCU.
t = 0 T 0 0 0 0 0 0 0 0 t = 1 T 0 0 0 0 0 0 0 1 t = 2 T 0 0 0 0 0 0 1 0 t = 255 T 1 1 1 1 1 1 1 1 t = 256 T 0 0 0 0 0 0 0 0
![Page 30: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/30.jpg)
8-bit register. Values starts from 0 and goes up to 255. Timer value increases by 1,after each period.
When the timer reaches its maximum value, in the next cycle, its value becomes 0 again and the process repeats itself.
The timer frequency can be factors of the base frequency of the MCU.
This process is independent of the CPU.
t = 0 T 0 0 0 0 0 0 0 0 t = 1 T 0 0 0 0 0 0 0 1 t = 2 T 0 0 0 0 0 0 1 0 t = 255 T 1 1 1 1 1 1 1 1 t = 256 T 0 0 0 0 0 0 0 0
![Page 31: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/31.jpg)
Maximum value of timer is n and clock period is t, then:
1. Timer period = t
2. Timer cycle period = (𝑛+1)×𝑡
3. Frequency of timer (f) = 1/𝑡
4. Frequency of timer cycle = 1/(𝑛+1)×𝑡
![Page 32: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/32.jpg)
Registers
Timers
![Page 33: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/33.jpg)
Interrupts means causing a break in a continuing process.
![Page 34: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/34.jpg)
Suppose you need to check for a condition A while running another condition B
![Page 35: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/35.jpg)
Simple Solution..
![Page 36: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/36.jpg)
Simple Solution.. while(1){ ---- -> if (Event A == true) ---- -> // print event A has occurred ---- ---- ---- -> Event B ---- ---- }
![Page 37: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/37.jpg)
Simple Solution.. while(1){ ---- -> if (Event A == true) ---- -> // print event A has occurred ---- ---- ---- -> Event B ---- ---- } Do you see the problem in this approach??
![Page 38: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/38.jpg)
Simple Solution.. while(1){ ---- -> if (Event A == true) ---- -> // print event A has occurred ---- ---- ---- -> Event B ---- ---- -> Suppose Event A happens here ---- }
![Page 39: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/39.jpg)
![Page 40: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/40.jpg)
![Page 41: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/41.jpg)
.
. while(1){ --- --- EVENT B --- --- } .
![Page 42: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/42.jpg)
.
. while(1){ --- --- EVENT B --- --- } .
![Page 43: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/43.jpg)
.
. while(1){ --- --- EVENT B --- --- } .
When event A occurs ,
call an interrupt
![Page 44: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/44.jpg)
.
. while(1){ --- --- EVENT B --- --- } . handleA(){ . }
When event A occurs ,
call an interrupt Done.
![Page 45: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/45.jpg)
.
. while(1){ --- --- EVENT B --- --- } . handleA(){ . }
![Page 46: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/46.jpg)
.
. while(1){ --- --- EVENT B --- --- } . handleA(){ . // print event A has occurred }
![Page 47: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/47.jpg)
![Page 48: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/48.jpg)
Interrupts are special events that can “interrupt” the normal flow of a program.
Whenever an Interrupt is called, the processor stops the normal program, handles the interrupt, and then resumes its normal work.
There are two types of interrupts:
External and Internal
![Page 49: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/49.jpg)
The controller monitors the input at the special pins INT0 and INT1, whenever external interrupt is set on.
We can configure the program to call an external interrupt whenever any of the following conditions are met.
Rising Edge
Falling Edge
Any change
Low level
![Page 50: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/50.jpg)
Registers
Timers
Interrupts
External Interrupts
![Page 51: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/51.jpg)
The internal interrupts are called when different specific conditions are met by the timer value.
This brings us to the next topic..
![Page 52: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/52.jpg)
Timers can generate certain interrupts: two, to be precise.
These are called OVERFLOW interrupt and COMPARE MATCH interrupt.
![Page 53: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/53.jpg)
An overflow interrupt is generated when the timer exceeds its maximum value and resets to 0
The interrupt may or may not have a handler. In either case, the timer continues to run; remember: timers are independent of the CPU.
![Page 54: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/54.jpg)
Suppose a timer of maximum value n has a time period t (also called as clock period).
Then :
1. Timer cycle frequency = 1/(𝑛+1)×𝑡
2. OVERFLOW interrupt frequency = 1/(𝑛+1)×𝑡
If OVERFLOW interrupt is enabled, then an interrupt is generated in every cycle.
![Page 55: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/55.jpg)
A compare match interrupt is called when the value of the timer equals a specific value, set by the user.
This value is set by setting the value of OCR register.
Before incrementing, the value of the timer is compared to OCR. If the two are equal, a COMPARE MATCH interrupt is generated
![Page 56: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/56.jpg)
Suppose a timer of maximum value n has a time period t (also called as clock period).
Then :
1. Timer cycle frequency = 1/(𝑛+1)×𝑡
2. COMPARE MATCH interrupt frequency = 1/(𝑛+1)×𝑡
If COMPARE MATCH interrupt is enabled, then an interrupt is generated in every cycle.
![Page 57: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/57.jpg)
![Page 58: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/58.jpg)
Registers
Timers
Interrupts
External Interrupts
Internal Interrupts
-- Overflow Interrupt
-- Compare Match Interrupt
![Page 59: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/59.jpg)
A timer works in three modes: Normal, CTC and PWM.
All three modes differ in the response of the controller to the interrupts generated.
The timer mode used so far in this presentation is normal mode.
![Page 60: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/60.jpg)
Standard mode: Timer starts at 0, goes to maximum value and then resets itself.
OVERFLOW and COMPARE MATCH interrupts generated as normal.
![Page 61: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/61.jpg)
Known as Clear Timer on Compare.
As evident by the name, the timer starts at 0 as usual, but instead of resetting after maximum value, it resets after reaching value specified in OCR register.
Compare match interrupt if enabled will be generated but not overflow interrupt (Why?)
![Page 62: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/62.jpg)
If clock time period is t:
1. Timer cycle time period = (𝑂𝐶𝑅+1)×𝑡
2. Frequency = 1/(𝑂𝐶𝑅+1)×𝑡
With the use of CTC Mode we can theoretically generate any frequency up to 8 MHz.
Example of 1 Hz generation.
![Page 63: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/63.jpg)
Registers Timers Interrupts External Interrupts Internal Interrupts
-- Overflow Interrupt -- Compare Match Interrupt
Timer Modes -- Normal Mode -- CTC ( Clear on Timer Compare ) Mode
![Page 64: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/64.jpg)
Known as Pulse Width Modulation
Simple method of obtaining analog output of any value between 0 and 5V.
How is it achieved??
![Page 65: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/65.jpg)
Suppose we need 3V for our device at a specified pin.
We supply 5V on it for (3/5)* 100 % = 60% of the time period and 0V for the remaining time period
The average voltage at the pin for a time period becomes 3V
If this step is repeated very fast (T is very small), then the output behaves as a analog signal of 3V.
![Page 66: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/66.jpg)
Vout = 3.75 V Vout = 0.625 V
![Page 67: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/67.jpg)
The PWM behaves in a similar way.
![Page 68: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/68.jpg)
The PWM behaves in a similar way.
This “analog” value is obtained using timers.
A specific pin is set as output. When the timer reaches 0, the voltage of the pin is set to 5V.
![Page 69: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/69.jpg)
The PWM behaves in a similar way.
This “analog” value is obtained using timers.
A specific pin is set as output. When the timer reaches 0, the voltage of the pin is set to 5V.
![Page 70: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/70.jpg)
The PWM behaves in a similar way.
This “analog” value is obtained using timers.
A specific pin is set as output. When the timer reaches 0, the voltage of the pin is set to 5V.
![Page 71: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/71.jpg)
The PWM behaves in a similar way.
This “analog” value is obtained using timers.
A specific pin is set as output. When the timer reaches 0, the voltage of the pin is set to 5V.
When the timer reaches the value specified by OCR, on the next clock, the pin voltage is set to 0 until the timer resets itself.
![Page 72: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/72.jpg)
The PWM behaves in a similar way.
This “analog” value is obtained using timers.
A specific pin is set as output. When the timer reaches 0, the voltage of the pin is set to 5V.
When the timer reaches the value specified by OCR, on the next clock, the pin voltage is set to 0 until the timer resets itself.
![Page 73: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/73.jpg)
If clock time period is t and maximum timer value is n:
1.Timer cycle time period =(𝑛+1)×𝑡
2.Frequency =1/(𝑛+1)×𝑡
3.Duty cycle =[𝑂𝐶𝑅/(𝑛+1)]×100%
4.Output voltage =[𝑂𝐶𝑅/(𝑛+1)]×5𝑉
COMPARE MATCH interrupt and OVERFLOW interrupt both will work properly.
Demo.
![Page 74: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/74.jpg)
Registers
Timers
Interrupts
External Interrupts
Internal Interrupts
-- Overflow Interrupt
-- Compare Match Interrupt
Timer Modes
-- Normal Mode
-- CTC ( Clear on Timer Compare ) Mode
-- PWM ( Pulse Width Modulation) Mode
![Page 75: Timers and Interrupts - students.iitk.ac.in · Interrupts are special events that can “interrupt” the normal flow of a program. Whenever an Interrupt is called, the processor](https://reader034.vdocuments.net/reader034/viewer/2022051923/6010cb30f2ef480386732df7/html5/thumbnails/75.jpg)