Here I'm talking about PIC 16F84 and This is the basic instruction set. Using these instructions and the algorithms,you can write codes.PIC programmer's model mentioned below.
Register Direct -: Here is the operand specified to be Working Register,for example,
clrw ; clear the working register
Most instructions specified at least one operand inworking register.In many cases w can be both source and destination,for instance
addwf f,w ; add the content of the working register to file and put it in to working register.
where W initially hold the one of the datum bytes and finally holds the outcome datum bytes.
Literal -: This address mode is specifying an operand which is constant data rather than a location,Example
addlw 120 ; add the literal constant decimal value 120 to the working register.
Bit test -: A few instructions can test or modifying the single bit of file,Example,
btfsc 3fh,6 : test the 6th bit of file h'3fh ; skip the next instruction if clear.
File Direct -: Following operands specifying about direct file addressing,Example,
clrf 20h ; clear the byte at file store address h'20h
incf 20h,f ; put the contents of the file'20h plus 1 in to the the file 20h
Suppose we wish to clear the all file registers from file 0Ch through to 3Fh in the file store,say to hold and array of 52 byte elements. The obvious
Way to do this is shown in figure,which uses the clrf function to each byte. Although it works,but inefficient and mind boggles if you want to clear a 1kb file store.So There got to be a better way...!!!
File Indirect -: The term indirect is used as this address register does not hold the operand datum it self,only a pointer address to it.The advantage of this seemingly perverse way of accessing data in the Data store,is that the effective address (ea)is a variable and this can be altered by the program as it progresses.This ea is the contents of this special pointer address register.
In our BASIC computer the File Select Register (FSR) is implemented as File4 in the Filestore.The indirect mechanism is evoked when the dummy address File0 (there is no physical file atlocation 0in the Data store)is used for the operand address,Thus,for example,if the contents of File4 happened to be 20h then:
Will effectively copy the contents of W out to File20h.This seems rather an obscure way of doing things,but by way of a justification let us revisit our array clearing example of Program.Repeating the same thing 52 times on successive file locations is a dubious way of doing this.Why not us ea pointerin to the array,and increment this pointer each time we do a Clear?That is,rather than using a constant address for the destination operand use a variable effective address.
circulating around the clrf instruction,which is ‘walked’ through the array of files by advancing the pointer in File4,the FSR,on each pass through the loop.Eventually the pointer moves beyond the desired range and the program then exits the loop and continues on to the next section of code.
rrf,rlf Instructions -: Two instructions are provided that can shift the contents of any file once left or right,with the out come either remaining in the file or appearing in the W register.As shown in Fig.3.7the out going bit(bit7 for rlff,d and bit 0 for rrff,d)is placed in the C flag,whilst the incoming bit at the opposite end is the previous value of C.Because of this circular’ action these instructions are said to implement a Rotate data through the Carry operation.
Improve your PIC knowledge through following Sample codes
Examples
Example3.1 Write a program to add the byte contents in File memory called NUM1 (File20h) to NUM2 (File21h).The out come is to be in SUM_H and SUM_L (File 22h and File 23h respectively)in the order high:low byte.
Example3.2 Write a program routine that will add two 16-bit numbers givinga 17-bit sum.The augend islocated in the two memory locations F20h:F21h in the order high:low byte thus AUGEND_HAUGEND_L F20h F21h.The addend is similarly situated ADDEND_H ADDEND_L F22h F23h.The sum is stored as three bytes in the order high : middle : low thus SUM_H SUM_M F24h F25h SUM_L F26h.
Example3.3 Write a program to divide the byte in the Working register by ten.The quotient is to be in File20h and the remainder in W.
Example3.4 As part of a program to convert degrees Celsius to Fahrenheit it is necessary to multiply the byte in W by nine.Devise a suitable coding.
Solution -: There are two ways of multiplying.The fundamental definition of multiplication is repetitive addition,thus we could add the datum nine times to implement our specified function.An alternative approach is the shift and add technique.Thus the ×9 function is implemented as ×8+×1.The former is carried out by shifting left three times (i.e.23).Thus we have:
Self-assessment questions
The PIC16F84 Microcontroller
Some Examples -:
The registers associated with parallel I/O are:
PORTA,File05h Only the lower five bits are implemented in this register file,feeding through to pins RA4…RA0.Pin RA4 is shared with the Timer peripheral. The upper three bits read as zero.
TRISA,File85h This is used to bitwise configure PortA bits as input or output.Setting TRISA[n] to 1 setsbit PORTA[n] as an input and to 0 as an output.Any type of reset sets the TRIS bits to 1 and the associated port bits to input.
PORTB,File06h A bidirectional 8-bit port connected to pins RB7…RB0.Bit RB0 doubles as hardware interrupt input.
TRISB,File86h This is used to bit wise configure PortB bits as input or output.Details are the same as TRISA.
Subroutines
Subroutines and Modules
The subroutine as the embodiment or means, whereby the concept of modularity may be implemented.
The call-return mechanism using the stack, together with nesting.
The mechanics of sending data to and returning data from a subroutine.
The idea of maximum stand-alone or minimal disturbance of the environment.
Implementing a software stack to pass parameters to a subroutine.
What are the advantages of Modular construction?
Flexibility.
Can re-use from previous systems.
Can buy standard boards or design specialist board in-house.
Easy to maintain.
Can you think of any disadvantages of modular construction?
Product could be bulky
May be more expensive due to additional sockets, plugs, etc.
May consume more power
A 1 ms delay subroutine
How to determine N for 1ms?
1 ms is 1000µs à 1000 cycles
Execution cycle budget: call : 2 cycles movlw: 1 cycle addlw: N cycles btfss: N + 1 cycles goto: 2 (N + 1) cycles return : 2 cycles If we want the total to be 1000 then 4+4N=1000 Then N=249
How could you modify the 1ms delay subroutine to give a 2ms delay?