I always seem to come with the most ridiculous questions, and today I will not disappoint
I have a chip that is particularly sensitive to timing mismatch on UART communications. And an application requiring low power from a battery so as usual I am running a lower frequency system. According the datasheet the best frequency for this application is 7.37MHz, which is no problem. Bootloader recompiled, boards and variants updated, simple test code started. Yet with the very first test code that I ran I noticed that delay() was slightly off on its timing and delayMicroseconds() is basically not working at all. I don't want to change something that would conflict with the core and libraries. I'd prefer to change the prescalar math where ever the clock to counter relationships are set up. I have been going through these files for a while and just figured I would ask if anyone knew where exactly the Arduino creates the relationship between all of the timer functions and the F_CPU. Does anyone happen to know?
gfvalvo - I was thinking of changing the clock divider in setup, but I've found that sometimes it is better to change it at its source. Also updating the core files libraries h and cpp files makes it more universally applicable.
westfw - you are my hero. and there it is in the delayMicroseconds() function where it only defines F_CPU greater than 8MHz or equal to 1MHz
Now to make sure I am not missing another reference to it somewhere else
fixed it up right as rain by the way. Now to make sure all my inter chip communications works. Thank you my friend. Here is a before and after of that code piece if anyone else ever needs to make that adaptation. The frequency is so close to the math for 8MHz that it uses the same math, but needed to be include in the 8MHz range instead of being neglected or treated like it is 1MHz
original
#elif F_CPU >= 8000000L
// for the 8 MHz internal clock
// for a 1 and 2 microsecond delay, simply return. the overhead
// of the function call takes 14 (16) cycles, which is 2us
if (us <= 2) return; //Â = 3 cycles, (4 when true)
// the following loop takes 1/2 of a microsecond (4 cycles)
// per iteration, so execute it twice for each microsecond of
// delay requested.
us <<= 1; //x2 us, = 2 cycles
// account for the time taken in the preceeding commands.
// we just burned 17 (19) cycles above, remove 4, (4*4=16)
// us is at least 6 so we can substract 4
us -= 4; // = 2 cycles
#else
// for the 1 MHz internal clock (default settings for common Atmega microcontrollers)
modified to reach down to 7.37MHz
#elif F_CPU >= 7370000L
// for the 8 MHz internal clock
// for a 1 and 2 microsecond delay, simply return. the overhead
// of the function call takes 14 (16) cycles, which is 2us
if (us <= 2) return; //Â = 3 cycles, (4 when true)
// the following loop takes 1/2 of a microsecond (4 cycles)
// per iteration, so execute it twice for each microsecond of
// delay requested.
us <<= 1; //x2 us, = 2 cycles
// account for the time taken in the preceeding commands.
// we just burned 17 (19) cycles above, remove 4, (4*4=16)
// us is at least 6 so we can substract 4
us -= 4; // = 2 cycles
#else