I'm working on a project at the moment that uses a custom SPI device but another part of the electronics requires a continuous clock signal that's at the same speed and preferably in sync with the SPI clock signal.
I'm not an expert with registers but feel this may be the way to do it. I'm using an ATMega328 (UNO).
Free pins I have are D3-D6 or any Analogue pin A0-A5.
I'm using the hardware serial pins D0 and D1 as well as the SoftwareSerial library for a second serial port on pins D6 and D7.
Running this code on an UNO will generate multiples of 8 pulses at 4Mhz with around about 16us delay between each burst on my scope. How do I get a continuous 4Mhz signal based on this without affecting SPI.
I've tried using the TimerOne library but it breaks parts of the software when attaching an interrupt (I think it may break the softwareSerial library). I feel there must be a better way since there's a clock divider already setup with SPI.
An internal hardware timer can be used to generate a clock. I don't know which timer SoftwareSerial is using, but there are three timers, and Timer0 is used by Arduino for timing.
It's doesn't need to be in sync but it does need to be at the same speed. Basically I'm using a JK Flip Flop to detect triggering of a sensor and the state of JK and CLK need to be in a stable state when a reset takes place.
I'm using the MOSI to reset the Flip Flop and would prefer the clock state to be consistent for predictable operation of the circuit. It could either be high or low.
In order to keep the Flip Flops alive the clock needs to be continuous.
Thanks Coding Badly (I should use that name!) - The use of Timer 2 is perfect but what are the four lines of code? I tried a couple of things with registers but it affected other bits of code that use the timer.
const byte CLOCKOUT = 9; // Uno, Duemilanove, etc.
// set up 4 MHz timer on CLOCKOUT (OC1A)
pinMode (CLOCKOUT, OUTPUT);
// set up Timer 1
TCCR1A = bit (COM1A0); // toggle OC1A on Compare Match
TCCR1B = bit (WGM12) | bit (CS10); // CTC, no prescaling
OCR1A = 1; // output every other cycle
Warning though, the clock is not necessarily in sync with SCK. I observed the signal being 62.5 ns out, or falling (when SCK is rising), or rising (when SCK is rising).
You're aware the SPI clock only occurs when a transfer occurs? Is that going to be a problem?
Maybe you can set a fuse bit to output the system clock, run it thru a flip flop to divide it by two, and use that.
Will be running all the time.
CrossRoads:
You're aware the SPI clock only occurs when a transfer occurs? Is that going to be a problem?
Maybe you can set a fuse bit to output the system clock, run it thru a flip flop to divide it by two, and use that.
Will be running all the time.
That would certainly be a highly viable approach. You might still suffer from the problem I had with the timer code. The SPI clock might start on a different clock cycle to the one (divided by 2) that you use.
I have a project that does requires an output clock to be synchronized with the SPI clock, but after much research, haven't found a way to do it with the Arduino SPI library which I prefer to use for its speed. Is it possible to make the SPI clock wait for the next rising edge of my output clock to synchronize with it? Maybe a change to the SPI library code?
The SPI library is pretty small, and just basically makes the SPI hardware do its thing. I doubt you can do that, unless you use bit-banged SPI. I have a bit-banged SPI library on my page about SPI.