Previous Page
    /******************************************************************************
    * LCD.c
    * 
    * Description: This source file allows the Kinetis K02 to communicate with
    *              the 16x2 Newhaven Display (NHD-C0216CiZ-FSW-FBW-3V3) via I2C.
    *  
    * Controller: Kinetis MK02F12810 I2C, 7-bit mode
    * 
    * Spencer Black April 4, 2015
    *******************************************************************************
    * Master Include File
    ******************************************************************************/
    #include "includes.h"

    /******************************************************************************
    * LCD Defines
    ******************************************************************************/
    #define COMMSEND      0x00   //Value sent before LCD instruction
    #define DATASEND      0x40   //Value sent before LCD data
    #define SLAVEADDRESS  0x3E   //Slave address (7C) shifted 1 bit toward LSB
    #define CLEAR         0x00   //Value to set the character to a blank space
    #define LINE1         0x02   //Value for beginning of Line 1
    #define LINE2         0xC0   //Value for beginning of Line 2

    /******************************************************************************
    * Public Function prototypes
    ******************************************************************************/
    void LCD_Init(void);
    void LCD_Disp_String(INT8U *c, INT8U numofchars);
    void LCD_Disp_Dec_Byte(INT8U num);
    void LCD_New_Line(INT8U line);
    void LCD_Clr_Disp(void);
    void LCD_Clr_Line(INT8U line);
    void LCD_Clr_Characters(INT8U line, INT8U start, INT8U end);

    /* Private */
    static void LCD_Dly_10us(void);

    /******************************************************************************
    * void LCD_Init(void)
    *
    * Description: Initialize the LCD's built in ST7032i I2C controller so the
    *              LCD is ready to use.
    *
    * Module: Dependent on I2C.c source file
    *
    * Parameters: None
    ******************************************************************************/
    void LCD_Init(void){
        SysTickDelay(40);           //DELAY: 40ms required for LCD power on
        I2C_Init();                 //Initialize I2C

        I2C_SetSlaveAddress(0x3E);
        I2C_Set_TX();
        I2C_SendStart(COMMSEND);    //Sends slave address and instruction
        I2C_Send_Char(0x38);        //Go to instruction table 0
        LCD_Dly_10us();             //DELAY: Minimum 10us required
        I2C_Send_Char(0x39);
        LCD_Dly_10us();             //DELAY: Minimum 10us required
        I2C_Send_Char(0x14);
        I2C_Send_Char(0x78);
        I2C_Send_Char(0x5E);
        I2C_Send_Char(0x6D);
        I2C_Send_Char(0x0C);        //Turn the display on
        I2C_Send_Char(0x01);        //Clear the display
        I2C_Send_Char(0x06);
        LCD_Dly_10us();             //DELAY: Minimum 10us required
        I2C_Set_RX();
        SysTickDelay(2);            //1ms delay needed after LCD initialization
    }

    /******************************************************************************
    * void LCD_Disp_String(INT8U *c)
    *
    * Description: Displays a string through the I2C interface onto the LCD
    *
    * Parameters: c is a pointer to a byte of data within the array
    *             numofchars is the number of characters in the array
    ******************************************************************************/
    void LCD_Disp_String(INT8U *c, INT8U numofchars){
        INT8U numchars = numofchars;
        static INT8U n = 0;

        I2C_Set_TX();                 //Initiate transmission
        I2C_SendStart(DATASEND);      //Prepare LCD for data send
        for(n=0;n<numchars;n++){
            I2C_Send_Char(*c);        //Send the data
            ++c;
        }
        I2C_Set_RX();                 //End transmission
    }

    /******************************************************************************
    * void LCD_Disp_Dec_Byte(INT8U num)
    *
    * Description: Converts a byte (max = 255) into decimal value and displays
    *              using the Disp_String function.
    *
    * Parameters: num is the byte value to be converted to decimal
    ******************************************************************************/
    void LCD_Disp_Dec_Byte(INT8U num){
        INT8U decval = num;
        static INT8U decimal[] = {0x00, 0x00, 0x00, 0xDF};//Max value = 255

        decimal[0] = ((decval / 100) + '0');
        decimal[1] = ((decval / 10) + '0');
        decimal[2] = ((decval % 10) + '0');

        LCD_Disp_String(decimal,4);
    }

    /******************************************************************************
    * void LCD_Clr_Disp(void)
    *
    ** Description: Clear the display and send the cursor back to location (0,0).
    *
    *  Parameters: None
    *
    ******************************************************************************/
    void LCD_Clr_Disp(void) {
        I2C_Set_TX();               //Initiate transmission
        I2C_SendStart(COMMSEND);    //Prepare LCD for instruction
        I2C_Send_Char(0x01);        //Clear the display
        I2C_Set_RX();               //End transmission
        SysTickDelay(2);            //DELAY: 1ms required after clear
    }

    /******************************************************************************
    * void LCD_Clr_Line(INT8U line)
    *
    ** Description: Clears the chosen line
    *
    *  Parameters: line is the line number to clear (1 or 2)
    *
    ******************************************************************************/
    void LCD_Clr_Line(INT8U line){
        INT8U linenum = line;
        static INT8U n = 0;

        if(linenum == 1){
            I2C_Set_TX();            //Initiate transmission
            I2C_SendStart(COMMSEND); //Prepare LCD for instruction
            I2C_Send_Char(0x39);
            I2C_Send_Char(LINE1);    //Move cursor to Beginning of line 1
            I2C_Set_RX();            //End transmission

            I2C_Set_TX();            //Initiate transmission
            I2C_SendStart(DATASEND); //Prepare LCD for data send
            for(n=0;n<16;n++){
                I2C_Send_Char(CLEAR);//Send the data
            }
            I2C_Set_RX();            //End transmission

            I2C_Set_TX();            //Initiate transmission
            I2C_SendStart(COMMSEND); //Prepare LCD for instruction
            I2C_Send_Char(0x39);
            I2C_Send_Char(LINE1);    //Move cursor to Beginning of line 1
            I2C_Set_RX();            //End transmission

        }else if(linenum == 2){
            I2C_Set_TX();            //Initiate transmission
            I2C_SendStart(COMMSEND); //Prepare LCD for instruction
            I2C_Send_Char(LINE2);    //Move cursor to Beginning of line 2
            I2C_Set_RX();            //End transmission

            I2C_Set_TX();            //Initiate transmission
            I2C_SendStart(DATASEND); //Prepare LCD for data send
            for(n=0;n<16;n++){
                I2C_Send_Char(CLEAR);//Send the data
            }
            I2C_Set_RX();            //End transmission

            I2C_Set_TX();            //Initiate transmission
            I2C_SendStart(COMMSEND); //Prepare LCD for instruction
            I2C_Send_Char(LINE2);    //Move cursor to Beginning of line 2
            I2C_Set_RX();            //End transmission

        }else{
            //Do Nothing
        }
        SysTickDelay(2);             //DELAY: 1ms required after clear
    }

    /******************************************************************************
    * void LCD_Clr_Characters(INT8U line, INT8U start, INT8U end)
    *
    * Description: Clears chosen characters from the display. clears all chars in
    *              a range of given positions. Similar to Clr_Line But it returns
    *              the cursor to the chosen start position.
    *
    * Parameters: line is the line number to clear (1 or 2)
    *             start is the starting position wanting to cleared
    *             end is the ending position want to be cleared
    *
    ******************************************************************************/
    void LCD_Clr_Characters(INT8U line, INT8U start, INT8U end){
        INT8U linenum  = line;
        INT8U startpos = (start - 1);      //Cursor is at position 1 already
        INT8U endpos   = (end - 1);
        static INT8U n = 0;

        if(linenum == 1){
            I2C_Set_TX();                  //Initiate transmission
            I2C_SendStart(COMMSEND);       //Prepare LCD for instruction
            I2C_Send_Char(0x39);
            I2C_Send_Char(LINE1);          //Move cursor to Beginning of line 1
            I2C_Send_Char(0x01 + startpos);//Move cursor to start position
            I2C_Set_RX();                  //End transmission

            I2C_Set_TX();                  //Initiate transmission
            I2C_SendStart(DATASEND);       //Prepare LCD for data send
            for(n=0;n<((endpos - startpos) + 1);n++){
                I2C_Send_Char(CLEAR);      //Send the data
            }
            I2C_Set_RX();                  //End transmission

            I2C_Set_TX();                  //Initiate transmission
            I2C_SendStart(COMMSEND);       //Prepare LCD for instruction
            I2C_Send_Char(0x39);
            I2C_Send_Char(LINE1);          //Move cursor to Beginning of line 1
            I2C_Send_Char(0x01 + startpos);//Move cursor to start position
            I2C_Set_RX();                  //End transmission

        }else if(linenum == 2){
            I2C_Set_TX();                  //Initiate transmission
            I2C_SendStart(COMMSEND);       //Prepare LCD for instruction
            I2C_Send_Char(LINE2);          //Move cursor to Beginning of line 2
            I2C_Send_Char(LINE2 + startpos);//Move cursor to start position
            I2C_Set_RX();                  //End transmission

            I2C_Set_TX();                  //Initiate transmission
            I2C_SendStart(DATASEND);       //Prepare LCD for data send
            for(n=0;n<((endpos - startpos) + 1);n++){
                I2C_Send_Char(CLEAR);      //Send the data
            }
            I2C_Set_RX();                  //End transmission

            I2C_Set_TX();                  //Initiate transmission
            I2C_SendStart(COMMSEND);       //Prepare LCD for instruction
            I2C_Send_Char(0x39);
            I2C_Send_Char(LINE2);           //Move cursor to Beginning of line 1
            I2C_Send_Char(LINE2 + startpos);//Move cursor to start position
            I2C_Set_RX();                  //End transmission

        }else{
            //Do Nothing
        }
        SysTickDelay(2);             //DELAY: 1ms required after clear
    }

    /******************************************************************************
    * void LCD_New_Line(INT8U line)
    *
    * Description: Changes the cursor position to the beginning of a chosen line.
    *
    * Parameters: None
    ******************************************************************************/
    void LCD_New_Line(INT8U line){
        INT8U chosenline = line;

        if(chosenline == 1){
            I2C_Set_TX();               //Initiate transmission
            I2C_SendStart(COMMSEND);    //Prepare LCD for instruction
            I2C_Send_Char(LINE1);       //Set cursor to beginning of line 1
            I2C_Set_RX();               //End transmission
        }else if(chosenline == 2){
            I2C_Set_TX();               //Initiate transmission
            I2C_SendStart(COMMSEND);    //Prepare LCD for instruction
            I2C_Send_Char(LINE2);       //Set cursor to beginning of line 2
            I2C_Set_RX();               //End transmission
        }else{
            //Do Nothing
        }
    }

    /******************************************************************************
    * LCD_Dly_10us(void)
    *
    * Description: Delays at least 10us and is designed for a 80MHz clock
    *              Currently set to ~10.24us with i=80.
    *
    * Parameters: None
    *
    * TDM 01/20/2013
    *   Edited By: Spencer Black April 4, 2015
    *       -Changed to 10us delay for LCD initialization
    ******************************************************************************/
    static void LCD_Dly_10us(void){
        static INT32U i = 0;
        for(i=0;i<80;i++){}
    }
    
    

Previous Page