PICmicro. PICMICRO Cult 2
The Register File
The REGISTER FILE is a set of RAM memory locations that means a memory with which is possible to read and modify the content without the help of the external programmers and directly from the running program on the PICmicro.
Given it's characteristics the REGISTER FILE is the memory normaly used for memorizing the variables of the program, that means all those values whose content differs during the execution.
Contrary to the PROGRAM MEMORY, the REGISTER FILE losses it's content when the PICmicro is turned off so it is necessary to reinitialize the values of all it's locations before you can use them.
Some locations of the REGISTER FILE and in particular those that can be found at the lower addresses are used for the hardware's control of the PICmicro as shown below.

The memory locations of the REGISTER FILE are addressed directly in a memory space that goes from 0x00 to 0x2F for a total of 48 byte, called page 0. Another address space called page 1 goes from 0x80 to 0xAF. In order to have access to this another space it is necessary to resort to two auxiliary bits RP0 and RP1 upon the the modality that we will explain further.
The first 12 locations of the page 0 (from 0x00 to 0x0B) and of the page 1 (from 0x80 to 0x8B) are those reserved for the special functions for the PICmicro's operation and can't be used for other purposes.
The 36 locations in page 0 indexed from 0x0C to 0x2F can be freely used by our programs for memorizing variables, counters, ecc.
In our example LED.ASM the directive:
ORG 0x0C
shows the starting address of the data area usable by our program.
The next directive:
Count RES 2
reserves a two location space, that the program will use for memorizing the delay counters of the Delay subroutine.
The specialized registries of the PIC are used frequently in the programs.
For example, it appelas at the specialized register couple TRISA (0x85) and TRISB (0x86), for defining which I/O lines are for input and which are for output. The same locical state of the I/O lines depends upon the value of the two registries PORTA (0x05) and PORTB (0x06).
Some registries returns the functioning state of the PICmicro's internal devices or the result of the arithmetical and logical operations.
It is necessary to be known exactly which function performs every specialized register and what effects are obtaining by manipulating the content.
For making easier the operations on the specialized registries, in the file P16F84A.INC (that as you remember was included into the source LED.ASM with the directive INCLUDE) Microchip has inserted a list with names that identifies univocally every specialized register and to which are associated the corresponding addresses in the area of the REGISTER FILE.
If, for example, we want to define all the port B output lines of the PIC acting on the register TRISB, we can choose to directly reference the register with it's address:
movlw B'00000000'
movwf 0x86
or, referencing the same register with it's symbolic name:
movlw B'00000000'
movwf TRISB
but being watchful to insert the directive INCLUDE "P16F84A.INC"in our source.
The ALU

The ALU (acronym of Arithmetic and Logic Unit) is the most complex component of the PICmicro because it contains all the circuits that perform the calculation and data manipulation functions during the execution of a program.
The ALU it's a component present in all the microprocessors and of this it's depending directly the calculation rating of the micro itself.
The ALU of the PIC16F84A is able to operate with 8 bit values, that means numeric values not higher then 255. There are microprocessors with ALU at 16, 32, 64 bit and above. The family Intel 80386, 486 and Pentium for example has an ALU at 32 bit. The reached calculation rating by these micro is considerably higher at the expense of the complexity of the inner circuits and accesories and consequently of the occupied space.
The accumulator or the W register
Directly connected to the ALU is the W register also called accumulator. This register consists in a simple memory location able to contain a single 8 bit value.
The major difference between the W register and the other memory locations is just that, in order to reference the W register, the ALU doesn't have to provide any memory location, but can have a direct access.
The W register it is used very often in the programs for the PICmicro.
Let's make a practical example. Let's supose that we want to insert in the 0xC memory location of the REGISTER FILE the value0x01. Searching through the PICmicro's instruction we quickly realize that it isn't a single instruction able to realize this operation but we necessary have to fall back upon the accumulator and to use two instructions in sequnce.
Let's see why:
As said in the previous steps, the opcode of a instruction can't be over 14 bit instead we need:
8 bit to specify the value that we intend to insert in the memory location,
7 bit to specify in which memory location we want to insert our value,
6 bit to specify what instruction we intend to use,
for a total of 8 + 7 + 6 = 21 bit.
Therefor we have to appeal at two instructions, that are:
movlw 0x01
movwf 0x0Cthat the first one is inserting in the W register the value 0x01H with the instruction MOVe Literal to W and then are "moving" it into the 0x0C location with the instruction MOVe W to F.
The Program Counter (PC)

As we have seen in the previous steps, the PIC16F84A starts the execution of the program with the Reset Vector that is the instruction memorized in the first memory location (address 0x000).
After performing this first instruction it goes at the next instruction memorized in the location 0x001 and so on. If there isn't any instruction able to influence somehow the execution of the program, the PICmicro will soon get to perform all the instructions from it's memory until the last available location.
We obviously know that it isn't like this and that any microprocessor or programming language has jump instructions, these are instructions able to modify the program's execution flow in base of the programmer's requirements.
One of these instructions is GOTO that allows us to change the execution sequence and to "jump" directly to any other point, in the program memory, and to continue therefor the execution starting from that point.
Let's make an example:
ORG 0x00
Point1
movlw 10
goto Point1
At the reset PICmicro will perform the instruction MOVLW 10 memorized at the location 0x000, which will insert in the accumulator the decimal value 10, therefor it will perform the next instruction GOTO Point1. This instruction will determin an unconditional jump to the memory location pointed by the Point1 label that means again at the location 0x000. Taking as a hole therefor, this program does nothing but continuously performing the two shown instructions.
During this cycle (or loop), in order to determin which will be the next instruction to be performed, the PIC uses a special register called PROGRAM COUNTER which function is exactly keep track of the address that contains the next instruction to be performed.
This register is automatically incremented at every instruction performed in order to determin the jump to the next instruction. At the time of RESET of the PIC, the PROGRAM COUNTER is reseted, this way determining the start of the execution starting from address 0x000.
The instruction GOTO allows the insertion into program of a new value in the PROGRAM COUNTER and the jump to any location of the PIC's program area.
The Stack Pointer
Another very useful instruction, that affects the PROGRAM COUNTER's value is the CALL with which is possible to make CALLS to SUBROUTINE.
This instruction works in similar manner as for GOTO. As well as GOTO actually allows to write in the PROGRAM COUNTER a new address of execution of the program. The main difference consists in the fact that before performing the jump, the PIC memorizes, in another special register, called STACK, the address of the one that it should be the next instruction to perform if it wouldn't have been met the CALL.
Let's see better with an example:
ORG 0x00 Point1 movlw 10 call Point2 goto Point1 Point2 movlw 11 return
In this case the PICmicro, after performing the instruction MOVLW 10 jumps to the instruction CALL Point2. But before jumping, memorizes into the STACK the address 0x002, that is the address of the next location after CALL. Therefor the execution goes to the instruction MOVLW 11 and to the instruction RETURN. This instruction, as it's name sais, allows to "return", that means to resume the execution starting with the next instruction after CALL that has determined the quit of the main flow of the program using the value memorized into the STACK register.
As said the new-performed operation it is called CALL TO SUBROUTINE, that means an momentary interruption of the normal program flow for "calling" in execution a series of instructions for returning then to the normal flow of execution.
On the STACK it is possible to store, one on the other, more addresses to recover them when are needed. This type of memorizing it is also called LIFO: Last In First Out, where the last element inserted (last in) must necessary be the first to come out (first out). Thanks to this characteristic it is possible to make more CALLS toghether that means one within the other and always keep track of the point where to resume the flow at the moment that is met a RETURN instruction.
Let's see another example:
ORG 0x00
Point1
movlw 10
call Point2
goto Point1
Point2
movlw 11
call Point3
return
Point3
movlw 12
returnIn this case in the Point2 subroutine it is made a further CALL to the Point3 subroutine. At the return of this last one the program must return at the Point2 subroutine, perform the RETURN and therefor return to the main flow.
The addresses to be memorized into the stack are two because it is met another CALL before meeting the RETURN corresponding to the first one.
The PIC16F84A has an 8 levels stack, that means a stack that allows up to 8 calls.
Is important to be sure, during the writing of a program, that there is always a RETURN instruction for every CALL in order to avoid dangerous misalignments of the stack that in execution can give rise to errors hardly to be notice.
Let's realize the "flashing lights"
Let's try now to fix into the memory the concepts learned untill now, revising the source LED.ASM presented into the first lesson in order to realize a four led sequential flashlight. The new modified source it is called SEQ.ASM
In the figure below it is reported the schematic of the new circuit, basically equal to the circuit presented in the first lesson, with the only difference that now the connected leds are four instead of one.

The used I/O lines are RB0 for the first led, RB1 for the second, RB2 for the third and RB3 for the fourth. Those will be therefor all configured as output at the start of the program changing the instructions:
movlw B'11111110' movwf TRISB
in
movlw B'11110000' movwf TRISB
where the four less significant bits, corresponding to the RB0,1,2,3 lines are set to zero for defining the output lines.
In the REGISTER FILE's memory area (that into the source starts with the directive ORG 0x0C) besides the two byte referenced by the Count label, we are keeping another byte with the Shift label that we will use to determine the sequence of the led lighting. The directive to be inserted is:
Shift RES 1
Before performing the main cycle (MainLoop label) we'll have to initialize the new Shift register at 00000001B with the following instructions:
movlw B'00000001' movwf Shift
At this point, in our program's main cycle, we'll be dealing with the transfer of the memorized value into the Shift register on the Port B obtaining therefor the lighting of the first led, with the following instructions:
movf Shift,W movwf PORTB
therefor to make the shift to the left of the value contained in Shift with a bit, with the next set of instructions:
bcf STATUS,C rlf Shift,F
the first instruction is needed to reset the CARRY bit of the state register STATUS that we'll analyze in the following lessons. The instrcution RLF Rotate Left F through Carry shifts with a bit to the left the value memorized in the Shift register inserting in the position occupied by the bit 0 the value of the Carry bit (that as said we will see further). In order to make sure that the inserted bit is always zero it is performed before the RLF the instruction BCF STATUS,C for reseting this bit.
At this point the Shift register will count 00000010, therefor, at the next cycle, once transfered this value on the B port it will be obtained the turn off of the LED1 and the turn on of the LED2 and so on for the next cycles.
When the bit 4 of the Shift will count 1, will mean that all the four leds were being turned on at least one time and is needed to restart from the led 1. The following instructions are performing this type of control:
btfsc Shift,4 swapf Shift,F
The BTFSC Shift,4 instruction controls exactly if the bit 4 of the Shift register counts 1. If yes, it performs the next instruction SWAPF Shift,F otherwise it jumps it.
The SWAP instruction in practice changes the four most significant bits contained in the Shift register with the four less significants. From the initial value of the Shift register equal to 00010000 obtained after some repetitions of the MainLoop cycle it is obtained the value 00000001 and in practice at the reignition of the first led.
Example of LED in sequence
If you want to have a little fun...
Introduction to the peripherals
The A and B ports, output stage of the input/output lines, input from the keyboard.
The PIC16F84A has a total of 13 I/O lines organized in two port named A PORT and B PORT.
The A PORT has 5 lines configurable as well in input as in output identified by the abbreviation RA0, RA1, RA2, RA3 and RA4.
The B PORT has 8 lines these too configurable as well in input as in output identified by the abbreviation RB0, RB1, RB2, RB3, RB4, RB5, RB6 and RB7.

The subdivision of the lines in two different ports is dictaded by the internal architecture bonds of the PIC16F84A that foresees the manangement of the data of a maximum length equal to 8 bit.
For the management of the I/O lines by the program, the PIC has two internal registers for every port called TRISA and PORTA for the A port and TRISB and PORTB for the B port.
The TRIS A and B registers, are determining the functioning in input or in output of every single line, the PORT A and B registers are determining the state of the output lines or are returning the state of the input lines.
Every contained bit in the mentioned registers corresponds univocally to an I/O line.
For example the bit 0 of the PORTA register and of the TRIS A register are corresponding to the RA0 line, the bit 1 to the RA1 line and so on.
If the bit 0 of the TRISA register is set to zero, the RA0 line will be configured as output line, therefor the value to which will be set the bit 0 of the PORTA register will determin the logical state of this line (0 = 0 volt, 1 = 5 volt).
If the bit 0 of the TRISA register is set to one, the RA0 line will be configured as input line, therefor the logical state in which will be set by the external circuits the RA0 line will reflect on the state of the bit 0 of the PORTA register.
Let's make a practical example, we assume that we want to connect a led on the RB0 line and a switch on the RB4 line, the code to be written will be the following:
movlw 00010000B tris B
where it will be set to 0 the bit 0 (RB0 line in output) and to 1 the bit 4 (RB4 line in input). We remind you by the way that in the assembler's binary notation the las bit on the right bit corresponds with the less significant bit therefor the bit 0.
For turning on the led we must write the following code:
bsf PORTB,0
For turning it off:
bcf PORTB,0
For reading the switch's state connected to the RB4 line, the code is:
btfss PORTB,4 goto SwitchAMassa goto SwitchAlPositivo
Output stages of the I/O lines
In order to make the PICmicro more adaptable to the different needs, the Microchip has implemented different typologies of ouput states for the I/O lines. Therefor there are some pin groups whose behaviour is lightly different from other groups. Knowing better the functioning of various output states we can utilize better their characteristics and optimize their use in our projects.
Output stage of the RA0, RA1, RA2 and RA3 lines
Let's start with the RA0, RA1, RA2 and RA3 line group for which we will reproduce, in the following picture, the schematic of the output stage extracted from the data sheet from Microchip:

As previously mentioned, the configuration of a line as input or output depends of the bit state in the TRIS register (TRISA for the A port and TRISB for the B port).
We'll take as an example the RA0 line and we'll analyze the functioning of the output stage both when the line is working in input, and when is working in output.
Working in input
In order to configure the RA0 line in input, we have to set to 1 the bit 0 from the TRISA register with the instruction:
bsf TRISA,0
This determines a switching to 1 of the flip-flop's logical state D-latch type indicated in the block with the name TRIS latch. For each I/O line there is one of these flip-flop and the logical state in which is found depends closely by the logical state of the relative bit in the TRIS register (in better words every bit of the TRIS register is physically implemented with a TRIS latch).
L'uscita Q del TRIS latch e' collegata all'ingresso di una porta logica di tipo OR. Questo significa che, indipendentemente dal valore presente all'altro ingresso, l'uscita della porta OR varra' sempre 1 in quanto uno dei suoi ingressi vale 1 (vedi tavola della verita'). In questa condizione il transistor P non conduce e mantiene la linea RA0 scollegata dal positivo d'alimentazione.
Allo stesso modo l'uscita negata del TRIS latch e' collegata all'ingresso di una porta AND quindi l'uscita di questa varra' sempre 0 in quanto uno dei suoi ingressi vale 0 (vedi tavola). In questa condizione anche il transistor N non conduce mantenendo la linea RA0 scollegata anche dalla massa. Lo stato logico della linea RA0 dipendera' esclusivamente dalla circuiteria esterna a cui la collegheremo.
Applicando 0 o 5 volt al pin RA0, sara' possibile leggerne lo stato sfruttando la circuiteria d'ingresso del blocco rappresentata dal TTL input buffer e dal latch d'ingresso.
The Power Down Mode
Il Power Down Mode o Sleep Mode e' un particolare stato di funzionamento del PICmicro utilizzato per ridurre il consumo di corrente nei momenti in cui il PICmicro non e' utilizzato perche' in attesa di un evento esterno.
Se prendiamo come esempio un telecomando per apricancello o per TV vediamo che per la maggior parte del tempo il PICmicro rimane in attesa che qualcuno prema un tasto. Appena premuto il PICmicro effettua una breve trasmissione e si rimette di nuovo in attesa della pressione del prossimo tasto.
Il tempo di utilizzo effettivo della CPU del PICmicro e' quindi limitato ai pochi millisecondi necessari per effettuare la trasmissione mentre per diverse ore non e' richiesta nessuna elaborazione particolare.
Per evitare di consumare inutilmente la limitata energia dalla batteria e' possibile spegnere buona parte dei circuiti di funzionamento del PICmicro e riaccenderli solo in corrispondenza di un qualche evento esterno.
Vediamo come.
L'istruzione SLEEP
L'istruzione SLEEP viene utilizzate per mettere il PIC in Power Down Mode e ridurre di conseguenza la corrente assorbita che passera' da circa 2mA (a 5 volt con clock di funzionamento a 4Mhz) a circa 2uA, ovvero 1000 volte di meno !
Per entrare in Power Down Mode basta inserire questa istruzione in un punto qualsiasi del nostro programma:
SLEEP
Qualsiasi istruzione presente dopo la SLEEP non verra' eseguita dal PICmicro che terminera' in questo punto la sua esecuzione, spegnera' tutti i circuiti interni, tranne quelli necessari a mantenere lo stato delle porte di I/O (stato logico alto, basso o alta impedenza) ed a rilevare le condizioni di "risveglio" di cui parleremo di seguito.
Per ridurre il consumo di corrente in questo stato, non devono esserci ovviamente circuiti collegati alle linee di uscita del PIC che consumino corrente. O meglio questi circuiti devono essere progettati in modo da limitare il loro assorbimento nelle condizioni di Power Down. Un altro accorgimento raccomandato dalla Microchip e' quello di collegare al positivo (Vdd) o al negativo (Vss) di alimentazione tutte le linee in alta impedenza non utilizzate compresa la linea RA4/T0CKI (pin 3).
- Ionela's blog
- 1143 reads





Post new comment