Download - MCU: Interrupts and Timers
![Page 1: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/1.jpg)
MCU: Interrupts and Timers
Ganesh Pitchiah
![Page 2: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/2.jpg)
What’s an MCU ?
![Page 3: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/3.jpg)
Frequency = 8 MHzTime Period = 1/f = 0.125 us
![Page 4: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/4.jpg)
Code for Switching LED
int a; // Define variable a to store value of voltage while(1){a = PINA.0; // read value at pin A.0 (make sure it is input)If (a==1) // if voltage is 5VPORTA.1=1; // Light the LEDelsePORTA.1=0; // Turn off the LED
}
![Page 5: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/5.jpg)
The Problem
------- while(1){ ---- -> Check value of a ---- -> Event ‘A’ : a == 1 ---- ---- ---- -> Event ‘B’ : a == 0 ---- ---- ---- ---- -> Suppose event ‘A’ occurs here ---- }
![Page 6: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/6.jpg)
![Page 7: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/7.jpg)
Interrupt Means
• To cause or make a break in the continuity or uniformity of (a course, process, condition, etc.
![Page 8: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/8.jpg)
The Problem & Solution
------- while(1){ ---- -> Check value of a ---- -> Event ‘A’ : a == 1 ---- ---- ---- -> Event ‘B’ : a == 0 ---- ---- ---- ---- -> Suppose event ‘A’ occurs here ---- }
![Page 9: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/9.jpg)
Interrupts
• Software Interrupt while (1) { keep checking all events only }• Hardware Interrupt
![Page 10: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/10.jpg)
Why Interrupts?
• Interrupts are special events that can “interrupt” the normal flow of a program.
• The processor stops the normal program, handles the interrupt, and then resumes its normal work.
![Page 11: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/11.jpg)
![Page 12: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/12.jpg)
Registers
• Small amount of storage available in MCU/PC.• Ex: A flip flop stores 1-bit of memory.
![Page 13: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/13.jpg)
Timers
![Page 14: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/14.jpg)
Timers
• A timer is a register. • 255 -> Maximum value 254
.
.
. 0 -> Starting value
7 6 5 4 3 2 1 0
![Page 15: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/15.jpg)
Timers
• 8-bit Register and Starts with 0 • Increase 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.
• This process is independent of the CPU.
7 6 5 4 3 2 1 00 0 0 0 0 0 0 00 0 0 0 0 0 0 10 0 0 0 0 0 1 01 1 1 1 1 1 1 1
![Page 16: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/16.jpg)
Simple statistics
• Maximum value of timer is n andclock 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 17: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/17.jpg)
![Page 18: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/18.jpg)
Timers and Interrupts
• Timers can generate certain two interrupts: 1. OVERFLOW interrupt and 2. COMPARE MATCH interrupt.
![Page 19: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/19.jpg)
OVERFLOW interrupt
• OVERFLOW is generated when a timer tries to exceed 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.
0 0 0 0 0 0 0 0
![Page 20: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/20.jpg)
OVERFLOW statistics
• Suppose a timer of maximum value n has a time period t (also called as clock period).
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 21: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/21.jpg)
OVERFLOW and COMPARE MATCH
![Page 22: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/22.jpg)
COMPARE MATCH interrupt
• There is a register called as OCR (Output Compare Register), whose value we can set.
• Before incrementing, the value of the timer is compared to OCR. If the two are equal, a COMPARE MATCH interrupt is generated.
![Page 23: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/23.jpg)
COMPARE MATCH statistics
• Suppose a timer of maximum value n has a time period t (also called as clock period).
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 24: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/24.jpg)
OVERFLOW and COMPARE MATCH
![Page 25: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/25.jpg)
Summary of Timers
• A timer is not affected by interrupts: it generated interrupts, but it does not stop running because of them.
• Interrupts is how timers are useful. Sample applications: digital clock, periodic events (such as blinking LEDs quickly for POV globe), etc.
![Page 26: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/26.jpg)
![Page 27: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/27.jpg)
Timer Modes
• A timer works in three modes: Normal, CTC and PWM.
• All three modes are again unaffected by interrupts, but all three modes can generate interrupts.
• The timer mode used so far in this presentation is normal mode.
![Page 28: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/28.jpg)
Normal Mode
• Standard mode: Timer starts at 0, goes to maximum value and then resets itself.
• OVERFLOW and COMPARE MATCH interrupts generated as normal.
![Page 29: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/29.jpg)
![Page 30: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/30.jpg)
CTC (Clear Timer on Compare) Mode
• Timer starts at 0 as usual, but instead of resetting after maximum value, it resets after reaching value specified in OCR register.
![Page 31: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/31.jpg)
CTC mode statistics
• If clock time period is t: 1. Timer cycle time period = ( +1)× 𝑂𝐶𝑅 𝑡 2. Frequency = 1/( +1)× 𝑂𝐶𝑅 𝑡• COMPARE MATCH interrupt will work
normally, but OVERFLOW interrupt will not work (Why?).
![Page 32: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/32.jpg)
![Page 33: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/33.jpg)
PWM (Pulse Width Modulation) Mode
• Simple method of obtaining analog output of any value between 0 and 5V.
• Desired output is x% of 5V. • If Ton = x% then average value is x% of 5V.
![Page 34: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/34.jpg)
PWM(Pulse Width Modulation) mode
![Page 35: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/35.jpg)
• 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.
PWM mode
![Page 36: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/36.jpg)
PWM statistics
• 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 will work properly.
![Page 37: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/37.jpg)
![Page 38: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/38.jpg)
Using CVAVR
Open CVAVR
Go to File New Projec
t
![Page 39: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/39.jpg)
CVAVR Home
![Page 40: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/40.jpg)
Go to File
![Page 41: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/41.jpg)
Choose New
![Page 42: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/42.jpg)
Choose Project
![Page 43: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/43.jpg)
Press Yes
![Page 44: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/44.jpg)
Select Chip
![Page 45: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/45.jpg)
Using CVAVR
Open CVAVR Go to File New Project
![Page 46: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/46.jpg)
Demo
![Page 47: MCU: Interrupts and Timers](https://reader036.vdocuments.mx/reader036/viewer/2022062310/5681613f550346895dd0abc6/html5/thumbnails/47.jpg)