The principle of operation of the frequency counter on the microcontroller avr. Homemade frequency counter on ATTINY2313. microcontroller software

The development of the design was prompted by a remark read on the DDS forum that there should have been other high-frequency dividers besides the 193 and 500 series, as well as a timely seen scheme of a new synthesizer for FM2006. After experiments, a simple frequency meter was born on LMX 2306, ATtiny 2313 microcircuits and a BC 1602 character-synthesizing liquid crystal indicator with the following characteristics:

  • Measuring frequency range from 300 Hz to 450 MHz
  • Sensitivity from 50 mV to 200 mV
  • Minimum measuring step:
  • In the range from 300 Hz to 4.5 MHz 1 Hz
  • 4.5 MHz to 80 MHz 25 Hz
  • 80 MHz to 450 MHz 100 Hz
  • Measurement time 0.1 sec / 1 sec
  • Measurement accuracy is not worse than 0.007%
  • Supply voltage 9V…15V
  • Current consumption (without indicator light) 20 mA

Description and configuration of the scheme (fig.1).

The signal from input F goes to the amplifying stage on the transistor VT1 from which it diverges to a programmable high-frequency divider, which is part of the DD1 microcircuit, as well as to the SA1 sliding switch, which selects the measurement range (up to 4.5 MHz / above 4.5 MHz). Further, the signal is further amplified and fed to the DD2 chip, which performs frequency counting, data output to the LCD and control of the DD1 chip. The circuit is powered by the DA1 stabilizer.

Switch SA2 selects the counting time and, accordingly, the measurement accuracy. The SB1 button is used to calibrate the frequency meter. To do this, an exemplary frequency of 1 MHz is applied to the input F and by pressing SB1 hold it until the readings on the LCD display are as close as possible to 1 MHz. Further calibration can be omitted.

You can also use the standard tuning procedure by applying any reference frequency to the F input and selecting C9 and C10 to achieve the desired LCD readings.

The chain D1, R5, R6, C7, together with the cascade on the transistor VT2, expands the pulses coming from the DD1 chip. When the maximum possible frequency is applied to the input F, but not more than 450 MHz, by selecting the resistor R5, stable LCD readings are achieved (if the oscilloscope is connected to the 9th leg of DD2, there should be something close to a meander). Capacitor C7 in the design we assembled moved to the collector VT2.

The Prog connector is used for in-circuit programming of the ATtiny 2313. If the microcircuit is flashed in the programmer, then the connector is not soldered. It is better to install the microcircuit in the socket.

Details.

Fixed resistors and ceramic capacitors size 0805 (surface mount). Transistor VT1 KT368 will be replaced by KT399, VT2 KT368 - by a lower frequency KT315 (with board adjustment). Chip DD2 ATtiny 2313-20 (with a clock frequency of up to 20 MHz) in a DIP package is installed on the side of the printed conductors. DA1 (also installed on the print side) - any 5-volt stabilizer with a current of more than 1 A, but if you do not use the LCD backlight, you can also use low-current 78L05. Quartz resonator Q1 - 11.0592 MHz in any design. Switches SA1 and SA2 - B1561(DPDT) or SS21 with a lever length of more than 5 mm. Tact button SB1 - TS-A1PS (TS-A2PS, TS-A3PS, TS-A4PS, TS-A6PS). Indicator BC1602 or BC1601, BC1604, as well as similar with HD-44780 controller from other manufacturers. Be sure to check the consistency of the conclusions! We can replace the VD2 1N4007 diode with any one with a suitable operating current. Power connector - AUB series 3.5mm stereo or similar with some board adjustment. Any low-power AC adapter with a suitable voltage is used for power supply. The signal to the board is fed through a single-core wire with a diameter of approximately 0.8 mm and a length of 5-8 cm.

You can exclude C4, R4 and switch SA1 from the circuit by connecting C8 with a jumper to the VT2 base. 6 leg DD2 should hang in the air. In this embodiment, the lower cutoff frequency becomes 1.5 MHz.

The printed circuit board is divorced in Sprint-Layout and made of one-sided foil fiberglass ( rice. 2).

High performance frequency counter that measures frequencies from 1Hz to 10MHz (9,999,999) with 1Hz resolution over the entire range. Ideal for function generators, digital scales or as a standalone device. It is cheap and easy to manufacture, assembled from readily available parts, small in size and can be panel mounted on many devices.

The circuit consists of seven 7-segment LEDs, an AVR ATtiny2313, and a few transistors and resistors. The AVR does all the work and no additional chips are needed. The microcontroller counts the number of pulses that came to its input in 1 second and displays this number. The most important thing is a very accurate timer, and it is implemented on a 16-bit Timer1 in CTC mode. Second, the 8-bit counter works like Counter0 and counts the pulses at input T0. Every 256 pulses, it causes an interrupt in which the program increases the multiplier. When we get a 1 second interrupt, the content of the multiplier is multiplied by 256 (left shift by 8 bits). The rest of the pulses counted by the counter is written to the register and added to the result of the multiplication. This value is then broken down into individual numbers, which are displayed on the indicators. Thereafter, before exiting the 1-second interruption, both counters are simultaneously reset and the measurement starts again. In its free time from interruption, the controller is engaged in dynamic indication.

Resolution and Accuracy:
The accuracy depends on the clock generator. Quartz must be of good quality and have as low a ppm (tolerance) as possible. It will be better if the frequency is a multiple of 1024, for example, 16 MHz or 22.1184 MHz. To measure frequencies up to 10 MHz, it is necessary to use quartz not less than 21 MHz, for example, 22.1184 MHz. The frequency meter can measure up to 47% of the natural crystal frequency. If there is a good industrial frequency meter, then you can calibrate the circuit by adding a tuning capacitor (1pF-10pF) between one of the quartz leads and ground, and adjust the frequency in accordance with the readings of the industrial frequency meter.

In the source archive there are several options for different quartzes, but you can compile your own version.

Waveform:
In principle, the device understands any waveform from 0 to 5V, not just rectangular pulses. Sinusoid and rectangular pulses are counted on the trailing edge when it goes below 0.8V.

The device does not have protection against exceeding the input voltage above 5 volts.

The device has a high-resistance input and does not load the circuit under test - you can even measure the frequency of 220 volt AC by touching the input with your finger. The frequency counter can be converted to measure frequencies up to 100 MHz in 10 Hz steps by adding a fast divider to the input.

Display:
Seven seven-segment indicators with a common anode were used in the dynamic indication mode. If the brightness is insufficient, you can reduce the values ​​​​of the current-limiting resistors, but you need to remember that the maximum pulse current of each output of the microcontroller is 40 mA. By default, the resistance of the resistors is 100 ohms. Insignificant zeros are blanked out by software. The values ​​are updated every second.

Printed circuit board:
109mm x 23mm double-sided PCB - unfortunately the 7 indicators didn't fit into the workspace of Eagle's free version, so they are hand-drawn. There are 3 wire connections to be made on the board - the first is the power supply and VCC output connection of the controller - this connection is shown on the silkscreen layer. The other two connect the decimal points of the indicators to the 330 ohm resistors located on the bottom layer. At the top of the board is the Atmel ISP-6 connector. Contact 1 is the first on the quartz side. This connector is optional and only needed for controller programming. The indicators should be soldered at some distance from the board so that you can get a soldering iron to the leads soldered from the top of the board.

This homemade ATTINY2313 frequency counter is designed to measure frequencies from approximately 4MHz to over 160MHz. It can be used as a frequency meter or as a TRX I/O device, for example on the 144MHz (2m) band.

Specifications of the frequency meter:

  • frequency measurement in the range of 4-160 MHz
  • display of measurements on the LCD
  • sensitivity 700mV
  • input voltage, max< 30В
  • power supply: 8-15V
  • very simple fee, minimal quantity
    elements, quick start
  • board dimensions: 37x80mm

The circuit worked perfectly in the range from 3.8 MHz to 162 MHz. The circuit is based on the ATTINY2313 microcontroller. Its advantage is the ability to operate at frequencies up to 20 MHz. The circuit uses 16 MHz quartz, so the processor itself should theoretically correctly measure frequencies up to 8 MHz.

It often turns out that the range up to 8 MHz is too small. An increase in the upper range can be obtained using a frequency divider (prescaler). The circuit uses the LB3500 prescaler, which allows you to measure up to 150 MHz.

Brief information about LB3500:

  • supply voltage - 4.5 ... 5.5V
  • current consumption - l6mA-24mA
  • input voltage - 100mV-600mV
  • output voltage - 0.9 Vpp
  • divisor - 8

Without the use of an additional divider, the circuit is capable of measuring frequencies up to 64 MHz. Adding an additional divider in the form of a binary counter 74LS293 (ICl) allows you to increase the measurement range to 150 MHz (max. for LB3500).

ICl divides the frequency by 4. Thus, the entire prescaler system (ICl and IC4) divides the input frequency by 32. The Tl transistor with elements C7, R2, R3 provides a high input impedance.

The input signal after separation goes to the input of the LB3500 chip. At the output of 9 IC4, the signal is 8 times lower frequency than at the input. Unfortunately, the output signal of the LB3500 chip is not consistent with TTL levels. To eliminate this drawback, a transistor T2 is added to the circuit, which is designed for matching. The PRI potentiometer ensures an exact match.

Built . It allows you to measure frequencies up to 10 MHz in four automatically switchable ranges. The smallest range has a resolution of 1 Hz.

Specifications of the frequency meter

  • Band 1: 9.999 kHz, resolution 1 Hz.
  • Band 2: 99.99 kHz, resolution up to 10 Hz.
  • Band 3: 999.9 kHz, resolution up to 100 Hz.
  • Band 4: 9999 kHz, resolution up to 1 kHz.

Description of the frequency counter on the microcontroller

The Attiny2313 microcontroller is powered by an external crystal oscillator with a clock frequency of 20 MHz (this is the maximum allowed frequency). The measurement accuracy of the frequency meter is determined by the accuracy of this quartz. The minimum half-cycle length of the measured signal must be greater than the period of the crystal oscillator (this is due to the limitations of the architecture of the ATtiny2313 microcontroller). Therefore, 50 percent of the oscillator clock is 10 MHz (this is the maximum measurable frequency).

Installing fuses (in PonyProg):

In this article, we will look at how to build a small, cheap and simple frequency counter capable of measuring frequencies up to 40 MHz with an error below 1%. Such accuracy is quite enough for debugging most of your own analog and digital devices. The device will allow you to analyze many aspects of the operation of circuits.

The schematic diagram of the frequency meter is shown in Figure 1.

Fig.1. Schematic diagram of the device

The frequency meter is assembled on a breadboard, the basis is the Atmel ATmega16 microcontroller, the clock source is the internal 8 MHz RC oscillator (this must be remembered when programming the microcontroller). Additionally, the input part uses a 4-bit counter 74HC191 as a divider of the measured frequency by 16 before it is fed to the input of the microcontroller. As you can see, only the output Q3 of the counter is used, the frequency at this output will be equal to the input frequency divided by 16.

The device input (probe) is point W1, which is directly connected to the microcontroller port PB0 and, through a divider, to the PB1 port.

To display the value of the measured frequency, a 4-digit seven-segment LED indicator with a common anode is used. This solution reduces the number of conductors for connecting the indicator. In the absence of a display of this type, it is possible to use different types of seven-segment indicators, however, adaptation of the microcontroller software will be required.

The layout and pin assignment of the applied indicator is shown in Figure 2.


Fig.2. Location and pin assignment of the applied 4-digit LED indicator.

Pins E1…E4 are used to turn on the corresponding digits (E1 - to turn on the right least significant digit).

Each I/O line of the ATmega16 microcontroller can provide up to 40 mA output current, so we do not need to use transistors and the display control signals (E1…E4) are connected directly to the microcontroller port.

Connector for in-circuit programming of microcontroller J1. After assembling and programming the microcontroller, you will need to calibrate the device, set some variables (for example, to increase the brightness of the display, reduce the flickering of the display). In other words, you will need to update the microcontroller software, and therefore the indicated connector must be installed on the board.

Frequency measurement algorithm

We all know that frequency is the number of repeated pulses per unit of time. However, frequency measurement with digital instruments, such as a microcontroller, which has its limitations, requires some research to achieve the desired results.

The maximum frequency that can be processed by the counter of the ATmega16 microcontroller cannot exceed the clock frequency divided by 2.5. Let's denote the maximum frequency - Fmax. The clock frequency for our microcontroller is 8 MHz, so we can measure signals up to 3.2 MHz directly. To measure the frequency above this level, we use a 4-bit counter as the input frequency divider. Now we can measure frequencies up to 16 times Fmax, but here a limitation is imposed by the counter 74191 and the actual maximum measured frequency does not exceed 40 MHz.

The algorithm that has been developed measures the original (input) frequency (we denote F o) and the frequency obtained from the divider (we denote F d). As long as the condition that the frequency is less than Fmax condition is met:

F o = 16×F d ;

But as you get closer F o to F max, more and more pulses must be processed and the expression above becomes:

F o < 16 × F d ;

Therefore, the measurement limit of the microcontroller can be automatically detected.

The frequency meter starts to measure the original frequency (processing and displaying values ​​on the display), and as soon as it detects the approach to the maximum frequency F max(using the above method), selects the frequency after the divider to be measured.

The algorithm is summarized in the diagram (Fig. 3)

Fig.3 Algorithm of operation of the frequency meter on the microcontroller

microcontroller software

The source code of the microcontroller program is provided with detailed comments, but some points require a separate explanation:

  • The code is designed so that the measured value is displayed on the display in "kHz". For example, if you see the value "325.8" on the display, it means 325.8 kHz, the value "3983" means 3983 kHz (or 3.983 MHz).
  • Timer/counter 0 of the microcontroller is used to count the input pulses directly;
  • Timer/counter 1 on the microcontroller is used to count input pulses after dividing by 16;
  • Timer/Counter 2 is configured as a prescaled timer by 1024 (CPU frequency is divided by 1024). Used to invoke the algorithm for calculating and selecting a frequency every period T of the timer. In our project T = 1024× 256/F cpu .
  • The constant "factor", defined at the beginning of the program with the value "31.78581", must be calibrated by measuring the reference frequency. Calculated according to the expression:

factor = F cpu /(1024× 256)=8.E6/(1024×256)=30.51757

The Anti-Flickering function is rather complicated, but very effective, especially when measuring non-constant frequencies. This function completely saves the indicator from quickly switching between different values, while continuing to display the exact value, and quickly changes the reading if the measured frequency has really changed.

Note

The ATmega16 microcontroller comes factory set to operate from an internal 1MHz RC oscillator. It is necessary to set the CKSEL3..0 Fuse-bits to “0100” using the serial programmer, which corresponds to the inclusion of the internal 8 MHz RC oscillator.

APPS:

- The source code of the microcontroller program

Translation that you can say:



What else to read