Pages: [1]   Go Down
Author Topic: Frequency detection works with Uno, not with Due  (Read 590 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everybody!

I'm pretty new to all this Arduino things, but I was looking to do a frequency detection and I found a code on Instructables.com The person who wrote it told there how it worked. I uploaded it on my Arduino Uno and indeed, it worked great! But I wanted to make the code bigger so I bought an Arduino Due (because he is a lot faster than the Uno), but I had to download the new beta-software (1.5 I think). I hooked up the Due and made exactly the same circuit (I only had to change 1 resistor because the Due works with 3.3V) and I took the same code but when I tried to upload it, I had a lot of errors...

This is the list of errors:

_1e_versie_Arduino_sketch.ino: In function 'void setup()':
_1e_versie_Arduino_sketch:34: error: 'cli' cannot be used as a function
_1e_versie_Arduino_sketch:39: error: 'ADCSRA' was not declared in this scope
_1e_versie_Arduino_sketch:40: error: 'ADCSRB' was not declared in this scope
_1e_versie_Arduino_sketch:42: error: 'ADMUX' was not declared in this scope
_1e_versie_Arduino_sketch:42: error: 'REFS0' was not declared in this scope
_1e_versie_Arduino_sketch:43: error: 'ADLAR' was not declared in this scope
_1e_versie_Arduino_sketch:45: error: 'ADPS2' was not declared in this scope
_1e_versie_Arduino_sketch:45: error: 'ADPS0' was not declared in this scope
_1e_versie_Arduino_sketch:46: error: 'ADATE' was not declared in this scope
_1e_versie_Arduino_sketch:47: error: 'ADIE' was not declared in this scope
_1e_versie_Arduino_sketch:48: error: 'ADEN' was not declared in this scope
_1e_versie_Arduino_sketch:49: error: 'ADSC' was not declared in this scope
_1e_versie_Arduino_sketch:51: error: 'sei' was not declared in this scope
_1e_versie_Arduino_sketch.ino: In function 'void ADC_vect()':
_1e_versie_Arduino_sketch:57: error: 'ADCH' was not declared in this scope
_1e_versie_Arduino_sketch:65: error: 'PORTB' was not declared in this scope
_1e_versie_Arduino_sketch.ino: In function 'void loop()':
_1e_versie_Arduino_sketch:74: error: 'PORTB' was not declared in this scope

This is the code by Amandaghaessei:

Code:
//sine wave freq detection with 38.5kHz sampling rate and interrupts
//by Amanda Ghassaei
//http://www.instructables.com/id/Arduino-Frequency-Detection/
//July 2012

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
*/

//clipping indicator variables
boolean clipping = 0;

//data storage variables
byte newData = 0;
byte prevData = 0;

//freq variables
unsigned int timer = 0;//counts period of wave
unsigned int period;
int frequency;


void setup(){
  
  Serial.begin(9600);
  
  pinMode(13,OUTPUT);//led indicator pin
  
  cli();//diable interrupts
  
  //set up continuous sampling of analog pin 0
  
  //clear ADCSRA and ADCSRB registers
  ADCSRA = 0;
  ADCSRB = 0;
  
  ADMUX |= (1 << REFS0); //set reference voltage
  ADMUX |= (1 << ADLAR); //left align the ADC value- so we can read highest 8 bits from ADCH register only
  
  ADCSRA |= (1 << ADPS2) | (1 << ADPS0); //set ADC clock with 32 prescaler- 16mHz/32=500kHz
  ADCSRA |= (1 << ADATE); //enabble auto trigger
  ADCSRA |= (1 << ADIE); //enable interrupts when measurement complete
  ADCSRA |= (1 << ADEN); //enable ADC
  ADCSRA |= (1 << ADSC); //start ADC measurements
  
  sei();//enable interrupts
}

ISR(ADC_vect) {//when new ADC value ready

  prevData = newData;//store previous value
  newData = ADCH;//get value from A0
  if (prevData < 127 && newData >=127){//if increasing and crossing midpoint
    period = timer;//get period
    timer = 0;//reset timer
  }
  
  
  if (newData == 0 || newData == 1023){//if clipping
    PORTB |= B00100000;//set pin 13 high- turn on clipping indicator led
    clipping = 1;//currently clipping
  }
  
  timer++;//increment timer at rate of 38.5kHz
}

void loop(){
  if (clipping){//if currently clipping
    PORTB &= B11011111;//turn off clippng indicator led
    clipping = 0;
  }

  frequency = 38462/period;//timer rate/period
  //print results
  Serial.print(frequency);
  Serial.println(" hz");
  
  delay(100);
}



I hope some of you guys can help me out, I would be the happiest man in the world! smiley-wink

Thank you so much!!

Best regards,
RobinC,
Belgium
Logged

Narvik, Norway
Offline Offline
Edison Member
*
Karma: 24
Posts: 1042
electronics engineer, teacher.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is a completly different chip.
I belive there is a job to do translating  avr-registers to new cpu
Logged

Ny

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow, I didn't know that, do you know where I can find something that could learn me to change these things? Thank you for your response!!
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 34541
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow, I didn't know that, do you know where I can find something that could learn me to change these things? Thank you for your response!!
No it is a totally different chip, there is not a one to one relationship between the registers.
You have to understand what the first code did and then try an implement it in the new processor.
This is way beyond you at the moment and is not the sort of thing you will find on a web page.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh no!  Is there somebody who understands those library's and will help me to change mines to the right ones that I need? smiley I would be sooo happy! Thank you very much! And thanks for the response! smiley-grin
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


First of all, like you I am just beginning to work with the DUE. I'm not a programmer, just a hobbyist. So take everything I say with a grain of salt and investigate for yourself.

First let's talk about the differences between the standard Arduino platform and the DUE.

Port mode: although you can use port modes, they are intended for byte transfer of data. In my opinion Port mode should not be used for setting or resetting a individual pens. If anything else is hooked up to that port, it could have devastating effect on the accuracy of your data. When you declare that you're going to use pen mode, you should not use port mode. Always use the mode that you declare, and in the same context.

Timers: most Arduino's only have 1 timer. The DUE has several. You need to select which Timer you need to use.
The dude is not assume that you want to use any particular timer.

ADC: instructions used for the standard Arduino's is not the same as the DUE. For that reason all of your ADC errors are being seen as variables not instructions.

Interrupts: for most Arduino's, There are only a few interrupt Capable pens. Globally setting these interrupts and clearing them is not a problem. However on the DUE, every pen is interrupt ready. Globally setting these pens could have A devastating effect on any input. So global functions do not work on the DUE. Each needs to be set and reset individually.

You need to go through the tutorial for the Arduino due. Read it carefully and practice the examples until you understand how it works.

Hope this helps.

Joe


Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Whoaw! Thank you very much for the information! I will read everything about the Due, I hope that I understand it more at that moment. I will also look at this with my teacher and I think your answer will help us as well! Thank you all for the response and if there is somebody who still has some additions, feel free to post it! Thank you in advance!

Best regards,
RobinC,
Belgium
Logged

Pages: [1]   Go Up
Jump to: