Pages: [1]   Go Down
Author Topic: Tone.cpp improvement  (Read 1331 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13482
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Searching through some core code I came across the tone lib and especially the function:

Code:
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)

This function tries to find an optimal prescaler for the timer. To do that it calculates the OCR  = F_CPU / frequency / 2 / SomePrescaler -1  nine times.
Because division is expensive I took an extra variable uint32_t ocrRaw = F_CPU / frequency / 2;  which is the repeating part of the formula
and replaced that in the rest of the function.

I tested with ToneKeyboard.pde sample sketch to see the impact on the size.
before patch => 3614 bytes
after patch => 3526 bytes
improvement == 88 bytes!

I did not test speed, I expect it to be slightly faster (no free Arduino nearby to test)

Disadvantages?
« Last Edit: January 12, 2013, 05:58:11 pm by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13482
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Posted as improvement - https://github.com/arduino/Arduino/issues/1220 -
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Toledo, OH
Offline Offline
God Member
*****
Karma: 35
Posts: 508
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Searching through some core code I came across the tone lib and especially the function:

Code:
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)

This function tries to find an optimal prescaler for the timer. To do that it calculates the OCR  = F_CPU / frequency / 2 / SomePrescaler -1  nine times.
Because division is expensive I took an extra variable uint32_t ocrRaw = F_CPU / frequency / 2;  which is the repeating part of the formula
and replaced that in the rest of the function.

I tested with ToneKeyboard.pde sample sketch to see the impact on the size.
before patch => 3614 bytes
after patch => 3526 bytes
improvement == 88 bytes!

I did not test speed, I expect it to be slightly faster (no free Arduino nearby to test)

Disadvantages?


Or, just replace the entire tone library with NewTone and save almost 1200 bytes.

Tim
Logged

Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13482
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are always other options, Tim!
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: