We are taught in computer classes that we can slow down a program by doing nothing. Compiler programs are built to prevent nothing from happening.
New SketchOpen a new sketch with your Arduino IDE. Remove all text and replace with the text below. Compile and look for errors.
// pragma declaration O0 tells compiler no optimization, default Os for size
#pragma GCC optimize ("-O0") // letter o then zero
#include <avr/io.h>
int main(void){ DDRB = 0xFF; // turn PORTB pins on
while(1) { PORTB++;
{for (long i=0 ; i<0x7FFF ; i++) {;}}}}Explaining the CodeThis is C programming language and it works like algebra. Arduino IDE understands this language and can generate an upload to your board.
#pragma GCC optimize ("-O0") doesn't look like a normal Arduino sketch. We are giving an order to the Arduino studio program not to improve our code for us. We are selecting the zero level of optimization.
The Arduino Uno board contains an ATmega328p avr chip. Arduino IDE knows this but the GCC language needs code included from a file named <avr/io.h>. Then it will know what the variables DDRB and PORTB mean.
A mathematical function named main has an integer value. The value of main is the program counter address of the running program. Main does not require an input number so we have int main (void) as a calculation.
First we decide DDRB is equal to 0xFF hexadecimal, 255 decimal. This turns all 8 pins on our chip PORTB into outputs. We can turn them on or off to control led lights. We've completed our void setup( ) from an Arduino sketch.
while(1) because one will always be true, this is our void loop( ). Each loop we increment the number value of PORTB++. Then we count through a loop of 0x7FFF (decimal 32, 767 ) performing a no operation {;} each time. Counting the brackets is important.
Upload to BoardWhen the code compiles without error you are ready to upload. Check your board and port settings. Upload.
Even better if you can wire up lamps for the other pins on PORTB. Watch the LED labelled L as it blinks, it is connected to header pin13.
Change Data TypeThis makes a difference. Our first sketch calculates delay using a long data variable. Long data variables take 32bits in memory. Change this to long long. and we will be working with 64bit numbers using more carry operations, taking more time.
Upload to your board. LED will flash more slowly. The Uno takes longer for this calculation. Change to integer, now it will go fastest. On board LED blinks quickly. It only has to calculate 16bit values, fewer carry operations.
Comment out the line #pragma GCC optimize ("-O0") and we default back to using -Os to generate a program optimized for size. Look in any normal sketch and you will see this selection.
The compiler default is like an autocorrect that doesn't want us wasting computer cycles doing a no operation (nop). It will also take out long variables if it decides we only need an integer. This can prevent your sketches from working as you expect.
On Your OwnChange the digits used for the number of no operations nop. What happens if you change it to 0x1FFF, the delay is shorter and the LED blinks faster.
But try a delay of 0x8FFF and the sketch stops working. Arduino math defaults to using the MSB most significant bit of our number for signed mathematics.
int16_t and uint16_tTry the data type int16_t, Arduino software will treat this as an ordinary integer of 15 bits up to 32, 767. It can also represent -32, 768 if the MSB most significant bit is set. Try different values, it will be the same as int.
Change the data type to uint16_t unsigned ordinary integer and we can make a sketch that blinks our LED with longer delays. Now our variable I will be a 16bit binary number with a range from 0 to 65, 535.
int32_t, uint32_t, int64_t and uint64_t, These are long and long long data types with different aliases for names. Try each and you will see the same results.
Optimization levelsA websearch provides this result:


_ztBMuBhMHo.jpg?auto=compress%2Cformat&w=48&h=48&fit=fill&bg=ffffff)








Comments