Hi All,
I'm experimenting with an Atmel Mega 1284p. I found this simple "Frequency Generator":
// Frequenz-/Rechteckgenerator
#define INLENGTH 5 //maximale Größe der Zahl
#define INTERMINATOR 'H' //'H' von 'Hz'
char inString[INLENGTH+2];
int inCount;
#define MinFreq 33
#define MaxFreq 65535
#define Ausgangspin 12 //muss kein PWM-Pin sein HR: 26
unsigned int Frequenz = 60000; //Hz (Maximum: 65535 Hz)
void setup(){
OSCCAL = 138;
Serial1.begin(9600);
Serial1.println("Bitte angeben wieviel Hz ausgegeben werden sollen (mit 'Hz')!");
//Serial1.print("Oscal: ");
//int temp = OSCCAL;
//Serial1.println(temp);
delay(1000);
tone(Ausgangspin, Frequenz);
}
void loop(){
Eingabe(); //Eingabe-Funktion, die Eingabe-String entgegennimmt
//Zeichenkette -> Zahl
//Alternative: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1176289764
Frequenz=atol(inString);
//Bereich prüfen
Frequenz=constrain(Frequenz, MinFreq, MaxFreq);
Serial1.print("Es wird gesetzt: ");
Serial1.print(Frequenz,DEC);
Serial1.println("Hz!");
delay(300);
tone(Ausgangspin, Frequenz);
}
void Eingabe(){
inCount=0; //Ziffern-Zähler rücksetzen
do
{
while (Serial1.available()==0);
inString[inCount] = Serial.read();
if(inString[inCount]==INTERMINATOR) break;
if((inString[inCount]<'0')||(inString[inCount]>'9')){
//continue; //geht nicht
inCount--; //-> Workaround
}
}
while(++inCount < (INLENGTH+1));
Serial1.flush();
}
The code runs on the 1284p @ 8MHz (Board Definition from: GitHub - JChristensen/mighty-1284p at v1.6.3 and Arduino IDE 1.6.12)
I checked the output with an oszi and at low frequencys like 5kHz or 10kHz and 20kHz, its except a few Hz up or down, correct. But at 30kHz up to 60kHz its about 10% less than expected (set to 60kHz gives me 54kHz). This is a very strange behaviour because I checked the setup with an Arduino Nano and it was all right (set to 60kHz gives me 60kHz).
I then tryed to go into the tone() function and it seems like it's using different counters. Maybee there is something wrong with the Board definition?
Is there a way of getting this work on 1284p?
Thank you very much!
BR