You are on page 1of 11

2021

15 July

Submitted to Madam komal Munir

Submitted by
M & M Lab Final Project
Muhammad Aqib 19-EE-194 (B)

Muhammad Mugheera 19-EE-170 (B)

Muhammad Moiz ur Rahman Malik 19-EE-198 (B)

Microprocessors & Microcontrollers


Microprocessor consists of only a Central Processing Unit, whereas Micro
Controller contains a CPU, Memory, I/O all integrated into one chip. ...
Microprocessor uses an external bus to interface to RAM, ROM, and other
peripherals, on the other hand, Microcontroller uses an internal controlling bus.
Final Project Analog to Digital Converter

Brief Description

Analog-to-Digital converters (ADC) translate analog signals, real world signals like temperature,
pressure, voltage, current, distance, or light intensity, into a digital representation of that signal. This digital
representation can then be processed, manipulated, computed, transmitted or stored. An ADC samples an analog
waveform at uniform time intervals and assigns a digital value to each sample. The digital value appears on the
converter’s output in a binary coded format. The value is obtained by dividing the sampled analog input voltage
by the reference voltage and them multiplying by the number of digital codes. The resolution of converter is set
by the number of binary bits in the output code. An ADC carries out two processes, sampling and quantization.
The ADC represents an analog signal, which has infinite resolution, as a digital code that has finite resolution.
The ADC produces 2N digital values where N represents the number of binary output bits. The analog input
signal will fall between the quantization levels because the converter has finite resolution resulting in an
inherent uncertainty or quantization error. That error determines the maximum dynamic range of the converter.

In many cases, the analog to digital conversion process is just one step within a larger measurement and
control loop where digitized data is processed and then reconverted back to analog signals to drive external
transducers. These transducers can include things like motors, heaters and acoustic divers like loudspeakers.
The performance required of the ADC will reflect the performance goals of the measurement and control loop.
ADC performance needs will also reflect the capabilities and requirements of the other signal processing
elements in the loop.

A multimeter is a measuring instrument that can measure multiple electrical properties. A typical
multimeter can measure voltage, resistance, and current, in which case it is also known as a volt-ohm-
milliammeter (VOM). Analog mustimeters use a microammeter with a moving pointer to display readings.
Digital multimeters (DMM, DVOM) have numeric displays and have made analog multimeters obsolete as they
are cheaper, more precise, and more physically robust than analog multimeters.

A multimeter is the combination of a DC voltmeter, AC voltmeter, ammeter, and ohmmeter. An un-amplified


analog multimeter combines a meter movement, range resistors and switches; VTVMs are amplified analog
meters and contain active circuitry. Digital mustimeters generally take measurements with accuracy superior to
their analog counterparts. Standard analog multimeters measure with typically ±3% accuracy. though
instruments of higher accuracy are made. Standard portable digital multimeters are specified to have an
accuracy of typically ±0.5% on the DC voltage ranges. Mainstream bench-top multimeters are available with
specified accuracy of better than ±0.01%. Laboratory grade instruments can have accuracies of a few parts per
million

Advantages & Disadvantages


 It offers automatic output display.  It does not do well with measurement
 It ensures accuracy. fluctuations.
 It has auto polarity functions.  It is more expensive than the analog type.
 It can be difficult to find one for your specific
needs.
Code:

#include "Includes.h"

//19-EE-194

//19-EE-170

//19-EE-198

// Configuration word for PIC16F877


__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON
& LVP_OFF & CPD_OFF & WRT_ON & DEBUG_OFF);

void InitADC(void)
{
ADCON1 = 0x80; // Make PORTA and PORTE analog pins
// Also, Vref+ = 5v and Vref- = GND
TRISA = 0x2f; // Make RA5, RA3, RA2, RA1, RA0 input
TRISE = 0x07; // Make RE0, RE1 and RE2 input
ADCON0 = 0x81; // Turn on the A/D Converter
}

/*
* Function Name: GetADCValue
* Input(s) : Channel name, it can be AN0, AN1, AN2, AN3, AN4
* AN5, AN6 or AN7 only. Channel is selected according
* to the pin you want to use in the ADC conversion.
* For example, use AN0 for RA0 pin. Similarly for
* RA1 pin use AN1 etc.
* Output(s): 10 bit ADC value is read from the pin and returned.
*/
unsigned int GetADCValue(unsigned char Channel)
{
ADCON0 &= 0xc7; // Clear Channel selection bits
ADCON0 |= (Channel<<3); // Select channel pin as ADC input

__delay_ms(10); // Time for Acqusition capacitor


// to charge up and show correct value
GO_nDONE = 1; // Enable Go/Done

while(GO_nDONE); // Wait for conversion completion


return ((ADRESH<<8)+ADRESL); // Return 10 bit ADC value
}
// Main Function
void main(void)

{
unsigned int ADC_value = 0;
unsigned int digit1, digit2, digit3, digit4;

InitADC(); // Initialize ADC


InitLCD(); // Initialize LCD

while(1)

{
ClearLCDScreen(); // Clear LCD screen
ADC_value = GetADCValue(AN7); // Read ADC value from RE2(AN7) pin

// ADC_value can have a value from 0 (0v) to 1023(5v) only.


// SO display 4 digits of ADC_value
digit1 = (unsigned int)(ADC_value/1000); // Calculate digit1 of ADC_value
digit2 = (unsigned int)((ADC_value - digit1*1000)/100); // Calculate digit2 of ADC_value
digit3 = (unsigned int)((ADC_value - (digit1*1000+digit2*100))/10); // Calculate digit3 of
ADC_value
digit4 = (unsigned int)(ADC_value - (digit1*1000+digit2*100+digit3*10)); // Calculate digit4 of
ADC_value

WriteDataToLCD(digit1+0x30); // Display digit1 of ADC_value on LCD


WriteDataToLCD(digit2+0x30); // Display digit2 of ADC_value on LCD
WriteDataToLCD(digit3+0x30); // Display digit3 of ADC_value on LCD
WriteDataToLCD(digit4+0x30); // Display digit4 of ADC_value on LCD

__delay_ms(500); // Half second delay before next reading


}

Outputs:
Theoretical Background and components:

MikroC:

The mikroC PRO for PIC is a full-featured ANSI C compiler for PIC devices from Microchip®. It is the
best solution for developing code for PIC devices. It features intuitive IDE, powerful compiler with
advanced optimizations, lots of hardware and software libraries, and additional tools that will help you in
your work.

Proteus:

The Proteus Design Suite is a proprietary software tool suite used primarily for electronic design
automation. It is a circuit designer and simulator used for running microcontrollers and other devices. We
can simulate complex circuits without any real-life risk of ruining the expensive devices before finalizing
them in real life.

PIC16F877a Pinout
So, now I hope that you got the complete understanding of PIC16F877a Basic Circuit, so now if you have
noticed that in the basic circuit, we have used all the power pins of PIC Microcontroller, while all the Ports Pins
were free.

So, now as we have powered up our PIC Microcontroller, the next thing we need to do is to design some code
and use the PIC Microcontroller Ports. First, let's have a look at these PIC16F877a Ports.

PIC16F877a has 5 Ports in total which are:

Port A:

It has 6 Pins in total starting from Pin # 2 to Pin # 7. Port A Pins are labelled from RA0 to RA5 where RA0 is
the label of the first Pin of Port A.

Port B:

It has 8 Pins in total starting from Pin # 33 to Pin # 40. Port B Pins are labelled from RB0 to RB7 where RB0 is
the label of the first Pin of Port B.

Port C:

It has 8 Pins in total. Its pins are not aligned together. The first four Pins of Port C are located at Pin # 15 - Pin #
18, while the last four are located at Pin # 23 - Pin # 26.

Port D:

It has 8 Pins in total. Its pins are also not aligned together. The first four Pins of Port D are located at Pin # 19 -
Pin # 22, while the last four are located at Pin # 27 - Pin # 30.

Port E:

It has 3 Pins in total starting from Pin # 8 to Pin # 10. Port E Pins are labelled from RE0 to RE2 where RE0 is
the label of the first Pin of Port E.

All these Ports are labelled in the below figure:

PIC16F877a, introduction to PIC16F877a, getting started with PIC16F877a, basics of PIC16F877a,


PIC16F877a basics, PIC16F877a tutorial, PIC16F877a intro

You can see all these PIC16F877a Ports in the above figure, now let's have a look at how to use them. First,
what you need to decide is whether you want your Port Pins to be Input or Output. Confused? :P Let's suppose
you have some sensor, and you want to get its value, then you must connect this sensor with PIC
Microcontroller, now in this case your PIC Pin will be acting as Input Pin because it will be inputting value
from the sensor. The sensor is sending the value and PIC is receiving it.

But in the case of a DC Motor Control with PIC, you must send commands from PIC Microcontroller to DC
Motor, so your PIC Pin is acting as Output Pin.
PIC16F877a Serial Port

PIC16F877a has one serial port in it which is used for data communication.

In the below figure, I have mentioned the Serial Pins of PIC16F877a.

PIC16F877a, PIC16F877a basic circuit, introduction to PIC16F877a, getting started with PIC16F877a, basics of
PIC16F877a, PIC16F877a basics, PIC16F877a tutorial, PIC16F877a intro

AS you can see in the above figure that:

Pin # 25 is acting as TX as well so if you want to do Serial Communication then it will be used for sending
the serial data.

Pin # 26 is acting as RX as well so if you want to do Serial Communication then it will be used for
receiving the serial data.

You should also have a look at What is Serial Port if you don't know much about Serial Port.

PIC16F877a I2C Communication

PIC16F877a also has one I2C Port using which we can easily do the I2C Communication.

These PIC16F877a I2C Communication Pins are shown in the below figure:

PIC16F877a, PIC16F877a basic circuit, introduction to PIC16F877a, getting started with PIC16F877a, basics of
PIC16F877a, PIC16F877a basics, PIC16F877a tutorial, PIC16F877a intro

As you can see in the above figure, PIC16F877a I2C Communication Pins are:

Pin # 18: It is acting as SCL which is an abbreviation of Serial Clock Line.

Pin # 23: It is acting as SDA which is an abbreviation of Serial Data Line.

Now you can see we have Serial Port and I2C Port in Port C, so we can use Port C as a simple Port but can also
do these two communications with its pins, so its totally on the programmer.
PIC16F877a Interrupts

I hope you all know about interrupts, if not then you should have a look at Interrupts in PIC Microcontroller.

PIC16F877a has 8 interrupt sources in it. An interrupt source is some event that generates interrupt, this source
could be a timer like interrupts are generated after every 1 sec, or it could also be pin state change event, like if
pin state is changed then interrupt will be generated.

So, PIC16F877a Interrupts can be generated by following 8 ways:

External Interrupts.

Timer Interrupts ( Timer0 / Timer1).

Port B State Change.

Parallel Slave Port Read/Write.

A/D Converter.

Serial Receive / Transmit.

PWM (CCP1 / CCP2).

EEPROM Write Operation.

PIC16F877a Interrupts are associated with below 5 registers:

INTCON

PIE1

PIR1

PIE2

PIR2

LCD 16×2 (LM016L):

The most used Character based LCDs are based on

Hitachi’s HD44780 controller or other which are

compatible with HD44580. we will discuss about


character-based LCDs, their interfacing with various microcontrollers, various interfaces (8-bit/4-bit),
programming, special stuff and tricks you can do with these simple looking LCDs which can give a new look to
your application.

For Specs and technical information HD44780 controller Click Here

LCD (2)

PIN Description

The most used LCDs found in the market today are 1 Line, 2 Line or 4 Line LCDs which have only 1 controller
and support at most of 80 characters, whereas LCDs supporting more than 80 characters make use of 2
HD44780 controllers.

Most LCDs with 1 controller have 14 Pins and LCDs with 2 controllers

PIC6877A MICROCONTROLLER:

There are 40 pins of this microcontroller IC. It consists of two 8 bit and one 16-bit timer. Capture and
compare modules, serial ports, parallel ports and five input/output ports are also present in it. This picture
shows the pinout diagram of PIC16F877A.

The pins of the micro controller that we will specifically be focusing on for this project are:
PIN 1: MCLR: The first pin is the master clear pin of this IC. It resets the microcontroller and is active
low, meaning that it should constantly be given a voltage of 5V and if 0 V are given then the controller is
reset. Resetting the controller will bring it back to the first line of the program that has been burned into the
IC.

 A push button and a resistor is connected to the pin. The pin is already being supplied by constant
5V. When we want to reset the IC we just have to push the button which will bring the MCLR pin
to 0 potential thereby resetting the controller.
 PIN 2: RA0/AN0: PORTA consists of 6 pins, from pin 2 to pin 7, all of these are bidirectional
input/output pins. Pin 2 is the first pin of this port. This pin can also be used as an analog pin AN0.
It is built in analog to digital converter.
 Analog to digital converter module: It has 8 bit ADC module which consists of 8 channels. We can use
8 analog sensors with this microcontroller.
 PIN 13: OSC1/CLKIN: This is the oscillator input or the external clock input pin.
 PIN 14: OSC2/CLKOUT: This is the oscillator output pin. A crystal resonator is connected between
pin 13 and 14 to provide external clock to the microcontroller. ¼ of the frequency of OSC1 is
outputted by OSC2 in case of RC mode. This indicates the instruction cycle rate.

 PIN 33-40: PORT B: All these pins belong to PORTB. Out of which RB0 can be used as the
external interrupt pin and RB6 and RB7 can be used as in-circuit debugger pins.

ADC in PIC Microcontroller PIC16F877A:

There are many types of ADC available and each one has its own speed and resolution. The most common
types of ADCs are flash, successive approximation, and sigma-delta. The type of ADC used in
PIC16F877A is called as the Successive approximation ADC or SAR in short. So let’s learn a bit about
SAR ADC before we start using it.

Successive Approximation ADC:

The SAR ADC works with the help of a comparator and some logic conversations. This type of ADC
uses a reference voltage (which is variable) and compares the input voltage with the reference voltage using
a comparator and difference, which will be a digital output, is saved from the Most significant bit (MSB).
The speed of the comparison depends on the Clock frequency (Fosc) on which the PIC is operating.

The PIC we are using has 10-bit 8-channel ADC. This means the output value of our ADC will be 0-1024
(2^10) and there are 8 pins (channels) on our MCU which can read analog voltage. The value 1024 is
obtained by 2^10 since our ADC is 10 bits. Let’s look at the picture below.

 Muhammad Aqib 19-EE-194 (B)


 Muhammad Mugheera 19-EE-170 (B)
 Muhammad Moiz ur Rahman Malik 19-EE-198 (B)

You might also like