Pages: [1]   Go Down
Author Topic: timer1 code help  (Read 512 times)
0 Members and 1 Guest are viewing this topic.
italy
Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!
I found a useful tutorial about using timers on arduino, and I tried to hack an example to make some tests.
This sketch, should work this way:
1- receive an external signal and trigger an external interrupt
2- the external interrupt function (tachSignal) sets timer1 to wait for a certain amount of time
3- when the ISR runs, it turns on an led and then sets timer1 for another time interval
4- when the ISR runs again, it turns off the led and reset timer1

here is the sketch:
Code:
// Arduino timer CTC interrupt example
// www.engblaze.com
 
// avr-libc library includes
#include <avr/io.h>
#include <avr/interrupt.h>
 
#define LEDPIN 2
int delayTicks = 100;
int outputTicks = 30;
int interruptIter = 0;
void setup()
{
attachInterrupt(0,tachSignal,FALLING);
  pinMode(LEDPIN, OUTPUT);
     // initialize Timer1
    cli();          // disable global interrupts
    TCCR1A = 0;     // set entire TCCR1A register to 0
    TCCR1B = 0;     // same for TCCR1B
    // enable global interrupts:
    sei();
}
 

void tachSignal(){
cli();          // disable global interrupts
  // set compare match register to desired timer count:
    OCR1A = delayTicks;
// turn on CTC mode:
    TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler:
    TCCR1B |= (1 << CS10);
    TCCR1B |= (1 << CS12);
// enable timer compare interrupt:
    TIMSK1 |= (1 << OCIE1A);
// enable global interrupts:
    sei();
interruptIter = 0;
}


ISR(TIMER1_COMPA_vect)
{
if(interruptIter == 0){
  digitalWrite(LEDPIN, HIGH);
cli();          // disable global interrupts
  // set compare match register to desired timer count:
    OCR1A = outputTicks;
// turn on CTC mode:
    TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler:
    TCCR1B |= (1 << CS10);
    TCCR1B |= (1 << CS12);
// enable timer compare interrupt:
    TIMSK1 |= (1 << OCIE1A);
// enable global interrupts:
    sei();
interruptIter = 1;}else{
  digitalWrite(LEDPIN, LOW);
  cli();          // disable global interrupts
  TCCR1A = 0;     // set entire TCCR1A register to 0
  TCCR1B = 0;     // same for TCCR1B
  // enable global interrupts:
  sei();}
}



void loop()
{
   
}

I'm not sure if this code is correct, I'm not sure about disabling interrupts within the interrupt routine.
Any advice is greatly appreciated!
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 112
Posts: 5281
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Variables used in an interrupt service routine have to be declared volatile.
Logged

Dallas, Texas
Offline Offline
God Member
*****
Karma: 31
Posts: 887
Old, decrepit curmugeon
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!
I found a useful tutorial about using timers on arduino, and I tried to hack an example to make some tests.
This sketch, should work this way:
1- receive an external signal and trigger an external interrupt
2- the external interrupt function (tachSignal) sets timer1 to wait for a certain amount of time
3- when the ISR runs, it turns on an led and then sets timer1 for another time interval
4- when the ISR runs again, it turns off the led and reset timer1

here is the sketch:

So have you tried to run the code?  How does it work?  What errors do you see?
Logged

New true random number library available at: http://code.google.com/p/avr-hardware-random-number-generation/

Current version 1.0.1

Pages: [1]   Go Up
Jump to: