I’m trying to build an 8 bit synthesizer based on the SID 6581 chip found in the Commodore64. It requires a 1MHz clock input. Right now I’m using the timer2 interrupt, but I’m not sure it’s working. Is there a more standard way to provide a reliable clock-out at a specific frequency than interrupts. Maybe a built-in function perhaps?
//Capture the current timer value. This is how much error we have
//due to interrupt latency and the work in this function
latency=TCNT2;
//Reload the timer and correct for latency. //Reload the timer and correct for latency. //Reload the timer and correct for latency.
TCNT2=latency + 257 - 16;
}
I think your method will not work for one main reason: you are trying to interrupting every microsecond, and your interrupt takes much longer to execute that a microsecond. This frequent of an interrupt will pretty much take up all of your processing time, and you will have to write an incredibly efficient ISR just to make sure you get out before the next interrupt. You will need to do direct register manipulation (e.g. no calls to digitalWrite() as this takes several microseconds to execute) and you should probably write the ISR in assembly just to maximize speed efficiency.
Instead of interrupts, you could set it up so one of the timers outputs a 50% duty-cycle PWM at 1 MHz, or you could modify your mega168 to run off of a 1 MHz clock (internal 8 MHz RC oscillator with CKDIV8 fuse bit programmed) and output that clock signal on the CKOUT pin (though I'm not sure you want your entire Arduino running at 1 MHz, and I'm not sure how happy the bootloader would be with this).
I'd recommend using a hardware PWM. This would be a case of set it up and forget it, with no need for interrupts or any other processor interaction. You will have to configure the PWM yourself, however, as the analogWrite() function will not produce the proper frequency. I suggest you use a fast PWM with Timer2 running with a prescaler of 1 (so it's counting at the full speed of 16 MHz), and a TOP of 15 and a compare match on 8. I've posted some sample code that shows how to do something like this in other threads.
Is there a more standard way to provide a reliable clock-out at a specific frequency than interrupts.
Yes, use a stable clock source like a crystal or oscillator. Interrupts and timers from a micro-controller seems like the LAST thing you would want to use.
Gentlefolk, the original post is from [u]2008/u and the original poster never replied. By now he's either figured out a solution, given up, or passed away.