Beruflich Dokumente
Kultur Dokumente
BEE31101
Instruction Sheet
Lab No. 6
Lab Title 6A. DC Motor Control
6B. Servo Motor Control
Semester 02
Session 2016/17
Lab Durations 4 Hours
Independent Studies 2 Hours
Electronic Engineering Laboratory IV (BEE31101) ii
Lab 6
Table of Contents
1.0 Outcomes A
2.0 Guidelines A
3.0 Procedures B
Overview of Lab 6A B
Overview of Lab 6B F
Lab Activity 1 H
Lab Activity 2 K
Lab Activity 3 L
Pre-Lab 1
Questions 4
Lab Activity 8
Lab Activity 2 8
Lab Activity 3 12
Observations 14
Lab Activity 1 14
Lab Activity 2 14
Lab Activity 3 15
Conclusion 17
References 18
1.0 Outcomes
2.0 Guidelines
1. Grouping: Lab group is not predetermine and consists with at most two team members.
2. Pre-Lab: Must be submitted to the instructor at the beginning of lab sessions. Verified by the
instructor and returned to the students at the end of lab session. The verified pre-lab will be
attached with the final report for submission.
3. Lab Activities: All lab activities such as sample code, examples and lab assignments must
be held in the respective lab location and completed within the given times.
4. Demonstration: Student must demonstrate the successful sample code, examples and lab
assignments to the respective instructor. Verification only will be given upon completion of all
lab activities and initialized by the instructor on the cover page.
5. Report Organization: Report must be organized according to given report template. All
source codes, schematic diagram, graphs and related materials must be printed and attached
as appendices.
6. Include reference used. Use the following format for references. References section should
be placed after conclusion section.
Book reference format and example:
Author, Books title. Edition, Location: Publisher, Year Published.
Gregory L. Moses, Digital Systems Lab Manual: A Design Approach. 11th ed., Wiley,
2010
Online reference format and example:
Author, Webs title. Retrieved at websites address on date accessed.
Digital Systems Design. Retrieved at http://www.wiley.com/gregory on September 2012
7. Report Submission: Report must be received by respective technical staff (at respective
lab) before consecutive lab session.
3.0 Procedures
Overview of Lab 6A
CCP module in PIC16F877A has a 16-bit register which can operate as 16-bit capture register (in
Capture Mode), or 16-bit compare register (in Compare Mode), or 16-bit PWM duty cycle register.
There is no 16-bit register in 16F877 since the microcontroller is an 8-bit processor. Therefore, two
8-bit register are used to form a 16-bit register: CCPR1H and CCPR1L for upper and lower byte of
the 16-bit register for CCP1 module and CCPR2H and CCPR2L for CCP2 module.
CCP modules are controlled by CCP control registers: CCP1CON for CCP1, and CCP2CON
for CCP2. The details of CCP1CON register is illustrated in Figure 1. As seen in the figure,
CCPM3:CCPM0 determine the three modes in CCP module. DCB1:DCB0 are used only for PWM,
which define the last two LSBs for 10-bit PWM duty cycle. *The next discussion is focused on
CCP1 module only, the same procedures can be applied on CCP2 module.
In PWM mode, the CCP1 pin of 16F877 produces up to a 10-bit resolution PWM output. Since
the CCP1 pin is multiplexed with the PORTC data latch, the corresponding TRISC bit must be
cleared to make the CCP1 pin an output. A PWM output has a time-base (i.e., pulse period) and a
time that the output stays high (i.e., duty cycle). The frequency of the PWM is the inverse of the
period (1/period). The PWM period is specified by writing to the PR2 register. PWM period in
second, Tpwm, is determined by the following formula:
2 1 4 2
where TMR2ps is the TMR2 pre-scale value, and Tosc is the oscillation period.
Since one instruction takes four oscillation periods, we have the term 4Tosc. Also, since the PR2
register content will increase by 1 every instruction cycle, we have the term [PR2] +1. Again, the
pre-scale further increase the number of actual oscillation cycles to the 4Tosc multiplied by the
pre-scaler of TMR2, we have the term TMR2ps. All together, we have the PWM period.
When TMR2 is equal to PR2, the following three events occur on the next increment cycle:
i. TMR2 is cleared
ii. The CCP1 pin is set (exception: if PWM duty cycle = 0%, the CCP1 pin will not be set)
iii. The PWM duty cycle is latched from CCPR1L into CCPR1H
The PWM duty cycle is specified by writing to the CCPR1L register and to the DCB1:DCB0
(CCP1CON<5:4>) bits. The CCPR1L contains the eight MSbs and CCP1CON<5:4> contains the
two LSbs. This 10-bit value is represented by DCB9:DCB0 in the equation below. The PWM duty
cycle in second, Dpwm, is given by the following formula:`
9: 0
where [DB9:DB0] is the 10-bit value which comes from CCPR1L<7:0> for eight MSBs
concatenated by CCP1CON<5:4> for two LSbs.
The DCB9:DCB0 bits can be written to at any time, but the duty cycle value is not latched into
CCPR1H until after a match between PR2 and TMR2 occurs (which is the end of the current
period). In PWM mode, CCPR1H is a read-only register. The CCPR1H register and a 2-bit internal
latch are used to double buffer the PWM duty cycle. This double buffering is essential for glitch-
less PWM operation. When CCPR1H and 2-bit latch match the value of TMR2 concatenated with
the internal 2-bit Q clock (or two bits of the TMR2 pre-scaler), the CCP1 pin is cleared. This is the
end of the duty cycle.
TMR2 = PR2 +1
TMR2 forced to 0h
TMR2 = PR2 +1
TMR2 forced to 0h
Let's consider the maximum PWM resolution, Rpwm. This is to decide how small a duty cycle
can be determined. First consider the period of PWM, Tpwm, with relation with the period of the
oscillator clock, Tosc. We have to know that the period of PWM is determined a multiple of the
period of oscillation. In other words, in a PWM period there must be n number of the oscillation
period: Tpwm = n.Tosc . By the way, the resolution in bits for a number n is determined by the power
of 2 of the number n. (Remember that, in ADC, the 10-bit resolution has the value in the range of
1 1024 or 20 - 210. In other words, the resolution in bits is the power of 2 for the maximum value
it can provide.)
Therefore, the above equation can be changed to: 2 . , where Rpwm is the
maximum PWM resolution in bits. Therefore, the equation for Rpwm can be rewritten as:
log 2
This equation can be further altered in terms of frequencies of PWM and oscillation:
log 2
Let's have an example. Assuming that the desired PWM frequency is 78.125kHz (i.e., PWM
period of 12.8s) with oscillation speed of 20MHz, and TMR2 prescale is 1. First thing we have to
find is the content of PR2 register.
From the PWM period equation, 2 1 4 2 , we can draw an
equation for [PR2], the content for PR2 register:
2 1
4 . 2
Since, Tpwm = 12.8s, Tosc = 1/20MHz = 0.05s, and TMR2ps = 1 (since prescale is set to 1)
12.8
2 1 64 1 63
4 0.05 1
Let's calculate the maximum resolution we get from PWM duty cycle under the example case.
From the maximum resolution equation,
12.8
8
0.05
So the maximum resolution of the PWM duty cycle in the example is 8. What does this mean?
What maximum resolution being 8 means that, simply put, the 10-bit PWM values determined by
DCB9:DCB0 is less than or equal to 28. In other words, even though PWM duty cycle can get you
maximum 10-bit resolution, it does not mean that it provides you with 10-bit resolution every time.
In the example case, it provides only 8-bit resolution. In analogy to PWM, for a given pulse period,
to have 8-bit resolution is to have a total number of 256 division scales in the pulse period. In our
example, the pulse period is 12.8s, therefore the each scale is 0.05s. In other words, there is no
way to reduce the scale to, for example, 12.8/512 =0.025s, since this scale is close to 1
instruction execution time. Duty cycle is simple the multiple of the minimum scale.
In other words, any value in DCB9:DCB0 greater than 255, would be regarded as if the same
as 255. In order to achieve higher resolution, the PWM frequency must be decreased. Or, in order
to achieve higher PWM frequency, the resolution must be decreased. The following table lists
example PWM frequencies and resolutions for fosc = 20 MHz. The TMR2 prescaler and PR2 values
are also shown.
Table 1: TMR2 Prescaler
PWM frequency 1.22kHz 4.88 kHz 19.53 kHz 78.12 kHz 156.3 kHz 208.3 kHz
TMR2 prescaler 16 4 1 1 1 1
[PR2] value FFh FFh FFh 3Fh 1Fh 17h
Maximum 10 10 10 8 7 5.5
Resolution [bits]
Now let's further discuss about the PWM duty cycle formula with the given example. Assume that
we want to have a 50% duty cycle, on for the half the pulse period and off for the rest of the pulse
period. From the PWM duty cycle equation, 9: 0
(note that the duty cycle is given with [sec] not in[%]), we get the equation for [DB9:DB0]:
9: 0
.
Since 50% of the PWM pulse period is (0.5)*(12.8s) = 6.4s, and with Tosc = 0.05s and TMR2ps=
1, we get
6.4
9: 0 128 0 80 0010000000
0.05 1
In practice, B'0010000' (or 0x20 as the upper 8 digits) is written to CCP1L register, and the
last two digits 00 to DCB1:DCB0 bits (CCP1CON<5:4>). If we follow all these steps, the pin
CCP1 would generate +5V pulse with its period 12.8s with on-period of 6.4s and off period
6.4s.
Overview of Lab 6B
Servos are controlled by sending an electrical pulse of variable width, or pulse width modulation
(PWM), through the control wire. There is a minimum pulse, a maximum pulse, and a repetition
rate. A servo motor can usually only turn 90 degrees in either direction for a total of 180 degree
movement. The motor's neutral position is defined as the position where the servo has the same
amount of potential rotation in the both the clockwise or counter-clockwise direction. The PWM
sent to the motor determines position of the shaft, and based on the duration of the pulse sent
via the control wire; the rotor will turn to the desired position. The servo motor expects to see a
pulse every 20 milliseconds (ms) and the length of the pulse will determine how far the motor
turns. For example, a 1.5ms pulse will make the motor turn to the 90-degree position. Shorter
than 1.5ms moves it to 0 degrees, and any longer than 1.5ms will turn the servo to 180 degrees.
When these servos are commanded to move, they will move to the position and hold that
position. If an external force pushes against the servo while the servo is holding a position, the
servo will resist from moving out of that position. The maximum amount of force the servo can
exert is called the torque rating of the servo. Servos will not hold their position forever though; the
position pulse must be repeated to instruct the servo to stay in position.
The relationship is linear, so use mathematics to determine the pulse which corresponds to a
given angle. Note that if you send a signal that is greater or lower than the servo can accept, you
might damage the actuator.
Servos are extremely useful in robotic application. The motors are small, and have built with a
motor, control circuitry, a set of gears, and the case, and are extremely powerful for their size. A
standard servo has 42 oz/inches of torque, which is pretty strong for its size. It also draws power
proportional to the mechanical load. A lightly loaded servo, therefore, doesn't consume much
energy. There are 3 wires in it: power, ground, and control. The amount of power applied to the
motor is proportional to the distance it needs to travel. So, if the shaft needs to turn a large
distance, the motor will run at full speed. If it needs to turn only a small amount, the motor will run
at a slower speed.
The control wire is used to communicate the angle. The angle is determined by the duration of
a pulse that is applied to the control wire. In other words, the duration of the pulse dictates the
angle of the output shaft.
Lab Activity 1
1. If PWM, rather than just on/off drive, is applied to an H-bridge, then continuously variable
reversible drive can be achieved. If it is an exact square wave of sufficiently high frequency,
the motor stands still, as the average current is zero. This is illustrated in Figure 4. If the
Bridge Drive changes to a Mark:Space ratio of less than 1 the current takes up a negative
average value. With a Mark:Space ratio of greater than 1 it takes up a positive average value.
By varying the PWM pulse width across its full range, from absolute minimum to absolute
maximum, the motor speed can be continuously varied, in both directions.
2. The source code in Program 1, demonstrates PWM waveform generation using CCP1 which
generated pulses with 0.5ms period and duty cycle of 50%. The pulse is a continuous stream
of 0.25ms long +5V DC signal followed by 0.25 ms long 0V signal.
3. Test the given source code using MPLAB simulation and observe the output.
4. Generate hex file, upload to Proteus circuit (Figure 5) and record your observation.
Figure 4
Program 1
clrf INTCON
clrf PIR1
movlw 0x4e ;50% Duty Cycle
movwf FIFTY
;PWM Period Setting
banksel PR2
movlw 0x9b ;2000Hz(0.5ms)Period
movwf PR2
banksel FIFTY
;PWM Duty Cycle
movf FIFTY, W ;Move 50% Duty Data to W reg.
movwf CCPR1L
bcf CCP1CON, B5 ;Under PWM mode
bsf CCP1CON, B4 ;2 LSB's of PWM duty cycle; the other
8bits
;are located at CCPR1L
banksel TRISC ;TRISC Setting for output
bcf TRISC, PWM1 ;set PORTC.2 as output
banksel T2CON
movlw 0x07 ;TMR2 Turn On and
movwf T2CON ;TMR2 Prescale 16
END
Lab Activity 2
1. Based on flow chart in Figure 6, create an application that could generate 8 kHz PWM signal
with 25% and 75% duty cycle from CCP1.
2. Execute your program and observe the simulation output. Please use the simulation circuit
provided as shown in Figure 5.
Start
Tosc = 0.05us
PWM Period = 8kHz
TMR2 prescaler 16
Lab Activity 3
1. Run Program 2 in your MPLAB and run the Proteus simulation circuit as shown in Figure 7.
2. Observe the output of the program. The TMR0 concept is applied as in the previous lab to
acquire the time value.
Overflow time = 4 x TOSC x Prescaler x (256 TMR0)
3. Modify Program 2 so that the servo will turn at +90 degree when a button located at RC2 is
pressed.
4. Again, modify the program in Step 4 if the crystal oscillator is changed to 20MHz (re-calculate
the value of TMR0 and COUNT register).
Program 2
#include <p16f877a.inc>
#define _XTAL_FREQ 40000000
COUNT EQU 0x20
ORG 0x00
BSF STATUS,RP0
MOVLW B'00000000' ; Motor located at PORTB
MOVWF TRISB
MOVLW B'11111111' ;BUTTON LOCATED AT PORTC
MOVWF TRISC
MOVLW B'00000100' ; Prescaler =32 (Bit 2,1,0 = 100)
MOVWF OPTION_REG
BCF STATUS,RP0
GOTO WAIT2
GOTO DELAY_1.5ms
END
About Students:
Name Matric No. Section: 1 / 2 / 3 / 4 / 5 / 6 / 7 / 8
/ 9 / 10 / 11 / 12
Instructors Name:
Assoc. Prof. Dr. Abd Kadir Mahamad
Dr. Chessda Uttraphan Eh Kan
Azmi Sidek
Mohamad Md Som
Munirah Abd Rahman
About Experiment:
Title Venue
MCAD1 / MCAD2 / MSKM /
Lab 6A: DC Motor Control MSK / MRK
Lab 6B: Servo Motor Control Date
/200
Electronic Engineering Laboratory IV (BEE31101) 1
Lab 6A: DC Motor Control
Lab 6B: Servo Motor Control
Pre-Lab
Marks Check by
(1 mark)
Questions
1. Discuss the outcome of source code in Lab Activity 1 if the prescaler is changed to 32.
(7 marks)
2. Briefly explain the significant of using PWM in controlling the rotation and speed of DC motor.
(8 marks)
3. Based on Program 2, prove (show your calculation) that 0.5ms for high pulse requires a pre-
loaded value of 100 to be inserted into TMR0.
(3 marks)
4. Based on Program 2, demonstrate the relationship of 19.5ms overflow time with the pre-
loaded value of 40 in the COUNT register.
(4 marks)
5. Based on Program 2, demonstrate the relationship of 1.5ms overflow time with the pre-
loaded value of 4 in the COUNT register.
(4 marks)
6. Based on Program 2, demonstrate the relationship of 18.5ms overflow time with the pre-
loaded value of 38 in the COUNT register.
(4 marks)
Lab Activity
Lab Activity 2
2. Explain how 25% duty cycle of 8kHz PWM will be represented in CCP1L and CCP1CON
register.
(4 marks)
3. Explain how 75% duty cycle of 8kHz PWM will be represented in CCP1L and CCP1CON
register.
(4 marks)
4. Modify the sample code of Program1 to generate PWM waveform with condition based on flow
chart in Figure 6.
(10 marks)
Lab Activity 3
1. Write down the modification of source code (from Program 2) for step 4.
(10 marks)
2. Write down the modification of source code (from Program 2) for step 5.
(10 marks)
Observations
Lab Activity 1
1. Discuss the difference of Proteus simulation outcome with respect to its expected theoretical
outcome.
(5 Marks)
Lab Activity 2
2. Conclude the effects of PWM duty cycle against dc motor speed and rotation.
(5 Marks)
3. Summarize the role of PR2, CCPR1L and CCP1CON in determining the PWM duty cycle.
(5 Marks)
Lab Activity 3
5. Detailed out the difference of the outcome after modification made in step 4.
(5 marks)
6. Write down your observation when the crystal value is changed in step 5.
(5 marks)
Conclusion
References
Scale
Criteria
5 4 3 2 1
LabActivities Varies,dependingonactivityassigned
Observations Varies,dependingonquestionassigned
Conclusion Accurate Accurate Astatementofthe Astatementofthe Noconclusionwas
statement of the statement of the resultsofthelab resultsis includedorshows
results of lab results of the lab indicateswhether incompletewith littleeffortand
indicates whether indicates whether resultssupportthe littlereflectionon reflectiononthe
results support results support hypothesis thelab. lab.
hypothesis. the hypothesis.
Possible sources Possiblesourcesof
of error and what erroridentified.
was learned from
the lab is
discussed.
Demonstration Demonstrateswith Exhibitslimited Demonstration
and clear understandingor without
understandingof principle understandingthe
Verifications principleor demonstrated; principle;cannot
conceptinvolved; minorinaccuracies relateconceptto
accuratelyrelates inrelating demonstration
conceptto demonstration.
demonstration.
Ethics Selfgenerated Solution/ideasare Copiedsolution/
solution/ideas. adoptedfrom ideasfromothers.
others.
Reportis Reportis
submittedbefore Reportis submittedmore
dateline. submittedaday thanadayafter
afterdateline dateline.
Coverpage
informationis Informationon
completelyfilled. coverpageisnot
completelyfilled.
Content Ideas are written Ideasarewritten Plagiarizes.Ideas
using own withlimited aredirectlycopied
original language knowledgeand fromreferences.
and clearly understandability.
focused. Ideasaresnipped Nocitationof
fromreference reference.
Reference is withoutfully
cited using ieee
understandthe
standards.
concept.
Referenceiscited
usingvarious
standards.