As promised, here's my frequency counter. Don't worry about the lcd stuff, that's just my way of displaying the value on a HD44780. It's very simple
#include <avr/io.h>
#include <avr/interrupt.h>
#include <string.h>
#include "lcd.h"
/* Definitions */
#define F_CPU 25000000
/* Prototypes */
void startTimer100mSec(void);
void startTimer1Sec(void);
void startTimer10Sec(void);
/* Declarations */
volatile unsigned long int overflow0, overflow1, frequency;
char disp[15];
/* Interrupt Handlers */
ISR (TIMER0_OVF_vect){
overflow0++;
}
ISR (TIMER1_OVF_vect){
overflow1++;
}
/* Functions */
void startTimer100mSec(void){
unsigned long int tmp;
tmp = F_CPU/10;
overflow0 = 0;
TCNT0 = 0;
while((overflow0*256 + TCNT0) < tmp) {}
return;
}
void startTimer1Sec(void){
overflow0 = 0;
TCNT0 = 0;
while((overflow0*256 + TCNT0) < F_CPU) {}
return;
}
void startTimer10Sec(void){
unsigned long int tmp;
tmp = 10*F_CPU;
overflow0 = 0;
TCNT0 = 0;
while((overflow0*256 + TCNT0) < tmp) {}
return;
}
int main(void)
{
DDRB = 0x3E; // PB0=Input, PB1-PB5=Output
/* Timer/Counter0 - Gate Time */
PRR = (0<<PRTIM0); // Power-on Timer/Counter0
TCCR0A = 0; // Normal Mode
TCCR0B = 1; // No Prescaler
TIMSK0 = 1; // Enable Overflow Interrupt
TCNT0 = 0;
sei();
/* Timer/Counter1 - Counter */
PRR = (0<<PRTIM1); // Power-on Timer/Counter1
TCCR1A = 0;
TCCR1B = 7; // Normal Mode, T1-Pin as Clk
TIMSK1 = 1; // Enable Overflow Interrupt
TCNT1 = 0; // Initialize Counter
sei(); // Enable Interrupts
/* Specifics */
lcd_init(LCD_DISP_ON);
while(1){
frequency = 0;
overflow0 = 0;
overflow1 = 0;
TCNT0 = 0;
TCNT1 = 0;
startTimer1Sec();
frequency = (65535*overflow1) + TCNT1;
sprintf(disp,"%lu",frequency);
lcd_clrscr();
lcd_home();
lcd_puts(disp);
}
return 0;
}