Professional Documents
Culture Documents
Low-Cost Bidirectional Brushed DC Motor Control
Low-Cost Bidirectional Brushed DC Motor Control
PR2
The PR2 register effects the PWM frequency/period.
The value to use for the PR2 register is calculated from
Equation 6.
CCP1CON
In addition to storing the lower 2 bits of the 10-bit PWM
duty cycle, CCP1CON is used to set up the ECCP in
PWM mode using bits CCP1CON<3:0> and can
change the motor direction using bits CCP1CON<7:6>.
When setting up the ECCP in PWM mode, there are
four possible configurations. These configurations
accommodate H-bridges with MOSFETS that are
active high, active low or a combination of both active
high and active low. Motor direction can be changed in
hardware by configuring bits CCP1CON<7:6> to be
‘01’ for forward or ‘11’ for reverse. The PIC16F684
ECCP hardware takes care of switching channels for
activating and modulating the appropriate MOSFET
drivers in the H-bridge.
T2CON
The T2CON register is used for setting up the Timer2
prescaler and turning on Timer2. The Timer2 prescaler
is contained in bits T2CON<1:0> and is used in
determining the PWM frequency, duty cycle and
resolution. Timer2 must be turned on by setting bit
T2CON<2> before the PWM signal will start. For an
algorithm that calculates the Timer2 prescaler and PR2
values given a known PWM frequency (see Figure B-1
in Appendix B).
FET QA QC FET
PIC16F684 Driver Driver
Logic ‘1’
P1A
Logic ‘0’
P1C QD
QB
P1D
CCP1CON<3:0> = ‘1100’
CCP1CON<7:6> = ‘01’
FET QA QC FET
PIC16F684 Driver Driver
Logic ‘0’
P1A
BDC
P1B
FET I FET
Driver Driver
Logic ‘1’
P1C QD
QB
Logic ‘0’
P1D
CCP1CON<3:0> = ‘1100’
CCP1CON<7:6> = ‘11’
FET QA QC FET
PIC16F684 Driver Driver
Logic ‘1’
P1A
Logic ‘0’
P1C QD
QB
Logic ‘0’
P1D
CCP1CON<3:0> = ‘1100’
CCP1CON<7:6> = ‘01’
V = V+ - V
V+
FET QA QC FET
PIC16F684 Driver Driver
Logic ‘1’
P1A
Logic ‘0’
P1C QD
QB
VACTUAL
0.1Ω current sensing
resistor
P1D
CCP1CON<3:0> = ‘1100’
CCP1CON<7:6> = ‘01’
Windows GUI
PIC16F684
Sensorless
BDC RPM and
Motor Current
Measurements
Physical Process
REFERENCES
[1] DS41202A, 14-Pin Flash-Based, 8-Bit CMOS
data sheet (PIC16F684) - www.microchip.com.
[2] DS21117A, Single-Ended, Rail-to-Rail I/O, Low
Gain PGA, MCP6S21/2/6/8 data sheet -
www.microchip.com
[3] HI-TECH C - www.htsoft.com
[4] MPLAB® IDE - www.microchip.com
1
Frequency = ------------------
Period
V BDC = V DD × ------------------
DC
Period
log ------------------------------------------------------------------------
F OSC
( FPWM × TMR2Prescaler )
Resolution = --------------------------------------------------------------------------------------
log ( 2 )
EQUATION 6: PR2
PR2 = ----------------------------------------------------------------------- – 1
Period
4 × T OSC × TMR2Prescaler
DC
CCPR1L:CCP1CON<5:4> = -------------------------------------------------------------
-
T OSC × TMR2Prescaler
EQUATION 8: RPM
2 2
P = I MAX × R = 3 × 0.1 = 0.9 W
V GAINMAX
2 = --------------------------- × 1024 , where X is bits of resolution
X
V DD
V GAINMAX
- × 1024
-------------------------- log ------- × 1024
4.8
V DD 5.0
X = log --------------------------------------------------- = --------------------------------------- = 9.94 bits
log ( 2 ) log ( 2 )
V GAIN = -----------------------------------------------
ADRESH:ADRESL-
X
× V GAINMAX
2
V GAIN
V ACTUAL = ----------------
Gain
V ACTUAL
I = -----------------------
R
FIGURE B-1: CALCULATING TIMER2 PRESCALER AND PR2 ALGORITHM GIVEN A PWM
FREQUENCY
Start
i=0
PR2 = 1000
Prescaler = 1
No
Is PR2 > 255? Done
Yes
No
Is Prescaler > 16? Prescaler = 2i
Yes
No
i=i+2
PR2 = unsigned int.
Prescaler = unsigned char.
i = unsigned char.
Start
Start
Initialize
PIC16F684
Measure PWM
Period
Is command No
received?
All samples Yes Average
taken? samples
Yes
No
Is command No
Done
Synchronize on valid?
PWM high edge
Yes
Process
Delay command
Is A/D
Conversion No
complete?
Yes
Log sample
Increment Delay
PIC16F684
• Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.
• There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip's Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
• Microchip is willing to work with the customer who is concerned about the integrity of their code.
• Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as “unbreakable.”
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.