Pic tutorial

Introduction to Microcontrollers

Circumstances that we find ourselves in today in the field of microcontrollers had their beginnings in the development of technology of integrated circuits. This development has made it possible to store hundreds of thousands of transistors into one chip. That was a prerequisite for production of microprocessors , and the first computers were made by adding external peripherals such as memory, input-output lines, timers and other. Further increasing of the volume of the package resulted in creation of integrated circuits. These integrated circuits contained both processor and peripherals. That is how the first chip containing a microcomputer , or what would later be known as a microcontroller came about.

Microcontroller PIC16F84

PIC16F84 belongs to a class of 8-bit microcontrollers of RISC architecture. Its general structure is shown on the following map representing basic blocks.

Program memory (FLASH)- for storing a written program. 
Since memory  made in FLASH technology can be programmed and cleared more than once, it makes this microcontroller suitable for device development. 

EEPROM - data memory that needs to be saved when there is no supply.
It is usually used for storing important data that must not be lost if power supply suddenly stops. For instance, one such data is an assigned temperature in temperature regulators. If during a loss of power supply this data was lost, we would have to make the adjustment once again upon return of supply. Thus our device looses on self-reliance. 

RAM - data memory used by a program during its execution.
In RAM are stored all inter-results or temporary data during run-time.


Oscillator circuit is used for providing a microcontroller with a clock. Clock is needed so that microcontroller could execute a program or program instructions.


Reset is used for putting the microcontroller into a 'known' condition. That practically means that microcontroller can behave rather inaccurately under certain undesirable conditions. In order to continue its proper functioning it has to be reset, meaning all registers would be placed in a starting position. Reset is not only used when microcontroller doesn't behave the way we want it to, but can also be used when trying out a device as an interrupt in program execution, or to get a microcontroller ready when loading a program.

Central Processing Unit

Central processing unit (CPU) is the brain of a microcontroller. That part is responsible for finding and fetching the right instruction which needs to be executed, for decoding that instruction, and finally for its execution.


Term "port" refers to a group of pins on a microcontroller which can be accessed simultaneously, or on which we can set the desired combination of zeros and ones, or read from them an existing status. Physically, port is a register inside a microcontroller which is connected by wires to the pins of a microcontroller. Ports represent physical connection of Central Processing Unit with an outside world. Microcontroller uses them in order to monitor or control other components or devices. Due to functionality, some pins have twofold roles like PA4/TOCKI for instance, which is in the same time the fourth bit of port A and an external input for free-run counter. Selection of one of these two pin functions is done in one of the configuration registers. An illustration of this is the fifth bit T0CS in OPTION register. By selecting one of the functions the other one is disabled.

Memory organization

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. 


Interrupts are a mechanism of a microcontroller which enables it to respond to some events at the moment they occur, regardless of what microcontroller is doing at the time. This is a very important part, because it provides connection between a microcontroller and environment which surrounds it. Generally, each interrupt changes the program flow, interrupts it and after executing an interrupt subprogram (interrupt routine) it continues from that same point on.

Free-run timer TMR0

Timers are usually the most complicated parts of a microcontroller, so it is necessary to set aside more time for understanding them thoroughly. Through their application it is possible to establish relations between a real dimension such as "time" and a variable which represents status of a timer within a microcontroller. Physically, timer is a register whose value is continually increasing to 255, and then it starts all over again: 0, 1, 2, 3, 4...255....0,1, 2, 3......etc.

EEPROM Data memory

PIC16F84 has 64 bytes of EEPROM memory locations on addresses from 00h to 63h that can be written to or read from. The most important characteristic of this memory is that it does not lose its contents with the loss of power supply. Data can be retained in EEPROM without power supply for up to 40 years (as manufacturer of PIC16F84 microcontroller states), and up to 1 million cycles of writing can be executed. 

ASM Language Programming

The ability to communicate is of great importance in any field. However, it is only possible if both communication partners know the same language, i.e follow the same rules during communication. Using these principles as a starting point, we can also define communication that occurs between microcontrollers and man . Language that microcontroller and man use to communicate is called "assembly language". The title itself has no deeper meaning, and is analogue to names of other languages , ex. English or French. More precisely, "assembly language" is just a passing solution. Programs written in assembly language must be translated into a "language of zeros and ones" in order for a microcontroller to understand it. "Assembly language" and "assembler" are two different notions. The first represents a set of rules used in writing a program for a microcontroller, and the other is a program on the personal computer which translates assembly language into a language of zeros and ones. A program that is translated into  "zeros" and "ones" is also called "machine language".


MPLAB is a Windows program package that makes writing and developing a program easier. It could best be described as developing environment for a standard program language that is intended for programming a PC. Some operations which were done from the instruction line with a large number of parameters until the discovery of IDE "Integrated Development Environment" are now made easier by using the MPLAB. Still, our tastes differ, so even today some programmers prefer the standard editors and compilers from instruction line. In any case, the written program is legible, and a well documented help is also available.

Program package MPLAB

Following the installation procedure, you can launch MPLAB by double-clicking the desktop icon. As you can see, MPLAB has the familiar look of Windows programs: a drop menu (uppermost line with standard options - File, Edit..etc.), toolbar (illustrated shortcuts for common actions) and a status line below the working area. There is a rule of thumb in Windows of taking the most frequently used program options and placing them below the menu, too. Thus, we can access them more quickly and speed up the work. In other words, what you have in the toolbar you also have in the menu.

Designing a project

Preparing the program for loading into microcontroller can boil down to few basic steps:

new assembler file

When 'project' part of the job is done, the following screen should appear.

Writing a program

Only after all of the preceding operations have been completed we are able to start writing a program. This sample program is fairly simple and serves to illustrate creation of a project.