Measuring motor speed and display result on LCD
For measuring motos speed there can Optical interrupter used like H21A1. This is a device where IR LED and photo-transistor is coupled in to plastic housing. The gap between then allows interrupting signal with opaque material and this way switching the output from ON to OFF.
This device can be connected to Microcontrollers ICP pin and this way measuring PWM disk (with hole in it) speed can be measured. Disk has to me fixed to axis of motor. Each time the hole of disk passes the gap, optical interrupter will form a pulse which goes to ICP pin to trigger the timer. If take measuring interval 1s, then counted pulses will be equal to turns in Hz.
Lets take Atmega8 microcontroller which is clocked at 8MHz. For this lets use timer pre-scaler 8, then timer will run at frequency equal 1MHz(period 1μs ). Each time the pulse reaches ICP(Atmega8 – PB0 pin) pin then on falling front of pulse input capture interrupt occur. Interrupt service routine counts the number of timer pulses between two pulses. Number of timer counts define the disk speed (RPM - revolutions per minute).
T – duration of one disk turn. The results will be displayed on 2x16 LCD. LCD is connected to AVR as follows:
LCD data pins to AVR PORTD;
LCD control pins to AVR PORTC (RS->PC0, R/W->PC1, E->PC2).
#define RPM 60000000u
#define ICP PINB0
//timer overflow counter
uint16_t PreviousTime, CurrentTime;
//timer1 input capture interrupt service routine
else if (calculate==1)
//Saving current timer value on falling edge
//form string with RPM value
//output to LCD
LCDinit();//init LCD 8 bit, dual line, cursor right
LCDstring("Count RPM", 9);
DDRB&=~(1<<ICP);//ICR1 as input
TCNT1=0;// start counting from zero
TIMSK|=(1<<TICIE1);//|(1<<TOIE1);//enable input capture interrupts
TCCR1B|=(1<<CS11);//start with prescaller 8, rising edge ICP1
LCD library can be found in previous posts.