1 to 99 pulses per rev tachometer software description
This is how the tachometer firmware works.
The period of a revolution is measured and this is used to calculate revolutions per minute.
The PIC16F628A (And other PIC’s) have a very useful built in 16 bit counter/timer (TMR1) whose value can be captured. It can be configured to work in several different ways but this is how it is used here. It is used as a free running counter which is incremented every 0.4 uS. Its value is captured every time an input pulse is received on the CCP1 from the sensor. A 16 bit counter is not large enough for our requirements so it is extended to 32 bits in software. Every time the 16 bit counter overflows it generates an interrupt. This interrupt increments the value of a 16 bit software counter. When an input pulse is received from the sensor the value of this 16 bit counter is also captured. The result is the value of the 32 bit counter is captured every time an input pulse is received. The difference in the last value captured and the current value captured is calculated. This is used to calculate the number of pulses received in one minute. (RPM) This is done by dividing this number into the number of 0.4 uS in one minute. 60 *1000000/0.4 = 150000000
There is a situation that could occur if an input pulse is received from the sensor at almost the same time as TMR1 overflows. We do not know if the value of TMR1 was captured just before or just after it incremented. The code which deals with CCP1 interrupts tests if a TMR1 overflow interrupt has also occurred. If so it tests the top bit of the captured value of TMR1. If it is set then the capture occurred just before TMR1 overflowed. If it is clear then the captured value of the top 16 bits of the 32 bit count of the number of 0.4 uS pulses is incremented.
This simple system worked well on the bench when the tachometer was being tested with a signal generator. When fitted to a drill press there was some jitter in the readings due to belt vibration and play in the pulley that had the reflective marker.
I decided to average out the period of revolution over several revolutions if the speed was high enough to allow it without excessively long update time. I decided that an update time of 0.5 seconds was about right. (Except when one revolution took more than 0.5 seconds.) We will call the start of a measurement T0. From T0 we count the number of pulses from the sensor as well as measuring the time elapsed since T0.
At every input pulse the elapsed time from T0 is compared with 0.5 seconds. If the value is greater than 0.5 seconds then the calculations are performed. The elapsed time since T0 is divided by the number of input pulses since T0. This gives the average period of one revolution. The speed calculation is done using this average value. Although the test is for a value of 0.5 seconds a pulse that occurs just before 0.5 seconds will mean that the next pulse will be nearly 1 second from T0 so in this case the update time will be almost 1 second.
The next step was to deal with multiple pulses per revolution for example from a gear sensor. All that needs to be done is the result of the preceding calculation to be divided by the number of pulses per revolution. (PPR)
There was only one spare input left on the PIC so a single push button is used to set the value of PPR. At power on the software looks to see if the button is pressed. If so then setup mode is entered. When the button is released the current PPR value is displayed. The button is then held down until the desired PPR value is displayed. I put in a 5 second delay before the new value is saved to EEPROM to avoid excessive writes. This is not really necessary as setup will only be used when fitting the tacho to a new machine.
The period of a revolution is measured and this is used to calculate revolutions per minute.
The PIC16F628A (And other PIC’s) have a very useful built in 16 bit counter/timer (TMR1) whose value can be captured. It can be configured to work in several different ways but this is how it is used here. It is used as a free running counter which is incremented every 0.4 uS. Its value is captured every time an input pulse is received on the CCP1 from the sensor. A 16 bit counter is not large enough for our requirements so it is extended to 32 bits in software. Every time the 16 bit counter overflows it generates an interrupt. This interrupt increments the value of a 16 bit software counter. When an input pulse is received from the sensor the value of this 16 bit counter is also captured. The result is the value of the 32 bit counter is captured every time an input pulse is received. The difference in the last value captured and the current value captured is calculated. This is used to calculate the number of pulses received in one minute. (RPM) This is done by dividing this number into the number of 0.4 uS in one minute. 60 *1000000/0.4 = 150000000
There is a situation that could occur if an input pulse is received from the sensor at almost the same time as TMR1 overflows. We do not know if the value of TMR1 was captured just before or just after it incremented. The code which deals with CCP1 interrupts tests if a TMR1 overflow interrupt has also occurred. If so it tests the top bit of the captured value of TMR1. If it is set then the capture occurred just before TMR1 overflowed. If it is clear then the captured value of the top 16 bits of the 32 bit count of the number of 0.4 uS pulses is incremented.
This simple system worked well on the bench when the tachometer was being tested with a signal generator. When fitted to a drill press there was some jitter in the readings due to belt vibration and play in the pulley that had the reflective marker.
I decided to average out the period of revolution over several revolutions if the speed was high enough to allow it without excessively long update time. I decided that an update time of 0.5 seconds was about right. (Except when one revolution took more than 0.5 seconds.) We will call the start of a measurement T0. From T0 we count the number of pulses from the sensor as well as measuring the time elapsed since T0.
At every input pulse the elapsed time from T0 is compared with 0.5 seconds. If the value is greater than 0.5 seconds then the calculations are performed. The elapsed time since T0 is divided by the number of input pulses since T0. This gives the average period of one revolution. The speed calculation is done using this average value. Although the test is for a value of 0.5 seconds a pulse that occurs just before 0.5 seconds will mean that the next pulse will be nearly 1 second from T0 so in this case the update time will be almost 1 second.
The next step was to deal with multiple pulses per revolution for example from a gear sensor. All that needs to be done is the result of the preceding calculation to be divided by the number of pulses per revolution. (PPR)
There was only one spare input left on the PIC so a single push button is used to set the value of PPR. At power on the software looks to see if the button is pressed. If so then setup mode is entered. When the button is released the current PPR value is displayed. The button is then held down until the desired PPR value is displayed. I put in a 5 second delay before the new value is saved to EEPROM to avoid excessive writes. This is not really necessary as setup will only be used when fitting the tacho to a new machine.