| ??? 02/12/04 23:02 Read: times |
#64669 - RE: calculating rpm using 89c2051 Responding to: ???'s previous message |
There may be a couple of issues here: 1/ the input circuit. Use a pulse generator to test with (or another micro to create the pulses). This will determine whether the input circuit is the issue or the code 2/ The fastest input you can feed a counter/timer input on a 8051 is half the clock/12. Assuming a 12mhz clock this would be 500khz. At an input of 250hz that is 4mS per pulse and the micro would have 4000 machine cycles (4000 instruction approx) between pulses - plenty of time. I normally use an interrupt input (capture preferably if the micro has it) and measure the period. In the interrupt routine you read the timer's current value then subtract that from the value you got from the last interrupt - this value is the period (in timer ticks) store this value, update the previous value with the current value and exit the interrupt routine. Your mainline code can then read the period then convert that to rpm by doing a division. If your cpu clock is 12mhz, timer tick is 1uS. Assuming 1 pulse per rev then calculation is: rpm = 60,000,000/period. This requires a 32bit divide. I also do a bit of fitering on the value to make it readable (the actual period fluctuates quite a bit on real engines). |



