Академический Документы
Профессиональный Документы
Культура Документы
[http://2.bp.blogspot.com/-
hdmCX6JoCrE/TxqTb8k4jBI/AAAAAAAAACs/8lfOKeRhMik/s1600/PWM-Duty+Cycle.gif]
PWM concerns about the duty cycle of a square wave signal, it's the technique in which we change the pulse
width to get the desired duty cycle.
setup_ccp1(CCP_PWM);
Now the final step is to set your duty cycle using the function: set_pwm1_duty(duty_parameter);
to get the duty_parameter of your desired duty cycle, note the following:
the maximum value of duty_paramter (MAX) = ((preload+1)*4) -1
so in our case the maximum value is ((255+1)*4)-1 = 1023 and it represents a 100% duty cycle.
now if we need a duty cycle of 50% it would be 1023*0.5 = 512, and a duty cycle of 25% would be 1023*0.25 =
256, thus,
Note: the capital letter L at the end of the parameter instructs the compiler to treat the number as Long
integer.
blog.ghatasheh.com/2012/01/dc-motor-bidirectional-speed-control.html 2/3
07/11/13 DC Motor Bidirectional Speed Control Using PWM | eProject
[http://4.bp.blogspot.com/-A7BSqk8rQvA/TxqnJn41spI/AAAAAAAAAC0/q1443MeZ8lc/s1600/Schematic.png]
Buttons UP/Down are used to increase/decrease the speed (the duty cycle)
Button Direction is used to flip the direction of rotation (CW/CCW)
CCP1 Pin 17 carries the PWM signal and connected to the motor enable pin ENA
Diodes D1-D4 are free-wheeling diodes used to protect the circuit from reverse currents.
C1 is an electromagnetic interference elimination capacitor, to protect against voltage spikes.
The C-Code
Code below is written using CCS PIC-C compiler V4.114
Code on GitHub [https://github.com/badr-ghatasheh/CCS_PIC16_MOTOR_CONTROLLER.git]
Good Luck!
Badr Ghatasheh
blog.ghatasheh.com/2012/01/dc-motor-bidirectional-speed-control.html 3/3