Searching \ for '[PIC] Software SPI' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/ios.htm?key=spi
Search entire site for: 'Software SPI'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] Software SPI'
2007\07\11@185444 by Andre Abelian

flavicon
face
Hi to all,

Sorry guys I forgot even say thank you all for my previews post.
I am having problem with software spi I put it together and I am
wondering if it looks ok. To my understanding it should work.

void write_umFPU_byte(BYTE data_byte, BYTE number_of_bits)
                        {
     BYTE i;
//    delay_us(10);
     for(i=0; i<number_of_bits; ++i)
                  {
     output_low(umFPU_CLK);                 //
     if((data_byte & 0x80)==0)              // MSB
        output_low(umFPU_DIN);              //
     else
        output_high(umFPU_DIN);             //
     data_byte=data_byte<<1;                //

     delay_us(30);
     output_high(umFPU_CLK);                //
     delay_us(30);
                   }
                       }

this is going to shift MSB first.
any idea ?
do you see any problem with the software ?


thanks
Andre

2007\07\11@191433 by Dario Greggio

face picon face
Andre Abelian wrote:
> do you see any problem with the software ?

apart from being extremely slow :) but this is probably how you wanted it...

and a max_no_of_bits of 8,

I'd change this one
>       data_byte=data_byte<<1;                //

to
>       data_byte <<= 1;                //

(clearer in this context, and surely better compiled..)


--
Ciao, Dario

2007\07\11@192937 by Harold Hallikainen

face
flavicon
face
In my most recent software SPI, I had a bitmask that I initialized at
0x80. I then anded it with the data byte, set the MOSI high or low as
required, then shifted the bitmask right one bit. This was looped until
the bitmask was shifted down to nothing. Something like this:

void SendByte(uint8_t data){
 uinit8_t BitMask=0x80
 while(BitMask){
   SCLK=0;
   if(BitMask & data)
     MOSI=1;
   else
     MOSI=0;
   SCLK=1;
   BitMask>>1;
 }
}

Harold


{Quote hidden}

> -

2007\07\12@032433 by Michael Rigby-Jones

picon face


{Quote hidden}

It will probably work ok, but a few comments:

1) Do you really need such slow timing, i.e. have you made it deliberately slow for EMI compliance, or do you have a very slow slave device?

2) Note that SPI devices work in one of four modes, which relate to the relationship between clock and data, i.e. the data can be clocked in on a rising or falling clock edge, and it can be a leading or trailing clock edge.  The PIC datasheets show the differences if you look under the MSSP peripheral.  The datasheet for your SPI peripheral should show a timing diagram from which which the appropriate mode can be worked out.

Just as a note on your loop, it's usualy better to decrement counters on a PIC as it often results in smaller and faster code e.g.

while( number_of_bits-- ) {
   /* send one bit */
}

This allows the compiler to use a desfsz instruction instead of having to increment the counter and then compare against a non-zero value.

Regards

Mike

=======================================================================
This e-mail is intended for the person it is addressed to only. The
information contained in it may be confidential and/or protected by
law. If you are not the intended recipient of this message, you must
not make any use of this information, or copy or show it to any
person. Please contact us immediately to tell us that you have
received this e-mail, and return the original to us. Any use,
forwarding, printing or copying of this message is strictly prohibited.
No part of this message can be considered a request for goods or
services.
=======================================================================

More... (looser matching)
- Last day of these posts
- In 2007 , 2008 only
- Today
- New search...