2.5 Memory organization PIC16F84 has two separate memory blocks, one for data and the other for program. EEPROM memory and GPR registers in RAM memory make up a block for data, and FLASH memory makes up a program block. Program memory Program memory has been realized in FLASH technology which makes it possible to program a microcontroller many times before it is 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 an interrupt in supply. EEPROM is not stored directly in memory space, 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 takes up 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 short 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 and have to do with working with certain blocks of the microcontroller. These are called Special Function Registers. Memory organization of microcontroller 16F84 Memory Banks Beside this 'linear' 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 and RP1 bits in STATUS register. Example: bcf STATUS, RP0 Instruction BCF resets 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. Usually, groups of instructions that are often in use, are connected into one unit which can easily be recalled in a program, and whose name has a clear meaning, so called Macros. With their use, selection between two banks becomes more clear and the program itself more legible. BANK0 macro Bcf STATUS, RP0 ;Select memory bank 0 Endm BANK1 macro Bsf STATUS, RP0 ;Select memory bank 1 Endm
| 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. By its incrementing or change (eg. in case of jumps) microcontroller executes program instructions one by one. Stack PIC16F84 has a 13-bit stack with 8 levels, or in other words, a group of 8 memory locations of 13 -bits width with special function. Its basic role is to keep the value of program counter after a jump from the main program to an address of a subprogram being executed has occured. 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 are executed at the end of a subprogram, program counter is taken from a stack so that program could continue where it 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 after instructions which exist on some bigger microcontrollers. In System Programming In order to program a program memory, microcontroller must be set to special working regime 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 can be 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 makes it with the help of 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 An example can be 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 zeros, and writing to it results in NOP operation (no operation).