Vous êtes sur la page 1sur 19



Conducted by Hamza Azeem.
In collaboration with Engineering Society,
College of Engineering.

PAF Karachi Institute of Economics and Technology.

Table of Contents
Part I. Microcontrollers..01
Part II. PIC Microcontroller...02
History of PIC Microcontroller..02
How to tell them apart? ................................................................................................03
Selecting the Microcontroller.........................................................................................04
Hardware Architecture PIC16F87705
PIC16F877 Pin out 05
Part III. Making Your First Project07
Hardware Design....08
Analog-to-Digital Conversion08
LM35-Precision Temperature Sensor09
16x2 Character LCD..09
Serial Communication RS232.10
Complete Hardware12
Software Design...13
Define ADC Resolution...13
Configure USART13
Configure ADC13
Fetch data from ADC14
Scaling the raw data..14
Send Information to PC.15
Send Information to LCD..15
Complete Software16

I. Microcontroller
1. What is a Microcontroller?
The microcontroller is simply a computer on a chip. It is one of the most important
developments in electronics since the invention of the microprocessor itself. It is essential for
the operation of devices such as mobile phones, DVD players, video cameras, and most selfcontained electronic systems. The small LCD screen is a good clue to the presence of an
MCU (Microcontroller Unit) it needs a programmed device to control it. Working
sometimes with other chips, but often on its own, the MCU provides the key element in the
vast range of small, programmed devices which are now commonplace. Although small,
microcontrollers are complex, and we have to look carefully at the way the hardware and
software (control program) work together to understand the processes at work. This manual
will show how to connect the popular PIC range of microcontrollers to the outside world, and
put them to work. To keep things simple, we will concentrate on just one device, the PIC
16F877, which has a good range of features and allows most of the essential techniques to be
explained. It has a set of serial ports built in, which are used to transfer data to and from other
devices, as well as analogue inputs, which allow measurement of inputs such as temperature.
All standard types of microcontrollers work in a similar way, so analysis of one will make it
possible to understand all the others. The PIC 16F877 is also a good choice for learning about
micro-controllers, because the programming language is relatively simple, as compared with
a microprocessor such as the Intel Pentium, which is used in the PC. This has a powerful,
but complex, instruction set to support advanced multimedia applications. The supporting
documentation for the PIC MCU is well designed, and a development system, for writing and
testing programs, can be downloaded free from the Microchip website

Figure 1.1: Microcontroller System Contrasted with Microprocessor System

Introduction to PIC Microcontroller with PROTEUS

Page 1

II. PIC Microcontroller

1. History of PIC Microcontroller
PICs are inexpensive one-chip computers designed and manufactured by Microchip
Technology Inc. The acronym originally stood for Programmable Intelligent Computer, but
Microchip's official name for these devices is now PICmicro microcontrollers. We will call
them PICs. In 1977, General Instruments., Microchips predecessor, developed the original
PIC, the PIC1650. The PIC1650 can be thought of as the grandfather of today's PI, and its
architecture, programming approach and other features directly correspond to those found in
modern PICs. Its instruction set and register arrangement mirror current PICs with only minor
General instruments sold its microcontroller business in the mid-1980s to the entity that later
became Microchip. Microchip's current product line includes nearly 200 PIC models.
Microchip has sold more than 2 billion PICs since the mid-1980s, and in 2002 was number
one worldwide in 8-bit microcontroller sales, based on number of units.

Figure 1.2: Simplified view of PIC Microcontroller

Introduction to PIC Microcontroller with PROTEUS

Page 2

2. How to tell them apart?

Microchip identifies PICs with a multipart identifier such as a 16F877A-E/P:
Microchip groups its PIC line in three performance and three memory type categories:

Introduction to PIC Microcontroller with PROTEUS

Page 3

3. Selecting the Microcontroller

The following table gives complete comparison of the famous and popular models of PIC.

Introduction to PIC Microcontroller with PROTEUS

Page 4

4. Hardware Architecture PIC16F877

Microcontrollers contain all the components required for a processor system in one chip: a
CPU, memory and I/O. A complete system can therefore be built using one MCU chip and a
few I/O devices such as a keypad, display and other interfacing circuits. We will now see
how this is done in practice in our typical microcontroller.

5. PIC16F877 Pin Out

Let us first consider the pins that are seen on the IC package, and we can then discover how
they relate the internal architecture. The chip can be obtained in different packages, such as
conventional 40-pin DIP (Dual In-Line Package), square surface mount or socket format. The
DIP version is recommended for prototyping, and is shown in Figure 1.3.
Most of the pins are for input and output, and arranged as 5 ports: A(5), B(8), C(8), D(8) and
E(3), giving a total of 32 I/O pins. These can all operate as simple digital I/O pins, but most
have more than one function, and the mode of operation of each is selected by initializing
various control registers within the chip. Note, in particular, that Ports A and E become
ANALOGUE INPUTS by default (on power up or reset), so they have to set up for digital
I/O if required. Port B is used for downloading the program to the chip flash ROM (RB6 and
RB7), and RB0 and RB4RB7 can generate an interrupt. Port C gives access to timers and
serial ports, while Port D can be used as a slave port, with Port E providing the control pins
for this function. All these options will be explained in detail later.

Introduction to PIC Microcontroller with PROTEUS

Page 5

Figure 1.3: PIC16F877 Pin out

Figure 1.4: Programmers for PIC Microcontroller

Introduction to PIC Microcontroller with PROTEUS

Page 6

Part III. Making your first project

1. Objective
Build a system which measures ambient temperature and displays it on LCD screen and
sends it to PC through serial port.
In this workshop we will build the above mentioned system in Proteus simulator, using the
interactive tools. Figure 3.1 shows the basic block diagram of the system.




Figure 3.1: Basic Block Diagram of the System

The system compromises of the following parts;

1. LM35 measurement through internal ADC.
2. Interfacing of 16x2 LCD in 4-Bit Mode.
3. Send data on Serial Port using USART.

In the next section we will complete our objective step-by-step by competing above
mentioned parts. First we will configure the ADC for reading LM35. Then after scaling it we
will move on to reading the inputs. After that we will send the data to LCD Screen and PC.

Introduction to PIC Microcontroller with PROTEUS

Page 7

2. Hardware Design
(a). Analog-to-Digital Conversion
Generally PIC Microcontrollers offer 10 Bit of Analog to Digital Conversion. This means
that when they measure an incoming voltage, they compare that voltage to a reference
voltage, and give you the comparison represented as a 10-bit number (0-1023).
You can wire as many analog inputs to the PIC as there are analog ports to accept them.
However, you will notice some strange effects when you do so. The ADC circuits will affect
each others readings, because all the circuits are pulling off the same +5V source. One thing
you can do to minimize this is by using decoupling capacitors by each analog input, to
smooth out dips and surges in the current caused by the other ADCs. Typically 0.1F to 1F
will do the job. Place the capacitors between power and ground, as physically close to the
ADC input as you can get.
It also helps if you increase the sampling time and the delay between conversion and reading
commands. Too much delay and you sacrifice interactivity; too little delay and you get
random numbers. Start with the numbers in the sample code above, and vary them until you
get readings youre happy with.
Finally, it helps to sample at a lower resolution. Sampling at 8 bits instead of 10 will improve
the speed and stability of a multiple ADC program.
Since the ADC registers provide you with 10 bit number converted from the analog input,
you can use the following formula to have input voltage;

Introduction to PIC Microcontroller with PROTEUS

Page 8

(b). LM35 Precision Centigrade Temperature Sensor

LM35 is an integrated circuit sensor that can be used to measure temperature with an
electrical output proportional to the temperature in centigrade. The main reason behind using
LM35 is that it doesnt need amplification like thermocouples. The conversion ratio of LM35

is 10mV/C . For e.g at temperature of 35 degrees LM35 will output 350 mV.
(c). Interfacing 16x2 LCD
The small LCDs are widely used as output devices with Microcontrollers. The best suitable
size is 16x2 LCD. They are very easy to use with small microcontrollers like 8-bit ones. The
following are the schematic diagrams with basic connections.

Introduction to PIC Microcontroller with PROTEUS

Page 9

(d). Serial Communication RS232 USART

MAX232 is used for level translation between TTL and RS232. It is very common and
integral component while communication between Microcontrollers and PC using RS232.
The USART of PIC works on0-5V levels while RS232 works over -15 to +15 level. The
MAX232 acts as a bridge between these two standards.
The RS232 is a type of serial communication whose voltage levels range from -15v to +15v.
RS232 doesnt deal with the software protocols. Its just the hardware specifications which
are followed in the serial port of PC. The main governing body is USART/UART. They deal
with all the baud rate, parity, stop/start bits, timings, encoding and all other software
specifications of serial communication.
Good news is that you really dont have to worry about all this software stuff while setting up
communication. All you need is to build a very common voltage level translator. But hey
what the heck is that ?
Hold on. As I said at the start
that PC uses RS232 which works on plus minus fifteen volts. And our lovely PIC
microcontroller features UART (or USART) which workd on zero to five volts range.
So to translate 0-5 Volts communication into -15 to +15v communication, we need to have a
level translator such as MAX232.
All you need is to circuit it between microcontroller and PC and a connecting cable, and voila
you are done with hardware.

The circuit comprises of ;

1 uF Polarized Caps Qty 4
10 uf Polarized Cap Qty 1
MAX232 IC Qty 1
DB9 Female Connector Qty 1
Introduction to PIC Microcontroller with PROTEUS

Page 10

Connect the components as shown above. I would recommend you to make a separate board
for this so that you can use it in various projects.
In our application, the RX will be connected to PIN C6 of PIC and TX to PIN C7.
The complete schematic is as follows;

The power connections for MAX232 are not shown in above diagram.

Introduction to PIC Microcontroller with PROTEUS

Page 11

(e). Complete Hardware:

Following is the complete schematic combining all above separate parts;

Introduction to PIC Microcontroller with PROTEUS

Page 12

2. Software Design:
The software is divided into following steps:

Define ADC Resolution.

Configure USART with RS232 settings.
Initialize ADC.
Reading ADC Value.
Conversion into Centigrade.
Send temperature reading to PC over USART.
Send temperature reading to 16x2 LCD.

Steps 4 to 7 will be performed under an infinite loop. But a delay will be added explained
(a).Define ADC Resolution
The above statement is used to define the resolution of ADC in bits. The maximum resolution
for PIC16F876 is 10-Bit. But you can define it to any number less than the maximum
(b). Initialize and configure USART with RS232 settings.
The following line is used to initialize and configure USART to start communication.
#use rs232 (baud=57600,rcv=PIN_C7, xmit=PIN_C6)
This line is used just after the oscillator clock declaration. Usually you have to give it
Baudrate and the pins used for communication. If you want to use the built-in hardware
USART then you use the above declared pins. But if you like more than one serial
communication ports, you can declare communication on any pins. But note that using pins
other than C6 and C7 will involve software serial data handling. And it can compromise
speed as compared to hardware USART.
(c). Initialize and Configure ADC
setup_adc_ports( ALL_ANALOG );
set_adc_channel( 0 );
These three lines setup the ADC and initialize it. setup_adc_ports is used to define which
pins are to be used as analog inputs. Most suitable is ALL_ANALOG as it makes all AN pins
as analog.

Introduction to PIC Microcontroller with PROTEUS

Page 13

The next statement defines the clock source for ADC. As PIC also has external clock feature
for ADC conversion. The best is internal clock.
To select the pin from which to read the analog voltages we use set_adc_channel . In our case
LM35 is connected to Pin AN0, so we select Channel 0.
(d).Fetch data from ADC
value = read_adc();
The function read_adc() is used to read the value from ADC Register. It contains the digital
equivalent value of analog input.
For e.g. if we input 2.5 volts and the resolution defined for ADC is 10-Bit, we will get a value
of 512 from read_adc().
(e). Scaling the raw data
As we know that we get a digital value equivalent to in out analog voltage. So first we
convert it into voltage value;

Please note that by default the reference voltage for ADC in PIC is 5V.
For e.g if we get a value of 512 from ADC of 10-Bit resolution, the formula will have
following values ;

We have got the voltage value which is coming from ADC, now we convert it into
Temperature with help of formula provided by LM35;

If voltage of 0.375 is read from ADC, the above formula will return Temperature of 37.5C.
Implementing both the equations in C;
Introduction to PIC Microcontroller with PROTEUS

Page 14

volt = (value / 1023) * 5;

temp = volt * 100;
Read and conversion of voltages into digital and then temperature is done. In next we will
send the data to PC using very simple command.
(f). Send temperature reading to PC over USART.
The very reason of popularity of CCS C Compiler is its ease of use and powerful commands.
Specially dealing with serial communication is very easy in this compiler. To send data to PC
all you need to do is to use the following one line function;
printf(Temperature: %.1f\n\r,temp);
If you have used TurboC you maybe very well familiarized with printf. This function is the
same of function but the only difference is that when used in TurboC it displays on Monitor
and when used in PIC it sends data over to serial port.
The end result of this function at serial port would be;
Temperature: 37.5
(g). Send temperature reading to 16x2 LCD.
As we have sent the reading to PC same way we will send the data to 16x2 LCD. This time
only one command will do the entire trick and that too is printf. Lets see how;
printf(lcd_putc,"\fTemp: %.0f",temp);

Introduction to PIC Microcontroller with PROTEUS

Page 15

(h). Complete Code

#include <16F877.h>
#use delay(clock=20000000)
#use rs232 (baud=57600,rcv=PIN_C7, xmit=PIN_C6)
#include <lcd.c>
float value;
float temp;
float volt;
void main()
//Initialize and Configure ADC
setup_adc_ports( ALL_ANALOG );
set_adc_channel( 0 );
//1 Sec Delay
//Read ADC Value
value = read_adc();
//Convert Value into Volts
volt = (value / 1023) * 5;
//Convert Volts into Temperature
temp = volt * 100;
//Send data to PC
printf("Temperature: %.0f\n\r",temp);
//Send data to LCD
printf(lcd_putc,"\fTemp: %.0f",temp);

Introduction to PIC Microcontroller with PROTEUS

Page 16

This workshop manual provides ground to basic tools and techniques and how to use them.
Though it doesnt concentrate much on theoretical side but provides hands-on knowledge on
how to make first application. This manual also provides reference to routine statements and
The application used here contains basic elements which are found in every microcontroller
project. Hence the application gives student an overview and confidence on this new

Mazidi, (2007). PIC Microcontroller and Embedded Systems : Peterson
Dogan Ibrahim, (2009). Advanced PIC Microcontroller Projects in C: Harper Collins

Introduction to PIC Microcontroller with PROTEUS

Page 17