This web site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more.

Pic microcontrollers - Programming rudiments

Last update: 31/07/2015


A microcontroller can be considered a natural evolution of the microprocessor but while any cpu needs of a large external circuitry to operate, microcontrollers have been created including, in the same package, almost all components needed to its funcionality.

On the market there are tens of makers and thousands of different microcontrollers simply to meet all market needs. Nowadays, in fact, we can find microcontrollers virtually in any electronic appliances of the real world or almost any mechanical electronically controlled machine. That's why a microcontroller is simply a small programmable electronic device created to resolve activities where a 'logic' is necessary.

Sometimes it is not easy the choice of the right microcontroller, there are many, probably too many  chips different for modules/functions, number of pins, speed, package, functionality.

Some maker provide an online microcontroller selector (like Microchip or STM) where we can select the right microcontroller according to our needs. If we have to use a chip, for instance, to switch on/off only a couple of led diodes even the simpler 6-8 pins microcontroller can be considered the right choice but if we have to switch a led diode according to an analog value on a particular pin we must use a microcontroller characterized by the presence of an analog/digital converter module.

There is an important point to consider regarding the choice of the microcontroller: there are a lot of makers creating a lot of wonderful microcontrollers but we have to be really careful to the price of their development environment. We can develop programs for such a chips using languages we already know: assembler, pascal, basic, c,  and other language subsets created in the freeware world but a professional development environment has a price, sometimes quite high.

Microchip microntrollers are widely used even for this reason: their development environment are not too much expensive, going from the freeware (assembler, c, jal) to a reasonable price (mikroc, mikrobasic, picbasic etc.). Moreover, often, it is possible to download fully useable demo versions limited only by the size of the program (to check before to buy).

 pic 12f675  

As an example here is the microchip 12F675 microcontroller. It is only an 8 pins device but it is characterized by the presence of:

  • 4 analog to digital converters (an0 to an3) - 10 bits resolution;
  • 6 general I/O pins;
  • 1 analog comparator;
  • internal or external oscillator;
  • interrupt on pin change;
  • watchdog timer;
  • 1K flash words;
  • programmable weak pull ups resistors;
  • 64bytes Sram and 128bytes EEprom;

... all this in only an 8 pins device that can be programmed and reprogrammed thousands of times. Interesting, isn't it?

The functions/modules performed by any pin of the microcontroller can be quickly explained according to the following table:


VDD-VSS These are the supply pins of the picmicro. Usually (but it is important to read the picmicro datasheet) the range is between 2V and 5.5V.



These pins are used to connect an RC or a Crystal  or an oscillator to provide the timing to the microcontroller. Many microcontrollers are characterized even by the presence of an internal oscillator that must be enabled during the programming phase (INTOSC). The datasheet reports the working frequency of a picmicro, generally are used crystal or oscillators starting to from 4 to 20 mhz (even more for modern devices). The datasheet will report the right values.
GP0....GP5 In case of  small devices, general purpose pins are represented  as GP0 ... GP5. They are normal pins that can be configured as input or output pins according to the data direction register: if TRISIO bit is set to 1 the port will be an input if the corrispondent bit of TRISIO is 0 the port pin will be used as output. The six bit register, infact, can set any pin to be input or output with the exception of GP3 that can be only an input pin.
 INT Every GPIO port pin can be configured as 'interrupt pin' according to the control register. It is an interrupt on change value. If a pin is set to be an interrupt pin every time its input value change the program will stop and will execute a routine or will wake the device from a 'sleep' state
CIN+/CIN- These pins regard the internal comparator module of the microcontroller and are multiplexed with the GP0 and GP1 pins.
ICSPDAT/ICSPCLK These pins are used for in-circuit programming (togheter MCLR and power pins)
T0CK1/T1CK1 These pin are timer/clock inputs. Usually are used to generate an interrupt when the internal counter reach the value of '0'. The timer 0 is an 8 bits timer while the timer1 is a 16bits timer
VREF It is used for an internally generated voltage reference value for the comparators input
MCLR-VPP When low resets the micro re-starting the program at the beginning and (VPP) a programming voltage input

Naturally such pins cannot perform all functions at the same time. Every function must be set through its control register. Modifying the value of the right register we can decide which function a particular pin must perform.

We have seen one of the most simple microcontroller on the market. But the market is full of more sophisticated microcontrollers characterized by  many input/output pins, comparators, a/d converters, uart, etc.


This is an 18pins picmicro very popular, the 16F628. In this case the  input/output ports are denominated  RA or RB.

Without considering the number of pins, there are a few differences from the above mentioned 12f675. Even here we have a/d converters, interrupts, comparators etc. but in this case we have even a couple of pins relative to an internal UART to communicate serially with other devices or with any appliance using such communication, even a personal computer.



Capture, Compare, PWM is a module strictly connected to the internal timers of the picmicro

  • capture: capture the value of timer1 when a particular event is on a particular pin
  • compare: compare the value of timer1 with a value charged on a particular register
  • PWM: generates an hardware pulse width modulated signal on the pin. Frequency and duty cicle can be modified.
PGC-PGD Are pins used for in-circuit programming (really interesting because we can program and reprogram many and many times the picmicro without remove it from the pcb)


Nowadays the microcontrollers word has reached a large development and there are 'families' of picmicro created for any necessity. Only considering the Microchip we can find picmicro:

  • with 6 upto 100 pins;
  • 8, 16 or 32 bits architecture;
  • 4 upto 80 mhz working frequency;
  • 0.25 upto 512K of programming memory and 0.256 upto 32K volatile memory;
  • presence of I2C, USART, SPI, CAN, USB and other communication ports;

The programming of such devices? in our humble opinion the use of the assembler can be limited to the simpler devices. A large number of (free or affordable) traditional languages and development environments have been created to simplify all aspects of the microntrollers programming and testing, providing all libraries necessary to a quick set up of the microcontrollers and routines very difficult to obtain using low level languages (floating point math for instance).

In any case, before staring to program a microcontroller, the reading of its datasheet have to be considered a must.

Diritti riservati 2008-2015. Tutti i marchi riportati sono detenuti dai legittimi proprietari. Tutto il materiale e` liberamente utilizzabile ma non per scopi commerciali. Non si assume alcuna responsabilita` sul materiale pubblicato per eventuali danni diretti o indiretti che possano derivare dall`utilizzo di schemi, progetti o altro materiale presente nel sito. Inviare una mail a: (contenuti) (webserver) per domande e/o suggerimenti.