2.5 Memory organization of a PIC microcontroller
PIC16F84 has two separate memory blocks, one for data and the other for program. EEPROM memory with GPR and SFR registers in RAM memory make up the data block, while FLASH memory makes up the program block.
Program memory
Program memory has been carried out in FLASH technology which makes it possible to program a microcontroller many times before it's installed into a device, and even after its installment if eventual changes in program or process parameters should occur. The size of program memory is 1024 locations with 14 bits width where locations zero and four are reserved for reset and interrupt vector.
Data memory
Data memory consists of EEPROM and RAM memories. EEPROM memory consists of 64 eight bit locations whose contents is not lost during loosing of power supply. EEPROM is not directly addressable, but is accessed indirectly through EEADR and EEDATA registers. As EEPROM memory usually serves for storing important parameters (for example, of a given temperature in temperature regulators) , there is a strict procedure for writing in EEPROM which must be followed in order to avoid accidental writing. RAM memory for data occupies space on a memory map from location 0x0C to 0x4F which comes to 68 locations. Locations of RAM memory are also called GPR registers which is an abbreviation for General Purpose Registers. GPR registers can be accessed regardless of which bank is selected at the moment.
SFR registers
Registers which take up first 12 locations in banks 0 and 1 are registers of specialized function assigned with certain blocks of the microcontroller. These are called Special Function Registers.
Memory Banks
Beside this 'length' division to SFR and GPR registers, memory map is also divided in 'width' (see preceding map) to two areas called 'banks'. Selecting one of the banks is done via RP0 bit in STATUS register.
Example:
bcf STATUS, RP0
Instruction BCF clears bit RP0 (RP0=0) in STATUS register and thus sets up bank 0.
bsf STATUS, RP0
Instruction BSF sets the bit RP0 (RP0=1) in STATUS register and thus sets up bank1.
It is useful to consider what would happen if the wrong bank was selected. Let's assume that we have selected bank 0 at the beginning of the program, and that we now want to write to certain register located in bank 1, say TRISB. Although we specified the name of the register TRISB, data will be actually stored to a bank 0 register at the appropriate address, which is PORTB in our example.
BANK0 macro
Bcf STATUS, RP0 ;Select memory bank 0
endm
BANK1 macro
Bsf STATUS, RP0 ;Select memory bank 1
endm
Bank selection can be also made via directive banksel after which name of the register to be accessed is specified. In this manner, there is no need to memorize which register is in which bank.
| Locations 0Ch - 4Fh are general purpose registers (GPR) which are used as RAM memory. When locations 8Ch - CFh in Bank 1 are accessed, we actually access the exact same locations in Bank 0. In other words , whenever you wish to access one of the GPR registers, there is no need to worry about which bank we are in! |
Program Counter
Program counter (PC) is a 13-bit register that contains the address of the instruction being executed. It is physically carried out as a combination of a 5-bit register PCLATH for the five higher bits of the address, and the 8-bit register PCL for the lower 8 bits of the address.
By its incrementing or change (i.e. in case of jumps) microcontroller executes program instructions step-by-step.
Stack
PIC16F84 has a 13-bit stack with 8 levels, or in other words, a group of 8 memory locations, 13 bits wide, with special purpose. Its basic role is to keep the value of program counter after a jump from the main program to an address of a subprogram . In order for a program to know how to go back to the point where it started from, it has to return the value of a program counter from a stack. When moving from a program to a subprogram, program counter is being pushed onto a stack (example of this is CALL instruction). When executing instructions such as RETURN, RETLW or RETFIE which were executed at the end of a subprogram, program counter was taken from a stack so that program could continue where was stopped before it was interrupted. These operations of placing on and taking off from a program counter stack are called PUSH and POP, and are named according to similar instructions on some bigger microcontrollers.
In System Programming
In order to program a program memory, microcontroller must be set to special working mode by bringing up MCLR pin to 13.5V, and supply voltage Vdd has to be stabilized between 4.5V to 5.5V. Program memory can be programmed serially using two 'data/clock' pins which must previously be separated from device lines, so that errors wouldn't come up during programming.
Addressing modes
RAM memory locations can be accessed directly or indirectly.
Direct Addressing
Direct Addressing is done through a 9-bit address. This address is obtained by connecting 7th bit of direct address of an instruction with two bits (RP1, RP0) from STATUS register as is shown on the following picture. Any access to SFR registers is an example of direct addressing.
Bsf STATUS, RP0 ;Bankl
movlw 0xFF ;w=0xFF
movwf TRISA ;address of TRISA register is taken from
;instruction movwf
Direct addressing
Indirect Addressing
Indirect unlike direct addressing does not take an address from an instruction but derives it from IRP bit of STATUS and FSR registers. Addressed location is accessed via INDF register which in fact holds the address indicated by a FSR. In other words, any instruction which uses INDF as its register in reality accesses data indicated by a FSR register. Let's say, for instance, that one general purpose register (GPR) at address 0Fh contains a value of 20. By writing a value of 0Fh in FSR register we will get a register indicator at address 0Fh, and by reading from INDF register, we will get a value of 20, which means that we have read from the first register its value without accessing it directly (but via FSR and INDF). It appears that this type of addressing does not have any advantages over direct addressing, but certain needs do exist during programming which can be solved smoothly only through indirect addressing.
Indirect addressing is very convenient for manipulating data arrays located in GPR registers. In this case, it is necessary to initialize FSR register with a starting address of the array, and the rest of the data can be accessed by incrementing the FSR register.
Such examples include sending a set of data via serial communication, working with buffers and indicators (which will be discussed further in a chapter with examples), or erasing a part of RAM memory (16 locations) as in the following instance.
Reading data from INDF register when the contents of FSR register is equal to zero returns the value of zero, and writing to it results in NOP operation (no operation).