General purpose character LCD library. I found the TextLCD library had too many constraints and it didn\\\\\\\'t supply acces to all functions in the HD44780 chipset, so i decided to write my own character lcd library.

charLcd.cpp

Committer:
lktromp
Date:
2011-03-25
Revision:
1:1349bedd5793
Child:
2:18f6402fd025

File content as of revision 1:1349bedd5793:

#include "mbed.h"
#include "charLcd.h"
#include "hd44780.h"
#include "main.h"
Serial pc(USBTX, USBRX);
#define DEBUG

    //constructor for 4-bit lcd control
    charLcd::charLcd(PinName rs, PinName en, PinName rw, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type) 
    : cl_rs(rs), cl_rw(rw), cl_en(en), cl_charLcdData(d7, d6, d5, d4) {
        pc.baud(115200);
        #ifdef DEBUG
            pc.printf("LCD 4bit init\r\n");
        #endif //DEBUG
        charLcdResetLCD();
    }

    //constructor for 8-bit lcd control
    charLcd::charLcd(PinName rs, PinName en, PinName rw, PinName d0, PinName d1, PinName d2, PinName d3, PinName d4, PinName d5, 
    PinName d6, PinName d7, LCDType type)
    : cl_rs(rs), cl_rw(rw), cl_en(en), cl_charLcdData(d7, d6, d5, d4, d3, d2, d1, d0) {
    
        pc.baud(115200);
        #ifdef DEBUG
            pc.printf("LCD 8-bit init\r\n");
        #endif //DEBUG
        charLcdResetLCD();
    
    }



void charLcd::charLcdResetLCD(void) {
    cl_en  = 1;
    cl_rs = 0;            // command mode
    cl_rw = 0;
    wait_ms(40);        // Wait 15ms to ensure powered up
    charLcdFunctionSet(0,1,0);
    wait_ms(5);        // Wait 15ms to ensure powered up
    charLcdFunctionSet(0,1,0);
    wait_us(100);        // Wait 15ms to ensure powered up
    charLcdFunctionSet(0,1,0);
    wait_us(100);        // Wait 15ms to ensure powered up
    charLcdFunctionSet(0,1,0);
    charLcdDisplayOn(0);
    charLcdClear();
    charLcdEntryMode(1,0);
    //wait_ms(20);        // Wait 15ms to ensure powered up
    //charLcdFunctionSet(0,1,0);
    //wait_ms(20);        // Wait 15ms to ensure powered up
    charLcdDisplayOn(1);
    wait_ms(20);        // Wait 15ms to ensure powered up
    charLcdCursor(1, 1);
    wait_ms(20);        // Wait 15ms to ensure powered up
    charLcdEntryMode(1,0);
}

void charLcd::charLcdClear (void){
    #ifdef DEBUG
            pc.printf("LCD Clear\r\n");
    #endif //DEBUG
    charLcdSendCommand(LCD_CLEAR);
}
void charLcd::charLcdReturnHome(){
    #ifdef DEBUG
            pc.printf("LCD Home\r\n");
    #endif //DEBUG
    charLcdSendCommand(LCD_HOME);
}

void charLcd::charLcdEntryMode(uint8 increment, uint8 shift){
    uint8 cmd = LCD_ENTRY_MODE;
    if (increment == 0x00){
        cmd |= LCD_ENTRY_DEC;
        #ifdef DEBUG
            pc.printf("LCD Entry Mode decrement\r\n");
        #endif //DEBUG
    } else {
        cmd |= LCD_ENTRY_INC;
        #ifdef DEBUG
            pc.printf("LCD Entry Mode Increment\r\n");
        #endif //DEBUG
    }
    if (shift == 0x00){
        #ifdef DEBUG
            pc.printf("LCD Entry Mode Static\r\n");
        #endif //DEBUGcmd |= LCD_ENTRY_STAT;
        
    } else {
        cmd |=LCD_ENTRY_SHIFT;
        #ifdef DEBUG
            pc.printf("LCD Entry Mode Shift\r\n");
        #endif //DEBUG
    }    
    charLcdSendCommand(cmd);
    
}

void charLcd::charLcdDisplayOn(uint8 on){
    uint8 cmd = LCD_DISPLAY_CTR;
    if (on == 0x00){
        cmd |= LCD_DISP_OFF;
        #ifdef DEBUG
            pc.printf("LCD Display Control Display off\r\n");
        #endif //DEBUG
    } else {
        cmd |= LCD_DISP_ON;
        #ifdef DEBUG
            pc.printf("LCD Display Control Display on\r\n");
        #endif //DEBUG
    }
    charLcdSendCommand(cmd);
}

void charLcd::charLcdCursor(uint8 cursor, uint8 blink){
    uint8 cmd = LCD_DISPLAY_CTR;
    if (cursor == 0x00){
        cmd |= LCD_CURSOR_OFF;
        #ifdef DEBUG
            pc.printf("LCD Display Control Cursor off\r\n");
        #endif //DEBUG
    } else {
        cmd |= LCD_CURSOR_ON;
        #ifdef DEBUG
            pc.printf("LCD Display Control Cursor on\r\n");
        #endif //DEBUG
    }
    if (blink == 0x00){
        cmd |= LCD_BLINK_OFF;
        #ifdef DEBUG
            pc.printf("LCD Display Control Cursor Blink off\r\n");
        #endif //DEBUG
    } else {
        cmd |=LCD_BLINK_ON;
        #ifdef DEBUG
            pc.printf("LCD Display Control Cursor Blink on\r\n");
        #endif //DEBUG
    }    
    charLcdSendCommand(cmd);
}

void charLcd::charLcdShift(uint8 left, uint8 cursor){
    uint8 cmd = LCD_SHIFT;
    if (left == 0x00){
        cmd |= LCD_SHIFT_LEFT;
        #ifdef DEBUG
            pc.printf("LCD Shift Control Shift Left\r\n");
        #endif //DEBUG
    } else {
        cmd |= LCD_SHIFT_RIGHT;
        #ifdef DEBUG
            pc.printf("LCD Display Control Shift Right\r\n");
        #endif //DEBUG
    }
    if (cursor == 0x00){
        cmd |= LCD_SHIFT_DISP;
        #ifdef DEBUG
            pc.printf("LCD Display Control Shift Display\r\n");
        #endif //DEBUG
    } else {
        cmd |= LCD_SHIFT_CURS;
        #ifdef DEBUG
            pc.printf("LCD Display Control Shift Cursor\r\n");
        #endif //DEBUG
    }
    charLcdSendCommand(cmd);
}

void charLcd::charLcdFunctionSet(uint8 dots, uint8 lines, uint8 bits){
    uint8 cmd = LCD_FUNCTION;
    if (dots == 0x00){
        cmd |= LCD_FUNC_5x8;
        #ifdef DEBUG
            pc.printf("LCD Display Function Control 5x8\r\n");
        #endif //DEBUG
    } else {
        cmd |= LCD_FUNC_5x10;
        #ifdef DEBUG
            pc.printf("LCD Display Function Control 5x10\r\n");
        #endif //DEBUG
    }
    if (lines == 0x00){
        cmd |= LCD_FUNC_1LINES;
        #ifdef DEBUG
            pc.printf("LCD Display Function Control 1 Line\r\n");
        #endif //DEBUG
    } else {
        cmd |= LCD_FUNC_2LINES;
        #ifdef DEBUG
            pc.printf("LCD Display Function Control 2 Lines\r\n");
        #endif //DEBUG
    }
    if (bits == 0x00){
        cmd |= LCD_FUNC_8BIT;
        #ifdef DEBUG
            pc.printf("LCD Display Function Control 8 BIT\r\n");
        #endif //DEBUG
    } else {
        cmd |= LCD_FUNC_4BIT;
        #ifdef DEBUG
            pc.printf("LCD Display Function Control 4 BIT\r\n");
        #endif //DEBUG
    }
    charLcdSendCommand(cmd);
//#define LCD_FUNCTION    0x20
//#define LCD_FUNC_5x10   0x04
//#define LCD_FUNC_5x8    0x00
//#define LCD_FUNC_2LINES 0x08
//#define LCD_FUNC_1LINES 0x00
//#define LCD_FUNC_8BIT   0x10
//#define LCD_FUNC_4BIT   0x00
}

void charLcd::charLcdSetCGRAMaddress(uint8 adr) {
    charLcdSendCommand(LCD_SET_CGRAM & adr);
}

void charLcd::charLcdSetDDRAMaddress(uint8 adr) {
    charLcdSendCommand(LCD_SET_DDRAM & adr);
}

uint8 charLcd::charLcdReadBusyFlag() {
    return 0x00;
}

void charLcd::charLcdWriteData(uint8 dat){
    cl_rs = 1;
    charLcdWriteByte(dat);
}

uint8 charLcd::charLcdReadData(){
    cl_rs = 0;
    return 0x00;
}
        
void charLcd::charLcdSendCommand(uint8 cmd) {
    cl_rs = 0;
    charLcdWriteByte(cmd);
}

void charLcd::charLcdWriteByte(uint8 byte) {
    cl_charLcdData = byte;
    pc.printf("Write %#X to LCD\r\n", byte);
    wait_us(40); // setup time
    cl_en = 0;
    wait_us(40); //hold time
    cl_en = 1;
}