Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

code speed optimization

code speed optimization

2007-12-26 by ahmed nabil

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
??

My question: How can i do that ?? is there are programs do that optimization
or time calculation??

thanks in advance

-- 
A H M E D   N A B I L


[Non-text portions of this message have been removed]

Re: [AVR-Chat] code speed optimization

2007-12-27 by Philippe Habib

One way that I do it is to have the routine set an I/O port on the  
way in and turn it off on the way out.  Then I attach an oscilloscope  
to the pin I'm using and measure the amount of time the pin stays  
high and that's the duration of my routine.

As with nearly anything else, there are programs that claim to do all  
sorts of things but none of them are any substitute for understanding  
what's going on.
Show quoted textHide quoted text
On Dec 26, 2007, at 7:06 AM, ahmed nabil 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
> ??
>
> My question: How can i do that ?? is there are programs do that  
> optimization
> or time calculation??
>
> thanks in advance
>
> -- 
> A H M E D   N A B I L
>
>
> [Non-text portions of this message have been removed]
>
>
>
>
> Yahoo! Groups Links
>
>
>
>

Re: [AVR-Chat] code speed optimization

2007-12-27 by David Kelly

On Dec 26, 2007, at 9:06 AM, ahmed nabil 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.

Not as important as you make it sound.

The most important instance I can think of has to do with sequentially  
toggling I/O bits to control an external device such as a text LCD  
which has a relatively slow Hitachi controller. One needs to be sure  
there is enough setup time before the next bits are written. This can  
be a problem with a 20 MHz AVR as the Hitachi is designed for a 1 MHz  
MC6800 bus.

Also important when one bit-bangs an SPI interface.

Other than that its handy to be able to profile interrupt routines. To  
know how long and how often they are running. As others have already  
stated, use a spare (or borrow temporarily) an I/O bit and toggle it  
going in and out. Many years ago companies made a big deal of software  
tools to profile code, and of hardware ICE with trace buffers to  
profile code usage. Not pushed as hard today because they usually were  
not worth the cost.

Have recommended many times here that one visually inspect generated  
code. Am often impressed at how avr-gcc recognized what it was being  
asked to do and reduced it to minimal code. But sometimes surprised at  
how much code was needed for other tasks. I *shouldn't* be surprised  
when applying a 32 bit mask to clear bits, then oring bits back into  
the original. Must be spoiled.

--
David Kelly N4HHE, dkelly@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.

Re: [AVR-Chat] code speed optimization

2007-12-27 by Ralph Hilton

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.net

Re: code speed optimization

2007-12-29 by Bruno

Hi.
I good approach for it is to calculate the MCU cycles. 
Doesn't matter if it is on C, to compile it, your compiler will 
create a file with your program in assembly.

In the book Excel by Example, it teaches you how to create a sheat 
to calculate the MCU cycles based on assembly file.
This will be more acurate than using an Osciloscope, but not easier.

Bruno

--- In AVR-Chat@yahoogroups.com, "ahmed nabil" 
<a.nabil.mohamednadim@...> 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
> ??
> 
> My question: How can i do that ?? is there are programs do that 
optimization
Show quoted textHide quoted text
> or time calculation??
> 
> thanks in advance
> 
> -- 
> A H M E D   N A B I L
> 
> 
> [Non-text portions of this message have been removed]
>

Re: [AVR-Chat] code speed optimization

2007-12-29 by Reza

1st of all, when you say something like "speed
optimization" you may have some of these problems:

1- you didnt analysed your application properly.
2- bcz you didnt do such application before, you may
afraid of program execution speed or something.

before any thing let me to say designing a
microcontroller based system is not a cheap thing. but
very important even for hubby. you have to analyse all
software and hardware requirements before selecting
microcontroller family to use, language to write code,
speed of microcontroller, etc.

for example:

assume we want to design a microcontroller based
system for some reasons, which:

must sample a series of incoming data rated at 20KHz
and apears ABOUT every 100 ms (not exactly 100ms!).

so we may use code to monitor incoming signals,
grabbing them, and save them. but this way may works
for this problem in application. and waste all micro
processing power. but if we should capture 10 bytes of
data by using an interrupt source and a simple
assembly language code, we only may waste 5% of cpu
power using a 8MHz clock freq.

or another sample:

assume we have a serial interface which some packets
comes. we must grab incoming packet, analyze it and
send desired reply to incoming packets. commonly most
of people use a simple interrupt driven routine to
grab data and queue it into micro controller memory,
then process it in a cyclic fashion. but for a high
rate of packets this method will not work perfectly.
in such situations i suggest to use a state machine
instead of simply grabbing data from UART.

as a result of my experiments, the codevision compiler
is not a good C compiler. i prefer GCC for high level
logics, not all logics. 1st i broke the entire logic
into two groups, critical ones which need to be very
fast, which must be written in assembly and slow
logics but complex ones, which MAY be written in C
language. for example i wrote a software on a ATmega64
microcontroller works at 4MHz which deals with
following resources:

1- sampling 14 bytes of data by a rete of 20KHz at
every about 150ms periods.
2- a fast UART based state machine which must response
to a host system, in less than 3ms after every packets
comes.
3- refreshing a LCD.
4- controlling a hardware with a 10ms response time.

after implementing the application, only 10-20% of CPU
power wasted for operating, and other times, CPU stay
in sleep mode to reduce power needs.

a simple note, but very important:

in implementing a design, when you felt reaching to
80% of any resources, (memory, CPU power, code size,
fpga cell used, amount of signals, etc...) it may be a
good time for thinking of some reasons to find why you
reach to this limit. 20% of any resources must left
free for enhancement and maintaining the design, not
to solve basic problems.

good luck;



      ____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

Re: [AVR-Chat] Re: code speed optimization

2007-12-29 by David VanHorn

> In the book Excel by Example, it teaches you how to create a sheat
> to calculate the MCU cycles based on assembly file.
> This will be more acurate than using an Osciloscope, but not easier.

I agree in principle, but in practice, with non-trivial routines, it
becomes unwieldy to calculate every possibility. Much easier to scope
it, throw real data in, and measure min and max.  Besides, simulated
results are just that. Simulated.  You might miss a path, but the
running code won't.

Re: [AVR-Chat] Re: code speed optimization

2007-12-30 by Roy E. Burrage

Nobody uses a frequency counter any more.

Why not set a multifunction counter up for a start/stop period 
measurement...then if you can talk to it over any interface bus, let 
that puppy, the microcontroller circuit, run and log the time periods to 
run the routine.  After a while, take an average.  If you have an el 
spiffo counter, you can program that to do the averaging too.


REB



David VanHorn wrote:

>>In the book Excel by Example, it teaches you how to create a sheat
>>to calculate the MCU cycles based on assembly file.
>>This will be more acurate than using an Osciloscope, but not easier.
>>    
>>
>
>I agree in principle, but in practice, with non-trivial routines, it
>becomes unwieldy to calculate every possibility. Much easier to scope
>it, throw real data in, and measure min and max.  Besides, simulated
>results are just that. Simulated.  You might miss a path, but the
>running code won't.
>  
>


[Non-text portions of this message have been removed]

AvrStudio 3.56 will not install

2007-12-31 by Owen A

Hi,
       I am trying to reinstall AvrStudio Ver 3.56, I need this version installed to handle the old style coff file from a C compiler for the 90s1200 / tiny11.
  I get the following error REGDBCREATEKEYEX FAILED and the install drops out.
  I have tried editing the registry so far to no avail,
  Any help with this would be appreciated.
   
  Thanking You,
  Owen.



       
---------------------------------
Make the switch to the world's best email. Get the new Yahoo!7 Mail now.

[Non-text portions of this message have been removed]

Re: [AVR-Chat] AvrStudio 3.56 will not install

2007-12-31 by Roy E. Burrage

What's your operating system Owen?

Did you have 3.56 installed before?  How did you uninstall?


REB


Owen A wrote:
Show quoted textHide quoted text
>Hi,
>       I am trying to reinstall AvrStudio Ver 3.56, I need this version installed to handle the old style coff file from a C compiler for the 90s1200 / tiny11.
>  I get the following error REGDBCREATEKEYEX FAILED and the install drops out.
>  I have tried editing the registry so far to no avail,
>  Any help with this would be appreciated.
>   
>  Thanking You,
>  Owen.
>
>
>  
>

Re: [AVR-Chat] AvrStudio 3.56 will not install

2007-12-31 by Owen A

Hi Roy,
            My OS is windows xp sp2,
  I had ver3.53 installed it was playing up so uninstall it with control panel / add romove programs now cannot install either ver3.56 or ver3.53.
   
  Thanking you,
  Owen.

  Owen Allison.
  
"Roy E. Burrage" <RBurrage@bellsouth.net> wrote:
          What's your operating system Owen?

Did you have 3.56 installed before? How did you uninstall?

REB

Owen A wrote:

>Hi,
> I am trying to reinstall AvrStudio Ver 3.56, I need this version installed to handle the old style coff file from a C compiler for the 90s1200 / tiny11.
> I get the following error REGDBCREATEKEYEX FAILED and the install drops out.
> I have tried editing the registry so far to no avail,
> Any help with this would be appreciated.
> 
> Thanking You,
> Owen.
>
>
> 
>


                         

       
---------------------------------
Make the switch to the world's best email. Get the new Yahoo!7 Mail now.

[Non-text portions of this message have been removed]

Re: [AVR-Chat] AvrStudio 3.56 will not install

2007-12-31 by Roy E. Burrage

If you've installed ver 4.xx, you might have to go into your registry 
files and delete everything to do with the older version.  Some files 
are called in very unusual places all over the registry.  That's a real 
pain in the tush, and other vital parts of one's anatomy.

You might be better off trying to install on another computer, perhaps 
one running Win98SE.  Is that possible?  We keep an old computer here 
just for that purpose when necessary.

The alternative would be to just re-write the code for updated 
software.  A pain, but sometimes the lesser of 2 evils when we consider 
the time involved.

Do you have the assembly level files?  Can you disassemble those?  I 
don't write code in C, nor have I ever needed to use a disassembler, but 
isn't that what those are for?  If you have the assembly files, the 
S1200 has 1K of program memory so it wouldn't be that big a deal to 
write the code all over again anyway.  Would it?

Life can sometimes be hell if we do updates.  I lost about 1200 dollars 
(US) worth of e-books with an "upgrade" a few years ago...and then got 
then to add insult to injury, charged 50 bucks more to be told there was 
nothing they could do to help.  Some of the books were no longer available.


REB


Owen A wrote:
Show quoted textHide quoted text
> 
>  Hi Roy,
>            My OS is windows xp sp2,
>  I had ver3.53 installed it was playing up so uninstall it with control panel / add romove programs now cannot install either ver3.56 or ver3.53.
>   
>  Thanking you,
>  Owen.
>
>  Owen Allison.
>  
>"Roy E. Burrage" <RBurrage@bellsouth.net> wrote:
>          What's your operating system Owen?
>
>Did you have 3.56 installed before? How did you uninstall?
>
>REB
>
>Owen A wrote:
>
>  
>
>>Hi,
>>I am trying to reinstall AvrStudio Ver 3.56, I need this version installed to handle the old style coff file from a C compiler for the 90s1200 / tiny11.
>>I get the following error REGDBCREATEKEYEX FAILED and the install drops out.
>>I have tried editing the registry so far to no avail,
>>Any help with this would be appreciated.
>>
>>Thanking You,
>>Owen.
>>    
>>

Re: [AVR-Chat] AvrStudio 3.56 will not install

2007-12-31 by Owen A

Hi Roy,
             Yes I went into the registry and deleted everything called Atmel, AvrStudio, & Avr Studio but to no avail, there must be something I missed but cannot think of what it could be, even deleted a couple of entries that I found out later pointed the C compilers to Avr Studio by mistake and had to reinstall the C compilers.
  Thank you for your help,
  Owen.

"Roy E. Burrage" <RBurrage@bellsouth.net> wrote:
          If you've installed ver 4.xx, you might have to go into your registry 
files and delete everything to do with the older version. Some files 
are called in very unusual places all over the registry. That's a real 
pain in the tush, and other vital parts of one's anatomy.

You might be better off trying to install on another computer, perhaps 
one running Win98SE. Is that possible? We keep an old computer here 
just for that purpose when necessary.

The alternative would be to just re-write the code for updated 
software. A pain, but sometimes the lesser of 2 evils when we consider 
the time involved.

Do you have the assembly level files? Can you disassemble those? I 
don't write code in C, nor have I ever needed to use a disassembler, but 
isn't that what those are for? If you have the assembly files, the 
S1200 has 1K of program memory so it wouldn't be that big a deal to 
write the code all over again anyway. Would it?

Life can sometimes be hell if we do updates. I lost about 1200 dollars 
(US) worth of e-books with an "upgrade" a few years ago...and then got 
then to add insult to injury, charged 50 bucks more to be told there was 
nothing they could do to help. Some of the books were no longer available.

REB

Owen A wrote:

> 
> Hi Roy,
> My OS is windows xp sp2,
> I had ver3.53 installed it was playing up so uninstall it with control panel / add romove programs now cannot install either ver3.56 or ver3.53.
> 
> Thanking you,
> Owen.
>
> Owen Allison.
> 
>"Roy E. Burrage" <RBurrage@bellsouth.net> wrote:
> What's your operating system Owen?
>
>Did you have 3.56 installed before? How did you uninstall?
>
>REB
>
>Owen A wrote:
>
> 
>
>>Hi,
>>I am trying to reinstall AvrStudio Ver 3.56, I need this version installed to handle the old style coff file from a C compiler for the 90s1200 / tiny11.
>>I get the following error REGDBCREATEKEYEX FAILED and the install drops out.
>>I have tried editing the registry so far to no avail,
>>Any help with this would be appreciated.
>>
>>Thanking You,
>>Owen.
>> 
>>


                         

       
---------------------------------
Make the switch to the world's best email. Get the new Yahoo!7 Mail now.

[Non-text portions of this message have been removed]

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.