On Wed, 26 Dec 2007 17:06:23 +0200 you ("ahmed nabil"
<a.nabil.mohamednadim@gmail.com>) wrote:
>hi ALL,
>i have a basic knowledge that optimizing my code time or speed is a very
>important matter
>when writing a professional code. Using languages like "C" in codevision for
>example, makes it
>harder to know the execution time of my instructions, i know the matter also
>depends on the
>clock that the MCU works on but i mean how many cycles will the program take
>??
You can use a timer to increment a count and then create a loop that runs the
code a number of times.
Here's an example:
#include <mega32.h>
#include <stdio.h> // Standard Input/Output functions
//#include <stdlib.h> //for rand
#include <delay.h>
register unsigned char i;
unsigned int tcount, endcount;
float timeout;
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Reinitialize Timer 0 value
TCNT0=0xF8; //7200Hz
tcount++;
}
void main(void)
{
PORTA=0x00;
DDRA=0x00;
PORTB=0x00;
DDRB=0x00;
PORTC=0x00;
DDRC=0x00;
PORTD=0x00;
DDRD=0x08;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 57.600 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x04;
TCNT0=0xF8;
OCR0=0x00;
// External Interrupt(s) initialization
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;
// USART initialization
// USART Baud rate: 115200
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x07;
// Global enable interrupts
#asm("sei")
while (1)
{
tcount=0;
for (i=0;i<250;i++)
{
//insert code here
}
endcount=tcount;
printf("\r \n");
printf("Endcount %u ", endcount);
printf("\r \n");
timeout=(float)endcount/7.2;
printf("Processing time %6.3f ms ", timeout);
printf("\r \n");
timeout=(float)endcount/1.8;
printf("Processing time / sample %6.3f us ", timeout);
printf("\r \n");
delay_ms(1000);
}
}
--
Ralph Hilton
http://www.ralphhilton.org
C-Meter: http://www.cmeter.org
FZAOINT http://www.fzaoint.netMessage
Re: [AVR-Chat] code speed optimization
2007-12-27 by Ralph Hilton
Attachments
- No local attachments were found for this message.