coding in C for SPI in microcontroller

Thread Starter

esha

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

Papabravo

Joined Feb 24, 2006
21,225
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.
 

Papabravo

Joined Feb 24, 2006
21,225
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.
 

Papabravo

Joined Feb 24, 2006
21,225
There are at least two ways. If your C-compiler supports bit operations you might write something like:
Rich (BB code):
    PORTA.3 = 0 ; // Take Chip Select Low
    spi_Write(spiData[0]) ;  // Write two bytes of SPI data
    spi_Write(spiData[1]) ;
    PORTA.3 = 1 ; // Take chip select inactive
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:
Rich (BB code):
#define SPI_CS  (1<<3)  // Define SPI Chip Select as bit 3 of PORTA
...
    PORTA &= ~SPI_CS ;  // The 1's complement of (1<<3) == 0x10 is 0xF7
    spi_Write(spiData[0]) ;
    spi_Write(spiData[1]) ;
    PORTA |= SPI_CS ;  // Take SPI Chip Select Inactive
Is that as clear as mud or what....?
 
Top