I want to generate a 1MHZ clock signal for the soundchip I'm using. Does anyone have a discrete code sample?
You want to use a microcontroller to generate the clock signal? Does the microcontroller need to do anything else?
You presumably mean 1 MHz, not .001 Hz.
Does the clock need to run continuously, or just during SPI transfers to/from the device?
The soundchip doesn't have an internal clock, so yes it needs to be contiguously supplying a clock signal to the sound chip. I've heard that it's possible with pwm.
Edit: It's 1MHZ indeed. Edited the post.
Per '328 datasheet:
"Clock Output Buffer
The device can output the system clock on the CLKO pin. To enable the output, the CKOUT
Fuse has to be programmed. This mode is suitable when the chip clock is used to drive other circuits
on the system. The clock also will be output during reset, and the normal operation of I/O
pin will be overridden when the fuse is programmed. Any clock source, including the internal RC
Oscillator, can be selected when the clock is output on CLKO. If the System Clock Prescaler is
used, it is the divided system clock that is output."
CLK0 is pin PB0.
So take that and run thru an external counter chip for nice stable 1 MHz clock.
For example:
But... that will be 16MhZ right? I don't think any of my counters can handle that
Use one of the internal timers, set it up to divide the clock by 16. Assuming you don't need them for anything else like PWM or Tone(). I'd use Timer1 or Timer2. Timer0 is needed for millis(), so that's the last one I'd mess with.
" I don't think any of my counters can handle that "
I provided a link to one.
Really all you need is 4 flip-flops - the output of each divides the input in half - 16/2 = 8. 8/2 = 4. 4/2= 2. 2/2 = 1.
A divide by 16 counter is doing the same.
Someone double check me on this, I don't have a good way to verify, but I think this will do the trick with no additional hardware.
//Use Timer/Counter1 to generate a 1MHz square wave on Arduino pin 9.
//J.Christensen 27Apr2012
void setup(void)
{
DDRB = _BV(DDB1); //set OC1A/PB1 as output (Arduino pin D9, DIP pin 15)
TCCR1A = _BV(COM1A0); //toggle OC1A on compare match
OCR1A = 7; //top value for counter
TCCR1B = _BV(WGM12) | _BV(CS10); //CTC mode, prescaler clock/1
}
void loop(void)
{
}
I tend to wonder if we'll ever see the day when you can't get a complete C=64; it seems like everybody and their brother is ripping the SID chip out, and discarding the rest. I keep thinking I need to find myself a C=64 system while I still can have one with its audio system intact...
Another thing I wonder: Didn't the Sega Genesis (or the Nintendo) have a separate sound chip (I believe the Sega used a Yamaha chip)? I wonder why (almost?) nobody is hacking those?
Especially for the time it was made, let alone the toy-pc it was used in, that chip was a work of art. But it was soon eclipsed by wave-table sound chips, and then the ones with 'environment' effects.
I went looking for 1 MHz resonator (don't need caps, right?) and wow, almost a dollar at DigiKey!
"don't need caps, right?"
Depends on the resonator. Some have built in caps, some don't.
cr0sh:
I tend to wonder if we'll ever see the day when you can't get a complete C=64; it seems like everybody and their brother is ripping the SID chip out, and discarding the rest. I keep thinking I need to find myself a C=64 system while I still can have one with its audio system intact...Another thing I wonder: Didn't the Sega Genesis (or the Nintendo) have a separate sound chip (I believe the Sega used a Yamaha chip)? I wonder why (almost?) nobody is hacking those?
I dont want to derail much but yes pretty much everything up till the playstaton and FM sound chips (outside of the Apple II) the main thing is not many programmed for them, so unless you want game music rips ... and people do pull them, especially the NES
and YES I need to get a 64 or at least a 128, I had a 128 years ago but I scrapped it for parts (talking 1990's though) kicking myself now.
Yep, that generates 1 MHz square wave.
Proof:
thats super, I actually have a 6502 project in the works and one of the dark clouds was generating a 1Mhz clock, though in my application its a serial bitstream, but that makes me feel a bit better without sitting down and doing it myself
You could have used Timer 2 if you wanted to keep the 16-bit timer for other uses, but if you just want the clock, well that does it alright.
For anyone that likes graphical pin-outs, I did this one today showing the timer inputs and outputs:
I also found this:
void setup()
{
pinMode(5, OUTPUT);
TCCR0A = (0 << COM0A1) | (0 << COM0A0) | (1 << COM0B1) | (0 << COM0B0) | (1 << WGM01) | (1 << WGM00);
TCCR0B = (0 << FOC0A) | (0 << FOC0B) | (1 << WGM02) | (0 << CS02) | (0 << CS01) | (1 << CS00);
OCR0A = 0xf;
OCR0B = 0x7;
}
void loop()
{
}
This also generates 1Mhz