Hey Rob,
On Sat, Dec 21, 2013 at 05:12:59PM +0100, Rob Tillaart wrote:
> int setClock(int freq) // freq in KHz
> {
> int prev = TWBR;
> if (freq > 0) TWBR = F_CPU/(freq*1000) -16 + 2; // something like that
> return prev;
> }
>
> a -1 or 0 would allow it to act as getClock()
>
> this would allow me to do
>
> prev = setClock(800); // high speed modus
> lcd.print("hello");
> setClock(prev); // back to normal modus
Huh? With the above implementation, this breaks? setClock returns the
old value of TWBR, not the old frequency, so you can't just pass it to
setClock again.
> better alternative is of course is to add a real getClock(); function.
I agree that having a getClock would make sense, since (as also pointed
out by Rob Giseburt later in this thread) that allows the user to get
feedback about the actual frequency used.
However, for the "restore the previous clock frequency value" use case,
perhaps it makes sense to add a getClockDivider() function that returns
the current setting as an architecture-dependent value that can be
passed to setClockDivider again?
This would allow restoring the previous settings, while saving a few
costly calculations (from TWBR to frequency, and back from frequency to
TWBR).
All of the above applies equally to SPI and Wire, btw.
Gr.
Matthijs