FMesure.c

The program shows how to measure the frequency on pin RB0 and write it to a LCD-display (by Gerhard Burger).



//----------------------------------------------------------------------------------------------------------------------
// This program is an example how to use the C2C-Pic-Compiler of Pavel Baranov
//
// Author: Geri Burger
// Version 1.0
// Compiler version: 
// Date: 18. May 2003
// 
// Required hardware: any PIC16F84-board (4MHz) and a LCD-display (see attached picture)
// Required files:   
//                  - this file                    FMeasure.c 
//                  - a LCD-Library                e.g. Lcd6Pin.c
//                  
// Further files:   - Schematics                   FMeasure.gif
//
// Description:
// The program shows how to measure the freuqency on pin RB0 and write it to a LCD-display.
// The variable AnzRB0int holds the sum of interrupts occured on Pin RB0 between a specifc time-window of 1 Second.
// The time-window is generated by Timer0. The prescaler divides the Timer Clock from CPU (= 4 Mhz /4 = 1MHz) through
// 64. 1000000/64 = 15625Hz. This freqency will further be divided by the software throug 125. (1000000 / 64 / 125 =125Hz)
// 125Hz coorespond to 8ms (T = 1/f  = 1/125). After a further division by 125 the time-window has a duration of = 1 Second.
// ==> AnzRB0Int directly corresponds to the measured frequency.
//
// Comments to accuracy: 
// Input frequency   0.........10 KHz    +/- 1Hz  (relative failure 0.1 %)
//                   >10KHz....<100 KHz  +/- 10Hz (relative failure 1 %)
// 
// Possible pin configuration (see also attached schematics)
// RB1       Lcd RS     
// RB2       Lcd Sel             
// RB3..RB6  LcdData4..LcdData7 
// RB0       frequency-input
// 
// If the Hardware for 3-pin-control of LCD is available, the program can also be run with the attached 3-Pin-Library
//
// Possible application:
// Measurement of the rotation speed of a spindle using a hall sensor. n = Frequency * 60 [rotations/minute]
//-----------------------------------------------------------------------------------------------------------------------

#include "Lcd6Pin.c"                          // include an LCD-library


#define INTF            0x02;                 // RB0-Interruptflag
#define T0IF            0x04;                 // Timer Interruptflag

unsigned int TMR0Overflows;
unsigned int AnzRB0Int;                       // variable holds the count of Interrupts occured on pin RB0
unsigned int Frequency_old;
unsigned int Frequency_new;


void interrupt( void )
{


  if (INTCON & T0IF != 0)                      // check if timer overflow
  {
    TMR0=130;                                  // initialize timer for new overflow (count from 130 to 255 = 125)    
    TMR0Overflows++;
    if (TMR0Overflows > 124)                   // 125-times * 8ms = 1 Second
    {
      TMR0Overflows = 0;
      Frequency_new = AnzRB0Int ;
      AnzRB0Int = 0;
    }
    clear_bit(INTCON, 2 );                     //clear TMR0-Interruptflag
  }

  if (INTCON & INTF !=0)                       // Interrupt from sensor on RB0
  {
    AnzRB0Int++;                               // increment count of interrupts 
    clear_bit(INTCON, 1);                      // clear RB0-Interruptflag
  }

}


void main(void)
{
  disable_interrupt( GIE );

  set_bit( STATUS, RP0 );
    OPTION_REG = 5;                           // set prescaler to 1:64
    set_bit (TRISB,0);                        // set RB0 to input
  clear_bit (STATUS, RP0 );                   // switch to Bank 0

  enable_interrupt(T0IE);                     // enable TMR0-Interrupt-bit
  enable_interrupt(INTE);                     // enable RB0-Interrupt-bit
  disable_interrupt(RBIE);                    // disable RB4-RB7-Interrupt
  enable_interrupt(GIE);                      // enable all interrupts

  TMR0Overflows = 0;
  Frequency_old=0;
  LcdSetup();                                 // setup LCD-Display
  delay_ms(100);                              // wait a little bit until Display is initialized

  LcdLine1();
  LcdClearLine1();
  LcdWrite("Frequency:");                      // Write the word Frequncy

  while (1==1)
  {
    if (Frequency_new != Frequency_old)       // check if measured frequency has been changed
    {
      LcdLine2();
      LcdClearLine2();
      LcdWriteInt(Frequency_new);             // write the frequency to the LCD-display
      LcdWrite("Hz ");
      Frequency_old=Frequency_new;
    }
    clear_wdt();
  }
}



http://www.sourceboost.com/home.html

Copyright © 2002-2006 SourceBoost Technologies