Time usage in a uC

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

Hi,

I am trying to measure elapse time on some of my functions.

		i = 1023;
		do
		{			
			safe_mem[i] = uartRf.RxBuf[i];
		}
		while(--i > 0);

What should I expect the elapsed time to be with a 7.3728Mhz external crystal and the optimizer set to -O3?

My measure is 1.8mS is that to much.
Is there a way of speed up this prosedure?

Leif

Br Leif G

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

You can use the AVR Studio simulator to count cycles. On your CPU one cycle is 135.6ns.

I would have built your code to get a ball park idea but you haven't shown the types for any of the variables so it would only be guesswork. I suppose, because a UART rxbuff is involved that's going to be uint8_t?

EDIT: OK with some guess work I built this (I built it for mega1284 in fact as so much RAM is involved) then I ran it in the simulator. It took 26,643 cycles which at your F_CPU is 3613.9us - 3.61ms

#include 

uint8_t safe_mem[1024];
struct {
  uint8_t RxBuf[1024];
} uartRf;

int main(void) {
	  uint16_t i;
      i = 1023;
      do
      {         
         safe_mem[i] = uartRf.RxBuf[i];
      }
      while(--i > 0); 
}

If you are getting 1.8ms you have done very well - I guess my types guess was wrong?!?

BTW in my case the generated code was:

int main(void) {
  ce:	2f ef       	ldi	r18, 0xFF	; 255
  d0:	33 e0       	ldi	r19, 0x03	; 3
	  uint16_t i;
      i = 1023;
      do
      {         
         safe_mem[i] = uartRf.RxBuf[i];
  d2:	f9 01       	movw	r30, r18
  d4:	e0 50       	subi	r30, 0x00	; 0
  d6:	ff 4f       	sbci	r31, 0xFF	; 255
  d8:	d9 01       	movw	r26, r18
  da:	a0 50       	subi	r26, 0x00	; 0
  dc:	bb 4f       	sbci	r27, 0xFB	; 251
  de:	8c 91       	ld	r24, X
  e0:	80 83       	st	Z, r24
      }
      while(--i > 0); 
  e2:	21 50       	subi	r18, 0x01	; 1
  e4:	30 40       	sbci	r19, 0x00	; 0
  e6:	a9 f7       	brne	.-22     	; 0xd2 
}

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

I am sorry to not define the type I used but as you figured out it is a uint8_t. I am testing it on my hardware which contain a xmega256A3. by using the standard way by led flash and a scop I get 1.8ms. But I also tried the memcpy and that one was faster approx 1.2ms. For the moment I am satisfied. Thanks for help and testing. This is absolutely a good forum.

Br Leif G

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

Quote:

by using the standard way by led flash and a scop I get 1.8ms

I'd be interested to see the Asm generated. Wonder why it's twice as quick as what I got?
Quote:

But I also tried the memcpy

Yup, hand crafted Asm is almost bound to give the best result.