I want to make a cal standard for audio level freqencys.
Oh, my bad.
I found a sketch in Reply#11 of this post.
But for reasons I can't figure, it doesn't work. It compiles (with the timerone lib installed) but it doesn't do anything.
This one works.
The Timer1 initialize parameter:
Timer1.initialize(30);
(30 uS period) produces a frequency of 33.333 kHz.
/*
* Timer1 library example
* June 2008 | jesse dot tane at gmail dot com
*/
#include "TimerOne.h"
void setup()
{
pinMode(10, OUTPUT);
Timer1.initialize(30); // initialize timer1, and set a 1/2 second period
Timer1.pwm(9, 512); // setup pwm on pin 9, 50% duty cycle
Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt
}
void callback()
{
digitalWrite(10, digitalRead(10) ^ 1);
}
void loop()
{
// your program here...
}
I found a frequency counter sketch
here:
And this one works:
// Arduino UNO DDS
#include <PWM.h>
int32_t frequency_9 = 4000; //frequency (in Hz)
int32_t frequency_3 = 3000; //frequency (in Hz)
void setup()
{
InitTimersSafe();
SetPinFrequencySafe(9, frequency_9);
SetPinFrequencySafe(3, frequency_3);
}
void loop()
{
int dutyC =512; // Duty Cycle 0 (0%) - 1023(100%)
pwmWrite(9, dutyC / 4);
pwmWrite(3, dutyC / 4);
delay(30);
}
Frequency Counter
0
4000
3996
3996
3996
3996
3996
3996
3995
3995
3996
3996
3996
3996
3996
Frequency Counter
#include <FreqCounter.h>
void setup() {
Serial.begin(57600); // connect to the serial port
Serial.println("Frequency Counter");
}
long int frq =0;
void
loop() {
FreqCounter::f_comp= 8; // Set compensation to 12
FreqCounter::start(1000); // Start counting with gatetime of 100ms
while (FreqCounter::f_ready == 0) // wait until counter ready
frq=FreqCounter::f_freq; // read result
Serial.println(frq); // print result
delay(20);
}
See attached scope screenshot.
Note, the filename of the Frequency generator is based on the period parameter of 30 uS, which equates to 33,333 Hz.
The attached serial terminal capture file using Clear Terminal shows a frequency of 33,298 (35 Hz lower than scope reading and period parameter.
Frequency Counter
33332
33298
33298
33298
33298
33298
33297
33298
33298
33298
33297
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33299
33298
33299
33298
33298
33298
33298
33298
33298
33298
33298
33298
33298
33299
33298
33298
33298
33298
33298
33298
33298
33298
33298
33299
33298
33298
33298
33299
33298
It's only 5 digits accuracy and it's off by 0.1060 %.
NOTE: The very FIRST reading is right on the money. All the rest are off by 35 Hz.
When I changed the frequency to 9900 Hz T = (101 us), the reading was 9891 (10 Hz off). (again 0.1 % error)
ClearTerminal_Capture__Frequency counter reading.txt (528 Bytes)