Part-4-Timer and Interrupt
Part-4-Timer and Interrupt
Part 4: 中斷與計時器
Timer and Interrupt
授課教師
Che-Wei LIN (林哲偉)
Embedded System-1
Outline
What is timer?
What is interrupt?
Experiments
Discussion
What’s the difference between timer and interrupt?
Embedded System-2
3
Interrupts (1)
Interrupt-driven I/O uses the processor’s interrupt system to
“interrupt” normal program flow to allow execution of an I/O
service routine
Disadvantages
Coding complexity for interrupt service routines
Embedded System-4
Interrupt Service Routines (ISR)
ISRs are special kinds of functions that cannot have any parameters, and they
If your sketch uses multiple ISRs, only one can run at a time, other interrupts
will be executed after the current one finishes in an order that depends on the
priority they have
millis() and delay() requires interrupts to work, so they will never work inside
an ISR.
Typically global variables are used to pass data between an ISR and the main
program. To make sure variables shared between an ISR and the main program
are updated correctly, declare them as volatile (global variables)
Embedded System-5
6
Embedded System-6
7
Returns
Embedded System-7
8
Embedded System-8
9
Syntax
ISR: the ISR to call when the interrupt occurs; this function must take
no parameters and return nothing. This function is sometimes referred
to as an interrupt service routine.
Embedded System-9
E1: Blink Without Delay
Experiment 1
Turn on and off a LED to a digital pin, without using delay()
millis() returns the number of milliseconds from when the program started
running based on the internal clock of the microcontroller.
Embedded System-10
E1: Blink Without Delay
Materials
Breadboard × 1
LED × 1
220 Ω resister × 1
Embedded System-11
E1: Open this example code from
"File" -> "Examples" -> "02.Digital" -> “BlinkWithoutDelay"
Embedded System-12
E1: Open this example code from
"File" -> "Examples" -> "02.Digital" -> “BlinkWithoutDelay"
Embedded System-13
E1: Blink Without Delay
We take the current time and subtract the previous time, and
check to see if this is greater than the interval time.
This is how we check to see how much time has elapsed since the
last time we lit or darkened our LED.
Embedded System-14
E1: Blink Without Delay
亮一秒、暗一秒
Embedded System-15
E1: Blink Without Delay
Code Reference
https://www.arduino.cc/reference/en/language/functions/time/millis/
https://www.arduino.cc/reference/en/language/variables/data-
types/unsignedlong/
https://www.arduino.cc/reference/en/language/variables/data-
types/unsignedint/
https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
Embedded System-16
E2: Using interrupt
Materials
Breadboard × 1
10 KΩ Resistor × 1
Button × 1
Embedded System-18
E2: Using interrupt
Code reference:
https://www.arduino.cc/reference/en/language/functions/external-
interrupts/attachinterrupt/
Embedded System-19
Lab requirements (1)
Please make a three digit time-meter (digit in tens, ones, zero-point ones)
(m)
(n)
Embedded System-20
Lab requirements (2)
Arduino Pinout
» Detail PIN assignments are needed to highlight in demo and final report
https://goo.gl/F8Zd7e
Embedded System-21
Lab requirements (3)
http://a7.fehmarnbeltachse.de/logic-diagram-of-ic-7447.html
Embedded System-22
Lab requirements (4)
In conclusion, the digit time-meter you made should count between 00.0~99.9
second, the time counting accuracy should above 95% (5% error is allowed)
If TA ask you to count 60 second, the maximum time error of 60 × 5% = 3 second is
allowed
Embedded System-23