You are on page 1of 43

HCMC University of Technology and Education

Faculty of Electrical & Electronic Engineering


No.1 Vo Van Ngan Street, Thu Duc Dist., HCMC, VN

MICROCONTROLLERS

NGUYEN THANH NGHIA


12/16/2020 11
NGUYEN THANH NGHIA
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
No.1 Vo Van Ngan Street, Thu Duc Dist., HCMC, VN

CHAPTER 11:
INTERRUPT

NGUYEN THANH NGHIA


12/16/2020 22
NGUYEN THANH NGHIA
Outline
1. Introduction.
2. Application of Interrupt.

NGUYEN THANH NGHIA 3


Chapter 11: Interrupt
1. Introduction
 PIC 16F887A has the following 13 interrupt sources :
External RB0/INT interrupt
Timer 0 overflow interrupt
Timer 1 overflow interrupt
TMR2 to PR2 Match interrupt
RB Port Change interrupt
Fail-Safe Clock Monitor Interrupt
A/D Converter interrupt
EUSART Transmit and Receive interrupt
Enhanced CCP Interrupt (Capture, Compare, PWM) interrupt
MSSP Interrupt
Anologue voltage Comparator
EEPROM Write Operation
Ultra Low-Power Wake-up Interrupt
NGUYEN THANH NGHIA 4
Chapter 11: Interrupt
1. Introduction
 Interrupt chematic

NGUYEN THANH NGHIA 5


Chapter 11: Interrupt
1. Introduction
1.1 Interrupt system registers
 When an interrupt request arrives, it doesn’t mean that an interrupt
will automatically occur, because it must also be enabled by the user
(from within the program). Because of this, there are special bits used
to enable or disable interrupts. It is easy to recognize them by the
letters IE contained in their names (stands for Interrupt Enable).
 Besides, each interrupt is associated with another bit called the
flag which indicates that an interrupt request has arrived regardless of
whether it is enabled or not. They are also easily recognizable by the
last two letters contained in their names- IF (Interrupt Flag).
NGUYEN THANH NGHIA 6
Chapter 11: Interrupt
1. Introduction
1.1 Interrupt system registers

 As seen, everything is based on a simple and efficient idea. When an


interrupt request arrives, the flag bit is set first.
 If the appropriate IE bit is not set (0), this condition will be completely
ignored. Otherwise, an interrupt occurs! If several interrupt sources are
enabled, it is necessary to detect the active one before the interrupt routine
starts execution. Source detection is performed by checking flag bits.
NGUYEN THANH NGHIA 7
Chapter 11: Interrupt
1. Introduction
1.1 Interrupt system registers

 It is important to know that the flag bits are not


automatically cleared, but by software while the interrupt
routine execution is in progress. If we neglect this detail,
another interrupt will occur immediately after returning to
the main program, even though there are no more requests
for its execution.
NGUYEN THANH NGHIA 8
Chapter 11: Interrupt
1. Introduction
1.1 Interrupt system registers

 The GIE bit enables all unmasked interrupts and disables all
interrupts simultaneously.
 The PEIE bit enables all unmasked peripheral interrupts
and disables all peripheral interrupts. This doesn’t concern
Timer TMR0 and PORTB interrupt sources.

NGUYEN THANH NGHIA 9


Chapter 11: Interrupt
1. Introduction
1.1 Interrupt system registers

 The GIE bit enables all unmasked interrupts and disables all interrupts
simultaneously.
 The PEIE bit enables all unmasked peripheral interrupts and disables all
peripheral interrupts. This doesn’t concern Timer TMR0 and PORTB
interrupt sources.

NGUYEN THANH NGHIA 10


Chapter 11: Interrupt
1. Introduction
1.2 INTCON register

• GIE - Global Interrupt Enable bit - controls all possible


interrupt sources simultaneously.
 1 - Enables all unmasked interrupts.
 0 - Disables all interrupts.
• PEIE - Peripheral Interrupt Enable bit acts similar to the
GIE it, but controls interrupts enabled by peripherals.
 1 - Enables all unmasked peripheral interrupts.
 0 - Disables all peripheral interrupts.
NGUYEN THANH NGHIA 11
Chapter 11: Interrupt
1. Introduction
1.2 INTCON register
• INTE - RB0/INT External Interrupt Enable bit controls
interrupt caused by changing the logic state of the RB0/INT
input pin (external interrupt).
 1 - Enables the INT external interrupt.
 0 - Disables the INT external interrupt.
• RBIE - RB Port Change Interrupt Enable bit. When
configured as inputs, PORTB pins may cause an interrupt by
changing their logic state . This bit determines whether an
interrupt is to occur or not.
 1 - Enables the port B change interrupt.
 0 - Disables the port B change interrupt.

NGUYEN THANH NGHIA 12


Chapter 11: Interrupt
1. Introduction
1.2 INTCON register
• T0IF - TMR0 Overflow Interrupt Flag bit registers the timer TMR0
register overflow, when counting starts at zero.
 1 - TMR0 register has overflowed
 0 - TMR0 register has not overflowed.
• INTF - RB0/INT External Interrupt Flag bit registers the change of the
RB0/INT pin logic state.
 1 - The INT external interrupt has occurred
 0 - The INT external interrupt has not occurred.
• RBIF - RB Port Change Interrupt Flag bit registers any change of logic
state of some PORTB input pins.
 1 - At least one of the PORTB general purpose I/O pins has changed
state.
 0 - None of the PORTB general purpose I/O pins has changed the state.

NGUYEN THANH NGHIA 13


Chapter 11: Interrupt
1. Introduction
1.3 PIE1 register

• ADIE - A/D Converter Interrupt Enable bit.


 1 - Enables the ADC interrupt.
 0 - Disables the ADC interrupt.
• RCIE - EUSART Receive Interrupt Enable bit.
 1 - Enables the EUSART receive interrupt.
 0 - Disables the EUSART receive interrupt.
NGUYEN THANH NGHIA 14
Chapter 11: Interrupt
1. Introduction
1.3 PIE1 register
• TXIE - EUSART Transmit Interrupt Enable bit.
 1 - Enables the EUSART transmit interrupt.
 0 - Disables the EUSART transmit interrupt.
• SSPIE - Master Synchronous Serial Port (MSSP) Interrupt
Enable bit - enables an interrupt request upon each data
transmission via SPI or I2C mode.
 1 - Enables the MSSP interrupt.
 0 - Disables the MSSP interrupt.
• CCP1IE - CCP1 Interrupt Enable bit enables an interrupt
request for PWM signal processing in CCP1.
 1 - Enables the CCP1 interrupt.
 0 - Disables the CCP1 interrupt.
NGUYEN THANH NGHIA 15
Chapter 11: Interrupt
1. Introduction
1.3 PIE1 register
• MR2IE - TMR2 to PR2 Match Interrupt Enable bit
 1 - Enables the TMR2 to PR2 match interrupt.
 0 - Disables the TMR2 to PR2 match interrupt.
• TMR1IE - TMR1 Overflow Interrupt Enable
bit enables an interrupt request to be generated upon
each timer TMR1 register overflow, i.e. when the
counting starts from zero.
 1 - Enables the TMR1 overflow interrupt.
 0 - Disables the TMR1 overflow interrupt.

NGUYEN THANH NGHIA 16


Chapter 11: Interrupt
1. Introduction
1.4 PIR1 register

• ADIF - A/D Converter Interrupt Flag bit.


 1 - A/D conversion is completed (bit must be cleared
from within the software).
 0 - A/D conversion is not completed or has not started.
• RCIF - EUSART Receive Interrupt Flag bit.
 1 - The EUSART receive buffer is full. Bit is cleared by
reading the RCREG register.
 0 - The EUSART receive buffer is not full.
NGUYEN THANH NGHIA 17
Chapter 11: Interrupt
1. Introduction
1.4 PIR1 register
• SSPIF - Master Synchronous Serial Port (MSSP)
Interrupt Flag bit.
 1 - The MSSP interrupt conditions during data
transmit/receive have occurred. This bit must be cleared
from within the software before returning from the
interrupt service routine.
 0 - No MSSP interrupt condition has occurred.
• CCP1IF - CCP1 Interrupt Flag bit.
 1 - CCP1 interrupt condition has occurred. This bit is
not used in PWM mode. This bit must be cleared by the
software .
 0 - No CCP1 interrupt condition has occurred.
NGUYEN THANH NGHIA 18
Chapter 11: Interrupt
1. Introduction
1.4 PIR1 register
• TXIF - EUSART Transmit Interrupt Flag bit.
• 1 - The EUSART transmit buffer is empty. The bit is cleared by any
write to the TXREG register.
• 0 - The EUSART transmit buffer is full.
• TMR2IF - Timer2 to PR2 Interrupt Flag bit
• 1 - TMR2 (8-bit register) to PR2 match has occurred. This bit must
be cleared from within the software prior to returning from the
interrupt service routine.
• 0 - No TMR2 to PR2 match has occurred.
• TMR1IF - Timer1 Overflow Interrupt Flag bit
• 1 - The TMR1 register has overflowed. This bit must be cleared
from within the software.
• 0 - The TMR1 register has not overflowed.
NGUYEN THANH NGHIA 19
Chapter 11: Interrupt
1. Introduction
1.5 Interrupt main commands
enable_interrupts(interrupt)
disable_interrupts(interrupt)
Interrupt Description
GLOBAL Global interrupt
PERIPH Peripheral interrupt
INT_RB Port B interrupt
INT_RDA Receive data from EUART interrupt
INT_TIMER0 Timer 0 interrupt
INT_TIMER1 Timer 1 interrupt
INT_TIMER2 Timer 2 interrupt
NGUYEN THANH NGHIA 20
Chapter 11: Interrupt
1. Introduction
1.6 Interrupt routine
# interrupt
void name()
{
// handle interrupt requirement
}
Interrupt: Copy what interrupt you want to
be enabled in enable_interrupts(interrupt)
then place is behind “#”
NGUYEN THANH NGHIA 21
Chapter 9: TIMER - COUNTER
2. Application of Interrupt
2.1 Application of T1 interrupt Application 1
 Control 8 LEDs by using PIC 16F887 with
TIMER1. In which the delay time is 210ms.
 Schematic:

NGUYEN THANH NGHIA 22


Chapter 9: TIMER - COUNTER
2. Application of Interrupt
2.1 Application of T1 interrupt Application 1
 Control 8 LEDs by using PIC 16F887 with
TIMER1. In which the delay time is 210ms.
 Flowchart:

NGUYEN THANH NGHIA 23


Chapter 9: TIMER - COUNTER
2. Application of Interrupt
2.1 Application of T1 interrupt Application 1
#INCLUDE<TV_16F887.C>
 Code: UNSIGNED INT8 X;
#int_timer1
void interrupt_timer1()
{
X=~X;
OUTPUT_D(X);
}
VOID MAIN()
{
SET_TRIS_D(0x00);
X=0X00; OUTPUT_D(X);
SETUP_TIMER_1(T1_INTERNAL | T1_DIV_BY_8);
SET_TIMER1(0);
ENABLE_INTERRUPTS(GLOBAL);
ENABLE_INTERRUPTS(INT_TIMER1);
WHILE(TRUE){ }
}
NGUYEN THANH NGHIA 24
Chapter 9: TIMER - COUNTER
2. Application of Interrupt
2.1 Application of T1 interrupt Application 1
 Explanation:
 Initialize timer1 to count internal pulses of 20MHz through
the divider 4, so frequency pulse is 5MHz. Using a prescaler
with a divider ratio of 8, frequency pulse is 5MHz/8 =
0.625MHz or equal to 625kHz, the period is 1.6µs.
 If the Timer1 is overflow then interrupt is occurred. In
interrupt routine, the value X is reversed and it is outputto
portD.

NGUYEN THANH NGHIA 25


Chapter 9: TIMER - COUNTER
2. Application of Interrupt
2.1 Application of T1 interrupt Application 1
 Explanation:
 Timer T1 counts 65536, the counting time is equal to the
number of pulses multiplied by the cycle each pulse: pulse ×
Txung = 65536 × 1.6µs = 104857.6 µs ≈ 105ms, so the light
on and off time of 8 LEDs are equal to 105ms or cycle equal
to 210ms.

NGUYEN THANH NGHIA 26


Chapter 9: TIMER - COUNTER
2. Application of Interrupt
2.1 Application of T1 interrupt Application 2
 The exactly seconds counter program uses
Timer1 interrupt. The counter value is
displayed on 2 seven segment LEDs with scans
mode using PIC 16F887 microcontroller.

NGUYEN THANH NGHIA 27


Chapter 9: TIMER - COUNTER
2. Application of Interrupt
2.1 Application of T1 interrupt Application 2
 Schematic:

NGUYEN THANH NGHIA 28


Chapter 9: TIMER - COUNTER
2. Application of Interrupt
2.1 Application of T1 interrupt Application 2
 Flowchart:

NGUYEN THANH NGHIA 29


Chapter 9: TIMER - COUNTER
2. Application of Interrupt
2.1 Application of T1 interrupt Application 2
VOID MAIN()
 Code: {
SET_TRIS_B(0x00); SET_TRIS_D(0x00);
#INCLUDE<TV_16F887.C> SETUP_TIMER_1(T1_INTERNAL |
UNSIGNED INT8 GIAY, BDT; T1_DIV_BY_8);
#int_timer1 SET_TIMER1(3036);
void interrupt_timer1() ENABLE_INTERRUPTS(GLOBAL);
{ ENABLE_INTERRUPTS(INT_TIMER1);
SET_TIMER1(3036); GIAY = 0; BDT=0;
BDT++; WHILE(TRUE)
} {
VOID HIENTHI() IF (BDT >=10)
{ {
OUTPUT_B(MA7DOAN[GIAY %10]); BDT = BDT-10;
OUTPUT_LOW(PIN_D0); IF (GIAY == 59) GIAY = 0;
DELAY_MS(1); OUTPUT_HIGH(PIN_D0); ELSE GIAY++;
OUTPUT_B(MA7DOAN[GIAY/10]); }
OUTPUT_LOW(PIN_D1); ELSE HIENTHI();
DELAY_MS(1); OUTPUT_HIGH(PIN_D1); }
} }

NGUYEN THANH NGHIA 30


Chapter 9: TIMER - COUNTER
2. Application of Interrupt
2.1 Application of T1 interrupt Application 2
 Explanation:
 This program initializes the timer T1 with 3036. Timer
counts to 62500 pulses then it is overflow. The number of
pulses count is 62500 pulses, each pulse is 1.6µs so the
period is 62500 ×1,6µs = 100.000µs = 100ms.
 When T1 interrupt is occurred, the BDT is increased with 1.
 When the BDT is 10 or more than 10, the second is
increased with 1 and the BDT = BDT – 10.

NGUYEN THANH NGHIA 31


Chapter 9: TIMER - COUNTER
2. Application of Interrupt
2.1 Application of T1 interrupt Application 2
 Explanation:
 You can calculate the period time as the following: the
delay time is 100ms or 100,000µs, each pulse cycle is 1.6µs.
Then the number of pulses counted by the delay time
divided by the pulse cycle. It means that number of pulse
is 100,000µs / 1.6µs = 62500 pulses. It matches with the 16
bit value of Timer1.
 If the number of pulses is greater than the number of the
timer bits, we must reduce the count time.
NGUYEN THANH NGHIA 32
Chapter 9: TIMER - COUNTER
2. Application of Interrupt
2.2 Application of ADC interrupt
 The temperature measurement program uses LM35
sensor. In this circuit, the PIC 16F887
microcontroller's ADC interrupt is used when the
ADC conversion is complete. The measurement
results is displayed on 3 seven segments LED
connected the scanning method. In addition, this
program is also used reference voltage VDD and VSS.

NGUYEN THANH NGHIA 33


Chapter 9: TIMER - COUNTER
2. Application of Interrupt
2.2 Application of ADC interrupt
 Schematic:

NGUYEN THANH NGHIA 34


Chapter 9: TIMER - COUNTER
2. Application of Interrupt
2.2 Application of ADC interrupt
 Flowchart:

NGUYEN THANH NGHIA 35


Chapter 9: TIMER - COUNTER
2. Application of Interrupt
2.2 Application of ADC interrupt
 Code:
#INCLUDE<TV_16F887.C>
UNSIGNED INT16 KQADC=0;
VOID MAIN()
{
UNSIGNED INT SOLANDO,MADONVI,MACHUC,MATRAM; SET_TRIS_B(0x00); SET_TRIS_D(0x00);SET_TRIS_A(0x01);
#INT_AD SETUP_ADC(ADC_CLOCK_DIV_2);
void interrupt_ADC() SETUP_ADC_PORTS(SAN0);
{ SET_ADC_CHANNEL(0); SOLANDO=0;
SOLANDO++; ENABLE_INTERRUPTS(GLOBAL);ENABLE_INTERRUPTS(INT_AD);
KQADC=KQADC+READ_ADC(ADC_READ_ONLY); WHILE(TRUE)
} {
VOID GIAIMA() IF (SOLANDO<100)
{ {
MATRAM = MA7DOAN[KQADC/100]; HIENTHI();
MACHUC = MA7DOAN[KQADC/10 % 10]; READ_ADC(ADC_START_ONLY);
MADONVI= MA7DOAN[KQADC % 10]; }
IF (MATRAM == 0XC0) MATRAM=0XFF; ELSE
} {
VOID HIENTHI () SOLANDO=0;
{ KQADC= KQADC /2.046;
OUTPUT_B(MADONVI); OUTPUT_LOW(PIN_D0); KQADC=KQADC/100;
DELAY_MS(1); OUTPUT_HIGH(PIN_D0); GIAIMA();
OUTPUT_B(MACHUC); OUTPUT_LOW(PIN_D1); KQADC=0;
DELAY_MS(1); OUTPUT_HIGH(PIN_D1); }
OUTPUT_B(MATRAM); OUTPUT_LOW(PIN_D2); }
DELAY_MS(1); OUTPUT_HIGH(PIN_D2); }
}

NGUYEN THANH NGHIA 36


Chapter 9: TIMER - COUNTER
2. Application of Interrupt
2.2 Application of ADC interrupt
 Explanation:
 The "READ_ADC (ADC_START_ONLY);"
command: this function is ADC convertion enable.
 The ADC interrupt will be occurred when the ADC
convertion is finished. the ADC interrupt subroutine will
increase the measurement variable and accumulate add
the measurement results.
 The "READ_ADC (ADC_READ_ONLY);“ command:
this function is to read the conversion results.
NGUYEN THANH NGHIA 37
Chapter 11: Interrupt
2. Application
2.3 Application of both Timer1 and ADC interrupt
 This program is to measure temperature and
count seconds. The seconds is counted using
Timer1 interrupt and display on 2 7-segment
leds. The temperature is measured using LM35
sensor. The ADC interrupt of PIC 16F887
microcontroller is used. In addition, display
measurement results on 3 7-segment leds
connected by scanning method, using reference
voltage VDD and VSS.
NGUYEN THANH NGHIA 38
Chapter 11: Interrupt
2. Application
2.3 Application of both Timer1 and ADC interrupt
 Schematic:

NGUYEN THANH NGHIA 39


Chapter 11: Interrupt
2. Application
2.3 Application of both Timer1 and ADC interrupt
 Flowchart:

NGUYEN THANH NGHIA 40


Chapter 11: Interrupt
2. Application
2.3 Application of both Timer1 and ADC interrupt
#INCLUDE<TV_16F887.C> MACHUC = MA7DOAN[KQADC/10 % 10];
UNSIGNED INT16 KQADC=0;  Code:
UNSIGNED INT GIAY,BDT,SOLANDO;
MADONVI= MA7DOAN[KQADC % 10];
IF (MATRAM == 0XC0) MATRAM=0XFF;
UNSIGNED MADONVI,MACHUC,MATRAM; }
#int_timer1 VOID HIENTHI ()
void interrupt_timer1() {
{ OUTPUT_B(MADONVI); OUTPUT_LOW(PIN_D0);
SET_TIMER1(3036); DELAY_MS(1); OUTPUT_HIGH(PIN_D0);
BDT++; OUTPUT_B(MACHUC); OUTPUT_LOW(PIN_D1);
IF(BDT>=10) DELAY_MS(1); OUTPUT_HIGH(PIN_D1);
{ OUTPUT_B(MATRAM); OUTPUT_LOW(PIN_D2);
BDT = BDT-10; DELAY_MS(1); OUTPUT_HIGH(PIN_D2);
IF (GIAY == 59) GIAY = 0; OUTPUT_B(MA7DOAN[GIAY %10]); OUTPUT_LOW(PIN_D3);
ELSE GIAY++; DELAY_MS(1); OUTPUT_HIGH(PIN_D3);
} OUTPUT_B(MA7DOAN[GIAY/10]); OUTPUT_LOW(PIN_D4);
} DELAY_MS(1); OUTPUT_HIGH(PIN_D4);
#INT_AD }
void interrupt_ADC() VOID MAIN()
{ {
SOLANDO++; SET_TRIS_B(0x00); SET_TRIS_D(0x00); SET_TRIS_A(0x01);
KQADC=KQADC+READ_ADC(ADC_READ_ONLY); SETUP_ADC(ADC_CLOCK_DIV_2);
} SETUP_ADC_PORTS(SAN0);
VOID GIAIMA() SET_ADC_CHANNEL(0);
{ SETUP_TIMER_1(T1_INTERNAL | T1_DIV_BY_8);
MATRAM = MA7DOAN[KQADC/100]; SET_TIMER1(0);

NGUYEN THANH NGHIA 41


Chapter 11: Interrupt
2. Application
2.3 Application of both Timer1 and ADC interrupt
 Code: ENABLE_INTERRUPTS(GLOBAL);
ENABLE_INTERRUPTS(INT_AD);
ENABLE_INTERRUPTS(INT_TIMER1);
SOLANDO=0; GIAY = 0; BDT=0;
WHILE(TRUE)
{
IF (SOLANDO<100)
{
HIENTHI();
READ_ADC(ADC_START_ONLY);
}
ELSE
{
SOLANDO=0;
KQADC= KQADC /2.046;
KQADC=KQADC/100;
GIAIMA();
KQADC=0;
}
}
}

NGUYEN THANH NGHIA 42


Chapter 11: Interrupt

The end!

NGUYEN THANH NGHIA 43

You might also like