Page 1 of 1

HCTimer2 - Arduino Timer 2 Library

Posted: Fri Mar 15, 2013 4:14 pm
by admin
HCTimer2 - Arduino Timer 2 Library


This Arduino library written by Hobby Comonents Ltd reprograms timer 2 so that it can be used for running your own code at timed intervals whilst allowing you to execute your normal loop() code at the same time.

This library is useful for use with our LED matrix and 7 segment modules, allowing you to put code that drives the modules within the timer interrupt function whilst leaving the main program loop free for other code.

You will need to download and unzip this library to the Aduino development environments library area

On Windows:
My Documents\Arduino\libraries\
My Documents\Arduino\libraries\
My Documents\Arduino\libraries\

On Mac:
Documents/Arduino/libraries/
Documents/Arduino/libraries/
Documents/Arduino/libraries/
or similarly for Linux.

Once you have installed the library cut and paste the Arduino sketch below for an example of how to use it.

Please log in to download the library

  1. /*  FILE:    HCTimer2_Example
  2.     DATE:    14/03/13
  3.     VERSION: 0.1
  4.  
  5.    
  6. This is an example of how to use the Hobby Components timer 2 interrupt library. This
  7. library reprograms timer 2 for use as a timer interrupt for executing code at regular
  8. intervals.
  9.  
  10. To initialise this library you will need call HCTimer2Init(prescaler, compare) once with
  11. the following parameters:
  12.      
  13. The prescaller parameter sets the timer 2 clock prescaler. Valid values are    
  14.  
  15.      T2_CLK_DIV_0
  16.      T2_CLK_DIV_8
  17.      T2_CLK_DIV_32
  18.      T2_CLK_DIV_64
  19.      T2_CLK_DIV_128
  20.      T2_CLK_DIV_256
  21.      T2_CLK_DIV_1024
  22.      
  23. The compare parameter sets the value timer 2 compares its counter to before triggering
  24. an interrupt. This is an 8 bit value from 0 to 255.
  25.      
  26. The two parameters can be used to alter the frequency of how often the timer interrupt
  27. triggers and therefore runs your code. The formula to calculate the timer interrupt time
  28. in seconds is: ((compare + 1) * prescaler) / 16000000MHz
  29.  
  30. To execute code each time the interrupt triggers, add the function HCTimer2() to your
  31. program and place any code you wish to execute when the interrupt triggers within it.
  32. Make sure that any code you place within this function is executed before the next
  33. interrupt is due to be triggered. You must also not place any code inside this function
  34. that requires the use of other interrupts such as the serial print functions.
  35.  
  36.  
  37.  
  38. You may copy, alter and reuse this code in any way you like but please leave
  39. reference to hobbycomponents.com in your comments. This software may not be
  40. used directly for the purpose of selling products that directly compete with
  41. Hobby Components Ltd's own range of products.
  42.  
  43. THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS LTD MAKES NO WARRANTIES, WHETHER
  44. EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  45. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
  46. HOBBY COMPONENTS LTD SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
  47. INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
  48. REASON WHATSOEVER.
  49.  */
  50.  
  51. /* Include the HCTimer2 library */
  52. #include <HCTimer2.h>
  53.  
  54. int Counter = 0;
  55.  
  56. void setup()
  57. {
  58.   /* Set the DIO for pin 13 (LED L) to an output */
  59.   pinMode(13, OUTPUT);
  60.  
  61.   /* Initialise the HCTimer2 library with a 15.616mS interval */  
  62.   HCTimer2Init(T2_CLK_DIV_1024, 243);
  63. }
  64.  
  65.  
  66. void loop()
  67. {
  68.   /* Place your normal code in here */
  69. }
  70.  
  71.  
  72. /* Place code inside this function that you would like to be executed
  73.    whenever the timer interrupt is triggered */
  74. void HCTimer2()
  75. {
  76.   Counter++;
  77.  
  78.   /* Toggle DIO 13 (LED L) once every 64 * 15.616mS = approx 1 second */
  79.   if (Counter == 64)
  80.     {
  81.       digitalWrite(13,!digitalRead(13));
  82.       Counter = 0;
  83.     }
  84. }