Hi, I have been lurking in this great forum for a while, now it's time for my first post!
I am building a creative clock generator module for a modular synthesizer wit an Atmega328P.
I need several very precise clock streams with a defined gate length, not only trigger signals.
First thing I tried was the obvious beginners code (example):
void loop() {
//Gate with a fixed lenght of 450?s
digitalWrite(clockPin, HIGH);
delayMicroseconds(450);
digitalWrite(clockPin, LOW);
//nextScheduledTrigger stores a dynamic value based on a pot setting
delayMicroseconds(nextScheduledTrigger);
}
Easy as it is, it works very well with very even gate signals, but as you see this code is blocking, which is undesired.
So I tried an approach based on the "Blink without Delay" example. http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
As a result I got the non blocking functionality working, but because there is a lot going in the loop the gate signal begins to jitter a lot when twisting knobs.
The length of the gate is shifting, I don't get even 450?s gates anymore.
I replaced all digitalRead and digitalWrite with the appropriate "PORTB &= B11111101;" kind of syntax to make IO requests faster.
This improved the performance but it is still not optimal.
So I am looking for alternatives.
I thought about a hardware solution to bring in an additional 555 timer IC. The idea was to just trigger the 555 and let the IC deal with the gate off. But I think this might be overkill, since I need several of those devices?
So my question is if there is possible a "magic" Atmega register I can set, which maybe generate a gate signal instead of a trigger when using digitalwrite?
I am not shy to dig deeper into the microprocessor, if necessary.
But It would be great if some veteran people would give me a hint where to look at or just tell me about your best practices how to do this kind of functionality.