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

Understanding Data Tables: PIC Tutorial 9

There exists a great option in the training set that permits you to make use of a data table.
A data table is just a list of data quotes, in which all is looked over based on a few considerations.


For instance, you could have a circuit that utilizes a PIC which counts the quantity of instances an input pin becomes high in 1 second. After that you can exhibit the number on a 7 segment display. As soon as the timing has launched, the PIC starts counting the quantity of occasions the pin goes high. After 1 second it visits the table and glances up the data it must display the number on the display that symbolizes the amount of situations the pin got high. This can be beneficial, since we don’t determine what the figure could be until the PIC has accomplished its estimate. By utilizing a table, we are able to allow the PIC determine which figure to portray. At this point, before I continue to show you how the data table functions, I might have to tell you that the PIC maintains path of whereabouts in the program it is whilst the program is operating. It facilitates for those who have performed certain programming in BASIC. Otherwise, don’t be anxious, you might want to continue to learn about the theory. Envision there is a BASIC program similar to the one presented below:

10               LET K=0
11               K=K+1
12               IF K>10 THEN GOTO 20 ELSE GOTO 11
20                     PRINT K
21                     END

The program begins at line 10. As soon as K is scheduled to 0, it next advances to line 11. After we have included 1 to K we after that proceed to line 12. At this point we might be curious if K is higher than 10. In case it is, next we head to line 20, or else we return to line 11. Line 20 documents the K, and line 21 concludes the program. BASIC employs line statistics to assist the programmer keep a record of where issues are, as labels are not authorized. The PIC employs labels to escape between destinations – or can it really? We utilize the labels to ensure that we be aware of where issues are, as well as to ensure we are able to inform the PIC in a simple way where to search. Precisely what occurs is the PIC takes advantage of an inner line counter called a Program Counter. The Program Counter (abbreviated to PC) trail of the memory destination of where the present instruction is. Whenever we inform the PIC to visit a selected label, it understands the memory spot and therefore augments the PC until it sees that memory destination. This is precisely the same method as we check out the BASIC program above. Below is a segment of code, with the memory spaces, or the items of the PC, beside every instruction:

PC Instruction

0000             movlw 03
0001             movwf 0C
0002  Loop    decfsc 0C
0003              goto Loop
0004  end

In the demonstration above, We have fixed the PC to 0000. On this we have the instruction movlw 03. When the PIC has implemented this data, it increments the PC in order that the subsequent instruction is scanned. At this point the PIC views movwf 0C. The PC is incremented yet again. Now the PIC studies decfsc 0C. In case the details of 0C are not 0, in that case the PC is incremented by 1, as well as the following instruction, goto Loop, informs the PC to return to position 0003, which there is the said Loop. In case the details of 0C is 0, then the PC is advised to increment by 2, put simply omit the subsequent instruction. This places the PC at position 0004, wherein the program finishes. The destinations are fixed by the assembler, and we don’t generally ought to be concerned what the PC is accomplishing. Until, we find the need to bring it under control just like while we do when utilizing data tables. The most convenient way to describe how a data table functions, is to begin with an illustration.

PC equ  02
movlw  03
call        table
             :
table      addwf PC
retlw      01
retlw      02
retlw      03
retlw      04
retlw      05
retlw      06
retlw      07
return
The initial instruction is allocating the label PC with the address of the Program Counter (02h). We will be soon after putting the value of 03h into the w register. We after that communicate to table. The foremost line in the subroutine table augments the details of the W register (03h) to the program counter. This triggers the program counter to raise by 3, or to put it a different way, stimulates the program counter to proceed down 3 lines. While the counter arrives 3 lines down it the PIC recognizes the instruction retlw. This command sends the value following it into the W register, after which comes back from the subroutine. RETLW basically signifies Return, Literal to W. See I placed a comma after the word Return. Since we are in a subroutine, we require a Return instruction to surface of it. Therefore the RET in the instruction. After the RETLW instruction is a number, and this is exactly what is put into the W register. In this instance it is the figure 3. We could designate any quantity to the W register, so long as this figure is combined with the Program Counter in the table subroutine, we are going to discover a retlw instruction. In the above illustration this implies we are able to possess any number from 1 to 7. In case we proceed past the subroutine, we might be able to finish up performing an additional section of the program. For this reason, it is usually a smart move to place the data table exactly towards the end of the PIC program, therefore if we do overshoot in that case we are going to arrive at the conclusion of the program anyhow.

Understanding Bit Operations: PIC Tutorial 8

Bit functions permit us to shape a single bit within a expression. They permit us to proceed,
set and get rid of single bits in registers or numbers that we stipulate.


PIC Tutorial - 7......................................PIC Tutorial - 9
At the conclusion of this course We will disclose a program designed to create a set of sequencing lights that proceed forward, then the reverse way. We observed this accomplished earlier when we examined the exclusive OR function, wherein we Exclusively ORed the ports with a expression. We have uptil now noticed a few bit functions when we establish the ports on the PIC, and

Let me reiterate their utilization here.
BCF

This instruction will wipe of a bit that we stipulate in a register that we designate. The syntax
is:
BCF      <register>,<bit>

We employed this earlier to alter from page 1 to page 0 by removing a bit in the STATUS register. We are able to likewise use it to fix a bit to 0 in any different register/location. For instance, in case we wished to set the 3rd bit in 11001101 saved in section 0C to 0, we might insert:

BCF 0C,03

BSF

This instruction would fix any bit we stipulate to 1 in any register that we indicate. We utilized this earlier to proceed from Page 0 to Page 1. The syntax is: BSF <register>,<bit>, and is utilized in precisely the same method as BCF above.

BTFSC

Up to now we could set or clear a bit in a register. However imagine if we need to basically check if a bit is a 1 or a 0 in a register? Surely, it is possible to use BTFSC. It states Bit Test Register F, and Skip If It Is Clear. This instruction is going to analyze the bit we designate in the register. In case the bit is a 0, the instruction would inform the PIC to by pass the subsequent instruction. We might utilize this instruction in case we wished to check a flag, for example the carry flag. This spares us needing to read the STATUS register and searching for the individual bits to learn which flags are fixed. 29 For instance, in case we wished to check if the Carry flag was set to 1 after we had added 2 figures, then we could type the following:

BTFSC           03h,0
carry on here if set to 1
or here if set to 0

In case the status of the bit is a 1, in that case the instruction subsequent to BTFSC would be completed. In case it is set to a 0, in that case the subsequent instruction is skipped. The following part of code exhibits in which it might be employed:
Loop    :
            :
            :
            BTFSC 03,0
            Goto Loop

In the above code, the PIC will simply get out of the loop in case bit 0 of the STATUS register (or the Carry flag) is defined to 0. Or else, the goto command would be conducted.

BTFSS

This instruction states Bit Test Register F, And Skip If Set. This can be comparable to the BTFSC instruction, apart from that the PIC would omit the subsequent instruction if the bit we have been evaluating is set to 1, instead of 0.

CLRF

This instruction would fix the whole details of a register to 0. The syntax is:

CLRF <register>
We employed this earlier to set the output of the Ports to 0, by applying CLRF 85h. We furthermore employed it to fix the Ports to include all pins to output by utilizing CLRF 05h.

CLRW

This could be resembling the CLRF instruction, except for clears the W register. The syntax is pretty simply:

CLRW

RLF And RRF

These directions would transport a bit in a register a single slot to the left (RLF) or the right (RRF) in a register. For instance, if we needed 00000001 and we employed RLF, in that case we might possess 00000010. At this point, what goes on in case there is 10000000 and applied the RLF instruction? Surely, the 1 would be positioned in the carry flag. In case we applied the RLF instruction once more, the 1 would reappear back at the start. The alike occurs, however in opposite, for the RRF instruction. The case in point below shows this for the RLF instruction, in which We have can see the 8 bits of a register, as well as the carry flag:
                            C 87654321
                            0 00000001
RLF                     0 00000010
RLF                     0 00000100
RLF                     0 00001000
RLF                     0 00010000
RLF                     0 00100000
RLF                     0 01000000
RLF                     0 10000000
RLF                     1 00000000
RLF                     0 00000001

Example Program

We are now gonna see an example code that one can compile and drive. It would generate a sequencing light beginning at PortA bit 0, going to PortB bit 8 and then returning.
Hook up LEDs to each one of the Port pins. We will have some of the bit procedures pointed out in this tutorial.

TIME EQU 9FH             ; Variable for the delay loop.
PORTB EQU 06H           ; Port B address.
TRISB EQU 86H             ; Port B Tristate address.
PORTA EQU 05H           ; Port A address.
TRISA EQU 85H             ; Port A Tristate address.
STATUS EQU 03H         ; Page select register.
COUNT1 EQU 0CH       ; Loop register.
COUNT2 EQU 0DH       ; Loop register.

BSF STATUS,5            ; Go to page 1
MOVLW 00H                ; and set up
MOVWF TRISB            ; both Ports A and B
MOVLW 00H                ; to output,
MOVWF TRISA            ; then return to
BCF STATUS,5             ; page 0.
MOVLW 00H                 ; Clear Port A.
MOVWF PORTA           ;

    ; Start of main program

RUN
MOVLW 01H              ; Set the first bit
MOVWF PORTB       ; on Port B.
CALL DELAY              ; Wait a while
CALL DELAY               ;

; Move the bit on Port B left, then pause.

RLF PORTB,1
CALL DELAY
CALL DELAY
RLF PORTB,1
CALL DELAY
CALL DELAY
RLF PORTB,1
CALL DELAY
CALL DELAY
RLF PORTB,1
CALL DELAY
CALL DELAY
RLF PORTB,1
CALL DELAY
CALL DELAY
RLF PORTB,1
CALL DELAY
CALL DELAY
RLF PORTB,1
CALL DELAY
CALL DELAY
RLF PORTB,1           ; This moves the bit into the carry flag

; Now move onto Port A, and move the bit left.

RLF PORTA,1            ; This moves the bit from the zero flag into PortA

CALL DELAY
CALL DELAY
RLF PORTA,1
CALL DELAY
CALL DELAY
RLF PORTA,1
CALL DELAY
CALL DELAY
RLF PORTA,1
CALL DELAY
CALL DELAY

; Move the bit back on Port A

RRF PORTA,1
CALL DELAY
CALL DELAY
RRF PORTA,1
CALL DELAY
CALL DELAY
RRF PORTA,1
CALL DELAY
CALL DELAY
RRF PORTA,1             ; This moves the bit into the zero flag

; Now move the bit back on Port B

RRF PORTB,1
CALL DELAY
CALL DELAY
RRF PORTB,1
CALL DELAY
CALL DELAY
RRF PORTB,1
CALL DELAY
CALL DELAY
RRF PORTB,1
CALL DELAY
CALL DELAY
RRF PORTB,1
CALL DELAY
CALL DELAY
RRF PORTB,1
CALL DELAY
CALL DELAY
RRF PORTB,1
CALL DELAY
CALL DELAY             ; Now we are back where we started,
                                     ;
GOTO RUN                 ; let's go again.

Understanding Logical and Arithmetic Operators: PIC Tutorial 7

Below, we will analyze how you can wiggle individual bits, carry out certain straightforward
arithmetic, as well as data tables.

Logical Managers

Within the last tutorial I presented the Exclusive OR operation. The ExOR function is understood as a logical operator. Within this tutorial I will enlighten the additional logical operators that the PIC promotes. There won’t be any kind of case in point programs, however We will learn easy methods to use the operators by applying small areas of code. AND The AND function basically analyzes two bits and delivers a 1 whether they are the same, and a 0 in case they are distinctive. For instance, if we mentioned 1 AND 1, the outcome is 1, while in case we declared 1 AND 0 the consequence would be 0. Needless to say, we are able to evaluate words also, as well as all of the the AND function accomplishes is review the two terms bit by bit. The instance below demonstrates two 8-bit words becoming ANDed together with the product:

              11001011
AND      10110011
Equals     10000011

I hope you agree, the outcome will simply possess a 1 whenever 2 1s hand in hand with one another in the a pair of words. We are able to utilize the AND function to verify the ports, for instance. In case we are checking a few I/O pins that are linked to a circuit, and we should keep an eye on a particular situation in which only a few of the pins are high, in that case we are able to pretty much read the port, after which AND the outcome with the condition we have been examining for, identical to the instance above. The PIC provides us two ingredients for AND.
They are ANDLW and ANDWF. ANDLW permits us to carry out an AND function with the details of the W register, and a amount that we stipulate.
The syntax is: ANDLW <number> wherein <number> is exactly what we are going to AND the contents of W with. The consequence of the AND function would be stored directly into the W register.
ANDWF permits us to carry out an AND function on the W register and a different register, for example a PORT. The syntax is: ANDWF <register>,d in which <register> is the register we are enthusiastic about, e.g. PORTA, and d shows the PIC where you should position the result. If d=0, the outcome is put in the W register, and of d=1 the end result is saved in the register we stipulated. The two parts of code below display a good example of each AND function. The initial is examining the status of the PORTA, in which we need to check if the inputs are 1100. We can place the outcome back into the W register

movlw         1100
ANDWF     05h,0

The second illustration might now verify the contents of the W register:
ANDLW     1100

OR

We have by now discovered one OR function, to be precise the XOR. This develops into a 1 if two bits are not the same, but are different. You can find another OR function called IOR, which is the inclusive OR. This function will generate a 1 in case either bit is a 1, but additionally if each bits are 1. Below is a clear-cut truth table to illustrate this:
A   B   O/P
0    0     0
0    1     1
1    0     1
1    1     1
Arithmetic Operators

ADD 

This function accomplishes what usually it claims. It contributes two figures! In case the consequence of adding the two figures surpasses 8 bits, in that case a CARRY flag will probably be set. The CARRY flag is situated at address 03h bit 0. When this bit is scheduled, then the two figures surpassed 8 bits. When it is a 0, in that case the consequence is located within 8 bits. As before, the PIC delivers us two styles of ADD, specifically ADDLW and ADDWF. As you might have assumed, this is quite like the above function. ADDLW offers the contents of the W register to that we stipulate. The syntax is: ADDLW <number> ADDWF add the contents of the W register and some other register that we designate. The syntax is: ADDWF <register>,d is where <register is the register we specify and d tells the PIC where to place the outcome. If d=0, the result is put in the W register, and is d=1 it set up in the register that we selected.

SUB

At this point, I guess you can’t presume what this function conducts! Indeed, you suspected it, this function
subtracts one bit from another. Again the PIC provides us 2 tastes: SUBLW and SUBWF. The syntax is precisely the similar to for the ADD function, apart from evidently you type SUB in place of ADD!

Increment 

In case we wished to include 1 to a number in the PIC, we could absolutely make use of the ADD function, and utilize the number one. ~The difficulty with this is that we must first place the figure into the W register, subsequently use ADDLW 1 control to increment it. In case we desired to include 1 to a register, it can be worse still. We first must place the number 1 into the W register, after that use ADDWF <register>,1. Therefore, for instance, to include 1 to location 0C, say, we would need to possess the following part of script:

movlw    01
addwf     0c,1

There exists an easier method of conducting this. We can exercise the command INCF. The syntax is: INCF <register>,d where <register>, is the register, or place, that we are concerned in, and d shows the PIC where you should position the outcome. In case d=0, the outcome is within the W register, and in case d=1, the consequence is set in the register we stipulated. By utilizing this individual instruction we are able to actually fifty percent of the coding. In case we desired the outcome restored into the W register, in that case employing the instance above, we might have had to include an additional command to shift the items of 0C back into the W register, after which place the 0C register back to no matter what it was. There exists increment command. It is INCFSZ. This command might increment the register that we stipulate, however if we the register equals 0 after the increment (that will occur while we include 1 to 127) after that the PIC will probably by pass the subsequent instruction. The portion of code below reflects this:

Loop         incfsz            0C
                 Goto Loop
                 :
                 :
Remainder of program.

In the above portion of code, 0C is going to be incremented by 1. We next own an instruction that informs the PIC to return to our tag named Loop, and increment 0C by 1 again. This continues until 0C equals 127. In this circumstance, when we increment 0C by 1, 0C is going to now match 0. Our INCFSZ instruction could very well inform the PIC to omit the subsequent instruction, which in this instance is the goto declaration, hence the PIC will go forward with the remaining of the program.

Decrement

We have by now discussed the decrement function in earlier training, therefore I won’t revise
it anymore.

Compliment

The final instruction in this discussion would reverse every single bit in the register that we stipulate. The syntax is: COMF <register>,d wherein <register is the register that we wish to reverse, and d will inform the PIC where to collect the result. If d=0, the result is saved in the W register. If d=1, the result is saved again into the register we assigned. The following example exhibits this instruction in practicality: 
0C = 11001100
COMF 0C,1
0C = 00110011

This could be utilized, for instance, to swiftly swap the pins of a port from output to input and so on.

Using Memory Space Efficiently: PIC Tutorial 6

Up till now, we have composed the PIC blink an LED on and off. Subsequently we were capable of
with our PIC by including a switch, therefore varying the flash speed.


The sole issue is, the program is quite lengthy and rather inefficient of memory space. It seemed ok while i was including the commands for the first time, however there ought to be an easier way of executing it. Positively there is, we will analyze how we were literally switching the LED on and off.

movlw          02h
movwf          PORTA
movlw          00h
movlw PORTA

At first we stuffed our w register with 02h, after that transfered it to our PortA register to switch the
LED on. To switch it off, we packed w with 00h after which shifted it to our PortA register. Inbetween all these routines we were forced to get in touch with a subroutine to ensure that we could observe the LED
flashing. Therefore, we needed to transfer two sets of info a couple of times (one time into the w register then to
PORTA) as well as call a subroutine two times (once for on then once for off).
Thus, how could we achieve this with added efficiency? Very simple. We utilize a different instruction known as
XORF.
The XORF instruction works an Exclusive OR function on the register that we stipulate with the info we provide. I believe I have to clarify what in the world an Exclusive OR is before we continue.
In case we have two inputs, and one output, the input can only be a 1 if, and as long as, the two inputs differ. While they are the same, then the output will probably be 0. The following is a truth table, for individuals who choose to check out these:

A          B            F
0           0            0
0           1            1
1           0            1
1           1            0

We will at this point check out what goes on if we render B the just like our earlier output, and simply
altering the value of A:
A           B          F
0            0          0
0            0          0
1            0          1
1            1          0
1            0          1

If we maintain the value of A same as 1, and we Exclusive OR it with the output, the output would toggle. In case you can’t notice this from the truth table, below it can be witnessed utilizing binary:

                        0       Current Output
EX-OR With 1 1       New Output
EX-OR With 1 0       New Output

Maybe you can find that by exlusive ORing the output with 1, we will be now toglling the output from 0 to 1 to 0.
Hence, to switch our LED on and off, we only require a couple of sentences:
MOVLW     02h
XORWF      PORTA,1
What precisely we will be accomplishing is adding our w register with 02h. We are in that case Exclusive ORing this number with no matter what is on our PortA. In case bit 1 is a 1, it is going to alter to a 0. In case bit 1 is a 0, it is going to alter to a 1.
Let’s examine this code once or twice, to display how it's running binary:
                      PORTA
                      00010
xorwf               00000
xorwf               00010
xorwf               00000
xorwf               00010

We don’t actually have to load the identical value into our w register every time, therefore it is possible to accomplish this one time at the start, and simply leap back to our toggle command. Additionally, we don’t
ought to fix a value on our PortA register. The reason? Surely, since in case on power up it is a 1, we can easily toggle it. I, alternatively a 0 on power up, we would even now toggle it.

Therefore you would want to see our newly formed code. The first one represents our blinking LED code, while the second shows the one with the addition of the switch:







Lets wish you can find that simply by making use of one easy instruction, we now have cut down the scale of
our program. The truth is, in order to display just how much we could reduce our programs by, We have demonstrated the two programs, just what were composed, and their dimensions in the table below:
Program               Alter                           Dimensions (Bytes)
Flashing LED         Original                             120
Flashing LED         Subroutine Added          103
Flashing LED         XOR Function Used       91
LED With Switch   Original                           132
LED With Switch   XOR Function Used       124.


Therefore, not just have we discovered a few novel instructions, we certainly in addition have decreased the size of our scripting!

How to Read from Input/Output Ports: PIC Tutorial 5

So far, we have been composing to Port A in order that we are able to switch an LED on and off. At this point, we will see how we are going to read the I/O pins on the ports.



This is exactly to ensure that we are able to link an external circuit, and influence any specific outputs it offers. Should you memorize from our earlier courses, if you want to establish the I/O ports, we needed to jump from Bank 0 to Bank 1. We will accomplish that initially:



At this point we have fixed bit 0 of Port A to input. we must now examine if the pin is high or low. To accomplish this, one can utilize just one of two instructions: BTFSC and BTFSS. The BTFSC instruction signifies ‘Do a bit test on the register as well as bit we designate. In case it is a 0, in that case we omit the subsequent instruction’. BTFSS implies ‘Do a bit test in the register and bit we establish. In case it is set to a 1, then we bypass the subsequent instruction.’ Which one we utilize, is determined by precisely how we wish our program to respond while we study the input. As an illustration, in case we are just awaiting the input to be a 1, then we might be able to utilize the BTFSS instruction in the following manner:
Code here
:
BTFSS               PortA,0
Goto start
Carry on here
:
:


The program would just shift onto ‘Carry on here’ provided that bit 0 on PortA is scheduled to a 1. We will currently write a program which could prompt an LED at one rate, however if a switch is confined it would flash the LED two times as slower. It is possible to perhaps exercise this program out for on your own, still We have incorporated the listing somehow. You could attempt and author the entire program, in order to check if in case you have understood the principles. We will be using the equivalent circuit as before, with the inclusion of a switch attached RA0 of the PIC and the positive rail of our supply.



What We have accomplished here is to switch the LED on. I subsequently determine if the switch is closed off. In case it is confined, next I connect to our delay subroutine. This provides us the equivalent delay as before, however we are at this point contacting it two times. The same thing applies to whenever the LED is off. In case the switch is not shut, then we have our previousy recorded on and off periods.

Have you been following these lessons from the beginning, you might be seeking to grasp that you have currently discovered ten of the 35 instructions for the PIC 16F84! And every bit of these happen to be learned merely by switching an LED on and off.

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.

Understanding Delay Loops: PIC Tutorial 3

There may be a minor downside of our flashing LED program. Every instruction needs 1
clock sequence to finish. In case we are utilizing a 4MHz crystal, then every instruction calls for
1/4MHz, or 1uS to finish.


Since we have been employing just five instructions, the LED would activate then off in 5uS. This could be much too rapid for folks to notice, in addition, it will seem that the LED is fully on. What we should instead accomplish is produce a inhibition between switching the LED on and turning the LED off. The theory of the inhibition is that we count down from a earlier quantity, so when it gets to zero, we quit counting. The zero value signifies the conclusion of the delay, and we keep working our process throughout the program. Therefore, the firstly we must do is to determine a constant to make use of as our counter. Let us term this constant COUNT. After that, we must determine how significant a number to begin counting from. Surely, the biggest figure we could include is 255, or FFh in hex., as I talked about in the earlier tutorial, the equ instruction assigns a expression to a register situation. This implies that no matter what quantity we allocate our COUNT, it would match the items of a register. In case we try to designate the value FFh, we are going to get a mistake once we get to compile the program. The reason being the location FFh is , therefore we can’t gain access to it. Therefore, how must we designate a genuine number? Certainly, it will require a small amount of lateral pondering. If perhaps we designate our COUNT to the address 08h, for instance, this would indicate a basic objective register destination. By default, the untouched areas are set to FFh. Consequently, if COUNT leads to 08h, you will encounter the value of FFh while we first power up. Nevertheless, I you, how can we fix COUNT to another number?, all we apply is ‘move’ a valuation to this destination first. As an illustration, suppose we wished for COUNT to possess a value of 85h, we can’t mention COUNT equ 85h since that is the position of out Tri-State register for Port A. Precisely what we accomplish is the following: movlw 85h;First put the value of 85h in the W register movwf 08h;Now move it to our 08h register. Subsequently, in case we express COUNT equ 08h, COUNT would match the value 85h. Delicate, isn’t it! Therefore, initially we determine our constant: COUNT equ 08h Following we must reduce this COUNT by one until it becomes zero. It simply so occurs that there exists one instruction designed to accomplish this for us, by making use of a ‘goto’ and a tag. The instruction we are going to apply is: DECFSZ COUNT,1 This instruction states ‘Decrement the register (here it's COUNT) by the number that tracks the comma. If we attain zero, hop two spots ahead.’ Lets find it in action first, before we place it into our course.


What we have performed is initially establish our constant COUNT to 255. The subsequent segment positions a tag, called LABEL close to our decfsz instruction. The decfsz COUNT,1 reduces the value of COUNT by one, and retains the end result straight into COUNT. Moreover it verifies to check if COUNT possesses a value of 0. If it doesn’t, it in that case triggers the program to shift to the subsequent line. Now we have a ‘goto’ declaration which delivers us back to our decfsz instruction. In case the value of COUNT performs equal , then the decfsz instruction results in our program to leap 2 spots ahead, and is sent to where We have claimed ‘Carry on here’. Therefore, since you can observe, we have brought about the program to sit in one spot for a predestined time before proceeding. This could be named a delay loop. In case we require a more substantial delay, we could pursue one loop by the next. The extra loops, the extended the delay. Let us at least two, assuming we want to observe the LED flash.. We will place these delay loops into our program, and accomplish by rendering it a genuine program by introducing comments:



It is possible to compile this program after which program the PIC. Obviously, be sure that you attempt
the circuit out to check if it does indeed function. The following is a circuit diagram that you should construct as soon as you have programmed the PIC.




Well done, you could have actually composed your first PIC program, as well as constructed a circuit to blink
an LED on and off. Until now, in case you have ensued these courses, you might have learned an overall of
seven instruction out of 35, but without doubt so far you might be controlling the I/O ports!
Would you attempt to change the delay loops to render the LED flash quicker – what appears the
minimal value of COUNT to essentially see the LED flash? Or maybe, you will want to include a 3rd or
supplementary delay loops after the initial one to stabilize the LED down. a
unique constant for each delay loop. You could potentially then actually fiddle with your delay loops to
render the LED flash at a specific speed, for instance after a second.
Within the next instructional let us see how we are able to utilize something known as a subroutine to maintain
the program compact and basic