This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Software spi c code for msp430f5510

Other Parts Discussed in Thread: MSP430F5510

i am a beginner i am facing problem while using hardware spi. i need a software spi code for micro controller . please......... help me to get my project run properly,

  • Biplab,


    why not trying to get rid of the hardware issues? There are code examples showing the use of the hardware SPI module.

    Anyway - a software SPI is like this:

    #define DATA_OUT_PORT    P1OUT
    #define DATA_OUT_PIN     0x01
    
    #define CLOCK_OUT_PORT   P1OUT
    #define CLOCK_OUT_PIN    0x02
    
    void bitbang_data_out( uint8_t data );
    
    void main( void )
    {
      ...
    
      CLOCK_OUT_PORT &= ~CLOCK_OUT_PIN;
    
      ...
    
      bitbang_data_out( 0x21 );
    
      ...
    }
    
    void bitbang_data_out( uint8_t data )
    {
      uint8_t i;
    
      for( i = 0; i < 8; i++ )
      {
        if( data & 0x80 )
        {
          DATA_OUT_PORT |= DATA_OUT_PIN;
        }
        else
        {
          DATA_OUT_PORT &= ~DATA_OUT_PIN;
        }
    
        CLOCK_OUT_PORT |=  CLOCK_OUT_PIN;
        CLOCK_OUT_PORT &= ~CLOCK_OUT_PIN;
    
        data <<= 1;
      }
    }

    Dennis

  • could u please explain ur code......? how it implies a software spi.......?
  • Try yourself first.
    What happens in the posted lines?
  • by the way ........... thank u so much for the solution........
  • No problem. But try to understand what happens there.

    Dennis
  • i hv gone thrugh ur code i encounter with the following difficulties...
    how do i come to know that spi is busy sending data??
    one more thing how do i intialize the spi clock?
  • i want to transmit 32 bit serial data in the form of byte. simultaneously i need to know the status of data sending, whether the data is sent or not. so that i want to give another 32 bit data for sending in output.
  • If you use the few lines I wrote for bitbanging the data out, then you don't have to think about the state of your SPI "bus". It is during the normal program flow, so it is busy as long as your program is currently executing this function. And you cannot adjust the clock speed in that case. It is the execution speed of the instructions. You could insert delays, but that would be the worst. The bitbang method isn't ideal when you could use an interrupt driven method instead. But for a few bytes it might be OK. If you want to send data to a graphic display that has hundreds or thousands of bytes, then you wouldn't be able do much else with your processor since it is busy with sending your data all the time.

    You can easily send 32 bits of data, too. You can either split it into 4 bytes or you could adjust the function for that.

    void bitbang_data_out( uint32_t data )
    {
      uint8_t i;
    
      for( i = 0; i < 32; i++ )
      {
        if( data & 0x80000000 )
        {
          DATA_OUT_PORT |= DATA_OUT_PIN;
        }
        else
        {
          DATA_OUT_PORT &= ~DATA_OUT_PIN;
        }
    
        CLOCK_OUT_PORT |=  CLOCK_OUT_PIN;
        CLOCK_OUT_PORT &= ~CLOCK_OUT_PIN;
    
        data <<= 1;
      }
    }

    Dennis

  • i want to interface this spi with 74HC595 shift register ic. thus i required a synchronous clock having the same freq with which data is transmitted.  how do i configure this with help of default clock of msp430f5510??

  • The users guide chapter about the clock system tells you how to setup a system clock (like SMCLK). The USCI chapter tells you abut the USCI module configuration.
    The USCI module takes a clock signal from the clock system (ACLK, SMCLK) and divides it by a programmable factor. This will be the bit clock for the transmission. In case of SPI, this clock signal is sent out when the USCI has to send something. It's not a continuous clock output. In fact, SPI is a synchronous bus. That means: one clock pulse, one bit sent and one bit received.
    If there are no bits to send, no clock pulses are emitted too. And nothing is received.
    For this reason. the clock signal will always have the exact 'frequency' as the data output, because the two are directly coupled together.

    The USCI will not detect whether there is a slave present at all. If there is no slave, you if you didn't select the slave, the USCI will be sending into the void and receiving '1' from the idle input line. You can't tell.

    For selecting the slave, you will have to use a normal digital I/O line. One line per slave. When done with you transfer (check the UCBUSY bit whether you're really done), you can de-select the slave again. Maybe with a short additional delay.
    If you only have one slave, you might be able to so without the chip select. But be aware that the chip select is also used for initial byte-border synchronization. After all, SPI is a bit stream bus with any number of bits during a transmission (y multiple of 8 bits is usual, but not mandatory), and without the chip select for indicating the start of a transmission, master and slave could have a different meaning about the number of bits already sent (e.g. due to glitches during port pin initialization).

**Attention** This is a public forum