Become a subscriber (Free)

Join 29,000 other subscribers to receive subscriber sale discounts and other free resources.
Don't worry -- youre-mail address is totally secure. I promise to use it only to send you MicroZine.

Beginners guide to the PIC Microcontroller

Part 2 : 

I/O Ports - using microcontrollers to communicate with the 'real' world.

A microcontroller is not much use if you can't control or measure real-world-signals and you do this using an I/O port.

An I/O port is usually as wide as the CPU part of the microcontroller and for mid range PIC is 8 bits wide.

In a any processor (or microcontroller) there is a data bus and address bus and the data bus defines the number of bits of data that can be transferred at any time; In the 12F,16F,18F series devices this is 8 bits at a time. It makes sense that the fastest way to get data into or out of the device is to use a port that has the same width - so ports on these devices have 8 bits.

Each bit of the device is just a clever collection of hardware blocks that allow digital Input and digital output:

Port pin digital hardware for a PIC

Note: Not all port pins have a schmitt trigger input - some have extra hardware and some have less - you have to read the specific datasheet.

The top data latch stores the value you want to output to the pin while the second one down (the TRIS latch) stores the state of the ouput (either digital output or tristated) i.e. the TRIS bit controls the tirstate buffer (triangle and dot). When it is tristated digital information can come into this pin and be read by the RD port and from there into the processing unit.

Other ports have more hardware supporting more functions such as interrupts, TTL buffers. Analogue functions are also multiplexed into some hardware port pins so the analogue signal can be routed into the ADC.

Each port is usually labeled as PORT followed by a capital letter starting at A. So the first port is PORTA and the second is PORTB etc.

Common Port Functions

Each port has a basic functionality that is the same whichever port you use and this is digital I/O.

Each pin of the port corresponds to a pin on the microcontroller which you can set to either digital input or digital output.  

Digital input

So for each port pin set as digital input:

Any voltage below 0.8V is read as logic zero.
any voltage above 2.0V is read as logic one.

Note: This is for a 5V or 3V system (check the data sheet for exact levels when using other power supply voltages).

Digital output

For digital output the following is true (when not loaded)

logic zero output : output voltage <= 0.6V
logic one output  : output voltage >= Vsupply - 0.7V

Note: The actual output voltage depends on the loading at the pin - if a high current is drawn the the output voltage will be lower (I/O pins can sink/source up to 25mA)

Specialized PORTs

Since devices have a small number of pins compared to the number of peripherals in the device each pin is usually multi functional.  You can either use it in its basic digital I/O mode or you can use one of its shared functions.  

Shared functions from internal modules have fixed positions on the device i.e. a USART receive pin in a 16F877 is on pin 26 - this pin is one of the following:

* PORTC bit 7 (RC7) - Digital I/O.
* USART Rx - RS232 Receiver input.
* DT - Data input: synchronous reception.

So you have to decide what you want to use the pin function to be before you wire up the circuit!

Analogue input

A common feature of the modern PIC microcontrollers is a built in 10 bit ADC - you can find this in the following devices (as well as many others):

* 12F675
* 16F88
* 16F877(A)
* 18F2550

This internal peripheral lets you read an analogue input (as long as it is within the range of the microcontroller power supply) and not only that - it has a multiplexer that lets you read analogue voltages from different pins.

Note: The exact pins that are allowed for reading analogue voltages are device specific so you need to read the data sheet to find out that information.

One of the device I find amazing is the 8 pin 12F675 - it has a full 10bit adc built into it, multiplexed to four pins, so you can make it into a 3 channel volt meter (one pin is needed for communication out..  Or you can use the ADC to measure the output of an LM35 temperature sensor.

Using the ADC

With a high level language such as 'C' reading the ADC is a trivial task as all the hard work is done for you: Here is the code to read an ADC using the MikroC compiler:

val = ADC_Read(0);

This reads a value from ADC input zero - and that's all the code you need to know about! - no register setup - no data sheet - totally easy.

Next time...

... Internal timers.

New! Comments

Have your say about what you just read! Leave me a comment in the box below.

Privacy Policy | Contact | About Me

Site Map | Terms of Use

Visit our Facebook Page:
To Visit Click Here

Recent Articles

  1. Arduino String: How to read commands from the serial port.

    For Arduino string operations you can use Object Class Strings or C style strings but which should you use? Also find out how to decode commands and control variables in your programs using strings.

    Read more

  2. A Real Time Clock design (DS1307) with a PIC microcontroller

    Real Time Clock Design (FREE): A Free and Complete RTC design using the DS1307 and a PIC micro (16F88) also re-targetable. This PIC project uses an I2C Clock chip and 7-segment display to create a fou…

    Read more

  3. How to use the ADXL345 for movement sensing and more.

    With the ADXL345 acellerometer you can detect up to 16g! You can also find out how to use it for tap detection and more.

    Read more

  4. Arduino Interrupt : There are Some You May Never Have Heard About!

    Arduino Interrupt Tutorial: Find out how many external there are on an Arduino Uno - The answer is more than two!

    Read more

  5. Easy Switch Debounce

    Switch debounce: Three different ways to debounce input push switches with one amazing method that you can't miss.

    Read more

  6. How to use the ADS1115

    A tutorial on using the ADS1115 precision 16 bit ADC for low power use.

    Read more

Readers Comments

"I wanted to thank
you so so so much
for all the information
you have provided in
your site it's


- Ranish Pottath

"This site really is
the best and my favorite.
I find here many useful
projects and tips."

- Milan


"Awesome site,
very, very easy and nice
to navigate!"

- Matt

Learn Microcontrollers

"Interested in

Sign up for The
Free 7 day guide:


"I am a newbie to PIC
and I wanted to say
 how great your
site has been for me."

- Dave


"Your site is a great
and perfect work.

- Suresh


"I couldn't find the correct
words to define
yourweb site.

Very useful, uncovered,
honest and clear.

Thanks so much for
your time and works.

- Anon

Back to Top