Dennis Crawley wrote: > I'm working on a PID regulator for a speed motor controller :p > > I'm planing to capture with CCP2 the period from a 9 pulses per > revolution encoder. I can't use other encoder than this! In order to > display in RPM I use this algorithm: > > RPM = F * 60 / 9 > since F = 1 / t(Period) > > RPM = 60M / t(us) / 9 > > or > RPM = 6666666 / t(us) > > is there a simplest method? Dennis: In another post, you asked about dividing 6666666 by 444 to get 15015... So I assume that your motor spins at around 15000 RPM. If it really does spin that fast, you might try working with frequency rather than period. In other words, count pulses over some period of time "t", and then just transform pulses/t into revolutions/minute through simple multiplication. If you count pulses over 60/9 = 6.666 seconds, for instance, the number of pulses will be exactly equal to the RPM. If 6-second updates aren't fast enough, you could count pulses over a shorter period of time, then multiply. If you counted pulses over, say, 60/(9*256) = 26.042 milliseconds, you'd just multiply your count by 256 (which takes zero instructions and zero cycles) to get RPM. Of course, your frequency resolution is decreased this way, so you'd have to find a balance between resolution and frequency of updates. -Andy === Andrew Warren -- aiw@cypress.com === Principal Design Engineer === Cypress Semiconductor Corporation === === Opinions expressed above do not === necessarily represent those of === Cypress Semiconductor Corporation -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu