Changes between Initial Version and Version 1 of ArduinoTricks


Ignore:
Timestamp:
Nov 8, 2013, 10:17:53 AM (6 years ago)
Author:
hazen
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ArduinoTricks

    v1 v1  
     1<toc>
     2
     3== Timing with Interrupts ==
     4
     5The ''''delay()''' function can be used to pause program execution for a number
     6of ms.  It has two problems:  it is not very accurate for periodic actions,
     7and the Arduino can''t do anything else while waiting for the delay.  The answer
     8to this is an '''interrupt'', which causes the CPU to stop what it is doing and
     9call a function, then return to whatever it was doing.  The source of the interrupt
     10can many things, but one of the simplest is a timer.  This allows a function to
     11be called at precise time intervals.  Here is an example:
     12
     13
     14{{{
     15  //                                                                                                                           
     16  // LED blink with interrupt                                                                                                   
     17  //                                                                                                                           
     18 
     19}}}
     20{{{
     21  #include <TimerOne.h>          // use function definitions in this file
     22 
     23}}}
     24{{{
     25  void setup() {
     26    Timer1.initialize( 100000);  // initialize timer, period = 100ms                                                           
     27    pinMode( 13, OUTPUT);        // set pin 13 (with LED) as output                                                             
     28    Timer1.attachInterrupt( blink);  // "attach" function blink()                                                               
     29  }
     30 
     31}}}
     32{{{
     33  // nothing to do in the main loop!                                                                                           
     34    void loop() {
     35  }
     36 
     37}}}
     38{{{
     39  unsigned char led_state = 0;
     40 
     41}}}
     42{{{
     43  // toggle the LED state when called                                                                                           
     44  void blink() {
     45    digitalWrite( 13, led_state);
     46    led_state ^= 1;
     47  }
     48
     49}}}
     50Before you can use this you must install the Timer1 library.
     51Get the code here:  [http://playground.arduino.cc/code/timer1 Timer1 page]
     52Read how to install a library here:  [http://arduino.cc/en/Guide/Libraries Arduino Libraries]
     53
     54NOTE:  Executing code takes time.  If you try to do too much in the interrupt function
     55and call it too fast, there may be no processor time left for the main ''''loop()''' function
     56to run.
     57
     58== Memory ==
     59
     60The Arduino processor ([http://www.atmel.com/devices/atmega328p.aspx ATMega328]
     61types of [http://playground.arduino.cc/Learning/Memory memory]
     62about are Flash (where the program is stored) and RAM (where your variable are stored).
     63
     64If you overflow the 32k flash with code (or data stored in flash using PROGMEM)
     65you''ll get an error message:  '''"Sketch too big"'''.
     66
     67RAM is more tricky.  You must keep track of how many bytes each of your variables uses
     68and avoid using close to the 2048 byte maximum.
     69
     70You can see the size of the static memory areas using the tool ''avr-size'.
     71On a linux machine, look for a directory called /tmp/buildxxxxxxxxxxxx with the
     72most recent time stamp:
     73
     74{{{
     75  $ ls -ldrt /tmp/build*
     76
     77}}}
     78In this directory should be a file named something like
     79
     80{{{
     81  your_sketch_name.cpp.elf
     82
     83}}}
     84Run the command:
     85
     86{{{
     87  $ avr-size your_sketch_name.cpp.elf
     88
     89}}}
     90{{{
     91   text    data     bss     dec     hex filename
     92   1244       2      17    1263     4ef sketch_mar14a.cpp.elf
     93
     94}}}
     95The values printed are as follows:
     96
     97{{{
     98    text - flash data used for code
     99    data - Memory with initialized data (the initial value has to be stored in FLASH too!)
     100    bss  - Memory that is initialized with zero''s (the compiler will add some code so it will initialize data & bss)
     101    dec & hex are a decimal and hex display of the combined RAM and FLASH size of your program.
     102
     103}}}
     104In the example there are two bytes of initialized data and 17 bytes of uninitialized memory, for
     105a total of 19 bytes of RAM used.
     106
     107''''However''' this only counts _static_ data (that is, variables declared outside any function).
     108It is safest to declare _all_ your variables like this so you can check using avr-size if you have
     109too much RAM used.
     110