How to simulate timer?

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is there a way to simulate the timers to measure the time also?
Thanks in advance.

My (HW && SW) Setup: (MyAVR USB Programmer | bread-board | Butterfly | Arduino 10k | ATtiny2313 | ATmega8) && (WinAVR | AVR asm | AVRstudio)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Yes.

Of course, digital answer :)

My (HW && SW) Setup: (MyAVR USB Programmer | bread-board | Butterfly | Arduino 10k | ATtiny2313 | ATmega8) && (WinAVR | AVR asm | AVRstudio)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I thought my answer had about as much detail as the question in fact ;-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are right :)

Would you be so kind to give me a hint how I can measure times generated by mean of timers, in AVRstudio? ;)

My (HW && SW) Setup: (MyAVR USB Programmer | bread-board | Butterfly | Arduino 10k | ATtiny2313 | ATmega8) && (WinAVR | AVR asm | AVRstudio)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That's still too general a question. However here's an example:

#include  
#include 

int main(void) {
	DDRB = 0xFF;
	TCCR0A = (1<<WGM01); // CTC mode
	OCR0A = 217; // some random value
	TIMSK0 = (1<<OCIE0A); // enable the A compare interrupt
	TCCR0B = (1<<CS00); // start timer with /1 prescale
	sei();
	while(1) {
	}
}

ISR(TIMER0_COMPA_vect) {
	PORTB ^= 0xFF;
}

Which (for 168) starts timer 0 in CTC mode and provides an ISR() to catch the event. If I now start that code in the simulator and put a breakpoint on the PORTB ^= 0xFF then on the Debug menu I use "AVR simulator options" and set the frequency of my simulated chip to 3.6864MHz (say) I can then run the code until it hits the breakpoint the first time.

The processor window tells me that it hit the BP after 258 cycles from power on. If I now rightclick the stopwatch field in the processor window one of the options is "Reset cycle counter". Having done that the stopwatch shows 0.00us. I now run again and when it hits the BP for the second time the cycle counter shows 218 and this is calculated to be 59.14us - so that's the period of my timer. If I run again the next stop is at 436 cycles, 118.27us (and 118.27 - 59.14 is 59.13)

So I know my timer is somewhere between 59.13us and 59.14us

I got to this result without calculation but if I put the figures back into avrcalc it tells me 59.13529

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
So I know my timer is somewhere between 59.13us and 59.14us

Nice!

I used a 4MHz clock instead and measured a stop watch of 54.50us.

According the formula:

T = (N+1) * (1/fc) =
= 218 * .25us = 54.50us

Thanks again.

P.S. Which avrcalc do you use? I found two software with the same name.

My (HW && SW) Setup: (MyAVR USB Programmer | bread-board | Butterfly | Arduino 10k | ATtiny2313 | ATmega8) && (WinAVR | AVR asm | AVRstudio)