Previous Page
    /******************************************************************************
    * LED.c
    *
    * Description: This source file initializes and controls the STP24DP05 LED
    *              driver using the SPI module on the kinetis K02
    *
    * Controller: Kinetis MK02F12810
    *
    * Spencer Black April 19, 2015
    *******************************************************************************
    * Master Include File
    ******************************************************************************/
    #include "includes.h"

    /******************************************************************************
    * LED Defines
    ******************************************************************************/
    //For initializations
    #define BLUECHANNEL  0x100
    #define GREENCHANNEL 0x200
    #define REDCHANNEL   0x400

    //For accessing the proper portion of the arrays
    #define BLUECHAN     2
    #define GREENCHAN    0
    #define REDCHAN      1
    #define LEDCHAN1  0x01
    #define LEDCHAN2  0x02
    #define LEDCHAN3  0x04
    #define LEDCHAN4  0x08
    #define LEDCHAN5  0x10
    #define LEDCHAN6  0x20

    //enumerated type for LED_Write_Reset function
    typedef enum {LedRight,LedLeft} DIRECTIONSTATE;
    typedef enum {Blue, Green, Red} COLORSTATE;
    /******************************************************************************
    * Global Variables
    ******************************************************************************/
    //Array for turning LEDs off
    static INT8U Off_Array[]    = {0x00, 0x00, 0x00};

    //Arrays for automatic control
    static INT8U Red_Array[]    = {0x00, 0x3F, 0x00};
    static INT8U Blue_Array[]   = {0x00, 0x00, 0x3F};
    static INT8U Green_Array[]  = {0x3F, 0x00, 0x00};

    static INT8U Temp_Array[]   = {0x00, 0x00, 0x00};

    //Storage for manual control array
    INT8U Manual_Array[] = {0x00, 0x00, 0x00};

    DIRECTIONSTATE CurrDirState = LedRight;
    COLORSTATE CurrColorState   = Blue;
    /******************************************************************************
    * Public Function prototypes
    ******************************************************************************/
    void LED_Init(void);
    void LED_Write_Reset(void);
    void LED_Write_Auto(INT8U autoledval);
    void LED_Write_Manual(INT8U ledchan, INT8U manledval);

    /******************************************************************************
    * void LED_Init(void)
    *
    * Description: Sets up SPI and the ports used on the Kinetis K02 for the
    *              STP24DP05 LED driver.
    *
    * Module: Dependent on SPI.c source file
    *
    * Parameters: None
    ******************************************************************************/
    void LED_Init(void){
        SPI_Init();                           //Initialize SPI

        PORTC_PCR8   = PORT_PCR_MUX(1);       //Alt setting for GPIO
        PORTC_PCR9   = PORT_PCR_MUX(1);       //Alt setting for GPIO
        PORTC_PCR10  = PORT_PCR_MUX(1);       //Alt setting for GPIO

        //Set all to output, clear any previous data and Enable all LED channels
        GPIOC_PDDR  |= GPIO_PDDR_PDD(BLUECHANNEL | GREENCHANNEL | REDCHANNEL);
        SPI_Send_Array(Off_Array);
        GPIOC_PDOR &= ~(BLUECHANNEL | GREENCHANNEL | REDCHANNEL);
    }

    /******************************************************************************
    * void LED_Write_Reset(void)
    *
    * Description: Writes a sequence of led values for startup to the LED driver.
    *              Dependent on a 10ms TimeSlice and a 2 second reset state
    *
    * Module: Dependent on SPI.c source file
    *
    * Parameters: None
    ******************************************************************************/
    void LED_Write_Reset(void){
        static INT8U resetcount = 0;
        static INT8U flag       = 0;

        if((resetcount % 5) == 1){
            switch(CurrDirState){
                case(LedRight):
                    switch(CurrColorState){
                        case(Blue):
                            if(flag == 0){
                                Temp_Array[BLUECHAN] = 0x01;
                                flag = 1;
                            }else if(flag == 1){
                                Temp_Array[BLUECHAN] = (Temp_Array[BLUECHAN] << 1);
                                if(Temp_Array[BLUECHAN] == 0x40){
                                    CurrDirState = LedLeft;
                                }
                            }else{
                                //Do Nothing
                            }
                        break;

                        case(Green):
                            if(flag == 0){
                                Temp_Array[BLUECHAN] = 0x00;
                                Temp_Array[GREENCHAN] = 0x01;
                                flag = 1;
                            }else if(flag == 1){
                                Temp_Array[GREENCHAN] = (Temp_Array[GREENCHAN] << 1);
                                if(Temp_Array[GREENCHAN] == 0x40){
                                    CurrDirState = LedLeft;
                                }
                            }else{
                                //Do Nothing
                            }
                        break;

                        case(Red):
                            if(flag == 0){
                                Temp_Array[GREENCHAN] = 0x00;
                                Temp_Array[REDCHAN] = 0x01;
                                flag = 1;
                            }else if(flag == 1){
                                Temp_Array[REDCHAN] = (Temp_Array[REDCHAN] << 1);
                                if(Temp_Array[REDCHAN] == 0x40){
                                    CurrDirState = LedLeft;
                                }
                            }else{
                                //Do Nothing
                            }
                        break;

                        default:
                            //Do Nothing
                        break;
                    }

                    SPI_Send_Array(Temp_Array);
                break;

                case(LedLeft):
                    switch(CurrColorState){
                        case(Blue):
                            Temp_Array[BLUECHAN] = (Temp_Array[BLUECHAN] >> 1);
                            if(Temp_Array[BLUECHAN] == 0x01){
                                flag = 0;
                                CurrDirState = LedRight;
                                CurrColorState = Green;
                            }
                        break;

                        case(Green):
                            Temp_Array[GREENCHAN] = (Temp_Array[GREENCHAN] >> 1);
                            if(Temp_Array[GREENCHAN] == 0x01){
                               flag = 0;
                                CurrDirState = LedRight;
                                CurrColorState = Red;
                            }
                        break;

                        case(Red):
                            if(flag == 2){
                                Temp_Array[REDCHAN] = 0;
                            }else{
                                //Do Nothing
                            }
                            Temp_Array[REDCHAN] = (Temp_Array[REDCHAN] >> 1);
                            if(Temp_Array[REDCHAN] == 0x01){
                                flag = 2;
                            }
                        break;

                        default:
                            //Do Nothing
                        break;
                    }
                    SPI_Send_Array(Temp_Array);
                break;
            }

            resetcount++;
        }else{
            resetcount++;
        }
    }

    /******************************************************************************
    * void LED_Write_Auto(INT8U autoledval)
    *
    * Description: Writes to the LED driver
    *
    * Module: Dependent on SPI.c source file
    *
    * Parameters: autoledval is the current duty cycle of the fan channels
    ******************************************************************************/
    void LED_Write_Auto(INT8U autoledval){
        INT8U autoinputduty = autoledval;

        if(autoinputduty >= 66){
            SPI_Send_Array(Red_Array);
        }else if(autoinputduty <= 33){
            SPI_Send_Array(Blue_Array);
        }else{
            SPI_Send_Array(Green_Array);
        }
    }

    /******************************************************************************
    * void LED_Write_Manual(INT8U ledchan, INT8U ledval)
    *
    * Description: Writes to the LED driver
    *
    * Module: Dependent on SPI.c source file
    *
    * Parameters: ledchan is the current channel to update
    *             manledval is the current duty cycle of the fan channels
    ******************************************************************************/
    void LED_Write_Manual(INT8U ledchan, INT8U manledval){
        INT8U ledchannel = ledchan;
        INT8U maninputduty = manledval;

        //Checks which led channel needs to be updated
        if(ledchannel == 0){
            //Checks the current duty cycle for the selected channel and updates
            // the bit that corresponds to the channel in the Manual_Array.
            if(maninputduty >= 66){
                Manual_Array[GREENCHAN] &= ~LEDCHAN1;
                Manual_Array[REDCHAN] |= LEDCHAN1;
                Manual_Array[BLUECHAN] &= ~LEDCHAN1;
            }else if(maninputduty <= 33){
                Manual_Array[GREENCHAN] &= ~LEDCHAN1;
                Manual_Array[REDCHAN] &= ~LEDCHAN1;
                Manual_Array[BLUECHAN] |= LEDCHAN1;
            }else{
                Manual_Array[GREENCHAN] |= LEDCHAN1;
                Manual_Array[REDCHAN] &= ~LEDCHAN1;
                Manual_Array[BLUECHAN] &= ~LEDCHAN1;
            }
            SPI_Send_Array(Manual_Array);
        }else if(ledchannel == 1){
            if(maninputduty >= 66){
                Manual_Array[GREENCHAN] &= ~LEDCHAN2;
                Manual_Array[REDCHAN] |= LEDCHAN2;
                Manual_Array[2] &= ~LEDCHAN2;
            }else if(maninputduty <= 33){
                Manual_Array[GREENCHAN] &= ~LEDCHAN2;
                Manual_Array[REDCHAN] &= ~LEDCHAN2;
                Manual_Array[BLUECHAN] |= LEDCHAN2;
            }else{
                Manual_Array[GREENCHAN] |= LEDCHAN2;
                Manual_Array[REDCHAN] &= ~LEDCHAN2;
                Manual_Array[BLUECHAN] &= ~LEDCHAN2;
            }
            SPI_Send_Array(Manual_Array);
        }else if(ledchannel == 2){
            if(maninputduty >= 66){
                Manual_Array[GREENCHAN] &= ~LEDCHAN3;
                Manual_Array[REDCHAN] |= LEDCHAN3;
                Manual_Array[BLUECHAN] &= ~LEDCHAN3;
            }else if(maninputduty <= 33){
                Manual_Array[GREENCHAN] &= ~LEDCHAN3;
                Manual_Array[REDCHAN] &= ~LEDCHAN3;
                Manual_Array[BLUECHAN] |= LEDCHAN3;
            }else{
                Manual_Array[GREENCHAN] |= LEDCHAN3;
                Manual_Array[REDCHAN] &= ~LEDCHAN3;
                Manual_Array[BLUECHAN] &= ~LEDCHAN3;
            }
            SPI_Send_Array(Manual_Array);
        }else if(ledchannel == 3){
            if(maninputduty >= 66){
                Manual_Array[GREENCHAN] &= ~LEDCHAN4;
                Manual_Array[REDCHAN] |= LEDCHAN4;
                Manual_Array[BLUECHAN] &= ~LEDCHAN4;
            }else if(maninputduty <= 33){
                Manual_Array[GREENCHAN] &= ~LEDCHAN4;
                Manual_Array[REDCHAN] &= ~LEDCHAN4;
                Manual_Array[BLUECHAN] |= LEDCHAN4;
            }else{
                Manual_Array[GREENCHAN] |= LEDCHAN4;
                Manual_Array[REDCHAN] &= ~LEDCHAN4;
                Manual_Array[BLUECHAN] &= ~LEDCHAN4;
            }
            SPI_Send_Array(Manual_Array);
        }else if(ledchannel == 4){
            if(maninputduty >= 66){
                Manual_Array[GREENCHAN] &= ~LEDCHAN5;
                Manual_Array[REDCHAN] |= LEDCHAN5;
                Manual_Array[BLUECHAN] &= ~LEDCHAN5;
            }else if(maninputduty <= 33){
                Manual_Array[GREENCHAN] &= ~LEDCHAN5;
                Manual_Array[REDCHAN] &= ~LEDCHAN5;
                Manual_Array[BLUECHAN] |= LEDCHAN5;
            }else{
                Manual_Array[GREENCHAN] |= LEDCHAN5;
                Manual_Array[REDCHAN] &= ~LEDCHAN5;
                Manual_Array[BLUECHAN] &= ~LEDCHAN5;
            }
            SPI_Send_Array(Manual_Array);
        }else if(ledchannel == 5){
        if(maninputduty >= 66){
                Manual_Array[GREENCHAN] &= ~LEDCHAN6;
                Manual_Array[REDCHAN] |= LEDCHAN6;
                Manual_Array[BLUECHAN] &= ~LEDCHAN6;
            }else if(maninputduty <= 33){
                Manual_Array[GREENCHAN] &= ~LEDCHAN6;
                Manual_Array[REDCHAN] &= ~LEDCHAN6;
                Manual_Array[BLUECHAN] |= LEDCHAN6;
            }else{
                Manual_Array[GREENCHAN] |= LEDCHAN6;
                Manual_Array[REDCHAN] &= ~LEDCHAN6;
                Manual_Array[BLUECHAN] &= ~LEDCHAN6;
            }
            SPI_Send_Array(Manual_Array);
        }else{
            //Do Nothing
        }
    }
    

Previous Page