Question: Lab 5 Task 4 : Frequency Counter Displayed on LCD Code Type in the following code and get the frequency counter functional. Use signal generator

Lab5
Task 4: Frequency Counter Displayed on LCD
Code
Type in the following code and get the frequency counter functional. Use signal generator ( sq wave, \(5\mathrm{~V},2.5\mathrm{v}\) offet) initially set to 1 kHz to confirm operation.
The code uses an interrupt on pin 2 to count pulses and displays the result on the LCD every second.
\#include
// Initialize the ICD object (RS, E, D4, D5, D6, D7)
// Variables for frequency counting
volatile unsigned long pulsecount \(=0\); // Counts pulses on pin 2
unsigned long frequency \(=\underline{\underline{p i}}//\) Calculated frequency
void setup ()(
// Set up the ICD
Lcsdubegind \((16,2) ; \quad //\) Initialize 16x2 ICD
led.amint("Freq: "); // Display static text
ladhastchas
// Set up the frequency input pin
ginMode(2, INPUT);
// Attach an interrupt to pin 2 for frequency counting
// Start serial conmunication (optional, for debugging)
Serial.begin (9600)
// Display update interval (1 second)
delay(1000);
\}
void \(\frac{1000()}{\text { I }}\)\{
// Disable interrupts to safely read pulse count
MeThaterpuputa();
frequency \(=\) pulseCount: //Copy pulse count to frequency variable
pulsecquat \(=\underline{\underline{0}}\)// Reset pulse count for next second
interrupts (): // Re-enable interrupts
// Display frequency on LCD
dschasetcursage (6,0); // Move cursor to print frequency
led.print (""); // Clear previous reading
lcd.drint(frequency): // Print the frequency in Hz
```
// Optional: print to Serial Monitor for debugging
Serial.wnkint("Frequency: "
Serial.print (frequency):
```
```
// Delay for 1 second before updating frequency again
delav(1000);
}
// Interrupt Service Routine to count pulses
void count{ulse(){
pulaeckunt++; // Increment pulse count on each interrupt
}
```
Explanation of the Code
1. Setup Function:
- The LCD is initialized and displays "Freq: Hz " as static text on the screen.
- An interrupt is attached to pin 2, triggering the countFulse() function on each rising edge, which increments the pulsecount variable.
2. Loop Function:
- Every second, the pulseCount (measured in pulses per second) is copied to frequency.
- The pulseCount is reset to 0 to start counting for the next interval.
- The frequency is displayed on the LCD and optionally printed to the Serial Monitor.
- The delav (1000) creates a 1-second interval between each frequency measurement.
3. countPulse() Interrupt Service Routine:
- This function increments the gulsecount variable each time a rising edge is detected, allowing accurate frequency counting.
Lab 5 Task 4 : Frequency Counter Displayed on LCD

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!