I am working on a project to calculate line frequency meter by reciprocal counting with Arduino. I am trying to print the the result on a 16*2 LCD but it is not displaying anything. It just gets on and stuck there. I got the project details from this website Line Frequency Meter Based On Reciprocal Counting
I modified the stock program to interface the LCD but it's not working. Here is the code I am using:
//To read and display the Line frequency
//using Reciprocal counting method
//clk for 'Time Counter' is generated with 'Tone' function
//'Time counter' is the physical counter T1
// 'EVENT Counter' is a software counter
#define Fin 6 // input pin for Line freq sig
#define clk 7 // input to ' Time ' counter
#include<LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
boolean FState; // status of the 50 Hz Signal
int countS; // Signal count
int countC=0; // Clock count
int LineFreq; // Result
void setup() {
tone(8, 5000); // generates ref freq of 5000 Hz at Pin 8
pinMode(Fin, INPUT); // set the Fin pin as INPUT
pinMode(clk, INPUT); // set the clk pin as INPUT
lcd.begin(16, 2);
}
void loop() {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Frequency of signal");
// to avoid partial cycles, first detect the rising edge
while(digitalRead(Fin)) {
//wait for '0' state of the Signal
delayMicroseconds(10);
}
//here '0' state
while(!digitalRead(Fin)) {
//wait for '1' state
delayMicroseconds(10);
}
//this is the rising edge of the Fin
//counting can start from here
// clear the TIME Counter T1
counterStart1();
//count 50 of the Line freq signals
while(countS<50) {
while(digitalRead(Fin)) {
//wait for '0' state
delayMicroseconds(1);
}
//here '0' state
while(!digitalRead(Fin)) {
//wait for '1' state
delayMicroseconds(1);
}
// here '1' state
// here one cycle is just over
//increment the event counter
countS=countS+1;
}
//here 50 Signal cycles over
// read the contents of T1 & compute frequency
countC = getCount1();
LineFreq = 25000000/countC;
//send to display
lcd.setCursor(0,1);
lcd.print(LineFreq);
lcd.print(" Hz");
delay(500);
delay(100);
// clear counter
countS=0;
// repeat
}
//The following code is obtained from on-line articles in the public domain
void counterStart1() {
// hardware counter setup, for the 16-bit Timer1 Timer/Counter
TCCR1A=0; // reset timer/countern control register A
TCCR1B=0; // reset timer/countern control register B
TCNT1=0; // initialize the counter value to 0; this register holds the current count
// set timer/counter1 hardware as a counter; it counts events on pin Tn (Arduino pin 5)
TCCR1B = TCCR1B | 7; // Counter Clock source = pin Tn (Arduino pin 5) , start counting now
// 7 in binary is 0111; OR-ing will set CS10,11,12 to 1's
return;
}
// the following code is obtained from on-line articles in the public domain
// Sub routine to get the current count
unsigned int getCount1() {
unsigned int count; // this variable returns the current count from the counting register
TCCR1B = TCCR1B & ~7; // Gate Off / Counter T1 stopped,
// this operation clears the bits (CS10,11,12)
count = TCNT1; // read the counting register
TCCR1B = TCCR1B | 7; // re-start counting by resetting the bits (CS10,11,12)
return count; // return the retrieved count to the calling function
}
I am getting a blank screen but the line at very first of the code "Frequency of signal" is printing accurately.
What is wrong with the code and what changes do I need?
Thanks in advance!