Construction portal - Home. Water heaters. Chimneys. Heating installation. Heaters. Equipment

Homemade frequency meter on ATTINY2313. Homemade frequency meter on ATTINY2313 Frequency measurement algorithm

In one of the previous articles devoted to the study of AVR microcontrollers, using an example, we looked at the use of a 16-bit timer/counter T1 and capture event interrupts. As a supplement to this material, I propose an improved version of the frequency meter. This project also uses a capture unit and additionally uses the clock input of an 8-bit timer.

The disadvantages of the old project were the small range of measured frequencies (~hundreds of kilohertz), which was associated with the method of measuring the signal period.

You probably remember that in the capture event interrupt, the counting register of the 16-bit timer was reset to zero, and the captured value, corresponding to the number of pulses of the microcontroller clock generator that fit into one period of the input signal, was stored in a variable. Calculations were performed based on this value.

When the frequency of the input signal increased, the microcontroller did not have time to process interrupts, missed them, and the frequency meter readings began to sharply diverge from reality.

In the new design, frequency calculation is performed over several periods of the input signal and without the constant use of the capture event interrupt. This reduces microcontroller overhead and allows you to measure significantly higher frequencies - ideally up to 1/2 Fcpu (microcontroller clock frequency).

So, let's move on to the description of the new frequency meter project.

Scheme

The input signal is supplied to the input of the capture circuit of timer T1 and the counting input of timer T0. In order for timer T0 to be clocked from an external signal, it must be configured accordingly.

Project structure

The project consists of 4 software modules.

bcd.c– contains a function for displaying binary numbers on the display

timer.c– contains a function for initializing timers T0 and T1, interrupt handlers, a function for capturing the values ​​of timer counting registers and program counters, and, finally, a frequency calculation function.

lcd_lib.c is a library for working with character display.

main.c- main program.

Frequency measurement method

The frequency of the input signal is measured using the time gate method. The essence of the method is to count the number of pulses of the measured and reference signals over a certain period of time.

To count the number of pulses of the measured signal, the counting input of a hardware timer is used. The microcontroller clock signal is used as a reference signal.

The time interval during which pulse counts are performed is measured using a hardware timer T1 capture circuit and a software delay.

The formula for calculating the frequency using the time gate method is:

Fx = Fo * (M/N),

where Fx is the frequency of the input signal, Fo is the frequency of the reference signal, M is the number of input signal pulses during the measurement time, N is the number of reference signal pulses during the measurement time.

Program algorithm

The project uses two timers - an 8-bit timer/counter T0 and a 16-bit T1. Timer T1 counts the number of microcontroller clock pulses (baseImp) that fit into a certain time interval, and timer T0 counts pulses of the measured signal (mesurImp).

The time interval during which pulse counts are performed is about one second. Since during this time both timers manage to overflow many times, the program uses additional program counters (timer0, timer1). These are 16-bit variables that are incremented in the T0 and T1 timer interrupts.

A general view of the timer operation cyclogram is shown in the figure below.

The program algorithm is as follows.

1. Timers and display are initializing

2. The microcontroller waits for the T1 timer capture circuit flag to be set, or, in simple terms, catches the rising edge of the measured signal.

3. After waiting for the flag to be set (Capture1 moment in the figure), the microcontroller saves the values ​​of the counting registers of timers T0 and T1, as well as the values ​​of program counters.

4. A software delay of one second is caused. Both timers continue to run.

5. At the end of the delay, the microcontroller waits for the capture circuit flag to be set

6. After waiting for the flag to be set (Capture2 moment in the figure), the microcontroller saves the values ​​of the counting registers T0 and T1 and the values ​​of the program counters.

7. The frequency value is calculated and displayed

8. Return to step number 2.

A few words about frequency calculation.

To calculate the number of reference signal pulses, use the following formula.

//number of program counter overflows

saveTimer12 = saveTimer12 – saveTimer11;

//number of reference signal pulses

baseImp = (icr12 + ( unsigned long)saveTimer12*65536) – icr11;

where saveTimer12, saveTimer11 is the value of the program counter timer1 at moments Capture2, Capture1, respectively; icr12, icr12 - the value of the counting register TCNT1 of timer T1 at the moments Capture2, Capture1, respectively; 65536 - T1 meter capacity

The frequency meter presented in this article allows you to measure frequencies from 10 Hz to 60 MHz with an accuracy of 10 Hz. This allows this device to be used for a wide range of applications, for example, measuring the frequency of a master oscillator, radio receiver and transmitter, function generator, quartz resonator, etc. The frequency meter provides good parameters and has good input sensitivity, thanks to the presence of an amplifier and a TTL converter. This allows you to measure the frequency of quartz resonators. If an additional frequency divider is used, the maximum measurement frequency can reach 1 GHz or higher.

The idea of ​​a frequency meter on a PIC microcontroller came to me after reading the AN592 apnot from Microchip, which describes frequency measurement on a PIC and presents a program. I developed a circuit and wrote a program in which I improved the measurement accuracy, and the frequency value is displayed on the LCD screen. The result is a fairly simple and effective frequency meter.

The frequency meter circuit is quite simple; most functions are performed by a microcontroller. The only thing is that the microcontroller needs an amplification stage to increase the input voltage from 200-300 mV to 3 V. A transistor connected in a common-emitter circuit provides a pseudo-TTL signal fed to the microcontroller input.

Some kind of “fast” transistor is needed as a transistor; I used BFR91 (domestic analogue of KT3198V).

The voltage Vke is set at 1.8-2.2 volts by resistor R3* in the circuit. Mine is 10 kOhm, but adjustments may be necessary. The collector voltage of the transistor is applied to the counter/timer input of the PIC microcontroller, through a series resistance of 470 ohms. To turn off the measurement, the built-in pull-down resistors are used in the PIC.

The PIC implements a 32-bit counter, partly in hardware, partly in software. The counting begins after the built-in pull-down resistors of the microcontroller are turned off, the duration is exactly 0.4 seconds. After this time, the PIC divides the resulting number by 4, and then adds or subtracts the appropriate intermediate frequency to obtain the actual frequency. The resulting frequency is converted for display on the display.

Calibration

In order for the frequency meter to work correctly, it must be calibrated. The easiest way to do this is to connect a pulse source with a precisely known frequency in advance and rotate the tuning capacitor to set the required readings.

If this method is not suitable, then you can use “rough calibration”. To do this, turn off the power of the device, and connect pin 10 of the microcontroller to GND. Then, turn on the power. The MK will measure and display the internal frequency. If you cannot adjust the displayed frequency (by adjusting the 33 pF capacitor), then briefly connect pin 12 or 13 of the MK to GND. It is possible that this will need to be done several times, because... the program checks these pins only once per measurement (0.4 sec). After calibration, disconnect the 10th leg of the microcontroller from GND without turning off the power to the device in order to save the data in the non-volatile memory of the MK.

List of radioelements

Designation Type Denomination Quantity NoteShopMy notepad
MK PIC 8-bit

PIC18F84J11

1 To notepad
Linear regulator

LM7805

1 To notepad
TransistorBFR911 To notepad
Rectifier diode

1N4007

1 To notepad
Capacitor1 µF1 To notepad
10 µF1 To notepad
Electrolytic capacitor1 µF1 To notepad
Capacitor0.1 µF1 To notepad
Capacitor33 pF1 To notepad
Trimmer capacitor33 pF1 To notepad
Resistor

470 Ohm

2 To notepad
Resistor

10 kOhm

1 Selection To notepad
Resistor

10 kOhm

1 To notepad
Variable resistor10 kOhm2

The peculiarity of the first frequency meter circuit on the AVR microcontroller is that it works together with the computer and is connected to the motherboard via the IRDA connector. The structure receives power from the same connector. The second frequency meter circuit is based on the Attiny2313 microcontroller and is capable of measuring frequencies up to 10 MHz. The third considered frequency meter design is based on the legendary Arduino board, which is also based on the AVR microcontroller.

The frequency counter circuit consists of an Attiny2313 microprocessor and a 74AC161 binary counter. The incoming signal for amplification goes to transistor VT1, then from its collector output it goes to input “C” of the binary counter. Control over the operation of the counter is assigned to the Attiny2313 MK, which resets, stops or starts the count by sending a control signal to the tenth output.


By briefly applying a logical zero to the reset input of the binary counter, the MK resets it to zero, and after that, sends a logical one level to the EP input, starting its operation. Then, it counts the pulses from the output of the high-order digit of the counter for half a second.

Frequency meter on the AVR microcontroller. Data signals to the computer come from the PD6 port of the Attiny2313. The PB1 port line is used for synchronization signals coming from the computer.

At the initial moment of time, the MK generates a starting pulse with a duration of about 1.6 μs, after which there is a pause. The program periodically accesses port 2F8H and, when registering a byte, initiates the transmission of clock pulses. These clock pulses will be sent when the number zero is sent to the infrared port of the computer. Pulse composition: The first bit is the start bit and the 8th bit is the number zero.

When a logic one level is detected, the microcontroller begins transmission by sending the 1st start pulse, setting a logic one on the data line and waits for the clock line to fall so that data pulses can be sent. If the data bit is zero, then "1" is set.

Since the transmission and reception speeds are the same, this allows you to gain independence from the set speed of the computer's IR port.

Fuses for the Ponyprog program and the firmware itself are available at the green link just above.

In this simple frequency counter project, the Arduino controller reads the voltage, then calculates its frequency and sends the data via USB UART to the computer on which you need to install the data reading and visualization program, application and sketch in the downloadable archive.

The Arduino board generates an accurate one-second time base for the counter by cascading two timers, timer0 and timer2. The connection between digital inputs 3 and 4 connects the output of timer 2 (250 Hz) to the input of timer 0. The program code waits for the output of timer 0 to become positive and starts counting the frequency of the input signal with timer 1. Timer1 is a 16-bit timer, it overflows When the value 2 16 is reached, after this, the value of the overflow register overF changes. At the end of the first second, a 16-bit register is written. The Arduino then sends 6 bytes of data to the PC. The connection diagram to Arduino is simple, and you can see it in the photo below.

First, the Arduino must be connected to the computer, and only then run the application in Visual Basc 6. The application looks for a Com port, sending bytes and waits for them to be received back. It takes a couple of seconds. The application must be disabled when you flash the board via Arduino IDE. The frequency input of the Arduino board is TTL signal levels, if the signal is weak, you need to add an amplifier.

In this article we will look at how to build a small, cheap and simple frequency counter that can measure frequencies up to 40 MHz with an error of less than 1%. This accuracy is quite sufficient 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, based on the ATmega16 microcontroller from Atmel, the clock source is an 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 applying it to the microcontroller input. As you can see, only the counter output Q3 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 port PB1.

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 wires for connecting the indicator. In the absence of a display of this type, it is possible to use various types of seven-segment indicators, but adaptation of the microcontroller software will be required.

The layout and purpose of the used indicator pins is shown in Figure 2.


Fig.2. Location and purpose of the pins of the applied 4-digit LED indicator.

Pins E1...E4 are used to turn on the corresponding bits (E1 – to turn on the right low-order bit).

Each I/O line of the ATmega16 microcontroller can provide an output current of up to 40mA, 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, adjust some variables (for example, to increase the brightness of the display, reduce flickering of the display). In other words, you will need to update the microcontroller software, and therefore the specified 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, measuring frequency using digital instruments such as a microcontroller, which has its limitations, requires some research to achieve the required results.

The maximum frequency that can be processed by the ATmega16 microcontroller counter 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, therefore we can directly measure signals with a frequency of up to 3.2 MHz. To measure frequencies above this level, we use a 4-bit counter as an input frequency divider. We can now measure frequencies 16 times higher Fmax, but here a limitation is imposed by the 74191 counter and the actual maximum measured frequency does not exceed 40 MHz.

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

F o = 16 × F d ;

But as we 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 measuring limit of the microcontroller can be automatically detected.

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

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

Fig.3 Algorithm for operating a frequency meter on a microcontroller

Microcontroller Software

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

  • The code is designed so that the measured value is displayed on the indicator in “kHz”. For example, if you see the value “325.8” on the display, this means 325.8 kHz, the value “3983” means 3983 kHz (or 3.983 MHz).
  • Timer/Counter 0 of the microcontroller is used to count input pulses directly;
  • Timer/counter 1 of the microcontroller is used to count the input pulses after the divider by 16;
  • Timer/Counter 2 is configured as a timer with a prescaler by 1024 (CPU frequency divided by 1024). Used to call the frequency calculation and selection algorithm every T timer period. 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 by the expression:

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

The Anti-Flickering function is quite complex, but very effective, especially when measuring non-constant frequencies. This function completely eliminates the need for the indicator to quickly switch between different values, continuing to display the exact value, and quickly changes the reading if the measured frequency does change.

Note

The ATmega16 microcontroller comes with factory settings configured to operate from an internal 1 MHz RC oscillator. It is necessary to use a serial programmer to set the Fuse bits CKSEL3..0 to the value “0100”, which corresponds to turning on the internal 8 MHz RC oscillator.

APPLICATIONS:

- Microcontroller program source code

Translation: Vadim

This DIY ATTINY2313 frequency meter is designed to measure frequencies ranging from about 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) range.

Technical characteristics of the frequency meter:

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

The circuit worked perfectly in the range from 3.8 MHz to 162 MHz. The basis of the circuit is 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 achieved by using a frequency divider (prescaler). The circuit uses the LB3500 prescaler, which allows measurements 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 the 74LS293 binary counter (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. Transistor Tl with elements C7, R2, R3 provides high input impedance.

The input signal after separation goes to the input of the LB3500 chip. At the output of IC4 9, the signal is 8 times lower in frequency than at the input. Unfortunately, the LB3500's output is not compatible with TTL levels. To eliminate this drawback, transistor T2 was added to the circuit, which is intended for matching. The PRI potentiometer ensures precise matching.

Related publications