avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] AVR assembly for fast bit bang


From: Peter Fuhrmann
Subject: Re: [avr-gcc-list] AVR assembly for fast bit bang
Date: Wed, 9 Nov 2005 16:31:44 +0100
User-agent: KMail/1.7.2

> void write_data (Word towrite, Byte nbits)
> {
>   Byte n;
>
>   for(n = 0; n < nbits; n++)
>   {
>
>     CLK_HIGH;
>     if( towrite & (0x0001 << n))
>     {
>       SDIO_HIGH;
>     }
>     else
>     {
>       SDIO_LOW;
>     }
>     CLK_LOW;
>
>   }
> }

This will give very slow code, because a left shift by a variable will allways 
take lots of cycles. Even with optimisation on this will give slow code. This 
should work better:

void write_data (Word towrite, Byte nbits)
{
  Byte n;
  for(n = 0; n < nbits; n++)
  {
    CLK_HIGH;
    if( towrite & 0x001)
    {
      SDIO_HIGH;
    }
    else
    {
      SDIO_LOW;
    }
    CLK_LOW;
    towrite >>= 1;
  }
}

Now always one shift is done with each cycle of the loop.

You should look at the dissassembler listing when you get this sort of 
problem. Then you can see, what the compiler does, and optimize your C code, 
so the compiler can generate better assembly for you.

I also suggest, you allways turn optimization on! This works very well with 
gcc, I always use -Os, and haven't had any broken code problems so far.

Regards,

Peter




reply via email to

[Prev in Thread] Current Thread [Next in Thread]