Difference between revisions of "6.8051 Interrupts"
(30 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:8051 tutorials]] | [[Category:8051 tutorials]] | ||
− | + | In this tutorial, we will look at 8051 Interrupts. Interrupts are useful in many cases wherein the process simply wants to continue doing its main job and other units(timers or external events) seek its attention when required. | |
− | + | In other words, the microcontroller, need not monitor the timers, the serial communication or the external pins P3.2 and P3.3. Whenever an event related to these units occur, it is informed to the microcontroller with the help of interrupts. | |
− | In this tutorial we will look at 8051 Interrupts. Interrupts are useful in many cases wherein the process simply wants to continue doing | + | |
− | In other words the microcontroller, need not monitor the timers, the serial communication or the external pins P3.2 and P3.3. Whenever an event related to these units occur, it is informed to microcontroller with the help of interrupts. | + | A single microcontroller can serve several devices by two ways: |
− | + | *<b>Polling:</b> The microcontroller continuously monitors the status of all the devices. Whenever any device needs the service,it provides the service and moves on to the next device until everyone is serviced. This will be done in an infinite loop. | |
− | ==Interrupt | + | |
− | + | *<b>Interrupts:</b> Whenever any device needs its service, the device notifies the microcontroller by sending it an interrupt signal Upon receiving an interrupt signal, the microcontroller interrupts whatever it is doing and serves the device. The program which is associated with the interrupt is called the interrupt service routine (ISR) or interrupt handler | |
− | + | ||
− | {| class=" | + | |
− | + | =What is an Interrupt?= | |
− | |- | + | An interrupt is an external or internal event to get the CPU's attention. Once the controller detects the interrupt, it suspends the current job and executes a special service routine know as Interrupt Service Routine(ISR). |
− | + | ||
+ | Upon activation of an interrupt, the microcontroller goes through the following steps | ||
+ | #First it finishes the instruction it is executing and saves the address of the next instruction (PC) on the stack. | ||
+ | #It also saves the current status of all the interrupts internally. | ||
+ | #It jumps to a fixed location in memory, called the interrupt vector table, that holds the address of the ISR. The microcontroller gets the address of the ISR from the interrupt vector table and jumps to it It starts to execute the interrupt service subroutine until it reaches the last instruction of the subroutine which is RETI (return from interrupt) | ||
+ | #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 two bytes of the stack into the PC. | ||
+ | #Then it starts to execute from that address. | ||
+ | |||
+ | =8051 Interrupt Structure= | ||
+ | |||
+ | 8051 Microcontroller has six interrupt sources as shown in the table below: | ||
+ | {| class="table table-striped table-hover table-condensed table-bordered" | ||
+ | |-class="info" | ||
+ | |Interrupt||ROM Location(Hex)||Pin||Flag Clearing||Interrupt no. in C | ||
|- | |- | ||
|Reset||0000||9||Auto||-- | |Reset||0000||9||Auto||-- | ||
Line 25: | Line 39: | ||
|Serial Com Interrupt(RI and TI)||0023||-||Program SW||4 | |Serial Com Interrupt(RI and TI)||0023||-||Program SW||4 | ||
|} | |} | ||
− | |||
*the reset vector has just 3 bytes allocated to it, meaning it can hold a jump instruction to the location where the main program is stored. | *the reset vector has just 3 bytes allocated to it, meaning it can hold a jump instruction to the location where the main program is stored. | ||
*The other interrupts have 8 bytes allocated to each of them, hence a small Interrupt service routine(ISR) can be placed here. However, if the ISR needs to larger in length, it has to placed else where and the allocated 8 bytes need to have the code that simple redirects the control to the ISR. | *The other interrupts have 8 bytes allocated to each of them, hence a small Interrupt service routine(ISR) can be placed here. However, if the ISR needs to larger in length, it has to placed else where and the allocated 8 bytes need to have the code that simple redirects the control to the ISR. | ||
Line 31: | Line 44: | ||
*''TF0'' and ''TF1'' are timer overflow interrupts for timer 0 and 1 respectively | *''TF0'' and ''TF1'' are timer overflow interrupts for timer 0 and 1 respectively | ||
*The ''Serial COM Interrupt'' can be configured to trigger upon transmit or receipt of a byte during serial communication. | *The ''Serial COM Interrupt'' can be configured to trigger upon transmit or receipt of a byte during serial communication. | ||
− | + | ||
− | It should be noted that when the MCU is reset, all the interrupts are disabled. Hence in order to use them, we should enable them. | + | =Enabling and Disabling the Interrupts= |
− | In 8051 '''Interrupt Enable(EA)''' Register is used to enable or disable the interrupt. The register is shown below: | + | It should be noted that when the MCU is reset, all the interrupts are disabled. Hence in order to use them, we should enable them. In 8051 '''Interrupt Enable(EA)''' Register is used to enable or disable the interrupt. The register is shown below: |
− | + | {| class="table table-striped table-hover table-condensed table-bordered" | |
− | {| class=" | + | |-class="info" |
− | |- | + | |EA |
− | + | ||
|- | |- | ||
− | | | + | |7||6||5||4||3||2||1||0 |
|- | |- | ||
|EA||-||ET2||ES||ET1||EX1||ET0||EX0 | |EA||-||ET2||ES||ET1||EX1||ET0||EX0 | ||
|} | |} | ||
− | + | *'''EA''': Global interrut controlle bit.<br> | |
− | *'''EA''': 0 | + | 0-Disables all interrupts.<br> |
+ | 1-Enables all interrupts.<br> | ||
+ | For all the below interrupts, setting(1) the bit enables the interrupt, 0 disables it. | ||
*'''ET2''': Timer 2 Overflow interrupt(8052) | *'''ET2''': Timer 2 Overflow interrupt(8052) | ||
*'''ES''':Serial Port Interrupt | *'''ES''':Serial Port Interrupt | ||
Line 51: | Line 65: | ||
*'''ET0''':Timer 0 overflow interrupt | *'''ET0''':Timer 0 overflow interrupt | ||
*'''EX0''':External Interrupt 0 on P3.2 | *'''EX0''':External Interrupt 0 on P3.2 | ||
− | + | ||
+ | Below image shows the Complete 8051 interrupt structure: | ||
+ | [[File:08051 Interrupts.png]] | ||
+ | |||
=Example 1: Timer Interrupts= | =Example 1: Timer Interrupts= | ||
− | + | To demonstrate use of timer interrupts, we will blink a LED1 connected to P0.0 at 50ms(using timer zero) and in the mean time we would also read the SW connected to P3.2 and display on LED2 connected to P0.1.<br/> | |
− | To demonstrate use of timer interrupts, we will blink a | + | ''Important thing to understand here is, in the main program, the switch is continuous read and displayed on the LED2, the timer keeps ticking on its own and generates interrupt every 50ms to toggle the LED2 connected to P0.0 is toggled.''<br/> |
− | ''Important thing to understand here is, in the main program, the switch is continuous read and displayed on the | + | |
− | '''In real time we should be able to see the | + | ===Timer Calculation for 50ms delay=== |
− | + | Fosc = 11.0592Mhz<br> | |
− | < | + | Delay = 50ms<br> |
− | + | $$RegValue = TimerMax-((Delay/1.085) * 10^6)$$ | |
− | + | RegValue = 65536 - (50ms/1.085)*10^6 = 65536 - 46082 = 19453 = 0x4BFD | |
− | + | ||
− | + | '''In real time we should be able to see the LED1 connected to P0.0 blink continuously and also switch status shown on LED2 P0.1.''' | |
− | + | ||
− | + | <html><script src="https://gist.github.com/SaheblalBagwan/201079572dd64bf2c97039884544d842.js"></script></html> | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | </ | + | |
=Example 2: External Interrupts= | =Example 2: External Interrupts= | ||
− | + | In this example, we will be using both the external interrupts INT0(P3.2) and INT1(P3.1). A low signal on these pins will generates the corresponding interrupts causing it to execute the respective ISR's. | |
− | In this example | + | INTO ISR will increment a counter whereas INT1 will decrement the same counter. |
− | + | The main program will just display this 8-bit interrupt counter on LED's connected to P0. | |
− | + | <html> | |
+ | <script src="https://gist.github.com/SaheblalBagwan/cc2334360d4873090b892fb523eb3b7a.js"></script> | ||
+ | </html> | ||
+ | |||
+ | |||
+ | =Example 3: Serial Interrupt= | ||
+ | In this example, we will be using the serial interrupt to receive and transmit the data. | ||
+ | The main program will just display this 8-bit ascii value of received char on the LED's connected to P0. | ||
+ | <html> | ||
+ | <script src="https://gist.github.com/SaheblalBagwan/5e294cb26cfa54daed8cca20e1c56f8d.js"></script> | ||
+ | </html> | ||
+ | |||
+ | |||
+ | =Example 4: All Interrupts= | ||
+ | In this example, we will be demonstrating the usage of all the interrupts.<br> | ||
+ | *<b>Timer0: </b>Will blink the LEDs with 1sec delay. | ||
+ | *<b>INT0: </b>Will increment a counter. | ||
+ | *<b>INT1: </b>Will Decrement the same counter. | ||
+ | *<b>Serial: </b>Pressing 'c' Will clear the same counter. | ||
+ | The main program will keep the track of the counter and transmit the same on UART. | ||
+ | <html> | ||
+ | <script src="https://gist.github.com/SaheblalBagwan/6aeb8089cb9183642b069e2c8e3a2557.js"></script> | ||
+ | </html> | ||
+ | |||
+ | =Downloads= | ||
+ | Download the sample code and design files from [https://github.com/ExploreEmbedded/8051_DevelopmentBoard this link]. | ||
− | |||
− | |||
− | + | {{DISQUS}} |
Latest revision as of 10:16, 2 September 2016
In this tutorial, we will look at 8051 Interrupts. Interrupts are useful in many cases wherein the process simply wants to continue doing its main job and other units(timers or external events) seek its attention when required. In other words, the microcontroller, need not monitor the timers, the serial communication or the external pins P3.2 and P3.3. Whenever an event related to these units occur, it is informed to the microcontroller with the help of interrupts.
A single microcontroller can serve several devices by two ways:
- Polling: The microcontroller continuously monitors the status of all the devices. Whenever any device needs the service,it provides the service and moves on to the next device until everyone is serviced. This will be done in an infinite loop.
- Interrupts: Whenever any device needs its service, the device notifies the microcontroller by sending it an interrupt signal Upon receiving an interrupt signal, the microcontroller interrupts whatever it is doing and serves the device. The program which is associated with the interrupt is called the interrupt service routine (ISR) or interrupt handler
Contents
What is an Interrupt?
An interrupt is an external or internal event to get the CPU's attention. Once the controller detects the interrupt, it suspends the current job and executes a special service routine know as Interrupt Service Routine(ISR).
Upon activation of an interrupt, the microcontroller goes through the following steps
- First it finishes the instruction it is executing and saves the address of the next instruction (PC) on the stack.
- It also saves the current status of all the interrupts internally.
- It jumps to a fixed location in memory, called the interrupt vector table, that holds the address of the ISR. The microcontroller gets the address of the ISR from the interrupt vector table and jumps to it It starts to execute the interrupt service subroutine until it reaches the last instruction of the subroutine which is RETI (return from interrupt)
- 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 two bytes of the stack into the PC.
- Then it starts to execute from that address.
8051 Interrupt Structure
8051 Microcontroller has six interrupt sources as shown in the table below:
Interrupt | ROM Location(Hex) | Pin | Flag Clearing | Interrupt no. in C |
Reset | 0000 | 9 | Auto | -- |
External HW Interrupt 0 (INT0) | 0003 | P3.2(12) | Auto | 0 |
Timer 0 Interrupt(TF0) | 000B | - | Auto | 1 |
External HW Interrupt 1 (INT1) | 0013 | P3.3(13) | Auto | 2 |
Timer 1 Interrupt(TF1) | 001B | - | Auto | 3 |
Serial Com Interrupt(RI and TI) | 0023 | - | Program SW | 4 |
- the reset vector has just 3 bytes allocated to it, meaning it can hold a jump instruction to the location where the main program is stored.
- The other interrupts have 8 bytes allocated to each of them, hence a small Interrupt service routine(ISR) can be placed here. However, if the ISR needs to larger in length, it has to placed else where and the allocated 8 bytes need to have the code that simple redirects the control to the ISR.
- INT0 and INT1 are external interrupts on P3.2 and P3.3 respectively. These can be configured to be low level triggered or edge triggered interrupt sources.
- TF0 and TF1 are timer overflow interrupts for timer 0 and 1 respectively
- The Serial COM Interrupt can be configured to trigger upon transmit or receipt of a byte during serial communication.
Enabling and Disabling the Interrupts
It should be noted that when the MCU is reset, all the interrupts are disabled. Hence in order to use them, we should enable them. In 8051 Interrupt Enable(EA) Register is used to enable or disable the interrupt. The register is shown below:
EA | |||||||
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
EA | - | ET2 | ES | ET1 | EX1 | ET0 | EX0 |
- EA: Global interrut controlle bit.
0-Disables all interrupts.
1-Enables all interrupts.
For all the below interrupts, setting(1) the bit enables the interrupt, 0 disables it.
- ET2: Timer 2 Overflow interrupt(8052)
- ES:Serial Port Interrupt
- ET1:Timer 1 overflow interrupt
- EX1:External Interrupt 1 on P3.3
- ET0:Timer 0 overflow interrupt
- EX0:External Interrupt 0 on P3.2
Below image shows the Complete 8051 interrupt structure:
Example 1: Timer Interrupts
To demonstrate use of timer interrupts, we will blink a LED1 connected to P0.0 at 50ms(using timer zero) and in the mean time we would also read the SW connected to P3.2 and display on LED2 connected to P0.1.
Important thing to understand here is, in the main program, the switch is continuous read and displayed on the LED2, the timer keeps ticking on its own and generates interrupt every 50ms to toggle the LED2 connected to P0.0 is toggled.
Timer Calculation for 50ms delay
Fosc = 11.0592Mhz
Delay = 50ms
$$RegValue = TimerMax-((Delay/1.085) * 10^6)$$
RegValue = 65536 - (50ms/1.085)*10^6 = 65536 - 46082 = 19453 = 0x4BFD
In real time we should be able to see the LED1 connected to P0.0 blink continuously and also switch status shown on LED2 P0.1.
Example 2: External Interrupts
In this example, we will be using both the external interrupts INT0(P3.2) and INT1(P3.1). A low signal on these pins will generates the corresponding interrupts causing it to execute the respective ISR's. INTO ISR will increment a counter whereas INT1 will decrement the same counter. The main program will just display this 8-bit interrupt counter on LED's connected to P0.
Example 3: Serial Interrupt
In this example, we will be using the serial interrupt to receive and transmit the data. The main program will just display this 8-bit ascii value of received char on the LED's connected to P0.
Example 4: All Interrupts
In this example, we will be demonstrating the usage of all the interrupts.
- Timer0: Will blink the LEDs with 1sec delay.
- INT0: Will increment a counter.
- INT1: Will Decrement the same counter.
- Serial: Pressing 'c' Will clear the same counter.
The main program will keep the track of the counter and transmit the same on UART.
Downloads
Download the sample code and design files from this link.