0% found this document useful (0 votes)
43 views

Practical 5 - ADC+PWM

The document describes an experiment involving interfacing analog to digital conversion and pulse width modulation hardware with a PIC microcontroller. The objectives are to write code for ADC and PWM modules, simulate related circuits in Proteus, and build the hardware circuits. Various tasks are outlined to test reading analog voltage values with the ADC and outputting PWM signals with varying duty cycles.

Uploaded by

edanish642004
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Practical 5 - ADC+PWM

The document describes an experiment involving interfacing analog to digital conversion and pulse width modulation hardware with a PIC microcontroller. The objectives are to write code for ADC and PWM modules, simulate related circuits in Proteus, and build the hardware circuits. Various tasks are outlined to test reading analog voltage values with the ADC and outputting PWM signals with varying duty cycles.

Uploaded by

edanish642004
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

ELECTRICAL ENGINEERING DEPARTMENT

ACADEMIC SESSION: _____________

DEC40053 – EMBEDDED SYSTEM APPLICATIONS

PRACTICAL WORK 5 : HARDWARE INTERFACING (ADC & PWM)

PRACTICAL WORK DATE :

LECTURER’S NAME:

CLO/PLO/DK/DP : CLO3/PLO5/DK6/DP1/DP3

TOTAL
STUDENT ID & NAME : MARKS
(100%)

(1)

(2)

(3)

DATE SUBMIT : DATE RETURN :


1 LEARNING OUTCOMES (LO)

1. Construct and simulate real time embedded system application based on PIC16F/PIC18F
microcontroller effectively

2 OBJECTIVE

1. Write C program for ADC and PWM module in PIC18.


2. Draw and simulate ADC and PWM circuit in Proteus.
3. Build ADC and PWM hardware circuit.

3 THEORY

A) Analog to Digital Converter (ADC)

The Analog-to-Digital Converter (ADC) allows conversion of an analog input signal to a 10-bit binary
representation of that signal. This device uses analog inputs, which are multiplexed into a single sample
and hold circuit. The output of the sample and hold is connected to the input of the converter. The
converter generates a 10-bit binary result via successive approximation and stores the conversion result
into the ADC result registers (ADRESL and ADRESH). The ADC voltage reference is software
selectable to either VDD or a voltage applied to the external reference pins. The ADC can generate an
interrupt upon completion of a conversion. This interrupt can be used to wake-up the device from Sleep.

Figure 5.1: ADC Transfer Function

An ADC transfer function Figure 5.1, can derive as a following formula:

DOUT (1024 )
=
V IN V ref + −V ref −
V
DOUT =1024× IN
V ref + −V ref −
Where Dout = digital data output (in decimal), Vin = analog input voltage.

B) Pulse Width Modulation (PWM)

Pulse-Width Modulation (PWM) is a scheme that provides power to a load by switching quickly
between fully on and fully off states. The PWM signal resembles a square wave where the high portion
of the signal is considered the on state and the low portion of the signal is considered the off state. The
high portion, also known as the pulse width, can vary in time and is defined in steps. A larger number of
steps applied, which lengthens the pulse width, also supplies more power to the load. Lowering the
number of steps applied, which shortens the pulse width, supplies less power. The PWM period is
defined as the duration of one complete cycle or the total amount of on and off time combined. PWM
resolution defines the maximum number of steps that can be present in a single PWM period. A higher
resolution allows for more precise control of the pulse width time and in turn the power that is applied to
the load.
The term duty cycle describes the proportion of the on time to the off time and is expressed in
percentages, where 0% is fully off and 100% is fully on. A lower duty cycle corresponds to less power
applied and a higher duty cycle corresponds to more power applied.
The standard PWM mode generates a Pulse-Width modulation (PWM) signal on the CCPx pin with up
to 10 bits of resolution. The period, duty cycle, and resolution are controlled by the following registers:
 PRx registers
 TxCON registers
 CCPRxL registers
 CCPxCON registers

CCP PWM output signal

PWM period

PWM pulse width


PWM duty cycle

4 EQUIPMENT / TOOLS

1. Personal Computer (PC)


2. MPLAB IDE software
3. Proteus software

5.0 To begin, create a folder named P5 on your desktop. Inside the folder, create sub folder CIRCUIT and
REFERENCE. Create a new MPLAB X project named PW5.

5.1 Find the value for ADCON0, ADCON1 and ADCON2 if we want to program ADC module in:

 Vref- = AVss
 Vref+ = FVR BUF2 (4.096V)
 Analog Channel = AN0
 A/D Result Format in Right Justified
 Conversion Time = 12 TAD
 A/D Conversion Clock = FOSC/16

ANSELA =

VREFCON0 =

ADCON0=

ADCON1=

ADCON2=
5.2 Create a main.c source file named as AN0. Copy and paste program given in Appendix A into your
coding window. Modify the void main (void) block with the source code below. Use Appendix C as
references for ADC configuration. Fill the red box using suitable instructions and register value you
have obtained.
Calculate Step Size given Vref = 4.096V, resolution = 10-bit.

}
//Your program end here

Compile your source code.

5.3 Construct circuit shown in Appendix B in Proteus 8. Set processor clock frequency to 64 MHz. Load the
hex file for this circuit.
Set value in DC generator to 4mV. Observe the output. Complete Table 1.
Modify the schematic in Appendix B. Replace DC generator with a circuit shown in figure below.

Using RV1, set voltage drop at pin AN0 (Vin) = 0V. Observe the output. Complete Table 2.

5.4 Task A:
In AN0.c source code, modify while (1) block with source code below.

unsigned int Dout;

Adjust RV1 to get voltage in range from 0.5V to 1.5V, observe the LEDs sequence.

Task B:
Modify while (1) block for PIC18 to turn on pin RD0 if Vin is greater than or equal to 1.4V.

5.5 Create a main.c source file named as AN1. Copy and paste program given in Appendix A into your
coding window. Modify the void main (void) block with the source code below. Use Appendix C as
references for ADC configuration. Fill the red box using suitable instructions and register value you
have obtained.
if (Dout > 125 && Dout < 250)
LATD = 0xAA;
else LATD = 0;
}
}
//Your program end here

Compile your source code.

5.6 Modify the PIC18 circuit you build in Appendix B as illustrated below. Use Pin RA1/AN1 as input.
Maintain other components. Set processor clock frequency to 64 MHz. Load the hex file for this circuit.
Change voltage drop at pin AN1 by adjusting RV1. Observe the output.

Task C:
Modify the program in order to send data 0b11111111 to LATD when Vin is greater than 2.0V and less
than 2.5V. For other Dout value, LATD = 0.

5.7 Find the value for ANSELA, ADCON0, ADCON1 and ADCON2 if we want to program ADC module
in:
 Vref- = AVss
 Vref+ = FVR BUF2 (2.048V)
 Analog Channel = AN2
 A/D Result Format in Left Justified
 Conversion Time = 12 TAD
 A/D Conversion Clock = FOSC/16

5.8 Create a main.c source file named as AN2. Copy and paste program given in Appendix A into your
coding window. Modify the void main (void) block with the source code below. Use Appendix C as
references for ADC configuration. Fill the red box using suitable instructions and register value you
have obtained.
In application block, write instruction to:

 If temperature is less than 20°C, Green LED will be switched on.


 If temperature is less than 28°C, Yellow LED will be switched on.
 For other temperature value, Red LED will be switched on.

//Application
}
}
//Your program end here

Compile your source code.


5.9 Construct circuit below. Use any three (3) pins as output for LEDs. Set processor clock frequency to 64
MHz. Load the hex file for this circuit.
Change temperature input by at pin AN2 by adjusting LM35. Observe the output.
5.1 Task D:
0 A PIC18 microcontroller is used to measure temperature in a room. A temperature sensor and three (3)
LEDs are connected to PIC18 microcontroller. The temperature sensor is connected to ADC pin and
used to control LEDs connected to the PIC18. LEDs will be switched on and off depending on ADC
output. The operation of the PIC18 is shown in Table D.

Table D
ADC input LEDs state
voltage
< 1V All LEDs off
< 2V One (1) LED on, other LEDs off
< 3V Two (2) LEDs on, other LED off
Other value All LEDs on

PIC18 used 64 MHz crystal. ADC is configured for 10-bit operation. Based on Table D, determine C
language program using PIC18 to perform the operation. LEDs are active high. Ignore any time delay
functions. The requirement of the design is AN4 as ADC input and any pins as output. Use same ADC
setting as step 5.1.

5.1 Using Appendix C as references for PWM configuration, calculate value to be loaded T2CON, PR2,
1 CCP1CON and CCPR1L register to create a 5 KHz PWM signal with 25% duty cycle.

T2CON =

PR2 =
CCP1CON =

CCPR1L =

5.1 Create a main.c source file named as pwm. Copy and paste program given in Appendix A into your
2 coding window. Modify the void main (void) block with the source code below. Fill the red box using
value you have obtained.

Compile your source code.

5.1 Construct circuit below. Set processor clock frequency to 64 MHz. Load the hex file for this circuit.
3 Observe the output.
5.1 Task E:
4 Create a 5 KHz PWM signal with 50% duty cycle.

Task F:
Create a 5 KHz PWM signal with 75% duty cycle.

6 RESULT

7 DISCUSSION

8 CONCLUSION
Appendix A

#include <xc.h>
#define _XTAL_FREQ 64000000L

void setup (void);


void delay (unsigned int x);
unsigned int (Dout);

void main (void)


{
setup ();
//Your program start here

}
//Your program end here

static void interrupt ISR (void)


{

/*
BAHAGIAN INI JANGAN KACAU :)
*/
void setup(void)
{

ANSELA = 0b00000000;
ANSELB = 0b00000000;
ANSELC = 0b00000000;
ANSELD = 0b00000000;
ANSELE = 0b00000000;
VREFCON0 = 0b10110000; // Fixed Voltage Reference is enabled, x4 (4.096V)
ADCON1 = 0b00110000; //0b00110000=4.096V 0b00100000=2.048V 0b00000000=Vdd
ADCON2 = 0b10000011;

LATA = 0x00;
LATB = 0x00;
LATC = 0x00;
LATD = 0x00;
LATE = 0x00;
}

void delay(unsigned int x)


{
for(;x>0;x--)__delay_ms(1);
}
Appendix B
Appendix C: Related Control Register for ADC, CCP and Timer2 Module in PIC18F45K22

ADCON0
ADCON1
ADCON2
ANSELA

VREFCON0
CCPxCON
T2CON
AN0

Items Lecturer Sign Lecturer name

1) MPLAB X

2) Proteus

1) Calculate step size

VREF+ =

VREF- =

VREF =

Resolution =

Step size =

2) Table 1

Dout (From Proteus 8) Dout


Vin
RB1 RB0 RD7 RD6 RD5 RD4 RD3 RD2 RD1 RD0 Decimal (Calculation)
4mV
8mV
12mV
16mV
20mV
24mV
28mV
32mV
36mV
40mV
3) Table 2

Vin Dout (From Proteus 8) Dout


(Calculation)
RB1 RB0 RD7 RD6 RD5 RD4 RD3 RD2 RD1 RD0 Decimal
1V
2V
3V
3.5V
4V

1) Circuit observation (Task A)

5) Circuit observation (Task B)


AN1

Items Lecturer Sign Lecturer name

1) MPLAB X

2) Proteus

6) Circuit observation

7) Circuit observation (Task C)


AN2

Items Lecturer Sign Lecturer name

1) MPLAB X

2) Proteus

8) Table 3

V ref (+)−V ref (−) (2 . 048−0 )V 2 . 048 V


Step Size= = = =8 mV
Note: Re solution 28 256

Temperature V IN =Temperature×10 mV V IN
DOUT =
Step Size
20
28

9) Circuit observation
Task D

Items Lecturer Sign Lecturer name

1) MPLAB X

2) Proteus

10) Circuit observation


PWM

Items Lecturer Sign Lecturer name

1) MPLAB X

2) Proteus

11) Duty cycle 25%

PR2 =

CCPR1L:CCP1CON<5:4> =

CCPR1L =

CCP1CON<5:4> =

Sketch the output signal and calculate PWM frequency.

Division = Time/Division =

ON time =

OFF time =

PWM period =

PWM frequency =
12) Duty cycle 50%

PR2 =

CCPR1L:CCP1CON<5:4> =

CCPR1L =

CCP1CON<5:4> =

Sketch the output signal and calculate PWM frequency. [5 marks]

Division = Time/Division =

ON time =

OFF time =

PWM period =

PWM frequency =
13) Duty cycle 75% [5 marks]

PR2 =
CCPR1L:CCP1CON<5:4> =

CCPR1L =

CCP1CON<5:4> =

Sketch the output signal and calculate PWM frequency. [5 marks]

Division = Time/Division =

ON time =

OFF time =

PWM period =

PWM frequency =

You might also like