Frequency Meter

 

History

Here is my new Frequency meter who was done with a LCD's cellular phone!!!  This is a simple project. The Frequency is passing  through an op-amp to convert it in a square wave. The ouput of the op-amp is feeding the 3*8 bits counter (24 bits) who can accumulate at a maximum of 16777216 count.  The maximum frequency you can measure without changing the time base of one second is 16.777216 Mhz.  The ATMega8 have 3 functions: enable the counter gate at each second, read his value and display it on the LCD.

 

Features

puce Small box
puce Frequency and period display
puce Auto Scale
puce LCD back light

 

Pictures

Click to enlarge

Outside

Inside

 

Sources codes & Schematics

-Schematic in PDF format

Use the Nokia LCD Library to make this code work


//**************************************************************************
// Frequancy Meter
// Version 1.0 Mars 2003
//
// 1.0 -> -First Release 
//      
// 
// Sylvain Bissonnette
//**************************************************************************


/*********************************************************/
/*                   I N C L U D E                     */
/*********************************************************/
#include "macros.h"
#include "iom8v.h"
#include <stdio.h>
#include <stdlib.h>
#include <STRING.H>
#include "NokiaLCD.h"

/*********************************************************/
/*                   E X T E R N                       */
/*********************************************************/
extern char UpdateLcd;

/*********************************************************/
/*                   P R O T O T Y P E                    */
/*********************************************************/
void Delay(int del);
void timer1_compa_isr(void);
void timer0_ovf_isr(void);
void GetCount(void);
void PrintFreq(void);
void Presentation(void);
void main(void);

/*********************************************************/
/*                   D E F I N E                       */
/*********************************************************/
#define  TRUE       1
#define  FALSE     0

#define   CLR     0x20
#define   OEH     0x10
#define  OEM      0x08
#define   OEL     0x04

/*********************************************************/
/*           G L O B A L    V A R I A B L E S          */
/*********************************************************/
unsigned long Count;
unsigned long Time;

/*********************************************************/
void Delay(int del)
{
int i,j;

for (j=0;j<del;j++)
   {
   for (i=0;i<320;i++);
   }
}

/*********************************************************/
#pragma interrupt_handler timer1_compa_isr:7
void timer1_compa_isr(void)
{
if ((PINB & 0x02) == 0x02)
   {
   GetCount();
   PrintFreq();
   }
}

/*********************************************************/
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
if (UpdateLcd == TRUE) LcdUpdate();
}

/*********************************************************/
void GetCount(void)
{
Count = 0;

PORTD &= ~OEH;
Delay(1);
Count = (unsigned long)((PINC & 0x3f) + (PIND & 0xc0));
PORTD |= OEH;

Count = Count << 8;

PORTD &= ~OEM;
Delay(1);
Count += (unsigned long)((PINC & 0x3f) + (PIND & 0xc0));
PORTD |= OEM;

Count = Count << 8;

PORTD &= ~OEL;
Delay(1);
Count += (unsigned long)((PINC & 0x3f) + (PIND & 0xc0));
PORTD |= OEL;

PORTD &= ~CLR;
Delay(1);
PORTD |= CLR;
}


/*********************************************************/
void PrintFreq(void)
{
char Str[7];
char Tmp[7];
unsigned char Len;

LcdGotoXY(1,4);
LcdStr(2,"      ");
LcdGotoXY(2,4);
LcdStr(2,"     ");

ltoa(&Str[0],Count,10);
Len = strlen(&Str[0]);
Str[Len] = 0x00;

if (Count < 1000)
   {
   LcdGotoXY(12-(2*Len),4);
   LcdStr(2,&Str[0]);
   LcdGotoXY(13,3);
   LcdStr(1," " );

   Time = 100000 / Count;
   ltoa(&Str[0],Time,10);

   if (Count <= 1000)
        {
        Tmp[0] = Str[0];
        Tmp[1] = '.';
     Tmp[2] = Str[1];
        Tmp[3] = Str[2];
     }
   if (Count <= 100)
        {
        Tmp[0] = Str[0];
        Tmp[1] = Str[1];
        Tmp[2] = '.';
        Tmp[3] = Str[2];
     }
   if (Count <= 10)
        {
        Tmp[0] = Str[0];
        Tmp[1] = Str[1];
        Tmp[2] = Str[2];
        Tmp[3] = ' ';
     }
   if (Count == 0)
        {
        Tmp[0] = ' ';
        Tmp[1] = ' ';
        Tmp[2] = '0';
        Tmp[3] = ' ';
     }
   Tmp[4] = 'm';
   Tmp[5] = 's';
   Tmp[6] = 0x00;
   LcdGotoXY(9,6);
   LcdStr(1,&Tmp[0]);
   return;
   }

if (Count < 10000)
   {
   Tmp[0] = Str[0];
   Tmp[1] = '.';
   Tmp[2] = Str[1];
   Tmp[3] = Str[2];
   Tmp[4] = Str[3];
   Tmp[5] = 0x00;
   LcdGotoXY(2,4);
   LcdStr(2,&Tmp[0]);
   LcdGotoXY(13,3);
   LcdStr(1,"K" );

   Time = 1000000 / Count;
   ltoa(&Str[0],Time,10);
   Tmp[0] = Str[0];
   Tmp[1] = Str[1];
   Tmp[2] = Str[2];
   Tmp[3] = ' ';
   Tmp[4] = 'u';
   Tmp[5] = 's';
   Tmp[6] = 0x00;
   LcdGotoXY(9,6);
   LcdStr(1,&Tmp[0]);
   return;
   }

if (Count < 100000)
   {
   Tmp[0] = Str[0];
   Tmp[1] = Str[1];
   Tmp[2] = '.';
   Tmp[3] = Str[2];
   Tmp[4] = Str[3];
   Tmp[5] = Str[4];
   Tmp[6] = 0x00;
   LcdGotoXY(1,4);
   LcdStr(2,&Tmp[0]);
   LcdGotoXY(13,3);
   LcdStr(1,"K" );

   Time = 10000000 / Count;
   ltoa(&Str[0],Time,10);
   Tmp[0] = Str[0];
   Tmp[1] = Str[1];
   Tmp[2] = '.';
   Tmp[3] = Str[2];
   Tmp[4] = 'u';
   Tmp[5] = 's';
   Tmp[6] = 0x00;
   LcdGotoXY(9,6);
   LcdStr(1,&Tmp[0]);
   return;
   }

if (Count < 1000000)
   {
   Tmp[0] = Str[0];
   Tmp[1] = Str[1];
   Tmp[2] = Str[2];
   Tmp[3] = '.';
   Tmp[4] = Str[3];
   Tmp[5] = Str[4];
   Tmp[6] = 0x00;
   LcdGotoXY(1,4);
   LcdStr(2,&Tmp[0]);
   LcdGotoXY(13,3);
   LcdStr(1,"K" );

   Time = 100000000 / Count;
   ltoa(&Str[0],Time,10);
   Tmp[0] = Str[0];
   Tmp[1] = '.';
   Tmp[2] = Str[1];
   Tmp[3] = Str[2];
   Tmp[4] = 'u';
   Tmp[5] = 's';
   Tmp[6] = 0x00;
   LcdGotoXY(9,6);
   LcdStr(1,&Tmp[0]);
   return;
   }

if (Count < 10000000)
   {
   Tmp[0] = Str[0];
   Tmp[1] = '.';
   Tmp[2] = Str[1];
   Tmp[3] = Str[2];
   Tmp[4] = Str[3];
   Tmp[5] = Str[4];
   Tmp[6] = 0x00;
   LcdGotoXY(1,4);
   LcdStr(2,&Tmp[0]);
   LcdGotoXY(13,3);
   LcdStr(1,"M" );

   Time = 1000000000 / Count;
   ltoa(&Str[0],Time,10);
   Tmp[0] = Str[0];
   Tmp[1] = Str[1];
   Tmp[2] = Str[2];
   Tmp[3] = ' ';
   Tmp[4] = 'n';
   Tmp[5] = 's';
   Tmp[6] = 0x00;
   LcdGotoXY(9,6);
   LcdStr(1,&Tmp[0]);

   return;
   }
}

/*********************************************************/
void Presentation(void)
{
unsigned char i;

LcdClear();
LcdUpdate();

LcdGotoXY(3,1);
LcdStr(1,"Freq Meter" );
for (i=0;i<9;i++) LcdLine(0,i,83,i,PIXEL_XOR);
LcdGotoXY(5,3);
LcdStr(1,"Sylvain");
LcdGotoXY(3,4);
LcdStr(1,"Bissonnette");
LcdGotoXY(4,6);
LcdStr(1,"Ver : 1.0");
LcdUpdate();
Delay(10000);
LcdClear();
LcdUpdate();
}

/*********************************************************/
void main(void)
{
unsigned char i;

LcdInit();
LcdClear();
LcdUpdate();

Presentation();

LcdGotoXY(3,1);
LcdStr(1,"Freq Meter" );
for (i=0;i<9;i++) LcdLine(0,i,83,i,PIXEL_XOR);

LcdGotoXY(12,4);
LcdStr(1," hz" );
LcdGotoXY(2,4);
LcdStr(2,"    0");
LcdGotoXY(1,6);
LcdStr(1,"Period: " );

TCCR0 = 0x05; //start timer0 / 1024

DDRB = 0x3f;
DDRC = 0x00;
DDRD = 0x3e;

PORTD |= OEH;
PORTD |= OEM;
PORTD |= OEL;
PORTD |= CLR;

//TIMER1 initialisation - prescale:256
// WGM: 4) CTC, TOP=OCRnA
// desired value: 2000mSec
// actual value: 2000.000mSec (0.0%)
TCCR1B = 0x00;
OCR1AH = 0x7A;
OCR1AL = 0x11;
TCCR1A = 0x40;
TCCR1B = 0x0C;

TIMSK = 0x11; //timer interrupt sources
SEI(); //re-enable interrupts
while(1)
      {
      Delay(10);
      }
}