amit78
Joined: 17 Jul 2009 Posts: 22 Location: Kolkata, India
|
How to drive a stepper motor using PWM |
Posted: Sat Apr 17, 2010 5:07 am |
|
|
Hi All,
I need to run a bipolar stepper motor using PIC. What I have in hand is a PIC18F452, 40 pin DIP, a 12V stepper motor and a L298 driver. In PIC18F452 I found 2 pwm out (PIN RC2 -> CCP1, PIN RC1-> CCP2). My first question: Is it possible to drive a stepper motor using two CCP?
As per my knowledge there are two coils. One is sin coil and another is cos coil. For micro stepping I have divided the stepper step (its 1.8 deg / step) into 6 steps each of 15 deg. But while sin and cos values going into –ve I need to switch them.
sin+ pin is set to CCP1 and sin– pin is set to GND. Same thing I did for cos+ and cos– pins using CCP2. But when sin value goes in –ve then I need to switch the CCP1 pin to sin- and GND for sin+, similarly for cos pins. But don’t know how to do this.
Can I use multiplexer to do this job? It’s because PIN RB3 of PIC18F452 is the alternate pin for CCP2 pin multiplexing. Can I use this for my purpose?
Here is my test code for a single step.
Code: |
#include <18F452.h>
#fuses HS,NOPROTECT,NOWDT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#define STEP_DELAY 500
#define driver_a PIN_C0
#define driver_b PIN_C4
void main()
{
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_ccp2(CCP_PWM); // Configure CCP2 as a PWM
setup_timer_2(T2_DIV_BY_16, 249, 8);
output_high(driver_a);
while (1)
{
set_pwm1_duty(64); // 15* duty cycle on sin coil - pin C2 (CCP1) (249 * sin(15*))
set_pwm2_duty(241); // 15* duty cycle on cos coil - pin C1 (CCP2)
delay_ms(STEP_DELAY);
set_pwm1_duty(125); // 30* duty cycle on sin coil - pin C2 (CCP1)
set_pwm2_duty(216); // 30* duty cycle on cos coil - pin C1 (CCP2)
delay_ms(STEP_DELAY);
set_pwm1_duty(176); // 45* duty cycle on sin coil - pin C2 (CCP1)
set_pwm2_duty(176); // 45* duty cycle on cos coil - pin C1 (CCP2)
delay_ms(STEP_DELAY);
set_pwm1_duty(216); // 60* duty cycle on sin coil - pin C2 (CCP1)
set_pwm2_duty(125); // 60* duty cycle on cos coil - pin C1 (CCP2)
delay_ms(STEP_DELAY);
set_pwm1_duty(241); // 75* duty cycle on sin coil - pin C2 (CCP1)
set_pwm2_duty(64); // 75* duty cycle on cos coil - pin C1 (CCP2)
delay_ms(STEP_DELAY);
set_pwm1_duty(240); // 90* duty cycle on sin coil - pin C2 (CCP1)
set_pwm2_duty(0); // 90* duty cycle on cos coil - pin C1 (CCP2)
delay_ms(STEP_DELAY);
delay_ms(STEP_DELAY*5);
}
}
// 15* => 15 degree
|
This is my circuit....
http://www.flickr.com/photos/41767017@N04/4527416195/
Kindly help...
Regards,
Amit |
|