Yahoo Groups archive

AVR-Chat

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

Message

RE: [AVR-Chat] Help with bit banged spi

2013-11-26 by Larry Viesse

I agree with David on the points he raised.  It would be very helpful if
Philippe could give us a little more detail as to how he has implemented his
bit banged SPI port.  Knowing what P4 and SPIOutBuf are used for and how
they are defined.

 

I once used this code to add an SPI port to an 8051
http://www.maximintegrated.com/app-notes/index.mvp/id/3524 .  Read the
application note carefully, especially the last sentence in the first
paragraph following the abstract.  It has been a while, but I believe that
it was quite fast.

 

Larry Viesse W7PAN

  

 

From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On Behalf
Of David Kelly
Sent: Tuesday, November 26, 2013 7:32 AM
To: AVR-Chat@yahoogroups.com
Subject: Re: [AVR-Chat] Help with bit banged spi

 

  


On Nov 26, 2013, at 4:46 AM, Cagri Tanriover <dr_cagri_tanriover@yahoo.co.uk
<mailto:dr_cagri_tanriover@yahoo.co.uk> > wrote:

> P4 = ( *(SPIOutBuf + j) & 0x80 ? P4 | 1 : 0 );

I suggest looking at the compiler output listing in assembly language.

Philippe is using an 8051 variant so I doubt he is using gcc, but in my
observation with optimization enabled gcc does better with SPIOutBuf[j] than
with *(SPIOutBuf + j). Other compilers appreciate having the pointers broken
out manually, namely classic 68k compilers under Unix.

8051's have single-bit types and perhaps P4 has been defined as an output
bit. If so AND-ing and OR-ing is useless. Hopefully the compiler optimized
them out.

While we are at it this doesn't look right: P4 &= 0; That clears all bits in
P4. How about P4 &= ~1; which is 0b1111 1110 and only clears the zeroth bit
and is exactly as fast at run time? Of course that only matters if P4 is
larger than 1 bit wide. But if P4 is a bit variable then use P4 = 1; and P4
= 0;

I think it might be best to unroll the bit shifting. This also has the
possible advantage of not destroying the original buffer:

for( j=0 ; j<Num ; j++ ) {
Tmp = SPIOutBuf[j];

if( Tmp & (1<<7) )
P4 |= 1; 
else
P4 &= ~1;
SPICLK = 1;
PICLK = 0;

if( Tmp & (1<<6) )
P4 |= 1; 
else
P4 &= ~1;
SPICLK = 1;
PICLK = 0;

if( Tmp & (1<<5) )
P4 |= 1; 
else
P4 &= ~1;
SPICLK = 1;
PICLK = 0;

if( Tmp & (1<<4) )
P4 |= 1; 
else
P4 &= ~1;
SPICLK = 1;
PICLK = 0;

if( Tmp & (1<<3) )
P4 |= 1; 
else
P4 &= ~1;
SPICLK = 1;
PICLK = 0;

if( Tmp & (1<<2) )
P4 |= 1; 
else
P4 &= ~1;
SPICLK = 1;
PICLK = 0;

if( Tmp & (1<<1) )
P4 |= 1; 
else
P4 &= ~1;
SPICLK = 1;
PICLK = 0;

if( Tmp & (1<<0) )
P4 |= 1; 
else
P4 &= ~1;
SPICLK = 1;
PICLK = 0;
}

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

Attachments

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.