Path: Home => AVR-Overview => Applications => Frequency counter    (Diese Seite in Deutsch: Flag DE) Logo
Frequency counter

40MHz-Frequency counter with ATmega8

This application of an AVR describes a frequency counter with ATMEL's ATmega8 with the following properties:

0. Content

1 Hardware

The frequency counter consists of an input unit (preamp and prescaler) and the processor unit.

1.1 Input unit

Scheme input unit
The input unit has an analogue and a digital input unit.
The signal on the analogue input is amplified by a fast operation amplifier NE592. Its output signal is adjusted to the the input of a NAND 74HCT132.
The digital input is directly fed into the NAND.
Depending from a processor signal the output of the NAND is either transferred directly to the processor's counting port or is divided by 16 by a 74HCT93.

1.2 Processor unit

Scheme processor unit
The processor unit is designed around the ATmega8. The processor is driven with an xtal of 16 MHz on its pins XTAL1 and XTAL2. The ceramic capacitors of 22 pF help to accelerate the oscillator during start-up.
The supply voltage of 5 volts is attached to pin 8 (GND) and 7 (GND) and blocked by a ceramic concensator of 100 nF. Supply of the AD converter is by 5 volt over an inductor of 22 µH to pin 22, also blocked by a 100 nF condensator. The internal reference voltage is filtered by a film capacitor of 100 nF.
The AD conversion input pin PC1 is attached to the potentiometer that selects the mode. The resistor of 100 k limits its output voltage to 2,5 V. If a certain mode shall be fixed, a trim potentiometer or a voltage divider with two resistors can be used.
On the AD converter channel ADC0 (on PC0) is attached to a voltage divider for the input signal to measure input voltages. In the default case, a maximum of 5.12 V can be measured. By changing the voltage divider resistors, range and resolution can be easily changed. The input of the ADC is blocked against high frequency influence.
The signals TXD and RXD make up the serial interface and attached to the driver IC MAX232. The electrolytic condensators on the MAX232 provide the necessary voltages for the serial RS232 interface. The serial signals are attached to a 10-pole connector that delivers a 9-pole standard serial connector. The signals CTS, DSR and CD are activated by resistors of 2k2 to the supply voltage.
The I/O pin PC5 drives the prescaler. If high, the prescaler is disabled, if low the signal is divided by 16.
The signal input from the preamp/prescaler is attached to the input INT0 (for edge detection) as well as to T0 (for counting signals).
The port bits PB0 to PB3 serve the four data bits of the LCD. The Enable-input of the LCD is driven by PB5, its RS input by PB4. The Enable-input is via a 100 k resistor attached to GND to disable the input in case the port bit is inactive. The V0 input of the LCD is attached to a trim potentiometer of 10 k to adjust the contrast of the LCD.
The port bits MOSI, SCK and MISO as well as the RESET signal are attached to a 10-pole connector, over which the processor can be programmed. The red LED is on if programming is active. Supply voltage lines are also attached to this connector.
The RESET input is attached with a 10 k resistor to the supply voltage.

1.3 LCD display


The LCD is attached over a standard 14-pole connector to the processor unit. You can use LCDs with 8, 16 or more characters per line and single or double lines (adjustable via software).

1.4 SIO interface

The counter has a SIO connection, so measuring data can be read and parameters can be written.

1.5 Hints for mounting

The complete circuit was build on a breadboard of 10 * 5 cm and wired with enammelled copper.


Two of the srews for the LCD also fix the breadboard.


All components, including the supply and a 9 V battery fit into a small casing.


2. Operation

Operation is very easy. A selection between analogue and digital input is not necessary, just attach the source to the required female connector. Just adjust the trim potentiometer to a setting where signals from the analogue input are measured, and where no false signals from the opamp confuse these measurements.

2.1 Potentiometer for mode selection

The mode selector (potentiometer) selects the mode. If no input signals are present, and a mode based on signal duration measurement is selected, update of the display is not performed immediately but on the first detected edge.

The display of results on a two-line-LCD with 16 characters is as follows:

ModeMeasuredMethodDisplay format
0FrequencyCounting, Prescaler=16F=99,999,999 Hz
1FrequencyCounting, Prescaler=1f= 9,999,999 Hz
2FrequencyPeriod durationv= 9,999.999 Hz
3Rounds per minutePeriod durationu= 9,999,999 rpm
4Period durationPeriod durationt=99,999,999 us
5High-periodPeriod durationh=99,999,999 us
6Low-periodPeriod durationl=99,999,999 us
7High-period portionPeriod durationP=100.0%
8Low-period portionPeriod durationp=100,0%

In a single-line LCD, the voltage is displayed only if mode 9 is selected. At less than 16 character per line, thousand separators and dimension are not displayed. Abbreviations for the measuring code are diplayed only if the displayed value does not require this space.

2.2 Volatge measurements

The voltage on the input pin is measured and displayed four times per second.

3. Software

The software is completely written in Assembler. Prior to assembling the source code the internal adjustments have to be made (see 3.1). During or after programming the hex code to the chip's flash the fuses have to be changed (see 3.2). Note that after setting the fuses, the chip is only accessible with an external xtal attached.

3.1 Adjustments prior to assembling

The following adjustments have to be made on the source code file fcountV03.asm:

3.2 Fuses

In its original form ATmega8 runs with its internal RC oscillator. To change this the fuses must be adjusted to the external xtal.

The following adjustments shown are with ATMEL's Studio: Fuses Studio Image 1 Fuses Studio Image 2

In PonyProg 2000 the same looks like this:

Fuses PonyProg

Please note: After changing the fuses, the ATmega8 only works with an xtal attached.

3.3 Commented source code

The source code can be displayed in HTML format (here) and downloaded as assembler source code file (here). The text file ReadMe3.txt has additional hints for programming and operation.

©2006-2009 by