Troubleshooting

LCD.cpp

Committer:
BenRJG
Date:
2018-11-04
Revision:
7:0213c3702c99
Parent:
6:f3d1ab8a9e99
Parent:
4:bb7a78619678
Child:
8:e34e23edfc84

File content as of revision 7:0213c3702c99:

#include "LCD.hpp"
#include "DataTypes.hpp"

void LCD::INIT()
{
    //All lines default low
    _LCD_RS = 0;
    _LCD_E  = 0;
    
    //LCD Initialise
    wait_ms(45); //Wait for LCD startup
    wait_us(3000);      //Wait whilst LCD busy
    _LCD_RS = control;   
    LCD_DDRAM = 0;  //Clear data line
    LCD_DDRAM = DDRAM>>4;  //Put data on line
    LCD_strobe();
    
    
    /*Step 1*/ //cmdLCD(DDRAM|0x08);
    /*Step 2*/ cmdLCD(DDRAM|lines2);  //Function Set 0x20|0x08 = 0x28
    /*Step 3*/ cmdLCD(DDRAM|lines2);  //Function Set 0x20|0x08 = 0x28
    /*Step 4*/ cmdLCD(DISPLAY|on);    //Display Control 0x08|0x0x04 = 0x0c
    /*Step 5*/ cmdLCD(CLEAR);         //Clear Display 0x01
    /*Step 6*/ cmdLCD(ENTRYMODE|I);   //Set entry mode 0x04|0x02 = 0x06

    cmdLCD(0x20); //return home location
}

void LCD::clear()
{
    cmdLCD(CLEAR);   
}

void LCD::display(BYTE* str, INT_32 line, INT_32 position)
{
    UINT_32 length  = strlen(str);
    UINT_32 line1 = length;
    UINT_32 line2 = 0;
    
    //if((line != NULL)&&(position != NULL))
    //{
        cmdLCD(line|position);
    //}
    UINT_32 p;
    if(line == LINE1)
    {
        if(position + length > 16)
        {
            line1 = findSpace(str);
            line2 = length - line1;
        }
        
        for(p=0; p<line1; p++)
        {
            putt(str[p]);   
        }
        
        cmdLCD(LINE2);
    }
    
    if(line == LINE2){line2 = length;}
    
    if((line2 <= 16)&&(line != 2))
    {
        for(p=0; p<line2; p++)
        {
            putt(str[line1 + p]);
        }
    }
    else
    {
        for(p=0; p<(13); p++)
        {
            putt(str[line1 + p]);
        }
        
        for(p=0; p<3; p++)
        {
            putt('.');
        }  
    }
}

void LCD::putt(BYTE c)
{
    wait_us(3000);
    _LCD_RS = text;
    set_LCD_data(c);
}

void LCD::cmdLCD(U_BYTE cmd)
{
    wait_us(3000);      //Wait whilst LCD busy
    _LCD_RS = control;   
    set_LCD_data(cmd);  //set data on bus
}

void LCD::LCD_strobe(void)
{
    wait_us(10);
    _LCD_E = 1;
    wait_us(10);
    _LCD_E = 0;
}

void LCD::set_LCD_data(U_BYTE d)
{
    // Send upper 4 bits then lower for bits
    // e.g. 11110000 => 1111 -> 0000
    
    LCD_DDRAM = 0;  //Clear data line
    LCD_DDRAM = d>>4;  //Put data on line
    LCD_strobe();
    wait_us(1000);
    LCD_DDRAM = 0;  //Clear
    LCD_DDRAM = d; //Put remaining data on line
    LCD_strobe();   
}

UINT_32 LCD::findSpace(BYTE* str)
{
    UINT_32 space = 0;
    INT_32 n = 16;
    while(!space & (n != -1))
    {
        n--;
        switch(str[n])
        {
            case ' ':
            case '.':
            case ',':
            case ':':
            case ';':
            case '-':
            case '/':
                space = 1;
            break;
        }
    }
    if(n == -1){ return 16; }
    else{ return n+1; }
}