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: Dave Hansen
Subject: Re: [avr-gcc-list] AVR assembly for fast bit bang
Date: Wed, 09 Nov 2005 09:00:58 -0500

From: David Kelly <address@hidden>
[...]
People keep saying "C isn't fast enough." I don't belive it. First
attempt:

#include <avr/io.h>

#define CLOCK_B (1<<0)
#define BIT_B   (1<<1)

void
myjunk(uint8_t byte) {
        uint8_t i;

        for( i = 0 ; i < 8 ; i++ ) {
                PORTA |= CLOCK_B;       //  rising edge of clock
                if( byte & (1<<7) )
                        PORTA |= BIT_B;         // set
                else
                        PORTA &= ~BIT_B;        // clear
                byte <<= 1;
                PORTA &= ~CLOCK_B;      //  falling edge of clock
        }
}

[...snip assembly...]

It might be tough to do better on AVR. My standard SPI routine uses a do-while loop, which might save an instruction or two, but made about a 30% difference on the PIC compiler I used. Something like

  void output_spi_byte(uint8_t byte)
  {
     uint8_t bit_ctr = 8;

     do
     {
        output_low(SPI_DATA);   // Set data bit low...
        if (byte & 0x80)
           output_high(SPI_DATA);   // ...or high, as required

        output_high(SPI_CLK);
        byte <<= 1;         // Shift acts as clock dwell delay
        output_low(SPI_CLK);

     } while (--bit_ctr);
  }

I don't have avr-gcc handy to see if it's any better than your code. I was more concerned with size than speed, as you may be able to tell. If speed is the ultimate object, unrolling the loop will help.

Regards,
  -=Dave






reply via email to

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