0% found this document useful (0 votes)
16 views24 pages

Chapter 5

Uploaded by

Sơn Đinh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views24 pages

Chapter 5

Uploaded by

Sơn Đinh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Chapter 5

INTERRUPTS STRUCTURE
What is Interrupt
It is an internal or external events that inform the CPU something needs to be serviced

Real life Example of External Interrupts


• Warning of not wearing seat belt in a car.
• Pressing a key on a keypad.
• Power failure detection in a microcontroller-based system.
• Counting the number of products in a production line.
Interrupts and Polling
• Interrupts: Signals sent by external devices to processor to get attention,
interrupting its current task for handling incoming signal, ensuring efficient
communication without continuous polling.
• Polling: Processor regularly checks status of each device to determine
action required, less efficient than interrupts, but useful in situations where
interrupts are not feasible or devices have low priority.
• Interrupts and polling are important techniques for managing
communication between computers and external devices.
• Interrupts allow for efficient communication, while polling can be useful in
certain situations.
• Both techniques have their own advantages and limitations.
• Interrupts and polling are complementary methods used by computers to
manage communication with external devices.
Interrupt Service Route (ISR)
•ISR executes when interrupt signal is sent.
•Used for handling tasks from peripherals.
•Important for embedded devices and real-time apps.
•System automatically calls corresponding ISR when interrupt is triggered.
•After completing its task, system returns to previous state and continues executing
tasks.
Interrupt Vector Table for ATmega32 AVR
Steps in executing an Interrupt
1.It finishes the instruction it is currently executing and saves the address of the next instruction (program
counter) on the stack
1.Execution completion and program counter storing on stack after current instruction
2.It jumps to a fixed location in memory called the interrupt vector table. The interrupt vector table directs
the microcontroller to the address of the interrupt service routine (ISR).
2.Using interrupt vector table's fixed memory location to guide microcontroller to ISR
address.
3.The microcontroller starts to execute the interrupt service subroutine until it reaches the last instruction of
the subroutine, which is RETI (return from interrupt).
3.Execution of ISR subroutine by microcontroller until reaching the final instruction,
which is RETI for returning from interrupt
4.Upon executing the RETI instruction, the microcontroller returns to the place where it was interrupted.
First, it gets the program counter (PC) address from the stack by popping the top bytes of the stack into the
PC. Then it starts to execute from that address.
4. After executing the RETI instruction, microcontroller retrieves the interrupted Program
Counter (PC) address by popping top bytes of the stack and begins execution from that
address.
Internal and External Interrupt Sources
• The AVR 8-bits microcontroller provide both internal
and external interrupt sources. The internal interrupts
are associated with the microcontroller's peripherals.
• They are the Timer/Counter, Analog Comparator,
etc.
• The external interrupts are triggered via external pins.
• On AVR ATmega32 microcontroller there are four (4)
external interrupts:
• RESET Interrupt - Triggered from pin 9.
• External Interrupt 0 (INT0) - Triggered from pin 16.
• External Interrupt 1 (INT1) - Triggered from pin 17.
• External Interrupt 2 (INT2) - Triggered from pin 3.
External Interrupt Pins
External Interrupts
• The number of external hardware interrupts varies in
different AVRs.
• The ATmega32 has three external interrupts: pin PD2
(PORTD.2), PD3 (PORTD.3) and PB2 (PORTB.2), designated as
INT0, INT1 and INT2 respectively.
• Upon activation of these pins, the AVR is interrupted in
whatever it is doing and jumps to the vector table to perform
the interrupt service routine.
External Interrupts INT0, INT1 and INT2
• The interrupt vector table locations $2, $4 and $6 are set
aside for INT0, INT1 and INT2, respectively.
• The hardware interrupts must be enabled before they
can take effect.
• This is done using INTx bit located in GICR (General
Interrupt Control Register).
GICR Register
INT1 INT0 INT2 - - - IVSEL IVCE
Interrupt Sense Control bits for INT0
MCUCR (MCU Control Register)

SE SM1 SM2 SM0 ISC11 ISC10 ISC01 ISC00

ISC01 and ISC00 - These bits define the level or


edge on the external INT0 pin that activates the
interrupts, as shown in the following table
ISC0 ISC00 Description
1
0 0 The low level of INT0 generates an interrupt request
0 1 Change on INT0 generates an interrupt request
1 0 Falling edge of INT0 generates an interrupt request
1 1 Rising edge of INT0 generates an interrupt request
Interrupt Sense Control bits for INT1
MCUCR Register
SE SM1 SM2 SM0 ISC11 ISC10 ISC01 ISC00

ISC11 and ISC10 - These bits define the level or edge


on the external INT1 pin that activates the interrupts,
as shown in the following table
ISC11 ISC10 Description
0 0 The low level of INT1 generates an interrupt request
0 1 Change on INT1 generates an interrupt request
1 0 Falling edge of INT1 generates an interrupt request
1 1 Rising edge of INT1 generates an interrupt request
Interrupt Sense Control bit for INT2
• Interrupt 2 (INT2) can only be configures as edge triggered
mode.
• In other words it can not be configured as level triggered.
• ISC2 bit of MCUCSR (MCU Control and Status Register)
defines whether INT2 interrupt will activate on falling or rising
edge.
MCUCSR Register
JTD ISC2 - JTRF WDRF BORF EXTRF PORF
IS2 Description
0 Falling edge of INT2 generates an interrupt
request
1 Rising edge of INT2 generates an interrupt
request
Experiment with Arduino
attachInterrupt()
Syntax: attachInterrupt(interrupt, ISR, mode);

• interrupt:interrupt number
• ISR: The name of the
function to be called when
an interrupt event is
generated.
• Mode: LOW; HIGH;RISING;
FALLING; CHANGE
•RISING: To trigger an interrupt when the pin transits from LOW to HIGH.
•FALLING: To trigger an interrupt when the pin transits from HIGH to LOW.
•CHANGE: To trigger an interrupt when the pin transits from LOW to HIGH
or HIGH to LOW (i.e., when the pin state changes ).
Experiment with Arduino
Example

const int interruptPin = 2; // số chân được sử dụng cho ngắt


volatile int state = LOW; // trạng thái biến được cập nhật bởi ISR

void setup() {
pinMode(interruptPin, INPUT_PULLUP); // cấu hình chân ngắt
attachInterrupt(digitalPinToInterrupt(interruptPin), ISR, CHANGE); // đăng ký ngắt
Serial.begin(9600); // cấu hình Serial monitor
}

void loop() {
// thực hiện các hoạt động khác trong vòng lặp chính
}

void ISR() {
state = !state; // cập nhật trạng thái biến
Serial.println(state); // hiển thị giá trị trạng thái mới
}
#include <TimerOne.h>
const int led = LED_BUILTIN; // the pin with a LED
int ledState = LOW;
volatile unsigned long blinkCount = 0; // use volatile for shared variables
void setup(void)
{
pinMode(led, OUTPUT);
Timer1.initialize(150000);
Timer1.attachInterrupt(blinkLED); // blinkLED to run every 0.15 seconds
Serial.begin(9600);
}
//Interrupt Programming
void blinkLED(void)
{
if (ledState == LOW) {
ledState = HIGH;
blinkCount = blinkCount + 1; // increase when LED turns on
} else {
ledState = LOW;
}
digitalWrite(led, ledState);
}
// The main program will print the blink count

void loop(void)
{
unsigned long blinkCopy; // holds a copy of the blinkCount
noInterrupts();
blinkCopy = blinkCount;
interrupts();
Serial.print("blinkCount = ");
Serial.println(blinkCopy);
delay(100);
}
BÀI TOÁN ĐO TỐC ĐỘ CAO (HIGH SPEED MEASUREMENTS)

Encoder tương đối Encoder tuyệt đối


BÀI TOÁN ĐO TỐC ĐỘ CAO (HIGH SPEED MEASUREMENTS)
Encoder tuyệt đối dùng xác định vị trí
BÀI TOÁN ĐO TỐC ĐỘ CAO (HIGH SPEED MEASUREMENTS)
Encoder tương đối
Dùng đo tốc độ
BÀI TOÁN ĐO TỐC ĐỘ CAO (HIGH SPEED MEASUREMENTS)

1.Pulses Counted in Time Interval: Count the


number of encoder pulses in a specific time
interval using interrupts to detect changes in
the encoder's output.
Pulses per Revolution (PPR): This is a
characteristic of the encoder and represents
how many pulses it outputs per full revolution
of the encoder shaft
CODE

// Khai báo các chân kết nối encoder void updateEncoder() {


const int encoderPinA = 2; // Chân A của encoder // Đọc giá trị từ encoder
const int encoderPinB = 3; // Chân B của encoder int MSB = digitalRead(encoderPinA); // Most significant bit
volatile long encoderValue = 0; // Biến lưu giá trị encoder int LSB = digitalRead(encoderPinB); // Least significant bit
volatile long lastEncoded = 0; // Biến lưu giá trị mã hóa trước đó int encoded = (MSB << 1) | LSB; // Kết hợp hai bit thành một mã hóa
unsigned long previousMillis = 0; // Thời gian trước đó int sum = (lastEncoded << 2) | encoded; // Tạo mã hóa 4 bit
const long interval = 1000; // Khoảng thời gian để tính toán RPM (1 giây) // Cập nhật giá trị encoder dựa trên mã hóa
void setup() { // Cài đặt các chân encoder là đầu vào với pullup if (sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011)
pinMode(encoderPinA, INPUT_PULLUP); encoderValue++;
pinMode(encoderPinB, INPUT_PULLUP); if (sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000)
// Gắn ngắt cho chân encoder encoderValue--;
attachInterrupt(digitalPinToInterrupt(encoderPinA), updateEncoder, CHANGE); lastEncoded = encoded; // Cập nhật mã hóa cuối cùng
attachInterrupt(digitalPinToInterrupt(encoderPinB), updateEncoder, CHANGE); }
// Khởi tạo Serial Monitor
Serial.begin(9600);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Tính toán RPM
long currentValue;
noInterrupts(); // Tạm thời vô hiệu hóa ngắt
currentValue = encoderValue;
encoderValue = 0; // Đặt lại giá trị encoder sau khi tính toán
interrupts(); // Kích hoạt lại ngắt
// Tính RPM: (số xung / số xung mỗi vòng) * 60 giây
float rpm = (currentValue / 100.0) * 60.0;
// Hiển thị RPM
Serial.print("RPM: ");
Serial.println(rpm);
}
}

You might also like