Tuesday 3 December 2013

PIC16F628A external interrupt code + Proteus simulation

This post answers the question, "How to capture a pulse to generate an interrupt in PIC16F628A" ?
Also, using PIC16 simulator (Proteus) you can verify this external interrupt code and change it according to your needs. This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the 'Downloadssection at the bottom of this page.

This PIC16F628A microcontroller tutorial provides the external interrupt code (e-g when you need to control servo motor which has position encoder, with your PIC microcontroller you can use this code). As we know, PIC16F628A microcontroller has one RB0/INT pin, this pin is used to service external interrupts in the circuit shown below.

It is assumed that you know how to blink an LED with PIC16F628A microcontroller. If you don't then please read this page first, before proceeding with this article.

The following diagram (made in Proteus) shows the PIC microcontroller circuit diagram[1].


Figure 1.   PIC16F628A external interrupt capture circuit
In the above circuit[2]
normally a low value is present on the RB0/INT pin because of the pull down resistor. But when push button is pressed then a low to high pulse on the RB0/INT pin generates the interrupt. And code is written in such a way that whenever interrupt is generated then RB4 pin is toggled.

Code

The code for the main function is shown below.


Figure 2.   Main function code for INT pin interrupt code for PIC16F628A
In the main function, firstly InitExternal_INT() function initializes interrupts on RB0/INT pin. Then RB4 pin direction is set to be an output. After that, NULL statements are executed in the while(1) loop forever. Rest of the work is done in the interrupt service routine code of RB0/INT pin.

The code for InitExternal_INT() function is shown below.



Figure 3.   InitExternal_INT function code
In the InitExternal_INT() function, RB0 pin is made input to capture the push button signal. Then global and INT pin interrupts are enabled, also INT pin is made posedge triggered.

Whenever a posedge pulse is received on the RB0/INT pin, then following code is executed (shown below).



Figure 4.   ISR code for external interrupts
In the RB0 INT pin interrupt service routine code (shown above), RB4 pin is toggled. This means that whenever you press the push button then LED attached with RB4 pin will toggle its state.

Using the code given in this post you will be able to easily handle interrupts in the PIC microcontroller. As an example, when you need to control servo motor (which has position encoder), then you can attach the output of position encoder on your PIC RB0/INT pin and capture interrupts on it. Whenever servo motor moves one position, then you will receive one interrupt. By incrementing a variable on each interrupt you can always keep track of servo motor position in real time.

You can leave your comments and suggestions in the comment section below.

Notes and References

[1]  This is just a sample circuit and code to test external interrupts on PIC16F628A. You can modify this code to meet your requirements.
[2]  In this circuit, PIC16F628A is running at 5 MIPS speed using 20 MHz external crystal.

Further Reading Suggestions

No comments:

Post a Comment