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

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

 From: David Kelly Subject: Re: [avr-gcc-list] AVR assembly for fast bit bang Date: Tue, 8 Nov 2005 19:07:45 -0600

```On Tue, Nov 08, 2005 at 03:45:54PM +0000, Mike S. wrote:
```
```Hello to all,
Can anyone tell me the best (faster) way to implement bit shifting
(serial synch protocol -in a bit bang fashion-) with two general
purpose digital pins (one pin for data the other for clock)? Using C
is not fast enough! I need assembly!
```
```
Sounds like a homework assignment. Smart instructors monitor this list.

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

Compiled with
"avr-gcc -O -gdwarf-2 -Wall -ffreestanding -mmcu=atmega64 -c junk.c"

Output of "avr-objdump -S junk.o":

myjunk(uint8_t byte) {
uint8_t i;

for( i = 0 ; i < 8 ; i++ ) {
0:   90 e0           ldi     r25, 0x00       ; 0
PORTA |= CLOCK_B;       //  rising edge of clock
2:   d8 9a           sbi     0x1b, 0 ; 27
if( byte & (1<<7) )
4:   88 23           and     r24, r24
6:   14 f4           brge    .+4             ; 0xc <myjunk+0xc>
PORTA |= BIT_B;         // set
8:   d9 9a           sbi     0x1b, 1 ; 27
a:   01 c0           rjmp    .+2             ; 0xe <myjunk+0xe>
else
PORTA &= ~BIT_B;        // clear
c:   d9 98           cbi     0x1b, 1 ; 27
byte <<= 1;
e:   88 0f           add     r24, r24
PORTA &= ~CLOCK_B;      //  falling edge of clock
10:   d8 98           cbi     0x1b, 0 ; 27
12:   9f 5f           subi    r25, 0xFF       ; 255
14:   98 30           cpi     r25, 0x08       ; 8
16:   a8 f3           brcs    .-22            ; 0x2 <myjunk+0x2>
18:   08 95           ret

Its not going to get much better than that.

--