coding in C for SPI in microcontroller

Discussion in 'Embedded Systems and Microcontrollers' started by esha, Mar 14, 2010.

  1. esha

    Thread Starter New Member

    Mar 14, 2010
    6
    0
    I'm working for freescale s08 microcontroller and i want help for SPI initialization code in C:(plzz help me with the initialization code.
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    I'm not familiar with that model number from the Freescale line. Can you provide a link to a data sheet.

    Some questions to think about:

    • Do you want code for an SPI master or an SPI peripheral?
    • What datarate did you have in mind?
    • Which of the four clock and data formats will you be using?
    Once you know the answers to those questions the code is two or maybe three assignments statements.
     
  3. esha

    Thread Starter New Member

    Mar 14, 2010
    6
    0
  4. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    What is the speed of your bus clock, and what is the speed of the SPI?
     
  5. esha

    Thread Starter New Member

    Mar 14, 2010
    6
    0
    Bus clock is 20MHz and SPI runs at a baud rate up to the bus clock divided by two in master mode.
     
  6. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    OK -- that's fine but in order to write the initialization code you have to pick one of the possible baudrates. You can change your mind later if you want, but for the time being you need to pick one.
     
  7. esha

    Thread Starter New Member

    Mar 14, 2010
    6
    0
    SPI baud rate will be 10MHz
     
  8. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    Here are the bit definitions and the initialization code you requested.
     
  9. esha

    Thread Starter New Member

    Mar 14, 2010
    6
    0
    thnks :)how do i make chip select active low during data transmission?
     
  10. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    There are at least two ways. If your C-compiler supports bit operations you might write something like:
    Code ( (Unknown Language)):
    1.  
    2.     PORTA.3 = 0 ; // Take Chip Select Low
    3.     spi_Write(spiData[0]) ;  // Write two bytes of SPI data
    4.     spi_Write(spiData[1]) ;
    5.     PORTA.3 = 1 ; // Take chip select inactive
    6.  
    If your C-compiler does not support operations on individual bits with a READ-MODIFY-WRITE operation then you can synthesize such an operation with the following:
    Code ( (Unknown Language)):
    1.  
    2. #define SPI_CS  (1<<3)  // Define SPI Chip Select as bit 3 of PORTA
    3. ...
    4.     PORTA &= ~SPI_CS ;  // The 1's complement of (1<<3) == 0x10 is 0xF7
    5.     spi_Write(spiData[0]) ;
    6.     spi_Write(spiData[1]) ;
    7.     PORTA |= SPI_CS ;  // Take SPI Chip Select Inactive
    8.  
    Is that as clear as mud or what....?
     
  11. esha

    Thread Starter New Member

    Mar 14, 2010
    6
    0
    Sorry for the late reply..thanks a lot :)
     
Loading...