Looking for a Command

Hey guys, I was wondering if there is any sort of command in the IDE to basically have a small loop running while other things in the main loop are changing., Any help would be appreciated especially in the next day.

Depends what you're trying to do.

If you're looking for timing type things, you use millis.

:slight_smile:

Fast enough?

ISR(TIMER2_OVF_vect) { // interrupt handler, will run at irq_freq (see below)
// reset timer2
TCNT2 = 0;

// put your code here....

}

void setup()
{
// Arduino runs at 16 Mhz...
// register names: atmega168 style
// Timer2 (8bit) Settings:
// prescaler values: CS22 CS21 CS20
// 0 0 0 stopped
// 0 0 1 /1 62500 Hz
// 0 1 0 /8 7813 Hz
// 0 1 1 /32 1953 Hz
// 1 0 0 /64 977 Hz
// 1 0 1 /128 488 Hz
// 1 1 0 /256 244 Hz
// 1 1 1 /1024 61 Hz
// irq_freq = 16MHz / ( 256 * prescaler )
//
// set irq to 488 Hz: CS22-bit = 1, CS20-bit = 1, CS21-bit = 0
TCCR2B |= ( (1<<CS22) | (1<<CS20) );
TCCR2B &= ~( (1<<CS21) );
// Use normal mode
TCCR2A &= ~((1<<WGM21) | (1<<WGM20));
// Use internal clock - external clock not used in Arduino
ASSR |= (0<<AS2);
//Timer2 Overflow Interrupt Enable
TIMSK2 |= (1<<TOIE2) | (0<<OCIE2A);
// reset timer 2
TCNT2 = 0;
// enable all interrupts
sei();
}

madworm,
Thanks for posting that timer code. It's the most concise and best commented example I have ever run across.
Thanks! :slight_smile:

Madworm, thank you for the code snippet, but it has several minor problems with it:

// Use internal clock - external clock not used in Arduino
ASSR |= (0<<AS2);

This line does not do what you think it does for two reasons:

  1. 0 << anything = 0. You cannot clear a bit using this formulation. All you are doing with this line is bitwise ORing the ASSR register with zero, which results in no change (x | 0 = x). If you want to clear the AS2 bit, you need to use:

ASSR &= ~(1 << AS2);

You use this formulation correctly when setting the timer clock, but for some reason you didn't apply it here.

  1. Setting timer2 to run off the I/O clock doesn't mean it's running off the internal 8 MHz oscillator. If your Arduino has its fuses configured to run off the external crystal, this crystal is the I/O clock. The only time ASSR will come in handy is if you have your Arduino configured to run off the internal oscillator and you want to clock timer2 using an external oscillator (in which case you would set the AS2 bit of ASSR). By default the AS2 bit is cleared, so this line is pretty much completely unnecessary anyway.

//Timer2 Overflow Interrupt Enable
TIMSK2 |= (1<<TOIE2) | (0<<OCIE2A);

Once again, 0 << OCIE2A = 0, so this is not doing anything. You would need to rewrite this as:

TIMSK2 |= 1 << TOIE2;
TIMSK2 &= ~(1 << OCIE2A);

sei();

Interrupts are enabled on Arduinos by default, so it isn't necessary to call sei() (it has already been called before your setup() routine was entered). Calling it again doesn't hurt anything, though.

  • Ben

jup, you're right.

luckily the code works nevertheless, but I fixed it :wink: