Skip to main content
deleted 166 characters in body; edited tags; edited title
Source Link
dda
  • 1.6k
  • 1
  • 12
  • 18

Arduino atmegaATmega 2560 frequency measurement

I would like to measure frequency with an Arduino 2560 board. The approach is to use a timer interrupt set to one second and pin interrupt to count interrupts in one second. The

The problem I have is that I get the wrong frequency. On my frequency generator I set up a frequency of 1000 Hz in my program I get 1006 Hz instead odof 1000 Hz..

What I'amam I doing wrong?

Here is my program: In. In the stup method I have declared a Timer, when Itimer. When the signal comecomes on pin 2, the counter is increased by 1. After one seccondsecond passed the flag bData is raised. If the flag is true then I printoutprint out a frequency on serial port.

#include <SPI.h>
#include <Ethernet.h>
#include <Math.h>
 

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
 
IPAddress ip(192, 168, 1, 177);
EthernetServer server(80);
 
const byte interruptPinTemp = 2;
const byte interruptPinSlanost = 3;
 
//konstante za SBE-3
const double g = 4.85442486e-003;
const double h = 6.77300393e-004;
const double i = 2.65502731e-005;
const double j = 2.06782794e-006;
const double f = 1000.0;
 
volatile int cnt = 0;
volatile int cnt1 = 0;
 
volatile double slanost = 0;
volatile double slanost1 = 0;
int timer1_counter;
boolean bData = false;
 
volatile int dvesek = 1;

void beriTemperatura() {
  cnt++;
}

void beriSlanost() {
  slanost++;
}
 

double calcTemp(double frekvenca)
  {
  double logRes = log(f / frekvenca);
  return 1.0 / (g + (h * logRes) + (i * pow(logRes, 2.0)) + (j * pow(logRes, 3.0))) - 273.15;
}
 

void setup() {
 
  Serial.begin(9600);       // inicializacija serijskega porta
  noInterrupts();           // disable all interrupts
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
 
  pinMode(interruptPinTemp, INPUT_PULLUP);
  pinMode(interruptPinSlanost, INPUT_PULLUP);
 
  attachInterrupt(digitalPinToInterrupt(interruptPinTemp), beriTemperatura, CHANGE);
  attachInterrupt(digitalPinToInterrupt(interruptPinSlanost), beriSlanost, CHANGE);
 
  TCCR1A = 0;
  TCCR1B = 0;
  timer1_counter = 34286;   // preload timer 65536-16MHz/256/2Hz
 
  TCNT1 = timer1_counter;   // preload timer
  TCCR1B |= (1 << CS12);    // 256 prescaler
  TIMSK1 |= (1 << TOIE1);   // enable timer overflow interrupt
  interrupts();
}

//Timer interrupt na 1 sekundo
ISR(TIMER1_OVF_vect)      {
  // interrupt service routine
{
  TCNT1 = timer1_counter;   // preload timer
 
    cnt1 = cnt ;               // prepisemo vrednost v cnt1
    bData = true;             // postavimo flag
    cnt = 0;  // resetiramo counter
  
}
 

void loop() {
  // če je postavljen flag, da so podatki jih izpišemo ali naredimo karkoli drugega
  if (bData == true)
  {
    bData = false;
    Serial.println(calcTemp(cnt1), 3);
    Serial.println(cnt1);
    
  }
 
 
 
}

Here is the frequency from my generator:   

enter image description here

andAnd here is a frequency measured with my board  :

enter image description here

whatWhat is wrong that I gotget 1006 Hz instead of 1000 Hz?

If I riseraise the frequnecyfrequency for example to 5 kHz then I get 5032 Hz in my program (32 Hz toto0 much).

thankThank you for any advice.

Arduino atmega 2560 frequency measurement

I would like to measure frequency with Arduino 2560 board. The approach is to use timer interrupt set to one second and pin interrupt to count interrupts in one second. The problem I have is that I get wrong frequency. On my frequency generator I set up a frequency of 1000 Hz in my program I get 1006 Hz instead od 1000 Hz..

What I'am doing wrong?

Here is my program: In the stup method I have declared a Timer, when I signal come on pin 2 the counter is increased by 1. After one seccond passed the flag bData is raised. If the flag is true then I printout a frequency on serial port.

#include <SPI.h>
#include <Ethernet.h>
#include <Math.h>
 

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
 
IPAddress ip(192, 168, 1, 177);
EthernetServer server(80);
 
const byte interruptPinTemp = 2;
const byte interruptPinSlanost = 3;
 
//konstante za SBE-3
const double g = 4.85442486e-003;
const double h = 6.77300393e-004;
const double i = 2.65502731e-005;
const double j = 2.06782794e-006;
const double f = 1000.0;
 
volatile int cnt = 0;
volatile int cnt1 = 0;
 
volatile double slanost = 0;
volatile double slanost1 = 0;
int timer1_counter;
boolean bData = false;
 
volatile int dvesek = 1;

void beriTemperatura(){
  cnt++;
}

void beriSlanost(){
  slanost++;
}
 

double calcTemp(double frekvenca)
 {
  double logRes = log(f / frekvenca);
  return 1.0 / (g + (h * logRes) + (i * pow(logRes, 2.0)) + (j * pow(logRes, 3.0))) - 273.15;
}
 

void setup() {
 
  Serial.begin(9600);       // inicializacija serijskega porta
  noInterrupts();           // disable all interrupts
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
 
  pinMode(interruptPinTemp, INPUT_PULLUP);
  pinMode(interruptPinSlanost, INPUT_PULLUP);
 
  attachInterrupt(digitalPinToInterrupt(interruptPinTemp), beriTemperatura, CHANGE);
  attachInterrupt(digitalPinToInterrupt(interruptPinSlanost), beriSlanost, CHANGE);
 
  TCCR1A = 0;
  TCCR1B = 0;
  timer1_counter = 34286;   // preload timer 65536-16MHz/256/2Hz
 
  TCNT1 = timer1_counter;   // preload timer
  TCCR1B |= (1 << CS12);    // 256 prescaler
  TIMSK1 |= (1 << TOIE1);   // enable timer overflow interrupt
  interrupts();
}

//Timer interrupt na 1 sekundo
ISR(TIMER1_OVF_vect)        // interrupt service routine
{
  TCNT1 = timer1_counter;   // preload timer
 
    cnt1 = cnt ;               // prepisemo vrednost v cnt1
    bData = true;             // postavimo flag
    cnt = 0;  // resetiramo counter
  
}
 

void loop() {
  // če je postavljen flag, da so podatki jih izpišemo ali naredimo karkoli drugega
  if (bData == true)
  {
    bData = false;
    Serial.println(calcTemp(cnt1), 3);
    Serial.println(cnt1);
    
  }
 
 
 
}

Here is the frequency from my generator:  enter image description here

and here is a frequency measured with my board  enter image description here

what is wrong that I got 1006 Hz instead of 1000 Hz?

If I rise the frequnecy for example to 5 kHz then I get 5032 Hz in my program (32 Hz to much).

thank you for any advice

Arduino ATmega 2560 frequency measurement

I would like to measure frequency with an Arduino 2560 board. The approach is to use a timer interrupt set to one second and pin interrupt to count interrupts in one second.

The problem I have is that I get the wrong frequency. On my frequency generator I set up a frequency of 1000 Hz in my program I get 1006 Hz instead of 1000 Hz.

What am I doing wrong?

Here is my program. In the stup method I have declared a timer. When the signal comes on pin 2, the counter is increased by 1. After one second passed the flag bData is raised. If the flag is true then I print out a frequency on serial port.

#include <SPI.h>
#include <Ethernet.h>
#include <Math.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 1, 177);
EthernetServer server(80);
const byte interruptPinTemp = 2;
const byte interruptPinSlanost = 3;
//konstante za SBE-3
const double g = 4.85442486e-003;
const double h = 6.77300393e-004;
const double i = 2.65502731e-005;
const double j = 2.06782794e-006;
const double f = 1000.0;
volatile int cnt = 0;
volatile int cnt1 = 0;
volatile double slanost = 0;
volatile double slanost1 = 0;
int timer1_counter;
boolean bData = false;
volatile int dvesek = 1;

void beriTemperatura() {
  cnt++;
}

void beriSlanost() {
  slanost++;
}

double calcTemp(double frekvenca) {
  double logRes = log(f / frekvenca);
  return 1.0 / (g + (h * logRes) + (i * pow(logRes, 2.0)) + (j * pow(logRes, 3.0))) - 273.15;
}

void setup() {
  Serial.begin(9600);       // inicializacija serijskega porta
  noInterrupts();           // disable all interrupts
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  pinMode(interruptPinTemp, INPUT_PULLUP);
  pinMode(interruptPinSlanost, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPinTemp), beriTemperatura, CHANGE);
  attachInterrupt(digitalPinToInterrupt(interruptPinSlanost), beriSlanost, CHANGE);
  TCCR1A = 0;
  TCCR1B = 0;
  timer1_counter = 34286;   // preload timer 65536-16MHz/256/2Hz
  TCNT1 = timer1_counter;   // preload timer
  TCCR1B |= (1 << CS12);    // 256 prescaler
  TIMSK1 |= (1 << TOIE1);   // enable timer overflow interrupt
  interrupts();
}

//Timer interrupt na 1 sekundo
ISR(TIMER1_OVF_vect) {
  // interrupt service routine
  TCNT1 = timer1_counter;   // preload timer
  cnt1 = cnt ;               // prepisemo vrednost v cnt1
  bData = true;             // postavimo flag
  cnt = 0;  // resetiramo counter
}

void loop() {
  // če je postavljen flag, da so podatki jih izpišemo ali naredimo karkoli drugega
  if (bData == true) {
    bData = false;
    Serial.println(calcTemp(cnt1), 3);
    Serial.println(cnt1);
  }
}

Here is the frequency from my generator: 

enter image description here

And here is a frequency measured with my board:

enter image description here

What is wrong that I get 1006 Hz instead of 1000 Hz?

If I raise the frequency for example to 5 kHz then I get 5032 Hz in my program (32 Hz to0 much).

Thank you for any advice.

Post Migrated Here from electronics.stackexchange.com (revisions)
Source Link
Ferguson
Ferguson

Arduino atmega 2560 frequency measurement

I would like to measure frequency with Arduino 2560 board. The approach is to use timer interrupt set to one second and pin interrupt to count interrupts in one second. The problem I have is that I get wrong frequency. On my frequency generator I set up a frequency of 1000 Hz in my program I get 1006 Hz instead od 1000 Hz..

What I'am doing wrong?

Here is my program: In the stup method I have declared a Timer, when I signal come on pin 2 the counter is increased by 1. After one seccond passed the flag bData is raised. If the flag is true then I printout a frequency on serial port.

#include <SPI.h>
#include <Ethernet.h>
#include <Math.h>


byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

IPAddress ip(192, 168, 1, 177);
EthernetServer server(80);

const byte interruptPinTemp = 2;
const byte interruptPinSlanost = 3;

//konstante za SBE-3
const double g = 4.85442486e-003;
const double h = 6.77300393e-004;
const double i = 2.65502731e-005;
const double j = 2.06782794e-006;
const double f = 1000.0;

volatile int cnt = 0;
volatile int cnt1 = 0;

volatile double slanost = 0;
volatile double slanost1 = 0;
int timer1_counter;
boolean bData = false;

volatile int dvesek = 1;

void beriTemperatura(){
  cnt++;
}

void beriSlanost(){
  slanost++;
}


double calcTemp(double frekvenca)
{
  double logRes = log(f / frekvenca);
  return 1.0 / (g + (h * logRes) + (i * pow(logRes, 2.0)) + (j * pow(logRes, 3.0))) - 273.15;
}


void setup() {

  Serial.begin(9600);       // inicializacija serijskega porta
  noInterrupts();           // disable all interrupts
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());

  pinMode(interruptPinTemp, INPUT_PULLUP);
  pinMode(interruptPinSlanost, INPUT_PULLUP);

  attachInterrupt(digitalPinToInterrupt(interruptPinTemp), beriTemperatura, CHANGE);
  attachInterrupt(digitalPinToInterrupt(interruptPinSlanost), beriSlanost, CHANGE);

  TCCR1A = 0;
  TCCR1B = 0;
  timer1_counter = 34286;   // preload timer 65536-16MHz/256/2Hz

  TCNT1 = timer1_counter;   // preload timer
  TCCR1B |= (1 << CS12);    // 256 prescaler
  TIMSK1 |= (1 << TOIE1);   // enable timer overflow interrupt
  interrupts();
}

//Timer interrupt na 1 sekundo
ISR(TIMER1_OVF_vect)        // interrupt service routine
{
  TCNT1 = timer1_counter;   // preload timer

    cnt1 = cnt ;               // prepisemo vrednost v cnt1
    bData = true;             // postavimo flag
    cnt = 0;  // resetiramo counter
  
}


void loop() {
  // če je postavljen flag, da so podatki jih izpišemo ali naredimo karkoli drugega
  if (bData == true)
  {
    bData = false;
    Serial.println(calcTemp(cnt1), 3);
    Serial.println(cnt1);
    
  }

 
 
}

Here is the frequency from my generator: enter image description here

and here is a frequency measured with my board enter image description here

what is wrong that I got 1006 Hz instead of 1000 Hz?

If I rise the frequnecy for example to 5 kHz then I get 5032 Hz in my program (32 Hz to much).

thank you for any advice