Advertisement
Guest User

ArduinoTimerInterrupt

a guest
Nov 3rd, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.60 KB | None | 0 0
  1. //------------------------------------------------------------------------------
  2. // Timer Interrupt Demo - Supports most Arduino-based shields
  3. // dan@marginallycelver.com 2013-11-03
  4. //------------------------------------------------------------------------------
  5. // Copyright at end of file.
  6. // please see http://www.github.com/MarginallyClever/ArduinoTimerInterrupt for more information.
  7.  
  8. #define LEDPIN (13)
  9. #define BAUD   (57600)
  10.  
  11.  
  12. /**
  13.  * Clock interrupt method.
  14.  */
  15. ISR(TIMER_COMPA_vect) {
  16.   digitalWrite(LEDPIN,!digitalRead(LEDPIN));
  17. }
  18.  
  19.  
  20. /**
  21.  * Set the clock 1 timer frequency.
  22.  * @input desired_freq_hz the desired frequency
  23.  */
  24. void timer_set_frequency(long desired_freq_hz) {
  25.   // Source: http://letsmakerobots.com/node/28278
  26.   // Different clock sources can be selected for each timer independently.
  27.   // To calculate the timer frequency (for example 2Hz using timer1) you will need:
  28.  
  29.   //  CPU frequency 16Mhz for Arduino
  30.   long clock_freq=16000000;
  31.   //  maximum timer counter value (256 for 8bit, 65536 for 16bit timer)
  32.   long max_counter=65536;
  33.   int prescaler_index=0;
  34.   int prescalers[] = {1,8,64,256,1024};
  35.   long counter_value;
  36.   do {
  37.     ++prescaler_index;
  38.     //  Divide CPU frequency through the choosen prescaler (16000000 / 256 = 62500)
  39.     counter_value = clock_freq / prescalers[prescaler_index];
  40.     //  Divide result through the desired frequency (62500 / 2Hz = 31250)
  41.     counter_value /= desired_freq_hz;
  42.     //  Verify counter_value < maximum timer. if fail, choose bigger prescaler.
  43.   } while(counter_value > max_counter && prescaler_index<5);
  44.  
  45.   if( prescaler_index>5 ) {
  46.     Serial.println(F("Timer could not be set: Desired frequency out of bounds."));
  47.     return;
  48.   }
  49.  
  50. #ifdef VERBOSE
  51.   Serial.print(F("counter_value  ="));  Serial.print(counter_value);
  52.   Serial.print(F(" prescaler_index="));  Serial.print(prescaler_index);
  53.   Serial.print(F(" = "));  Serial.print(((prescaler_index&0x1)   ));
  54.   Serial.print(F("/"));  Serial.print(((prescaler_index&0x2)>>1));
  55.   Serial.print(F("/"));  Serial.println(((prescaler_index&0x4)>>2));
  56. #endif
  57.  
  58.   // disable global interrupts
  59.   cli();
  60.  
  61.   // set entire TCCR1A register to 0
  62.   TCCR1A = 0;
  63.   // set entire TCCR1B register to 0
  64.   TCCR1B = 0;
  65.   // set the overflow clock to 0
  66.   TCNT1  = 0;
  67.   // set compare match register to desired timer count
  68.   OCR1A = counter_value;
  69.   // turn on CTC mode
  70.   TCCR1B |= (1 << WGM12);
  71.   // Set CS10, CS11, and CS12 bits for prescaler
  72.   if( prescaler_index&0x1 ) TCCR1B |= (1 << CS10);
  73.   if( prescaler_index&0x2 ) TCCR1B |= (1 << CS11);
  74.   if( prescaler_index&0x4 ) TCCR1B |= (1 << CS12);
  75.   // enable timer compare interrupt
  76.   TIMSK1 |= (1 << OCIE1A);
  77.  
  78.   sei();  // enable global interrupts
  79. }
  80.  
  81.  
  82. /**
  83.  * First thing this machine does on startup.  Runs only once.
  84.  */
  85. void setup() {
  86.   Serial.begin(BAUD);  // open coms
  87.   pinMode(LEDPIN, OUTPUT);
  88. }
  89.  
  90. /**
  91.  * Runs after setup() and repeats forever
  92.  */
  93. void loop() {
  94.   Serial.println("1hz");  timer_set_frequency(1);  delay(2000);
  95.   Serial.println("2hz");  timer_set_frequency(2);  delay(2000);
  96.   Serial.println("4hz");  timer_set_frequency(4);  delay(2000);
  97.   Serial.println("8hz");  timer_set_frequency(8);  delay(2000);
  98.   Serial.println("16hz");  timer_set_frequency(16);  delay(2000);
  99.   Serial.println("32hz");  timer_set_frequency(32);  delay(2000);
  100.   Serial.println("64hz");  timer_set_frequency(64);  delay(2000);
  101.   Serial.println("128hz");  timer_set_frequency(128);  delay(2000);
  102.   Serial.println("256hz");  timer_set_frequency(256);  delay(2000);
  103.   Serial.println("512hz");  timer_set_frequency(512);  delay(2000);
  104.   Serial.println("1000hz");  timer_set_frequency(1000);  delay(2000);
  105.   Serial.println("2000hz");  timer_set_frequency(2000);  delay(2000);
  106.   Serial.println("4000hz");  timer_set_frequency(4000);  delay(2000);
  107.   Serial.println("10000hz");  timer_set_frequency(10000);  delay(2000);
  108. }
  109.  
  110.  
  111. /**
  112. * This file is part of Arduino Timer Interrupt.
  113. *
  114. * Arduino Timer Interrupt is free software: you can redistribute it and/or modify
  115. * it under the terms of the GNU General Public License as published by
  116. * the Free Software Foundation, either version 3 of the License, or
  117. * (at your option) any later version.
  118. *
  119. * Arduino Timer Interrupt is distributed in the hope that it will be useful,
  120. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  121. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  122. * GNU General Public License for more details.
  123. *
  124. * You should have received a copy of the GNU General Public License
  125. * along with Arduino Timer Interrupt. If not, see <http://www.gnu.org/licenses/>.
  126. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement