Understanding Interrupts: PIC Tutorial 10

The topic of interrupts may well gonna be the lengthiest and
toughest to go thru. You cannot find any uncomplicated method of detailing interrupts, however
with a little luck towards the end of this part you may be able to apply interrupts into your own programs.

PIC Tutorial - 9......................................PIC Tutorial - 11

We have separated the section into 2 stages. That is to enable separate the topic into sections, also to provide you with an handy plit up for easy understanding. What exactly is an interrupt? Surely, as the term indicates, an interrupt is a technique or a signal that prevents a microprocessor/microcontroller from whatever thing its performing that something different could happen. Allow me to give you an daily illustration. Think you are relaxing in your own home, conversing to another person. All of a sudden the phone sounds. You quit talking, and grab the telephone to talk to the caller. Once you have your telephone interaction, you decide to return to conversing to the individual before the telephone rang. It is possible to consider the principal routine while you chatt to somebody, the phone ringing brings about to disrupt your conversing, and the break in routine is the method of speaking on the phone. While the phone discussion comes to an end, then you go back to your primary routine of chatting. This illustration is precisely how an interrupt a processor to take action. The primary program is operating, carrying out certain function in a circuit, however when an interruption takes place the primary program halts while a different routine is performed. routine ends, the processor moves back to the primary routine just as before. The PIC possesses 4 sources of interrupt. They could be broken down into a couple of groups. Two are sources of interrupts which can be utilised outwardly to the PIC, whilst the other two are inner processes. Let me clarify the two external types here. The other two is going to be described in different tutorials once we arrive at timers and storing data. Should you check out the pin-out of the PIC, you will notice that pin 6 it is RB0/INT. At this point, RB0 is clearly Port B bit 0. The INT represents that it could as well be configures as an outside interrupt pin. Furthermore, Port B pins 4 to 7 (pins 10 to 13) may also be utilized for interrupts. Before we are able to employ the INT or another Port B pins, we must accomplish two tasks. Very first we must inform the PIC that we will utilize interrupts. Next, we must designate which port B pin we are going to be utilising as an interrupt rather than as an I/O pin. Inside the PIC you can find a register known as INTCON, and is at address 0Bh. In this register you will discover 8 bits that may be enabled or disabled. Bit 7 of INTCON is known as GIE. This is the Global Interrngupt Enable. Fixing this to 1 informs the PIC that we will employ an interrupt. Bit 4 of INTCON is known as INTE, INTerrupt Enable. Putting this bit to 1 conveys to the PIC that RB0 is going to be an interrupt pin. Configuring bit 3, called RBIE, informs the PIc that we are going to be utilizing Port B bits 4 to 7. At this point the PIC understands when this pin can be high or low, have to halt what it’s performing and proceed with an interrupt routine. At this point, we must inform the PIC whether or not the interrupt will likely be on the ascending edge (0V to +5V) or the dropping edge (+5V to 0V) transformation of the signal. Put simply, do we wish the PIC to interrupt each time the signal moves from low to high, or from high to low. By delinquency, this can be established to be placed on the rising edge. The edge ‘triggering’ is scheduled up in an additional register called the OPTION register, at address 81h. The bit we are enthusiastic about is bit 6, often referred to as INTEDG. Setting this to 1 triggers the PIC to disrupt on the mounting edge (default state) and setting it to 0 stimulates the PIC to disrupt on the sliding edge. If you would like the PIC to activate on the rising edge, then you certainly don’t have to do anything to this bit. At this point, sadly, the Option register is in Bank 1, meaning that we enjoy to modify from bank 0 to bank 1, set the bit in the Option register, after that return to bank 0. The key here is to accomplish every bit of the Bank 1 registers in a single strike, for example establishing the port pins, after that returning to Bank 0 if you are done. Fine, consequently we have notified the PIC which pin will probably be the interrupt, and where edge to trigger, what goes on in the program and the PIC any time the interrupt happens? A couple of stuff take place. Very first, a ‘flag’ is scheduled. This informs the internal processor of the PIC that an interrupt has transpired. Next, the program counter (which I talked about within the previous tutorial) tips to a specific address within the PIC. Let’s swiftly check out all these individually. Interrupt Flag In our INTCON register, bit 1 is the interrupt flag, called INTF. At this point, whenever any interrupt arises, this flag will likely be fixed to 1. When there isn’t an interrupt, the flag is placed to 0. As well as that is just about all accomplishes. At this point you may be pondering ‘what is the point?’ Surely, even though this flag is scheduled to 1, the PIC is not able to, and will not, react to another interrupt. Therefore, let’s express that we bring about an interrupt. The flag will likely be fixed to 1, and the PIC might go to our routine for working the interrupt. When this flag wasn’t fixed to 1, and the PIC was permitted to continue answering the interrupt, then continuously pulsing the pin could keep the PIC returning to the beginning of our interrupt routine, and by no means completing it. Returning to my illustration of the telephone, it’s similar to lifting the telephone, and immediately once proceeding to discuss it begins ringing yet again since another person wishes to speak with you. It is advisable to complete one dialogue, then grab the phone again to speak with the subsequent individual. You can find a small problem with this flag. Even though the PIC quickly sets this flag to 1, it doesn’t set it again 0! That activity must be exercised by the programmer – i.e. you. This can be effortlessly accomplished, since I’m certain presume, and needs to be achieved after the PIC has carried out the interrupt routine.

Memory Location

Whenever you initially power up the PIC, or in case there exists a reset, the Program Counter tips to address 0000h, that could be immedaitely at the outset of the program memory. But, in the event there is an interrupt, the Program Counter would indicate address 0004h. Therefore, while we are composing our program that will have interrupts, we firstly must inform the PIC to hop over address 0004h, and maintain the interrupt routine which begins at address 0004h discrete from the remainder of the program. This can be hassle-free to perform. Initially, we commence our program with a command known as ORG. This command indicates Origin, or start. We stick to it with an address. Since the PIC commences at address 0000h, we type ORG 0000h. After that we must bypass over address 0004h. We accomplish this by putting a GOTO instruction, accompanied by a label which tips to our primary program. We after that adhere to this GOTO command with one more ORG, this moment with the address 0004h. It will be after this command that we insert our interrupt routine. At this point, we might be able to possibly type in our interrupt routine straight following the second ORG command, or we are able to position a GOTO statement which points to the interrupt routine. It truly is related to option on your part. To inform the PIC it offers arrived at the conclusion of the interrupt routine we must position the command RTFIE towards the end of the routine. This command signifies return from the interrupt routine. While the PIC notices this, the Program Counter indicates to the final position the PIC was at before the interrupt occurred. We have established below a brief section of code to display the above:

There are a couple of stuffs you should be informed when utilizing interrupts. The initial tends to be that if you might be utilizing the identical register in your primary program and the interrupt routine, in mind that the details of the register will most likely alter when the interrupt takes place. For instance, let’s utilizing the w register to forward data to Port A primary program, therefore you can be additionally utilizing the w register in the interrupt routine to shift data from one destination to another. In case you are not cautious, the w register would include the last value it received while it had been in the interrupt routine, so when you return from the interrupt this information is going to be delivered to Port A rather than the value you possessed before the interrupt occurred.

The means around this is to momentarily save the details of the w register prior to when you utilize it once again in the interrupt routine. The second is the fact you can find a delay between when one interrupt takes place and when the subsequent one can arise. While you understand, the PIC possesses an exterior clock, which could possibly be a crystal or it could be a resistor-capacitor combo. No matter what the frequency of this clock, the PIC divides it by 4 after which employs this for it’s inner timing. For instance in case you have a 4MHz crystal linked to your PIC, in that case the PIC would perform the instructions at 1MHz. This interior timing is known as an Instruction Cycle. At this point, the data sheet claims (undoubtedly in diminutive print) that you need to enable 3 to 4 instruction rounds between interrupts. My would be to enable 4 rounds. The reason behind the delay is the PIC requires time to leap to the interrupt address, the flag, and arrive back away from the interrupt routine. Therefore, keep this in your mind if you work with an alternate circuit to activate an interrupt for the PIC. At this point, a point to is the fact that if you utilize bits 4 to 7 of Port B as an interrupt. You are unable to choose specific pins on Port B to function as an interrupt. Therefore, in case you allow these pins, they likely could be all obtainable. Therefore, for instance, you can’t simply have bits 4 and 5 – bits 6 and 7 will likely be empowered at the same time. What exactly is the purpose of getting four bits to represent an interrupt? Surely, you might have a circuit hooked up to the PIC, in case anyone of four lines go high, in that case this may be an issue that you require the PIC to influence instantly. One illustration of this could be a home security alarm, in which four sensors are linked to Port B pins 4 to 7. Any specific sensor can prompt the PIC to trigger an alarm, and the alarm signaling routine is the interrupt routine. This spares checking the ports constantly and permits the PIC to continue with different matters. Within the next tutorial, we are going to compose a program to manage an interrupt.



Leave a Comment