Introduction to Microcontrollers – Getting Started with PIC16F84A
As introduce in the article, introduction to embedded systems, Microcontrollers are the brain of an embedded system. So, In this article, I will be explaining PIC16F84A – an eight bit microcontroller from Microchip. You may now ask, “Why do we start with this particular microcontroller, aren’t there any other?” Well, it is obvious that an entity discussed at introduction will be easy to understand. Apart from this, there are some strong reasons that I chose to introduce PIC16F84A such as simple architecture, less amount of instructions etc.,
Does microcontrollers simpler than this exist? – Yes. For example, the PIC12F series. But they are too simple to begin with. So, let us go on and get our microcontroller – The PIC16F84A. It costs around Rs.120
Here I cannot explain all the features, working and specifications of the microcontroller, I will explain what is needed to get you started (And believe me you can do a lot of projects with this knowledge). Okay, now lets get into the game.
PIC16F84A is an 8 bit microcontroller which means that it is capable of processing only 8-bits at a time. It also comes in various packages. But we are only concerned with the PDIP (Plastic Dual Inline Package) which is normally used by hobbyists. Following are the specifications of the PIC16F84A
- Only 35 Instructions – which makes it a popular RISC (Reduced Instruction Set Computer).
- Operating frequency – up to 20 MHz
- 68 Bytes data RAM
- 64 Bytes of data EEPROM
- 15 Special function registers(SFRs)
- Operating voltage – 2.0 to 5 volts
- 13 I/O pins – You can configure the 13 pins either as input or output individually.
- Each pin can source/sink 25mA current.
- Supports ICSP(In Circuit Serial Programming)
- Flash memory can be erased/written 10,000 times.
- EEPROM memory can be erased/written 10,000,000 times
- In built Watchdog timer.
Don’t panic if you don’t know what are the features/specs listed above, I will cover all in due course.
Pin diagram of PIC16F84A:
As you can see, it is a 18 pin IC. Below are the pin descriptions:
Pin 5: Vss – This is the ground pin of the IC and must be connected to the negative terminal of the battery.
Pin 14: Vdd – This is the supply pin of the IC and must be connected to the positive terminal of the battery. (Remember maximum you can use a 5 volt battery, battery rated beyond this value may damage this buddy).
Pin 4: MCLR – Memory Clear. This is a active low pin. That means, it performs its assigned function, when it is kept low (connected to the ground). Obviously, this pin is used to clear the temporary RAM memory. Always, when the controller is in operation, this pin is connected to positive supply
Pins 15&16: Oscillator in/out – here you connect a crystal oscillator. Maximum frequency you can use is 20MHz. You can use a 4MHz crystal also. The higher the frequency of crystal used, the faster the controller works (But they’ve set limit for it – Maximum 20MHz which is faster enough for all applications using this controller. To get a picture, if you use a 20MHz crystal oscillator, it can execute 20,000,000 instructions per second!). Here we will mostly use crystal oscillators as they are very cheap, easy to use, accurate and small.
Pins 1,2,3,6,7,8,9,10,11,12,13,17&18: These are the 13 I/O pins. They are grouped into two groups. Port A which contains 5 pins (17,18,1,2&3) and Port B which contains 8 pins(6,7,8,9,10,11,12&13)
As mentioned earlier, these 13 I/O pins can be individually configured either as input or output and each pin can supply (source) or it can absorb(sink) a maximum of 25mA current. So, each pin can drive a LED easily, but it cannot drive any dc motors directly, and for this purpose we use special IC’s called motor drivers.
The Watchdog Timer:
I’ve listed that this PIC contains an onboard timer called watchdog timer. Watchdog timer is an internal timer inside the chip. You can enable or disable the timer by programming. The main use of the timer is to reset the microcontroller when its program goes faulty or it enters into a infinite loop. To understand the watchdog timer clearly, consider it as a count-down timer and say, it starts counting down from 1000 and comes to zero. At the start of the program, the watchdog timer will be started. Before the timer reaches zero (in our example), the program must reassign it to 1000 again. i.e., the watchdog timer is never allowed to go to zero in normal operation. Say, for each 3 instruction, we reset the watchdog timer to its initial value. So, if any error happens in our program, or when our program enters any infinite loop, it cannot reset the watchdog timer. Hence, when the timer reaches zero count, it resets the microcontroller and the program begins from the beginning. Thus the watchdog timer, monitors the working of the program and resets the program when any faults occur. This timer is used in advanced applications.
There are two types of registers here – General Purpose Registers(GPR) and Special Function Registers (SFR). Its obvious from the name that the general purpose registers are used to store any arbitrary value to operate on. The special function registers are used to perform some functions which controls the device.
Here, as you can see, the registers are split into two banks, Bank 1 and Bank 2 (given below in the above diagram). Here, registers from 00H to 0BH and 80H to 8BH are SFRs remaining are GPRs. Each bank is 128 bytes in length and first 12 locations in each bank are reserved for SFRs and the remaining are GPRs implemented as static RAM. Here we are more concerned with SFRs than the GFRs. Here, I will explain 5 SFRs in detail – STATUS, PORTA, PORTB, TRISA, TRISB. (Note that all are 8 bit registers) and W register.
STATUS: We use this register mainly for one purpose – to switch between the banks. When the fifth bit of this register is set, we are in bank 1, when the fifth bit is reset, we are in bank 0
PORTA: The five of the eight bits in the register corresponds to the 5 pins of port A (17,18,1,2&3 pins). Here the least significant bit corresponds to RA0
PORTB: The eight bits of this register corresponds to the 8 pins of port B(6,7,8,9,10,11,12&13 pins). Same is the case here – LSB is RB0 and MSB is RB7
TRISA: This register is used to set (decide) whether the port A pins are input or output. Ones represent input and zeros represent outputs. For example, 10010 written on the TRISA register specifies that RA0 is configured as output, RA1 as input, RA2 as output, RA3 as output and RA4 as input. (pretty clear?)
TRISB: Similar is the case with TRISB register. It is used to configure port B pins as inputs/outputs.
W Register: This is the general purpose register accessible by program. You can write values directly only into W register. So, when you want to write values into any of the registers, say TRISA or PORTA etc, you must first write the values to w register and then move it to the target register using another instruction in the next step. It corresponds to A register (Accumulator) in the 8085 processor. (Note: The W register is not in the register banks. It is found separated from the register banks. All registers in the register banks are not accessible directly, and values can be written to them only through the W register.
In next tutorial, I will show you how to write program into this controller.