How to Write Codes: PIC Tutorial 11

We dealt with a lot of basics within the last tutorial,
therefore I feel the time has come that we composed
our first program.

PIC Tutorial - 10

The program we will write would count the quantity of occasions we turn a switch on, and then exhibit the number. The program would count from 0 to 9, viewable on 4 LEDs in binary form, along with the input or interrupt will likely be on RB0. The number one thing we must conduct is inform the PIC to leap over the address in which the Program Counter points to whenever an interrupt takes place. You will observe that We are employing an unique method of exhibiting hexadecimal numbers. Before I happened apply F9h in which h indicated hexadecimal. We could write this as 0xF9, which is the structure we are going to employ from now on.

Now we need to tell the PIC that we are going to use interrupts, and we are
using RB0 pin 6 as an interrupt pin:
bsf INTCON,7;GIE – Global interrupt enable (1=enable)
bsf INTCON,4;INTE - RB0 interrupt enable (1=enable)
I am going to clear the interrupt flag just in case (I never trust anything!)
bcf INTCON,1;INTF - Clear flag bit just in case

Currently we must establish our 2 ports. Keep in mind that
as we are now utilizing RB0 as an interrupt pin, this needs to be established as an input:

We are going to use a variable called COUNT to store the number of switch
counts. We could just simply increment the value on Port A, but you will see why I am
using a variable when we write our interrupt routine.

Therefore, our principal program is composed, and at this point we must inform the PIC how to proceed whenever an interrupt takes place. Within this example, our interrupt will probably be the switch. Just what we would like the PIC to is one to the adjustable COUNT every time the switch is confined. Nevertheless, we just wish to show the how many occasions the switch shuts from 0 to 9. Above, I stated we might be able to have simply incremented the value on Port A every time there is an interrupt. However, Port A has 5 bits, in case we simply incremented the port, we are going to possess the highest count of 31. There are a couple of explanations why I selected not to move up to 31.

Initially, we will employ a 7-segment screen, which could at the most only go from 0 to 15 (0 to F in hex). Next, I additionally wish to show you a few of the arithmetic commands which you stumbled on in the past few lessons. Therefore we will continue with our interrupt routine. Currently the firstly we must accomplish is briefly store the details of our w register, since we have been applying this to shift the contents of COUNT to PORTA. In case we don’t save it, in that case we might be able to deliver a totally different number because of our arithmetic. Therefore let’s accomplish that first:

At this point we we understand if the value of COUNT is 9 or
more. Just what we need to accomplish now is if COUNT is more than 9, place it back to 0, or else return to the main program to
ensure that we are able to deliver it to Port A. The BTFSS command since you understand
would the subsequent
instruction in case the carry flag is scheduled i.e COUNT = 10:

The only thing which is remaining to do now is enter collectively as well as
determine values to our constants, which we are able to perform right at the start of our program.
Each time you activate the switch on, the LEDs are going to count up in binary
from 0000 to 1010
then back to 0000.

The following figure shows the circuit diagram compatible with the above explained code. Interestingly you will find that the timing capacitor has been included in the design. This is nice little ploy through which you get the freedom of avoiding the inclusion of the capacitor in case you don't have any with you during that time.
Here the capacitance comes into play via the stray capacitance across the oscillator pin and ground.
Of course it might not seem to be a very intelligent way of avoiding a capacitor practically since the stray value might vary with different given conditions.
Another section which can be witnessed in the circuit is the denouncing network across the switch. This prevents interference while mechanical switching and prevents the PIC from getting confused if the switching was a single toggle or multiple toggles.

2 thoughts on “How to Write Codes: PIC Tutorial 11

  1. Have questions? Please feel free to post them through comments! Comments will be moderated and solved ASAP.
  2. hi sir ! I'm also reading about arduino though i don't have much exposure to higher level of programming. Your article is really helpful. You are very generous of giving us the basics and the foundation of understanding the next level of learning. Thanks for that.
    My concern about going deeper about Arduino is getting stucked in the middle of doing the project. But I want to learn how to implement Arduino in a practical way, that is why this topic still interests me.

Leave a Comment