From: David Kelly <[EMAIL PROTECTED]>
[...]
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
_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.org
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list