# Mini frequenzimetro: dove sbaglio?

**URL:** <https://forum.arduino.cc/t/mini-frequenzimetro-dove-sbaglio/927582>\
**Category:** Software\
**Created:** [November 21, 2021, 11:19pm UTC](https://forum.arduino.cc/t/mini-frequenzimetro-dove-sbaglio/927582 "2021-11-21T23:19:39Z")\
**Posts on this page:** 1\
**Showing post:** 43

<div class="post-metadata">

**Author:** ![Datman](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/datman/32/219519_2.png) [@Datman](https://forum.arduino.cc/u/Datman)\
**Post date:** [November 24, 2021, 10:13am UTC](https://forum.arduino.cc/t/mini-frequenzimetro-dove-sbaglio/927582/43 "2021-11-24T10:13:40Z")

</div>

Ecco, ho incorporato la libreria nel programma (per tenere in ordine ho usato file diversi, schede nell'IDE diverse):

```cpp
 // D5 - Frequency input 1, Counter 1, 16 bit

unsigned long f_freq;

volatile unsigned char f_ready;
volatile unsigned char f_mlt;
volatile unsigned int f_tics;
volatile unsigned int f_period;
volatile unsigned int f_comp;

unsigned long f = 0;
unsigned long f1 = 0;

void setup()
{
pinMode(5, INPUT); // This is the frequency input
Serial.begin(9600);
}

void loop()
{
f_comp=10; // Cal Value / Calibrate with professional Freq Counter
start(100); // 100 ms Gate Time

while (f_ready == 0);
f=f_freq;
uint32_t freq_ric=(long)((f*0.64-10695)/10); // -10695 = -10700 +5 di approssimazione.
freq_ric=((int)(freq_ric+3)/5)*5;
uint32_t f_ric=freq_ric;
uint8_t cifra1=f_ric/10000;
f_ric=f_ric%10000;
uint8_t cifra2=f_ric/1000;
f_ric=f_ric%1000;
uint8_t cifra3=f_ric/100;
f_ric=f_ric%100;
uint8_t cifra4=f_ric/10;
uint8_t cifra5=f_ric%10;

Serial.println(f);
Serial.print(freq_ric); Serial.print(" "); Serial.print(cifra1); Serial.print(" "); Serial.print(cifra2); Serial.print(" ");
Serial.print(cifra3); Serial.print(" "); Serial.print(cifra4); Serial.print(" "); Serial.println(cifra5); 
}

/*
  Martin Nawrath KHM LAB3
  Kunsthochschule f¸r Medien Kˆln
  Academy of Media Arts
  http://www.khm.de
  http://interface.khm.de/index.php/labor/experimente/  
*/

void start(int ms)
{
  TIMSK0 &=~(1<<TOIE0); // disable Timer0 //disable millis and delay
  delayMicroseconds(50); // wait if any ints are pending
  f_period=ms;
  
  if (f_comp ==0) f_comp=1; // 0 is not allowed in del us

  // hardware counter setup ( refer atmega168.pdf chapter 16-bit counter1)
  TCCR1A=0; // reset timer/counter1 control register A
  TCCR1B=0; // reset timer/counter1 control register A
  TCNT1=0; // counter value = 0
  // set timer/counter1 hardware as counter , counts events on pin T1 ( arduino pin 5)
  // normal mode, wgm10 .. wgm13 = 0
  
  TCCR1B |= (1<<CS10) ;// External clock source on T1 pin. Clock on rising edge.
  TCCR1B |= (1<<CS11) ;
  TCCR1B |= (1<<CS12) ;
  
  // timer2 setup / is used for frequency measurement gatetime generation
  TCCR2A=0;
  TCCR2B=0;
  
  // timer 2 presaler set to 128 / timer 2 clock = 16Mhz / 256 = 62500 Hz
  TCCR2B |= (1<<CS20) ;
  TCCR2B &= ~(1<<CS21) ;
  TCCR2B |= (1<<CS22) ;

  //set timer2 to CTC Mode with OCR2A is top counter value
  TCCR2A &= ~(1<<WGM20) ;
  TCCR2A |= (1<<WGM21) ;
  TCCR2A &= ~(1<<WGM22) ;
  OCR2A = 124; // CTC divider by 125
 
  f_ready=0; // reset period measure flag
  f_tics=0; // reset interrupt counter
  GTCCR = (1<<PSRASY); // reset presacler counting
  TCNT2=0; // timer2=0
  TCNT1=0; // Counter1 = 0
  
  TIMSK2 |=(1<<OCIE2A); // enable Timer2 Interrupt  
                              // External clock source on T1 pin. Clock on rising edge.
  TCCR1B |= (1<<CS12) | (1<<CS11) | (1<<CS10); // start counting now     
}
// ******************************************************************
// Timer2 Interrupt Service is invoked by hardware Timer2 every 1ms = 1000 Hz
// 16Mhz / 128 / 125 = 1000 Hz
// here the gatetime generation for freq. measurement takes place: 

ISR(TIMER2_COMPA_vect)
{
// multiple 2ms = gate time = 100 ms
if (f_tics >= f_period)
  {           
  // end of gate time, measurement ready
  // GateCalibration Value, set to zero error with reference frequency counter
  // delayMicroseconds(FreqCounter::f_comp); // 0.01=1/ 0.1=12 / 1=120 sec 
  delayMicroseconds(f_comp);
  TCCR1B = TCCR1B & ~7; // Gate Off / Counter T1 stopped 
  TIMSK2 &= ~(1<<OCIE2A); // disable Timer2 Interrupt
  TIMSK0 |=(1<<TOIE0); // enable Timer0 again // millis and delay
  f_ready=1; // set global flag for end count period   
                          // calculate now frequeny value
  f_freq=0x10000 * f_mlt; // mult #overflows by 65636
  f_freq += TCNT1; // add counter1 value
  f_mlt=0;
  }
f_tics++; // count number of interrupt events
if (TIFR1 & 1) 
  { // if Timer/Counter 1 overflow flag
  f_mlt++; // count number of Counter1 overflows
  TIFR1 =(1<<TOV1); // clear Timer/Counter 1 overflow flag
  }
// PORTB = PORTB ^ 32; // int activity test
}

byte n;
void displ()
{
switch(n) // catodo comune
             // D76543210  
  { // AB-CDEFG
  case 0: PORTD|=0b11011110;
          PORTD&=0b11111110;
  break;
  case 1: PORTD|=0b01010000;
          PORTD&=0b01110000;
  break;
  case 2: PORTD|=0b11001101;
          PORTD&=0b11101101;
  break;
  case 3: PORTD|=0b11011001;
          PORTD&=0b11111001;
  break;
  case 4: PORTD|=0b01010011;
          PORTD&=0b01110011;
  break;
  case 5: PORTD|=0b10011011;
          PORTD&=0b10111011;
  break;
  case 6: PORTD|=0b10011111;
          PORTD&=0b10111111;
  break;
  case 7: PORTD|=0b11010000;
          PORTD&=0b11110000;
  break;
  case 8: PORTD|=0b11011111;
          PORTD&=0b11111111;
  break;
  case 9: PORTD|=0b11011011;
          PORTD&=0b11111011;
  break;
  }
}

```

---

_[View the full topic](https://forum.arduino.cc/t/mini-frequenzimetro-dove-sbaglio/927582)._
