You are on page 1of 10

ELECTRICAL ENGINEERING DEPARTMENT

ACADEMIC SESSION: SESSION 2 2022/2023


DEC40053 – EMBEDDED SYSTEM APPLICATIONS
PRACTICAL WORK 5 : HARDWARE INTERFACING –PWM PROGRAMMING

DATE:
LECTURER’S NAME:
GROUP NO.:
SECTION/CLASS:
TOTAL
STUDENT ID & NAME: MARKS
(100%)

(1)

(2)

(3)

DATE SUBMIT : DATE RETURN :

1
PRACTICAL SKILL ASSESSMENT RUBRIC (CLO3)

Practical Work Number : ______________ Practical Work Title :______________________________________________________________

Student Name Registration Number


1.
2.

Student Number 1 2
Score description

Weightage
Item Aspects Poor Moderate Good Excellent Score
2 3 4 5

Able to write program Able to build program Able to write program


Write program Unable to write code with minimum
A code with moderate code without
(20%) program code. supervision 4
supervision supervision
Able to compile and Able to compile and Able to compile and
Compile and Unable to compile and
execute program execute program execute program
B execute program execute program 1
code successfully with code successfully with code successfully
(5%) code
moderate supervision. minimum supervision. without supervision.
Able to use the Able to use the
Able to use the
Component Unable to use the components in Proteus components in
C components in Proteus 1
selection (5%) components in Proteus with moderate Proteus with minimum
without supervision.
supervision. supervision.
Able to construct Able to construct Able to construct
Construct circuit Unable to construct
D circuit in Proteus with circuit in Proteus with circuit in Proteus 2
(10%) circuit in Proteus
moderate supervision. minimum supervision. without supervision.
Able to perform Able to perform
Able to perform
simulation with logic simulation with correct
Perform simulation Unable to perform simulation. The result is
E error. The result is output. The result is 6
(30%) simulation. not shown within
shown within shown within stipulated
stipulated time
stipulated time time
Able to troubleshoot Able to troubleshoot Able to troubleshoot
Troubleshooting Unable to troubleshoot
F syntax/logic error with syntax/logic error with syntax/logic error 2
(10%) syntax/logic error
moderate supervision. minimum supervision. without supervision.

Total Practical Skill 80%

2
REPORT ASSESSMENT RUBRIC (CLO3)

Practical Work Number : ______________ Practical Work Title :______________________________________________________________

Student Number 1 2
Score description

Weightage
Item Aspects Poor Moderate Good Excellent Score
2 3 4 5

G Questions (10%)
1

Some formatting errors Proper formatting with Proper formatting, Proper formatting,
or missing sections. labelled sections with section labelled and section clearly labelled
Result and
algorithm, schematic, organized with and well organized
H Documentation 2
no comments and no algorithm, schematic, with appropriate
(10%)
output a few comments and algorithm, schematic,
only a few output comments and output

Total Report Marks 20%

3
1 LEARNING OUTCOMES (LO):

1. : Construct and simulate real-time embedded system application based on


PIC16F/PIC18F microcontroller effectively

2 OBJECTIVE

1. Construct DC motor interface to control direction with H-Bridge motor driver.


2. Apply program code to control DC motor speed using PWM with CCP.

3 THEORY

A. DC Motor

The DC motor direction can be change by changing the power supply polarity, there are few
methods to change the polarity. The most popular are: by using H-Bridge motor driver and
relays interface.

Figure 1: H-Bridge motor driver interface

L293 Motor Driver (H-Bridge)

The L293D is a 16-pin Motor Driver IC which can control a set of two DC motors
simultaneously in any direction. The L293D is designed to provide bidirectional drive currents of
up to 600 mA (per channel) at voltages from 4.5 V to 36 V (at pin 8!). You can use it to control
small dc motors - toy motors.

Figure 2: Motor Controller Circuit using L293D Table 1: Input Logic table of L293D

4
PWM Overview

PWM, or pulse width modulation is a technique which allows us to adjust the average value of
the voltage that’s going to the electronic device by turning on and off the power at a fast rate.
The average voltage depends on the duty cycle, or the amount of time the signal is ON versus
the amount of time the signal is OFF in a single period of time.

Figure 3: PWM pulse

Standard PWM Operation

The standard PWM function described in this section is available and identical for CCP and
ECCP modules. 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:

• PR2 registers
• TxCON registers
• CCPRxL registers
• CCPxCON registers

Steps For Programming

1. Load the PR2 value which will decide the period of the pulse.
2. Set the duty cycle by loading value in the CCPR1L: CCP1CON<5: 4>
3. Configure the CCP1CON register for setting a PWM mode.
4. Initialize the pin CCP1as an output pin which will give PWM output.
5. Configure the T2CON register and enable TMR2 using T2CON

Calculation of PR2 register

PR2= [FOSC/(FPWM x 4 x N)] - 1

Duty cycle = %duty cycle x PR2 value → CCPR1L: CCP1CON<5: 4>

4 EQUIPMENT / TOOLS

i. PC installed software:
a. MPLABX IDE integrates with XC8 compiler.
b. PROTEUS Professional

ii. PIC Trainer

5
5 PROCEDURE
5.1 PART A: CONTROL DC MOTOR USING MOTOR DRIVER

Program A is used to control the direction of DC motor continuously as below:


i. motor FORWARD 5 second
ii. motor STOP 1 second
iii. motor REVERSE 5 second
iv. motor STOP 1 second

5.2 Construct circuit shown in Figure 4 in Proteus.


U2(VS)

16 8 U2
U1
2 15 2 3
RA0/AN0 RC0/T1OSO/T1CKI IN1 VSS VS OUT1
3 16 7 6
RA1/AN1 RC1/T1OSI/CCP2/UOE IN2 OUT2
4 17 1
RA2/AN2/VREF-/CVREF RC2/CCP1/P1A EN1
5 23
RA3/AN3/VREF+ RC4/D-/VM
6 24
RA4/T0CKI/C1OUT/RCV RC5/D+/VP
7 25 9
RA5/AN4/SS/LVDIN/C2OUT RC6/TX/CK EN2
14 26 10 11
RA6/OSC2/CLKO RC7/RX/DT/SDO IN3 OUT3
13 15 14
OSC1/CLKI IN4 GND GND OUT4
33 19
RB0/AN12/INT0/FLT0/SDI/SDA RD0/SPP0
34 20 L293D
RB1/AN10/INT1/SCK/SCL RD1/SPP1
35 21
RB2/AN8/INT2/VMO RD2/SPP2
36 22
RB3/AN9/CCP2/VPO RD3/SPP3
37 27
RB4/AN11/KBI0/CSSPP RD4/SPP4
38 28
RB5/KBI1/PGM RD5/SPP5/P1B
39 29
RB6/KBI2/PGC RD6/SPP6/P1C
40 30
RB7/KBI3/PGD RD7/SPP7/P1D
R1
8 10k
RE0/AN5/CK1SPP
9
RE1/AN6/CK2SPP
10
RE2/AN7/OESPP
18 1
VUSB RE3/MCLR/VPP
PIC18F4550

Figure 4: DC Motor One Direction

5.3 Write a Program A , save it as MOTOR1.c then build the program until successful. If any error
occurs, fix it.

Program A

#include <xc.h>

#define EN1 RC2


#define IN1 RD4
#define IN2 RD5

void delay_ms (unsigned int x);

void main (void)


{
//configure I/O pins
_______________;
_______________;
_______________;

while (1)
{
//motor FORWARD 5 second
EN1= 1;
IN1=1;
IN2=0;
delay_ms(5000);
6
//motor STOP 1 second
EN1 = 0;
delay_ms (1000);

//motor REVERSE 5 second


EN1 = 1;
IN1=0;
IN2=1;
delay_ms(5000);

//motor STOP 1 second


EN1 = 0; //motor OFF
delay_ms (1000);
}
}

void delay_ms (unsigned int x)


{
for (; x>0; x--)
__delay_ms(1);
}

Observation:
State your observation of the circuit.

Lecturer’s signature: ______________________ Date: ___________


5.3 PART B: DC Motor Speed Control using PWM

By using the same circuit in step 5.1, add 3 switches (active LOW) at pins RE0 (SW1),
RE1(SW2) and RE2 (SW3). Write program to perform as below:

i. If SW1 is pressed, the motor will rotate forward 30% of speed


ii. If SW2 is pressed, the motor will rotate forward 60% of speed
iii. If SW3 is pressed, the motor will rotate forward 100% of speed

a) Draw schematic diagram

7
b) Calculate the value of PR2 register and CCPR1L for each speed:

Use PWM frequency 1.22 kHz, TMR2 prescale value 16 and FOSC= 20 MHz.

PR2= [FOSC/(FPWM x 4 x N)] - 1

5.4 Write a Program B, save it as MOTOR2.c then build the program until successful.

Program B

#include <xc.h>
#define _XTAL_FREQ 20000000

#define EN1 RC2


#define INP1 RD4
#define INP2 RD5

void main(void)
{
// Configure I/O pins
_______________
_______________
_______________

// configure PWM mode


T2CON = _____________;
CCP1CON = __________;
PR2 = ________; // full scale
CCPR1L = ____; //zero duty cycle
TMR2ON=1;
__delay_ms(1);

while (1)
{
if ( __________ ) // SW1 is pressed
{
CCPR1L = ___; // 30% Duty cycle
//motor FORWARD
INP1=__;
8
INP2=__;
}

else if ( __________ ) // SW2 is pressed


{
CCPR1L = ____; // 60% Duty cycle
//motor FORWARD
INP1=__;
INP2=__;
}
else if ( __________ ) // SW3 is pressed
{
CCPR1L = _____; // 100% Duty cycle
//motor FORWARD
INP1=__;
INP2=__;
}
else
CCPR1L = 0;
}
}

Observation:
State your observation of the circuit.

Lecturer’s signature: ______________________ Date: ___________

6 DISCUSSION / TASK

1. Describe Duty Cycle in PWM . ( 2 marks)

2. List two methods to interface microcontroller and DC motor..


(2 marks)

3. Find the PR2 value and the prescaler needed to get 1.22 kHz PWM frequencies:
(6 marks)

7 CONCLUSION

Appendix 1

9
10

You might also like