الجمعة، 10 يوليو 2015

Understanding Subroutines: PIC Tutorial 4

A subroutine is an integral part of code, or program, that may be referred as and when you may need
it. Subroutines are employed in cases where you are accomplishing the identical function frequently.




The benefits of employing a subroutine are that it will likely be simpler to modify the value once inside a subroutine instead of, say, ten times all through your program, as well as it contributes greatly to decrease the level of memory your program consumes inside the PIC.
We will check out a subroutine:


Initially, we need to provide our subroutine a designation, and in this situation We have selected ROUTINE. We after that type the code that we would like to conduct as normal. That is why, We have selected the delay in our flashing led program. Lastly, we conclude the subroutine by keying the RETURN instruction. To begin the subroutine from anywhere in our program, we quickly type the instruction CALL and then the subroutine designation. We will consider this in a little more depth. Once we arrive at the section of our program that CALL xxx, in which xxx is the name of our subroutine, the program leaps to anywhere the subroutine xxx is installed. The instructions inside the subroutine are carried out . Whenever the instruction RETURN is accomplished, the program leaps returning to our principal program to the instruction subsequent to our CALL xxx instruction. It is possible to call the similar subroutine several times as you would like, which explains why utilizing subroutines lessens the general duration of our program. Nevertheless, there are a couple of factors you should know of. Initially, as with our principal program, any specific constants needs to be acknowledged before you can use them. These may be possibly acknowledged within the subroutine itself, or directly at the beginning of the principal program. I propose you that you acknowledge everything at the beginning of your main program, since then you recognize that things are in an identical position. Next, one should make sure that the main program skips over the subroutine. What I imply with this is should you place the subroutine directly at the conclusion of your primary program, except if you utilize a ‘Goto’ declaration to leap off of where the subroutine is, the program would continue and implement the subroutine regardless of whether you require it to or otherwise. The PIC would not distinguish between a subroutine and the principal program. We will check out our flashing led program, however this time we are going to make use of a subroutine for the delay loop. Ideally, you will discover how much less complicated the program appears, as well as you might find how the subroutine applies practically.




Eventually, you can observe that by utilizing a subroutine for our delay loop, we might have downsized the dimensions of the program. Every time we desire a delay, possibly when the LED is on or off, we basically call the delay subroutine. At the conclusion of the subroutine, the program leads back to the line following our ‘Call’ instruction. In the illustration above, we flip the LED on. We after that contact the subroutine. The program then comes back in order that we are able to switch the LED off. We call the subroutine once more, just in case the subroutine might have completed, the program comes back and the subsequent instruction it recognizes is ‘goto Start’. For anybody that may be intrigued, our first program was 120 bytes long. Through the use of the subroutine, we could reduce our program size down to 103 bytes. This could not sound to be that fantastic, however considering the fact that we only have 1024 bytes overall inside the PIC, every small amount benefits. Within the next instructional, let us check out reading from the ports.

ليست هناك تعليقات:

إرسال تعليق