Great, sounds like I'm on the right track. I already made the changes you suggested.Using the arduino compiler libraries for timers will not give the precision frequencies that my code will do using a timer interrupt.
Those libraries wont be free running, so there will be overhead after each timed period.
It may be usable with their interrupt functions provided there is no attached latency especially variable latency.
Do you have a freq counter? If so you could write a quick test in code setting the timer to 50uS and using the timer interrupt toggle an output pin. Toggling at 50uS is 100uS period = 10 kHz output freq from the pin. Then the frequency counter will tell you if it is 10000 Hz or there is latency.
That sounds much better! If you can just set an interrupt to occur after every 512 instructions there will be no variable latency. Then my code will work fine and you will get very precise frequencies.
Just change this section of code to work with 31.25 kHz interrupt freq;
Then all the notes will be exact again.Rich (BB code):if(bres1 >= 2000000) // using 31.25 kHz * 64 { bres1 -= 2000000; note1_duty = 10; }
Regarding the note ON period and variable name you are correct, I just rushed that code example. A better name would be note1_onperiod or something like that.
It's your baby so just change var names to whatever suits your code and your preferences.
I do not have a dedicated frequency meter, but I do have a DMM with that functionality, as well as several scopes. I don't anticipate that being an issue, as I can always adjust things in code.
It had not occurred to me that there might be some latency due to using the timer library. I guess I just assumed it accessed the timers on the AVR directly, and that it wouldn't pose a problem. I guess I'll test it out when I return to my home in Vermont, which is where I have my scopes. I'll check how accurate it is and we can go from there.
I'll let you know what happens.
Thanks,
Matt