Hello,
I need to generate a signal from a CTIMER to feed a stepper motor drive to control the speed of the motor and to being able to count the number of pulse I generated and stop the signal when an number of pulses have been reach.
I succeed to do it at constant speed (when I don't need to change the frequency of my signal) by doing like this :
// To start my timer
CTIMER_StopTimer(CTIMER1);
CTIMER_Reset(CTIMER1);
CTIMER_SetupPwm(CTIMER1, CTIMER_MAT_OUT, 50, freq, CTIMER1_TICK_FREQ, false);
CTIMER_StartTimer(CTIMER1);
// To count the number of pulses before to stop motor
void CTIMER1_Callback_interrupt(uint32_t flags)
{
countIncMotor++;
if( countIncMotor >= M1_stepsNumberToRun) // arret moteur nombre de pas atteint
{
Moteurs.b.bOnOffM1 = false;
CTIMER_StopTimer(CTIMER1);
GPIO_PinWrite(GPIO, MOTOR1_INIT_ENABLE_PORT, MOTOR1_INIT_ENABLE_PIN, 1);
printf("Pulse motor stop, pulse idx =%d\n",countIncMotor );
}
}
When I don't change the frequency signal during motor is on, no problem the number of speed counted by the CTIMER1_Callback_interrupt correspond to the number of pulses observed on the oscilloscope and the motor stop at expected position.
But when I had some acceleration and deceleration during motor movement the motor finished always with a position with more steps than expected almost like if the callback missed some pulses during PMW reinitialization. Do you see any work around to avoid this problem?
How I do to change the frequency :
// loop task
while(1)
{
DelayMS(TIME_BETWEEN_FREQ_CHANGE_IN_MS);
if(Moteurs.b.bOnOffM1)
{
if( nbLoopOccured == 0 ) M1_currentFreq = M1_miniFreq;
if( nbLoopOccured < (M1_maxFreq - M1_miniFreq)/M1_freqDiff) // acceleration
{
CTIMER_StopTimer(CTIMER1);
CTIMER_Reset(CTIMER1);
M1_currentFreq = M1_currentFreq + M1_freqDiff;
CTIMER_SetupPwm(CTIMER1, CTIMER_MAT_OUT, 50, M1_currentFreq, CTIMER1_TICK_FREQ, false);
CTIMER_StartTimer(CTIMER1);
printf("acceleration %d < %d freq:%d\n", (nbLoopOccured), (M1_maxFreq - M1_miniFreq)/M1_freqDiff , M1_currentFreq);
}
else // deceleration
{
if( M1_currentFreq> M1_miniFreq)
{
CTIMER_StopTimer(CTIMER1);
CTIMER_Reset(CTIMER1);
M1_currentFreq = M1_currentFreq - M1_freqDiff;
CTIMER_SetupPwm(CTIMER1, CTIMER_MAT_OUT, 50, M1_currentFreq, CTIMER1_TICK_FREQ, false);
CTIMER_StartTimer(CTIMER1);
printf("deceleration %d < %d freq:%d\n", (nbLoopOccured), (M1_maxFreq - M1_miniFreq)/M1_freqDiff , M1_currentFreq);
}
}
Hello @AntoineB
If using LPC54608 control motor, recommend you use SCTIMER. How about using it, check whether can resolve your issue.
BR
Alice