MAPLE Rev5, IDE 0.0.9
Right now I have the PWM output working for the correct freq and Duty. I am trying to have the PWM output only run so long controlling the steps of a stepper motor. I know the number of steps, freq and duty. I tried using a second timer to control this, but it either over runs or not constant every time. Sometimes it works, but mostly 1 or more steps off.
Below I have it run .8sec(200 steps for 1 turn of motor at 250hz per step), delay 2 sec and restart for testing. How can you make the interrupt more constant when nothing else is going on? Even if the interrupt has overhead and would be off, it should be off the same amount every time right?
;
int pwmcount=0;
void PWM_STOP(void);
void setup(){
Timer3.setPeriod(4000); //.004sec,
pinMode(28,PWM); //pin 28 PWM output
pwmWrite(28,57500); //almost full duty
Timer4.setChannel1Mode(TIMER_OUTPUTCOMPARE);
Timer4.setPeriod(.8*1000000); //1000000=1sec, 200step per turn, .004*200=.8 for 1 turn
Timer4.setCompare1(1);
Timer4.attachCompare1Interrupt(PWM_STOP);
}
void loop(){}
void PWM_STOP(void)
{
pwmcount=pwmcount+1; //easy way to stop start PWM
if((pwmcount%2)==0) //stop the PWM
{
pinMode(28,INPUT); //stop PWM set as input
Timer4.pause(); //pause timer interrupt
delay(2000); //delay 2 sec
Timer4.generateUpdate(); //reset timer
Timer4.resume(); //resume timer
}
else
pinMode(28,PWM); //turn PWM back on
}
Thanks