[ATtiny85] measuring PWM duty cycle using timer1 / floating point math in ISRs #782
dragoncoder047
started this conversation in
Support / Q & A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
So I have a DC gearmotor hooked up to an H-bridge with an AS5600 encoder monitoring the position. The AS5600 is configured to output the angle as a PWM duty cycle, at 910 Hz, and I want the Attiny to monitor the position and velocity and control the motor based on commands sent over I2C.
I am measuring the duty cycle by enabling pin change interrupts (on PB3 only, if that makes any difference), setting the Timer1 prescaler to the value that gets it closest to 910 Hz, and then bending the system clock down using OSCCAL so the Timer1 overflow rate is just under 910 Hz. Then at the top of the PCINT0 ISR, I read PB3, on the falling edge I record TCNT1 as the high time and return, and on the rising edge I record TCNT1 again as the total time and then set TCNT1 back to 0. On each rising edge, in the ISR I also do a little math to map the 2.9-97.1% duty cycle range to an 0-255 angle, and also detect zero crossings and update a total turns count.
The interrupt also uses the PWM signal as a timebase to run the PID loop to maintain the speed/position requested (I have millis()/micros() disabled so I can use timer0 for PWM), but because the code there is not as time-critical I do interrupts again before entering that section of the ISR, then run the lowpass filters and PID loops.
At the minimum / maximum duty cycle (2.9%) the Attiny only has 32µs = (at 7MHz) 223 clocks to run the interrupt on one edge before it has to run the interrupt on the other edge (no matter which one of them I put the heavy calculations in), so the interrupt has to be substantially less than 223 instructions for this to work.
Unfortunately all I have gotten is garbage from the angle/speed calculations when using slow floating point math, and I'm not sure how to do this without using floating-point. Do you have any suggestions for how to speed up these calculations?
Beta Was this translation helpful? Give feedback.
All reactions