Problem AC Frequency Meter 50Hz using Arduino Due

Hi all ,
i have problem when i use arduino due.

the code below was run perfectly and get good result on arduino uno

#include <LiquidCrystal.h>    // include Arduino LCD library

 
void setup(void) {
 
Serial.begin(9600);
 
  // Timer1 module configuration
  TCCR1A = 0;
  TCCR1B = 2;   // enable Timer1 module with 1/8 prescaler ( 2 ticks every 1 us)
  TCNT1  = 0;   // Set Timer1 preload value to 0 (reset)
  TIMSK1 = 1;   // enable Timer1 overflow interrupt
 
  EIFR |= 1;  // clear INT0 flag
  attachInterrupt(0, timer1_get, FALLING);  // enable external interrupt (INT0)
}
 
uint16_t tmr1 = 0;
float period, frequency;
 
void timer1_get() {
  tmr1 = TCNT1;
  TCNT1  = 0;   // reset Timer1
}
 
ISR(TIMER1_OVF_vect) {  // Timer1 interrupt service routine (ISR)
  tmr1 = 0;
}
 
// main loop
void loop() {
 
  // save current Timer1 value
  uint16_t value = tmr1;
  // calculate signal period in milliseconds
  // 8.0 is Timer1 prescaler and 16000 = MCU_CLK/1000
  period = 8.0 * value/16000;
  // calculate signal frequency which is = 1/period ; or = MCU_CLK/(Prescaler * Timer_Value)
  if(value == 0)
    frequency = 0;   // aviod division by zero
  else
    frequency = 16000000.0/(8UL*value);
 
 
  Serial.print(frequency);
  Serial.println(" Hz  ");
  // print period
  
  delay(500);
 
}

but when i upload on my arduino due board , this message was show on

'TCCR1A' was not declared in this scope

how to solve that ? thanks :slight_smile:

As written, that code is incompatible with a Due. It uses register names and resources of an AVR processor. The Due is based on an Atmel SAM3X8E.

Search for DueTimer into the library manager. it enable interrupt timer for DUE, the code should be more easy to adapt after...

gfvalvo:
As written, that code is incompatible with a Due. It uses register names and resources of an AVR processor. The Due is based on an Atmel SAM3X8E.

the code is different or we can just add some library ?

nitrof:
Search for DueTimer into the library manager. it enable interrupt timer for DUE, the code should be more easy to adapt after...

i have read on github about DueTimer , what function should i use to make an AC frequency counter?

I did not play much with timer like this, but here waht I would do:
you can start a timer a low frequency, or short period if you want... :

Timer3.start(1000); // Calls every 1ms

your ISR could increment a counter.
I do not know how you get the input, but the code seem to suggest that you check for the tension to cross 0V.
attachInterrup should be the same.
At that point, you can chec how many counter value ( so uSec ) you have and reset your counter...

I can see two methods to retrieve frequency of an periodic AC signal with the DUE (providing this signal is strictly between 0V and 3.3V):

1/ Use the window feature of the ADC peripheral: trigger an interrupt each time the input signal crosses an high threshold, measure time (e.g. with micros()) between two triggers

2/ Depending on the amplitude of the signal, if close to 3.3V, connect the input to a digital schmitt trigger input and use a simple attachinterrupt() function. You will find Schmitt trigger digital pins page 12 of Sam3x datasheet.

ard_newbie :o I didn't knew that the DUE have an internal schmitt trigger!!! cool to know !! And lokking at the data sheet... it is many !! cool. Maybe the note should be added to DUE pinout diagram?? (BTW onedhaid, Very nice reference to work with DUE. )

ard_newbie, I will try to look for more detail about detail about the schmitt trigger property. I'm curious. Unless you can already point me to the right page... (still a 1400 pae long data sheet.. lol)
I really like this idea.

[EDIT] Unless mistaken, I think I found it: 31.5.9 Input Glitch and Debouncing Filters, P624.

Regards.

Nitrof