I'm using a PIC16F887 to drive a stepper motor via a dedicated driver. The driver accepts pulse signals and I'm using the hardware PWM on the PIC to deliver this. I'm looking for high start torque, which forced me to implement a ramp up on the stepper motor speed, as steppers offer more torque at low speeds. Motor / driver is set to quarter step operation, which means it needs 800 pulses per rotation, ie. 800Hz PWM signal for 60RPM. I'm planning to ramp up to 240RPM, which would be 3200Hz. I've calculated that using the T2 prescaler I can achieve these frequencies by changing the PR2 register from 77 to 18. Based on this I've put together a short function, using delays:
void slow_ramp(void)
{
//PWM code for slow ramp start
CCPR1L = 0b00000010; //set to 2, so pulse width is 2 x 16 = 32 with 16 prescaler
while (PR2 != 18) //ramp up speed slowly from 60RPM [PR2 = 77] to 240RPM [PR2 = 18]
{
while (TMR2IF == 0) {} //wait for overflow
PR2--;
__delay_ms(5);
}
}
This works, but I'm not sure whether I need to use the "while (TMR2IF == 0) {}" statement, or can I simply change the PR2 whenever?
I was tempted to use interrupts based on the TMR2IF flag, but I already use INTs as a precise 100ms timer, so I'd rather not complicate that.
Regards,
dsc.
void slow_ramp(void)
{
//PWM code for slow ramp start
CCPR1L = 0b00000010; //set to 2, so pulse width is 2 x 16 = 32 with 16 prescaler
while (PR2 != 18) //ramp up speed slowly from 60RPM [PR2 = 77] to 240RPM [PR2 = 18]
{
while (TMR2IF == 0) {} //wait for overflow
PR2--;
__delay_ms(5);
}
}
This works, but I'm not sure whether I need to use the "while (TMR2IF == 0) {}" statement, or can I simply change the PR2 whenever?
I was tempted to use interrupts based on the TMR2IF flag, but I already use INTs as a precise 100ms timer, so I'd rather not complicate that.
Regards,
dsc.