Neelkanth's PIC 16f877a notes

My Notes on PIC 16f877a Micro-controller

Reference link:

What You Need To Get Started

Most development setups consist of four main components:

1.      A computer running the MPLAB® X Integrated Development Environment
2.      A C Compiler (unless you are programming in assembly since an assembler is included with the IDE)
3.      A programmer / debugger such as one of the following:
a.       MPLAB® Real ICE
b.      MPLAB® ICD 3
c.       MPLAB® PICkit 3
4.      A target board with your PIC® microcontroller of choice. This could be something like:
a.       Your own hardware
b.      A Microchip demonstration board
c.       A third-party demonstration board
d.      A breadboard

MPLAB® PICkit 3

MPLAB® X IDE works in conjunction with an in-circuit debugger (programmer/debugger) and a target system which may consist of a demonstration board or your own hardware.





The PICkit3 programmer can program any PIC chips.
PICkit 3 terminals




The MCLR connector is an external reset to the PIC. It is an active low pin. It is normally held HIGH by a 10KΩ pull-up resistor to 5V. If brought low, it resets the program. When programming the microchip, the MCLR is temporarily brought low to cause a reset, then all the data is transferred to flash memory.
VDD supplies positive voltage to the programmer.
VSS gets connected to ground.
PGD is the data line of the programmer. It transfers all the data of the program.
PGC is the clock line. This allows synchronous communication between the programmer and the microchip being programmed.
PGM is an optional line. It allows the programmer to run on low voltage. We won't use in our circuit.

Connecting the PICKit3 ICSP to a PIC chip

Connecting the PicKit3 ICSP to a PIC chip is as easy as can be.
All of the connectors have a direct equal on the PIC chip and directly match up.


The circuit schematic below shows the connection of the programmer ICSP to a target PIC chip.

With the PICKit3 connected to a computer via USB, data (such as a compiled program) can travel from the host computer to the flash memory of a PIC microchip.
And this is how a PIC microcontroller can be programmed. 

In-Circuit Debuggers (Programmer-Debuggers)

Programming




When acting as a programmer these devices allow user generated code to be programmed into the PIC®Microcontroller's non-volatile program memory. After programming, once the MCLR pin is released, the PIC Microcontroller will reset and the application code will begin to run.

Debugging


When acting as a debugger the devices will program a version of the user code into the PIC®Microcontroller, however after programming MPLAB® X IDE will retain "control" over the device. The user will be able to use the debug commands of the IDE to control the program and view the memory contents inside the PIC® Microcontroller. This control and monitoring capability is what gives the user the ability to debug program errors.


PIC 16f877a microcontroller Memory layout


Up to 8K words x 14 bit of Flash Program Memory, [ Program Memory: The code is saved here . Non-volatile Memory]
Up to 368 x 8 bytes of Data Memory (RAM),      
[RAM Memory: Got gets executed here. Volatile memory] 
Up to 256 x 8 bytes of EEPROM Data Memory 
[ Non-Volatile memory]  
Snapshot from MPLABx IDE



1.      PIC 16f877A has  8K words x 14 bits of Flash program memory that can be electrically erased and reprogrammed. Each time we burn program into the micro, we erase an old program and write a new one.
2.      PIC 16f877a has 13 bit program counter which is capable of addressing an 8k word x 14 bit program memory space. PC[12:0]
3.      Flash memory makes it possible to program a microcontroller many times before installing to device and even after the installation we can change the program.
4.      RAM Data Memory is used for storing data temporarily during program execution and it is volatile. That is, this memory is cleared when the power is gone or after CPU reset. RAM Data Memory locations are also called General Purpose Registers (GPR). These two memories have faster response time.
5.      The third memory is EEPROM memory which is an abbreviation for Electrically Erasable Programmable Read Only Memory. EEPROM memory can be read and write electrically, can be accessed through program. It is a non volatile memory but has slower response time. EEPROM memory can be used to store data such as sensor logs, device parameters which should not be loss during power loss or CPU reset


Program memory Map (Flash) and stack (RAM)

1FFF h = 8191 d and 1 Word = 14 bits


The “reset vector” is the default location a CPU will go to find the first instruction it will execute after a reset. The reset vector is a pointer or address, where the CPU should always begin as soon as it is able to execute instructions. The address is in a section of non-volatile memory initialized to contain instructions to start the operation of the CPU, as the first step in the process of booting the system containing the CPU.

RAM/Data Memory Organization

A register is a place inside the RAM, which used to read or write the data/program. The memory of the PIC is divided into a series of registers. Each of the registers has its own address and memory locations.

According to the type of working and usage, the registers in PIC are classified into two categories.

1.      General Purpose Registers (GPR):

Generic c program which on getting converted into assembly instructions are executed at these locations of RAM memory.
Refer PIC 16f877a data sheet

2.      Special Function Registers (SFR):

The special function registers are also “memory registers” which is used for special dedicated functions. These registers perform various dedicated functions inside the PIC chip. Each special function inside this PIC chip is controlled by using these registers. These registers are used by the CPU and peripheral modules for controlling the desired operation of the device. These registers are normally implemented as in the form of static RAM memory.
In short, generic c program instructions which on getting converted into assembly instructions cannot be executed in these locations of RAM memory. These registers are mostly used for controlling timers, counters, ADC etc peripherals in a microcontroller.

The Special Function Registers can be classified into two sets:
1.      core (CPU)
2.      peripheral

The data memory is partitioned into multiple banks which contain the General Purpose Registers and the Special Function Registers. Bits RP1 (Status<6>) and
RP0 (Status<5>) are the bank select bits.
Each bank extends up to 7Fh (128 bytes). The lower locations of each bank are reserved for the Special Function Registers. Above the Special Function Registers are General Purpose Registers, implemented as static RAM. All implemented banks contain Special Function Registers. Some frequently used Special Function Registers from one bank may be mirrored in another bank for code reduction and quicker access.


RP1:RP0
Bank
00
0
01
1
10
2
11
3



PIC 16f877a peripherals
Parallel Slave Port:
parallel slave port (PSP) is an interface found on some PIC microcontrollers.[1] It allows 8-bit asynchronous bidirectional data transfer between the PIC and external devices, such as other microcontrollers or personal computers.

Synchronous Serial Port
Synchronous Serial Port (SSP) is a controller that supports the Serial Peripheral Interface (SPI), 4-wire Synchronous Serial Interface (SSI), and Microwire serial buses. A SSP uses a master-slave paradigm to communicate across its connected bus.

What is brown-out reset
A brown out is a loss of power (or enough of a loss of power so that the PIC won't function reliably). 
  
All that Brown Out Reset does is automatically it puts the processor in reset, when the power coming into the chip isn't sufficient for the processor to work reliably.  Essentially, if lack of power is going to cause funky behavior, the processor will hold in reset rather than trying to keep it running.  It should then resume, once the voltage has gone above the brownout threshold. 
power-on reset (PoR)


What is power-on-reset
A power-on reset (PoR) is a circuit that provides a predictable, regulated voltage to a microprocessor or microcontroller with the initial application of power. The PoR system ensures that the microprocessor or microcontroller will start in the same condition every time that it is powered up.
A PoR system can be a peripheral, but in sophisticated processors or controllers the PoR is integrated on the main chip. The most basic PoR system can comprise a resistor and capacitor connected with values tailored so that, when power is first applied, the capacitor takes a predictable and constant time to charge up. For computer use, however, additional components are often required, including a circuit called a Schmitt trigger. When the PoR circuit is designed, the charge-up time should be adjusted by trial and error so that all the processor or controller circuits can set themselves to the correct initial values before the computer begins to function.
A well-designed PoR circuit can ensure that when power is applied to a computer, it will start up properly every time (or almost every time), and will never (or rarely) freeze up right away. This feature not only saves the user a great deal of frustration, but it offers a last resort in case of a stubborn system crash: the so-called cold boot, where the computer is completely powered-down for a minute or two, and then powered-up all over again.
When a PIC® MCU is first powered up, it will run through some defined hardware functions internally to prepare the MCU for proper operation. This can include the oscillator start-up and power stabilization. The Power On Reset will hold the device in reset while these operations happen. It prevents the device from running any software until a minimum level Vdd voltage threshold is met and the oscillator is stable. POR also handles some internal settings for the hardware to operate properly. Even though the device can be reset by other means, such as the MCLR pin, the internal circuitry will only be reset in this start-up mode with a POR.
The POR can hold the device in reset longer for environments that are known to be noisy by using the Power-up Timer.
Program counter in PIC 16f877a
The Program Counter (PC) is 13 bits wide. The low byte comes from the PCL register which is a readable and writable register. The upper bits (PC<12:8>) are not readable, but are indirectly writable through the PCLATH register. On any Reset, the upper bits of the PC will be cleared.

Opcodes and Operands
An opcode is short for 'Operation Code'.
An opcode is a single instruction that can be executed by the CPU. In machine language, it is a binary or hexadecimal value such as 'B6' loaded into the instruction register.
In assembly language mnemonic form an opcode is a command such as MOV or ADD or JMP.
For example
MOV AL, 34h
The opcode is the MOV instruction. The other parts are called the 'operands'.
Operands are manipulated by the opcode. In this example, the operands are the register named AL and the value 34 hex.

Disadvantage of goto statement in microcontroller where program counter memory boundary is small

A computed GOTO is accomplished by adding an offset to the program counter (ADDWF PCL). When doing a table read using a computed GOTO method, care should be exercised if the table location crosses a PCL memory boundary (each 256-byte block).