Yahoo Groups archive

AVR-Chat

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

Message

Re: [AVR-Chat] Bit-bang SPI

2007-05-31 by Ralph Hilton

On Wed, 30 May 2007 16:36:56 -0400 you wrote:

>Can anyone point me to a bit-bang SPI example? I'm trying to talk to one of
>these:
>http://www.parallax.com/detail.asp?product_id=29123
>
>Perhaps someone has an equivalent to SHIFTOUT and SHIFTIN in C?
>
>Thanks,
>Brian Gregory

Here's code for bit banging an ADC chip showing how a shift in and out can be
done:

#define LTCSDI PORTD.2  
#define LTCCS PORTD.3
#define LTCSDO PIND.4
#define LTCSCK PORTD.5

char i;
bit inbit;
bit msb;
bit datasign;
bit adcflag; 
flash char LTCspeeds[10]={16,8,24,4,20,12,28,2,18,30};  //stored in reverse
order for easy out with >>
char speed=3, speedout;
char outbyte1, outbyte2, outbyte3, outbyte4;
unsigned long int rawdataval, mcdataval;
unsigned long int lastmcdataval; 

void readbit(void)
{     
LTCSCK=1;   
delay_us(2);
inbit=LTCSDO;
delay_us(2);
LTCSCK=0;   
}

void setsdi(void)
{
LTCSDI=speedout&1;   
speedout>>=1;
}

void main(void)
{
sleep_enable(); 
LTCCS=1; //start conversion
idle(); //take a nap until timer 0 interrupt
while (1)
{
if (adcflag)
{      
    
    speedout=LTCspeeds[speed];
    rawdataval=0; 
    LTCCS=0;
    while (LTCSDO==1);
    setsdi();
    readbit(); //EOC   
    setsdi();
    readbit(); //dummy
    setsdi();
    readbit(); //sign
    datasign=inbit;
    setsdi();
    readbit(); //msb
    msb=inbit;
    setsdi();
    for (i=0;i<23;i++)
    { 
       readbit();
       rawdataval<<=1;
       rawdataval+=inbit;
    }
    LTCCS=1; //start next conversion 
--
Ralph Hilton
http://www.ralphhilton.org
C-Meter: http://www.cmeter.org
FZAOINT http://www.fzaoint.net

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.