0

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!

dda
1,5951 gold badge12 silver badges17 bronze badges
asked May 3, 2018 at 16:51
4
  • Which is it? Not displaying anything, or only displaying "Frequency of signal"? Commented May 3, 2018 at 18:39
  • I fixed your indentation, as it was nearly impossible to read. Commented May 3, 2018 at 18:43
  • If the latter, it seems like you signal on pin 6 isn't there, or is too weak. Ending up with your code waiting for 50 transitions that just aren't there? Commented May 3, 2018 at 18:50
  • It is only displaying frequency of the signal only, which is written at the top of code. After that nothing is working. I checked the pin 6 signal And it was working fine. Commented May 4, 2018 at 0:33

1 Answer 1

2

Your variables are int, which on an Arduino is a 2-byte signed type by default, and has a range of -32768 to 32767.

When you execute:

LineFreq = 25000000/countC;

is the result of the math a 2-byte int, or a 4-byte long? Regardless, you are possibly assigning a 4-byte value to an int.

You can declare LineFreq to be type long, and also append an L to the end of the constant value 25000000 to force long math, so that you have:

long LineFreq;
[...]
LineFreq = 25000000L/countC;
answered May 3, 2018 at 18:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.