Previous Page
    /******************************************************************************
    * SPI.c
    *
    * Description: This source file initializes the SPI module on the Kinetis K02
    *              assuming physical connection to PTC5(SCK) and PTC6(SOUT)
    *
    * Controller: Kinetis MK02F12810
    *
    * Spencer Black April 12, 2015
    *******************************************************************************
    * Master Include File
    ******************************************************************************/
    #include "includes.h"

    /******************************************************************************
    * SPI Defines
    ******************************************************************************/
    #define PCS0  0x1       //channel for data to be sent too (channel 0)
    /******************************************************************************
    * Public Function prototypes
    ******************************************************************************/
    void SPI_Init(void);
    void SPI_Send_Array(INT8U *Array);

    /******************************************************************************
    * SPI_Init(void)
    *
    * Description: Sets up the SPI module on the Kinetis K02
    *
    * Parameters: None
    ******************************************************************************/
    void SPI_Init(void){
        SIM_SCGC6   |= SIM_SCGC6_SPI0_MASK; //Initialize SPI clock

        PORTC_PCR4   = PORT_PCR_MUX(1);     //Alt setting for GPIO (latch signal)
        PORTC_PCR5   = PORT_PCR_MUX(2);     //Alt setting for SCK
        PORTC_PCR6   = PORT_PCR_MUX(2);     //Alt setting for SOUT
        GPIOC_PDDR  |= GPIO_PDDR_PDD(0x10); //Set to output
        GPIOC_PDOR  &= ~GPIO_PDOR_PDO(0x10);//Set to low

        //Set master mode, stop transfer
        SPI0_MCR    |= (SPI_MCR_MSTR_MASK | SPI_MCR_HALT_MASK);

        //Set PCS0 to active High
        SPI0_MCR    &= ~SPI_MCR_PCSIS(1);

        //Set the Baud Rate to 1MHz ((40MHz/Prescale)*(1/Baud Rate))
        //Prescale = 5 and Baud Rate = 8
        SPI0_CTAR0  |= (SPI_CTAR_PBR(2) | SPI_CTAR_BR(3));

        //Frame Size = 8 bits
        SPI0_CTAR0  &= ~SPI_CTAR_FMSZ(8);

        SPI0_MCR    &= ~SPI_MCR_MDIS_MASK; //Enable module
    }

    /******************************************************************************
    * SPI_Send_Array(INT8U *Array)
    *
    * Description: Sends data through the SPI module to channel 0. Set for 8-bit
    *              data packages in order to interface with the STP24DP05
    *              (8x3 bit LED Driver)
    *
    * Parameters: Array is a pointer to an array of 24-bits in 8-bit chunks
    ******************************************************************************/
    void SPI_Send_Array(INT8U *Array){
        static INT8U i = 0;
        static INT8U transmitcount = 0;        //Array index counter
        INT8U data  = *Array;                  //Array

        SPI0_MCR   &= ~SPI_MCR_HALT_MASK;      //Start Transfer
        while (transmitcount < 3){             //Only want 3 8-bit data sequences
            SPI0_PUSHR = data;                 //Send data
            for(i=0;i<50;i++);                 //Need delay so data can shift in
            Array += 1;                        //Increment through array
            data = *Array;
            transmitcount++;                   //Increment counter
        }
        transmitcount = 0;                     //Reset counter
        SPI0_MCR   |= SPI_MCR_HALT_MASK;       //Stop Transfer
        while(!(SPI0_SR & SPI_SR_TCF_MASK));   //Wait for transfer complete flag
        GPIOC_PDOR  |= GPIO_PDOR_PDO(0x10);    //Pulse low to latch the data
        GPIOC_PDOR  &= ~GPIO_PDOR_PDO(0x10);
    }
    

Previous Page