Practical 5 - ADC+PWM
Practical 5 - ADC+PWM
LECTURER’S NAME:
CLO/PLO/DK/DP : CLO3/PLO5/DK6/DP1/DP3
TOTAL
STUDENT ID & NAME : MARKS
(100%)
(1)
(2)
(3)
1. Construct and simulate real time embedded system application based on PIC16F/PIC18F
microcontroller effectively
2 OBJECTIVE
3 THEORY
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.
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.
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
PWM period
4 EQUIPMENT / TOOLS
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
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.
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
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:
//Application
}
}
//Your program end here
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.
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
}
//Your program end here
/*
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;
}
ADCON0
ADCON1
ADCON2
ANSELA
VREFCON0
CCPxCON
T2CON
AN0
1) MPLAB X
2) Proteus
VREF+ =
VREF- =
VREF =
Resolution =
Step size =
2) Table 1
1) MPLAB X
2) Proteus
6) Circuit observation
1) MPLAB X
2) Proteus
8) Table 3
Temperature V IN =Temperature×10 mV V IN
DOUT =
Step Size
20
28
9) Circuit observation
Task D
1) MPLAB X
2) Proteus
1) MPLAB X
2) Proteus
PR2 =
CCPR1L:CCP1CON<5:4> =
CCPR1L =
CCP1CON<5:4> =
Division = Time/Division =
ON time =
OFF time =
PWM period =
PWM frequency =
12) Duty cycle 50%
PR2 =
CCPR1L:CCP1CON<5:4> =
CCPR1L =
CCP1CON<5:4> =
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> =
Division = Time/Division =
ON time =
OFF time =
PWM period =
PWM frequency =