Arduino World
Published © GPL3+

Arduino Toolboxes: PWM Frequency Meter

A simple but beautifully accurate PWM frequency meter.

IntermediateShowcase (no instructions)8,488
 Arduino Toolboxes: PWM Frequency Meter

Things used in this project

Story

Read more

Code

fm2

C/C++
#include <LiquidCrystal.h>
#include "TimerOne.h"
#include <Math.h>
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//time defines
unsigned long T1 = 0;
unsigned long T2 = 0;
unsigned long T3 = 0;
unsigned long Told = 1;
// define a bool value to know when the cycle is done and need to calculate
bool Cycle = false;
int reciveddata = 128;
void setup()
{
  //initiate timer1 with 1 sec delay
  Timer1.initialize(1000000);
  //add the function that will be run every 1 sec
  Timer1.attachInterrupt(thedisplayfunction);
  // enable the interrupt in rising endge
  attachInterrupt(0, isr, RISING);
  // Initialize LCD
  lcdcharinitiate();
  lcd.begin(16 , 2);
  // generate pwm signal for testing
  analogWrite(11, reciveddata);
}
void loop()
{
  //we dont need to do anything here
  //i dont like using loop and i avoid it when i can
}
void isr()
{
  // get the current time
  unsigned long now = micros();
  if (Cycle)
  {
    // if the cycle is done the get the total time
    T2 = now;
    Cycle = !Cycle;
    T3 = T2 - T1;
  }
  else
  {
    // if the cycle is not done then calculate the start of the cycle time
    T1 = now;
    Cycle = !Cycle;
  }
}
void thedisplayfunction()
{
  if (T3 != Told)
  {
    lcd.clear();
    lcdDot(10);
    lcd.setCursor(14, 1);
    lcd.print("Hz");
    double theRealValue = 1 / (((double)T3) / 1000000);
    if (isinf(theRealValue))
    {
      theRealValue = 0;
    }
    String theStringValue = String(theRealValue);
    int theStringLenght = theStringValue.length();
    int p = 13;
    for (int i = theStringLenght ; i >= 0 ; i--)
    {
      int thenum = (int)theStringValue[i] - 48;
      numberprinter(thenum, p);
      p--;
    }
    Told = T3;
  }
}
//find the needed characer and added it to the needed position
void numberprinter(int num , int pos)
{
  if (num == 0)
  {
    lcdnumber0(pos);
  }
  if (num == 1)
  {
    lcdnumber1(pos);
  }
  if (num == 2)
  {
    lcdnumber2(pos);
  }
  if (num == 3)
  {
    lcdnumber3(pos);
  }
  if (num == 4)
  {
    lcdnumber4(pos);
  }
  if (num == 5)
  {
    lcdnumber5(pos);
  }
  if (num == 6)
  {
    lcdnumber6(pos);
  }
  if (num == 7)
  {
    lcdnumber7(pos);
  }
  if (num == 8)
  {
    lcdnumber8(pos);
  }
  if (num == 9)
  {
    lcdnumber9(pos);
  }
}
//initiate the lcd custom charcters
void lcdcharinitiate()
{
  //number 0 charaters 2x2 decimals
  byte C0[8] = {0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02};
  byte C1[8] = {0x0E, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x0E};
  byte C2[8] = {0x0E, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0E};
  byte C3[8] = {0x0E, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0E};
  byte C5[8] = {0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0E};
  byte C4[8] = {0x0E, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A};
  byte C6[8] = {0x0E, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02};
  byte C7[8] = {0x18, 0x18, 0x18, 0x18, 0x18, 0x1E, 0x1F, 0x1F};

  lcd.createChar(0 , C0);
  lcd.createChar(1 , C1);
  lcd.createChar(2 , C2);
  lcd.createChar(3 , C3);
  lcd.createChar(4 , C4);
  lcd.createChar(5 , C5);
  lcd.createChar(6 , C6);
  lcd.createChar(7 , C7);
  lcd.begin(16 , 2);
}
//lcd draw character functions
void lcdnumber0(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(4));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(5));
}
void lcdnumber1(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(0));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(0));
}
void lcdnumber2(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(1));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(2));
}
void lcdnumber3(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(1));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(1));
}
void lcdnumber4(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(5));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(6));
}
void lcdnumber5(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(2));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(1));
}
void lcdnumber6(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(2));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(3));
}
void lcdnumber7(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(6));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(0));
}
void lcdnumber8(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(3));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(3));
}
void lcdnumber9(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(3));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(1));
}

void lcdDot(int startposition)
{
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(165));
}

Credits

Arduino World

Arduino World

0 projects • 96 followers
15 Years Embedded System Programming and Development Experience

Comments