Frequency Measurement and Pulse Width Measurement using Microcontroller (AT89C51)

Frequency Measurement Pulse Width Measurement using Microcontroller and Circuit designers and developers need different measuring equipment, such as digital multimeter, CRO, DSO, frequency counter, logic analyzer, etc. to test their circuits. When designing any PWM oscillator or generator or have generated any waveform, it is necessary to measure either the frequency or the time period. Especially in the case of the PWM generator, it is necessary to measure the frequency, the start time and the deactivation, and the most necessary is the duty cycle. And during testing and troubleshooting it has to measure all these parameters again.


Now if you measure these parameters using CRO, then

  • 1st You must calculate the number of horizontal divisions for the ON period and the OFF period
  • Then multiply it by the time / division factor. They’ll get Ton and Toff
  • Then, after making some calculations, they will find out the frequency and cycle of the debt
  • And this has to be repeated every time they change their design


Therefore, this is a time-consuming and time-consuming process. CRO is used if DSO (digital storage oscilloscope) is used, then all readings are available directly. But they have to pay much more cost (at least twice the CRO) for this. Also, CROs and DSOs are only available in well-equipped laboratories, as they are not low-cost tools, such as digital multimeters. So, fans may not have these tools in their personal home labs.

Here we have designed such a circuit that will give you direct readings of four parameters (1) frequency (2) start time (3) Stop time and (4) duty cycle in one second with an accuracy of almost 99% . The circuit is very simple, easy to build with fewer components, a micro-controller (AT89C51), an LCD and additional components such as crystal, capacitors, resistors, transistors etc. some of the characteristics of the circuits are

  1. Measures the frequency from 50 Hz to 65 KHz (display in Hz)
  2. Measures ON and OFF time periods from 25 µs to 65 ms (display in µs)
  3. Measures and displays the tax cycle as a percentage
  4. Displays all four parameters on the 20 × 4 LCD panel
  5. Repeat the operating mode to continuously measure all four parameters


So let’s see how this circuit is built

Hardware section:

This figure illustrates the hardware connections for the frequency and pulse width measurement circuit. The major components are the AT89C51 microcontroller and the 20 × 4 LCD. The D0-D7 data pins of the LCD are connected to the P2 port of the AT89C51. The RS and E control pins are connected to the P1 ports P1.0 and P1.1. The RW pin is connected to the ground. A 1 KΩ vessel is connected to the VEE pin, as shown to vary the LCD brightness. A 12 MHz crystal along with two 33 pf capacitors (C2 and C3) are connected to the XTAL1 and XTAL2 pins of the microcontroller to provide the required internal clock. A 1 µF capacitor in parallel with a push button (reset) is connected between the Vcc and the reset pin (no. 9) of the microcontroller to form the “power on reset” circuit. The Reset button is used to manually reset the micro controller.

A RED indicator is connected to pin P1.2 for indication. The SPDT switch is connected to pin P1.7 as shown to select repeated or non-repeat operating modes. The signal is applied to pins P3.3 and P3.4 via transistor Q1 (type 2N2222) which is configured in switch mode. This small circuit with a transistor and two resistors will turn any input signal into a square or rectangle shape and will limit the signal amplitude to Vcc (ie 5 V max)

Frequency Measurement and Pulse Width Measurement using Microcontroller (AT89C51)

Working and operation of Frequency Measurement and Pulse Width Measurement using Microcontroller (AT89C51)

The PWM signal (or any other waveform) is applied to the base of the transistor. Because the transistor functions as an inverter, the inverted wave is applied to pins P3.3 and P3.4. The negative edge of the P3.3 pin will generate external interruption. This interruption will begin to count pulses on pin P3.4. The pulses are metered for exactly 1 second. This gives us directly the frequency value as the number of pulses / second. After that, the internal stopwatch will measure the time (in micro sec) for which the pulse amplitude remains high which gives Ton value and the same for Toff value. At the last duty cycle it is calculated using the equation

Duty cycle = [Ton / (Ton + Toff)] × 100%

The LED indicates the counting and calculation process. Finally, all four values ​​are displayed on the LCD screen. Now, if the SPDT (or slide) switch is not in the repeat position, then the measurement process is completed, but if it is in the repeat position, the same process is repeated above and it continuously measures all four parameters. This complete operation is based on the program loaded in the microcontroller. So now we understand the software.

Software section:


The program is written in embedded C language and compiled by the KEIL compiler (IDE). The complete program is divided into 9 different functions with one main function. Of these nine functions, five functions are intended for LCD manipulation, one for measuring start time and time off, one for measuring duty cycle, one stop function to measure frequency, and the last is the delay function.

LCD handling functions:

  1. Writecmd function sends the command byte to the LCD. It takes an byte of argument and sends it to P2
  2. Writedata function sends the data byte to be displayed on the LCD screen. It also takes an byte of argument and sends it to P2
  3. Writestr function writes the whole string (message) on the LCD screen. A pointer is needed as an argument that indicates the address of the first character of the string. then by pointer sends the whole character one by one to P2
  4. Busy function generate delay each time before any byte is sent to the LCD
  5. Display function has two arguments. It converts HEX numbers into decimals and then decimals into ASCIIs so it can be displayed on the screen.

Measurement functions: –

  1. The periodic function measures the ON and OFF time. It measures the real time for which the pulse amplitude remains high and remains low using the stopwatch 1. Wait for a cycle first (start time and deactivation time). Then start the stopwatch and measure the time in micro seconds until the pulse is high. This will measure the turn off time, as we measure the inverted pulse width. Then again he waits for a cycle. Again, the stopwatch will run until the pulse is low. This will measure the ON time. Both ON and OFF time values ​​are stored in four different variables.
  2. The frequency function is a stop function. It measures the number of pulses per second. It starts as soon as a negative edge reaches the external interrupt pin P3.3. First delete the number in TH0 and TL0 and then start counting external pulses that arrive on the P3.4 pin. The meter stops after exactly 1 second. The number in TH0 and TL0 is stored in 2 variables that give directly the value no. of pulses / second.
  3. The Dutycycle function calculates the wave debt cycle. Get the Ton and Toff values ​​from the period function and calculate the debt cycle using the formula. It also decreases the Ton and Toff values ​​if they do not have a 16 bit range (full variable).

Duty cycle = [Ton / (Ton + Toff)] × 100%

The last one is the delay function that generates an exact delay of 1 second using stopwatch 1. It generates 50 ms delay using stopwatch and rotates it in a loop 20 times. So finally we get 50 × 20 = 1000 ms = 1 sec.


The main function performs the following tasks

  • First, it initializes ports and pins as input or output.
  • Next, it initializes the LCD screen and displays the message “Measuring pulse frequency and width”.
  • Then, to calculate the frequency, initialize stopwatch 0 as a counter to count external pulses and stopwatch 1 as a timer to generate a delay of 1 sec.
  • Then it will wait for an external outage. The interruption is generated by the negative edge of the input signal. As the frequency is calculated, it is displayed as 5-digit Hz.
  • After that, the period function will measure the ON and OFF time, and these will be displayed on the LCD.
  • After that, the dutycycle function will calculate the service cycle and is also displayed
  • Finally, it resets the flag and checks the status of pin P1.7. If 0, repeat the same operation and if its program 1 goes into a continuous loop

Here is the complete code C with the necessary comments

You can find more projects here

http://projugaadu.com/category/projects/

Leave a Comment