100% found this document useful (1 vote)
78 views23 pages

Part-4-Timer and Interrupt

This document discusses using timers and interrupts in embedded systems. It introduces timers, which are used to generate precise timing delays, and interrupts, which allow a program to respond immediately to external events. The key steps of an interrupt include saving the program counter, running the interrupt service routine, and then restoring normal execution. Experiments are described to blink an LED without delay using millis() and attachInterrupt() to toggle an LED when a button is pressed. Finally, requirements for a lab project to build a three-digit timer using interrupts and a seven-segment display are provided.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
78 views23 pages

Part-4-Timer and Interrupt

This document discusses using timers and interrupts in embedded systems. It introduces timers, which are used to generate precise timing delays, and interrupts, which allow a program to respond immediately to external events. The key steps of an interrupt include saving the program counter, running the interrupt service routine, and then restoring normal execution. Experiments are described to blink an LED without delay using millis() and attachInterrupt() to toggle an LED when a button is pressed. Finally, requirements for a lab project to build a three-digit timer using interrupts and a seven-segment display are provided.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 23

嵌入式系統

Introduction to Embedded System Design

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

 An interrupt is like a hardware-initiated subroutine call


 Advantages
 Immediate response to I/O service request

 Normal execution continues until it is known that I/O service is needed

 Disadvantages
 Coding complexity for interrupt service routines

 Extra hardware needed

 Processor’s interrupt system I/O device must generate an interrupt request


Embedded System-3
Interrupts (2)

 Interrupts are useful for making things happen automatically in


microcontroller programs, and can help solve timing problems.

 Good tasks for using an interrupt may include reading a rotary


encoder, or monitoring user input.

 Using an interrupt can free the microcontroller to get some other


work done while not missing the input

Embedded System-4
Interrupt Service Routines (ISR)

 ISRs are special kinds of functions that cannot have any parameters, and they

shouldn’t return anything

 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

Basic Steps for an Interrupt (1)


 An interrupt cycle begins at the next fetch cycle if:

 Interrupts are enable

 Interrupt request is active

 Program counter (PC) is saved on the stack

 PC is loaded with address of interrupt service routine (ISR)

 Different schemes for determining this address

Embedded System-6
7

Basic Steps for an Interrupt (2)

 Interrupt service routine executes

 Saves any registers that will be altered so that normal


program flow is not disturbed

 Performs input and/or output operations to clear the


interrupt request

 Restores saved registers

 Returns

 Normal execution resumes

Embedded System-7
8

Program Flow for an Interrupt

Embedded System-8
9

Syntax

 Introduction of attachInterrupt() function


 attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)

 interrupt: the number of the interrupt (int)

 pin: the pin number (Arduino Due, Zero, MKR1000 only)

 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.

 mode: defines when the interrupt should be triggered. Five constants


are predefined as valid values:

» LOW, CHANGE, RISING, FALLING and HIGH

Embedded System-9
E1: Blink Without Delay

 Experiment 1
 Turn on and off a LED to a digital pin, without using delay()

 Introduction of delay() function


 Pauses the program for the amount of time (in milliseconds) specified as
parameter. (There are 1000 milliseconds in a second.)

 Introduction of millis() function


 The loop() functions opens with a variable declaration and initialization
using the millis() function.

 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

 Arduino or Genuino Board × 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

 At the beginning of the program we set the variable


previousMillis to 0, and the variable interval to 1000.

 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

 Using interrupt to change the state of the LED

 Introduction of INPUT_PULLUP parameter


 There are 20K pullup resistors built into the Atmega chip that can be
accessed from software. These built-in pullup resistors are accessed by
setting the pinMode() as INPUT_PULLUP. This effectively inverts the
behavior of the INPUT mode, where HIGH means the sensor is off, and
LOW means the sensor is on.

 When connecting a sensor to a pin configured with INPUT_PULLUP, the


other end should be connected to ground. In the case of a simple switch,
this causes the pin to read HIGH when the switch is open, and LOW when
the switch is pressed.
Embedded System-17
E2: Using interrupt

 Materials
 Breadboard × 1

 Arduino or Genuino Board × 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)

 Minimal function are described as follows:

 Two buttons (implemented by interrupt) (Input pins)


» Clear (Reset) (m)

» Start and Pause (n)

 Two digits of time display (Output pins)


» Digit in tens (a)

» Digit in ones (b) https://goo.gl/qEFHFf

» Digit in zero-point ones (c)


(a) (b) (c)

(m)

(n)
Embedded System-20
Lab requirements (2)
 Arduino Pinout
» Detail PIN assignments are needed to highlight in demo and final report

» Difficulties: Number of pins in Arduino UNO are limited

https://goo.gl/F8Zd7e

Embedded System-21
Lab requirements (3)

 Use IC 7447 to utilize fewer pins to control seven-segment display

http://a7.fehmarnbeltachse.de/logic-diagram-of-ic-7447.html

Embedded System-22
Lab requirements (4)

 Step by step suggestion of implementation process in this lab.

 Check how to control seven segment LED display

 Check how to make a time counter by using timer function in Arduino


» You can use variables to count time in Arduino

» Remember to assign a initial value

 Check how to change state by using button interrupt in Arduino


» You can use the button to change the state of the time counter
− Start, Pause, Reset

 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

You might also like