Introduction to Microcontrollers – Getting Started with PIC16F84A

Anish December 26, 2011 11 Comments

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.

Description:

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

Features:

  • 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.

Registers:

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.

 

Comments
  • MrPumpKing
    February 6, 2013

    where is the next tutorial of this? .. how could i subscribe in this tutorial?

  • Murphy
    December 18, 2012

    I enjoyed and understood your teaching but when is the next tutorial coming up? How do i program two pic16f84a so that i can configure many ports to build my moving message of 7×45 dot matrix display for school project

  • Anish
    January 9, 2012

    @harika: Could you make yourself clear?. Assuming that you’ve doubt about watchdog timer, what do you mean by “capacitor charges”

  • harika
    January 8, 2012

    THANKU sir,for such a fair explanation.i have a doubt if a timer sets to ‘0’ then only capacitor charges. in normal operation y it does not set to ‘0’ and hw capacitor charges

  • pasha
    December 31, 2011

    you have done an amazing job.you simplified a difficult topic.waiting for next tutorial

  • Ash
    December 30, 2011

    Excellent job on part one, easy to follow and understand. Please continue the tutorials in the same concept and this will be a great series which I for one appreciate as a newcomer to the exciting world of PIC’s.

    Many Thanks

  • Shurid
    December 28, 2011

    Very organized tutorial,Anish. Thanks a lot for your effort. Waiting for the next part.

    One more thing, do u have any plan to write something about uC programming in the series of this tutorial? Please write something from the basic level of uC programming in C. It would be more helpful for beginners like me.

    • December 29, 2011

      @Shurid: Yeah, I will consider that.

  • Anish
    December 28, 2011

    @pallavi – Hi, there are many online resources – exploit the power of google. You can start learning on this very microcontroller – PIC16F84A. At first, it may seem daunting, but if you get started, it goes on as smooth as a river.Just buy that controller, and follow these posts posted here.

  • Pallavi
    December 28, 2011

    can you suggest some “really useful” e-books about microcontrollers? …. also there are so many microcontroller ICs; I am getting confused about from where should I start learning….please suggest….

  • abouzied007
    December 26, 2011

    Very simple and organized topic …..wait for the next.

Leave a Reply

Your email address will not be published. Required fields are marked *