Frequency Counter

Hi, I am completely new to AVR programming and finding it quite hard to understand, as my C programming is weak. I have got a part of my project that requires this. Basically I have built a liquid level sensing circuit based on capacitance to frequency converter and have a frequency based signal as an output. The signal is of the range 1Hz to 100Khz. I require a basic code to be able to read this frequency out, and this is in an AVR studio environment.The device is set to ATmega16 and the Device frequency needs to be 8.0Mhz. If theres someone to help with a basic code to start off with, will be very kind.

Thanks

You will have to turn the signal into a 5 volt square wave to get the higher frequencies and read as digital input. As I have discovered, the analogs reads are quite slow.

const int CAP = 2;
unsigned long StartTime = 0;
unsigned long EndTime=0;
unsigned long Duration=0;

void setup() {
  pinMode(CAP, INPUT);           // initialize the LED pin as an Input:
  Serial.begin(57600);            // initialize serial communications:
}

void loop() {
   while(digitalRead(CAP) != LOW){}                   // Wait for falling edge  
   while(digitalRead(CAP) != HIGH){}                  // Wait for rising edge 
   CAPStartTime=micros();  
   while(digitalRead(CAP) != LOW){}                   // Wait for Rising edge       
   while(digitalRead(CAP) != HIGH){}                  // Wait for Rising edge  
   CAPEndTime=micros();   
   CAPDuration=CAPEndTime-CAPStartTime; 
   DrumDuration=DrumEndTime-DrumStartTime;     
   Serial.print(CAPDuration);     
   Serial.print("  ");   
   Serial.println(1000000/CAPDuration);   
   delay(2000);                                                  // Don't want to send data too quickly 
}

@ Marsheng, Tks Marsheng, I have already reduce the signal voltage to 5v so as to read a digital output. I tried running your code but ends up with errors. I know this is very layman of me, but do i need to add some headers to the code, and secondly, do i need to declare some values.

Tks again

I copied and pasted from my code and changed a bit. Where are the errors?

Change the

unsigned long StartTime = 0; unsigned long EndTime=0; unsigned long Duration=0;

to

unsigned long CAPStartTime = 0; unsigned long CAPEndTime=0; unsigned long CAPDuration=0;

Hi Marsheng, I have changed the bit u ve asked me to, but still end up with quite some few errors. Its basically saying most of the functions to be undeclared. I guess I will need to understand some basics here before I get anywhere, and maybe then come back to u.

Tks .