timing < 1ms

Searching this forum I was directed to http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=50106, which discusses timing.

I am unable to get the the first full routine example to function correctly...

Here is the code example (with Serial coms added)

#include <avr/io.h> 


void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);
}

void loop()                     // run over and over again
{
  DDRB |= (1 << 0); // Set LED as output 

  TCCR1B |= (1 << CS10); // Set up timer 

  for(;;){
    //Serial.println(TCNT1,DEC);
    // Check timer value in if statement, true when count matches 1/20 of a second 
    if (TCNT1 >= 50000) 
    { 
      Serial.println(TCNT1,DEC);
      PORTB ^= (1 << 0); // Toggle the LED 
      TCNT1=0; 
    } 
  }    
}

" if (TCNT1 >= 50000) " never becomes true as far as I can tell.

If TCNT1 is declared as an int, it will never be greater than 32k.

Try :
if ((unsigned int)TCNT1 >= 50000)

TCNT1 never seems to get beyond 255 (DEC).

Not sure if the following will fix yr problem but its worth doing anyway:

Unless you are sure of the state of the timer registers when loop() is called, it's a good idea to set them explicitly. Assuming you just want normal counting mode with the prescaler set to 1:
TCCR1A = 0;
TCCR1B = (1 << CS10);

The code you posted sets CS10 but leaves the other registers in whatever state the arduino initializes Timer1, which may or may not be what you want.

Good luck

Thank you for your knowledge Mem, that corrected the problem.

I have something to add:

I'm working on the wiring board, but this can as easily be applied to arduino.

I've had to do some digging around tonight in the wiring source in wtimer.c or whatever in the SVN and I noticed a function called attachTimer, I bet this could help me, but basically I've set up an output compare timer to:

No prescaling, compare with 8 bit number, on compare reset, and toggle OC0 pin which is pin 14 of the uC, pin 28 of I/O.

Thats great, although it isn't the right frequency, that is a minor problem and can be adjusted exactly how I need it later.

My problem is that I want this OC0 to trigger an interrupt, and I know its either vector 12, 14, 16 or somewhere around there, it's the output match for timer0. I can always look it up.

But how would I write it in my code? I've tried something like ISE() and SIGNAL(SIG_TIMER0_COMP) and things like that, but then it tells me the vector is overdefined or something like that.

The original code that I am trying to emulate compiles and programs this (another IDE, and possibly another compiler, I dont know):
INTERRUPT(_VECTOR(14)) // called at 8 KHz
{
code
}

And the timer control code is:
TCCR0A = 0x02;
TCCR0B = 0x02; // clkIO/8, so 1/8 MHz
OCR0A = 62;//125; // 8 KHz

TIMSK0 = 0x02;

I know its a bit different (Ithink) on the ATMEGA128, but I can sort that out, I just need my interrupt code to work. Do I need to sei to set interrupts? I set the I bit in SREG.

Thank you, whoever has the courage to respond :wink:

Assuming you are using winavr as your development environment, I suggest declaring your vector as SIGNAL(SIG_TIMER0_COMP). If the compiler is saying that it is already defined then something else is using the vector you want. I note that you are using something other then the Arduino so I can't comment on what that could be but there are a few things you can try.

  1. use another timer that does not have an interrupt vector defined.
  2. add your code to the existing vector for your timer. Be careful that you don't upset the existing functionality.
  3. Remove (Comment out) the existing vecter code and replace it with yours
  4. consider using polling if appropriate rather than an interrupt.

I hope one of the above works for you.