Beruflich Dokumente
Kultur Dokumente
ON
INTRODUCTION TO PIC
MICROCONTROLLERS WITH
INTERACTIVE SIMULATION IN PROTEUS
Conducted by Hamza Azeem.
In collaboration with Engineering Society,
College of Engineering.
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
(www.microchip.com).
Page 1
Page 2
Page 3
Page 4
Page 5
Page 6
LCD
LM35
PIC16F877
PC
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.
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;
Page 8
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.
Page 9
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.
Page 11
Page 12
2. Software Design:
The software is divided into following steps:
1.
2.
3.
4.
5.
6.
7.
Steps 4 to 7 will be performed under an infinite loop. But a delay will be added explained
later.
(a).Define ADC Resolution
#DEVICE ADC=10
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
resolution.
(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 );
setup_adc(ADC_CLOCK_INTERNAL );
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.
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
Page 15
Page 16
Summary
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
functions.
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
technology.
References
Mazidi, (2007). PIC Microcontroller and Embedded Systems : Peterson
Dogan Ibrahim, (2009). Advanced PIC Microcontroller Projects in C: Harper Collins
Page 17