A basic library for the Freetronics arduino LCD shield.

Dependents:   freetronicsLCDShield_HelloWorld prototipo prototipo Juego

freetronicsLCDShield

Comments

A library for the arduino targeted freetronics LCD shield (http://www.freetronics.com/products/lcd-keypad-shield#.UmzHGBDvfRU) This is my first attempt at publishing a library so bare with. The LCD is fully functional, the buttons however are not yet fully implemented.

Member functions

Constructor

freetronicsLCDShield (PinName rs, PinName e, PinName d0, PinName d1, PinName d2, PinName d3, PinName bl, PinName a0);

Custom character generation

void writeCGRAM (char address, const char *ptr, char nbytes);

Characters that aren't included in the default character map can be written by the LCD controller 's character generator ram spaces (i.e. 0, 8, 16 .. 7 * 8). Each custom character consists of eight bytes in witch the lower 5 bits correspond to a pixel on the display. For example; the ° character witch isn't included in the default character map could be stored in the character generator RAM with:

<instanceName>.writeCGRAM(0, {0b00001100, 
                              0b00010010, 
                              0b00010010,
                              0b00001100,
                              0b00000000,
                              0b00000000,
                              0b00000000,
                              0b00000000}, 8);

Note: The binary values must be replaces by their corresponding hex or decimal values.

The ° character can now displayed by calling <instanceName>.putc(0) function.

Back-light control

void setBackLight (bool blStatus);
void setBackLight (float blIntensity); 

Cursor control

void home(void); 

The cursor returns home (i.e. the cursor is placed at 0,0), the screen is NOT cleared. Consecutive writes to the LCD overwrite the display.

void setCursorPosition (int line, int col);
void setCursor (bool cStatus = true, bool blink = false); 

These are rather self-explanatory

Shift instructions

void shiftLeft (void);
void shiftRight (void); 
void shift (bool direction); 

When direction is set the display shifts left, else it shifts right

Clearing the display

void cls (void); 

Button support

float readButton(void); 

There is rather limited button support, you can read the status of the button pin as a float value.

Hello world

#include "mbed.h"
#include "freetronicsLCDShield.h"

// These are the custom chars 8 of them 1 per row.
const char CGRAM_DATA[64]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,
                           0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x1F,
                           0x00,0x00,0x00,0x00,0x00,0x1F,0x1F,0x1F,
                           0x00,0x00,0x00,0x00,0x1F,0x1F,0x1F,0x1F,
                           0x00,0x00,0x00,0x1F,0x1F,0x1F,0x1F,0x1F,
                           0x00,0x00,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,
                           0x00,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,
                           0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F};

/* freetronicsLCDShield LCD(rs, e, d0, d1, d2, d3, bl, a0); */
freetronicsLCDShield LCD(PTA13, PTD5, PTA4, PTA5, PTC8, PTC9, PTA12, PTB0);
 
int main() {
    // Write custom generated chars to the 
    LCD.writeCGRAM (0x00, &CGRAM_DATA[0], 8);
    LCD.writeCGRAM (0x08, &CGRAM_DATA[8], 8);
    LCD.writeCGRAM (0x10, &CGRAM_DATA[16], 8);
    LCD.writeCGRAM (0x18, &CGRAM_DATA[24], 8);
    LCD.writeCGRAM (0x20, &CGRAM_DATA[32], 8);
    LCD.writeCGRAM (0x28, &CGRAM_DATA[40], 8);
    LCD.writeCGRAM (0x30, &CGRAM_DATA[48], 8);
    LCD.writeCGRAM (0x38, &CGRAM_DATA[56], 8);
    
    LCD.cls();
    
    // The backlight can be turned on or off //
    LCD.setBackLight(true); wait(2.0); 
    // The setBackLight method is overloaded so you can specify it's intensity by supplying it with a float value
    LCD.setBackLight((float) 0.05);
    
    while (true) {
        LCD.cls();
        LCD.setCursor(false);
        // You may want to set the cursor position at a specific location
        LCD.setCursorPosition (0, 0);
        LCD.printf("* Hello  world *");
    
        // Shift the text on the LCD left ...
        for(int i = 0; i < 3; i++) {LCD.shift(true); wait(0.5);}
        
        // Shift right ...
        for(int i = 0; i < 6; i++) {LCD.shift(false); wait(0.5);}
        
        // Shift the text back to left ...
        for(int i = 0; i < 3; i++) {LCD.shift(true); wait(0.5);}
        
        LCD.setCursorPosition (1,0);
        // Let the cursor blink 
        LCD.setCursor(true, true);
        LCD.putc('A'); wait(0.5);    
        LCD.putc('w'); wait(0.5);     
        LCD.putc('e'); wait(0.5);    
        LCD.putc('s'); wait(0.5);    
        LCD.putc('o'); wait(0.5);    
        LCD.putc('m'); wait(0.5);    
        LCD.putc('e'); wait(0.5);    
        
        LCD.cls();
        LCD.setCursor(false);
        // You may want to set the cursor position at a specific location
        LCD.setCursorPosition (0, 0);
        LCD.printf("Backlight = %0.3f", 0.000);
        int n = 0;

        for (float intensity = 0.125; intensity <= 1.0; intensity += 0.125) {
           // Fire up the backlight 
            LCD.setBackLight((float) intensity);
            // Display intensity
            LCD.setCursorPosition(0,11);
            LCD.printf("%0.3f", intensity);
            
            // Print the custom char's 0 .. 7
            LCD.setCursorPosition(1,4+n);
            LCD.putc(n);
            n++;
            wait(0.5);
        } 
   }
} 

Picture

/media/uploads/KKempeneers/sv105343.jpg

freetronicsLCDShield.cpp

Committer:
KKempeneers
Date:
2014-12-13
Revision:
4:3a0c74cef8e8
Parent:
1:ddcefddda4a7

File content as of revision 4:3a0c74cef8e8:

/* mbed freetronicsLCDShield Library, written by Koen J.F. Kempeneers
 * kkempeneers(at)skynet.be
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "mbed.h"
#include "freetronicsLCDShield.h"

#define PULSE_E     wait(0.000001f);    \
                    _e = 0;             \
                    wait(0.000001f);    \
                    _e = 1;


freetronicsLCDShield::freetronicsLCDShield (PinName rs, PinName e, PinName d0, PinName d1, PinName d2, PinName d3, PinName bl, PinName a0) 
            : _rs(rs), _e(e), _d(d0, d1, d2, d3), _bl(bl), _a0(a0) {
    // Class constructor
    // Init the display, wait 15ms to insure the power is up
    _e = true;
    _rs = false;
    wait(0.015f);
    
    for (int i = 0; i < 3; i++){
        writeByte(0x3);
        wait(0.00164f);  // this command takes 1.64ms, so wait for it
    }

    writeByte(0x2);     // 4-bit mode
    writeCommand(0x28); // Function set 001 BW N F - -
    writeCommand(0x0C); // Display on/off controll 0000 1 D C B (D(isplay) On/Off C(ursor) On/Off B(link) On/Off 
    writeCommand(0x6);  // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
    cls();
    
    // Set the PWM period to 20ms
    _bl.period_ms(1);
    _bl.write(0.0);
}

void freetronicsLCDShield::setCursorPosition (int line, int col) {
    // Set the new cursorposition
    writeCommand(0x80 + (line * 0x40) + col);
}

void freetronicsLCDShield::setBackLight (bool blStatus) {
    // Switch the backlight on (true) or off (false)
    _bl = (blStatus) ? 1.0 : 0.0;
}

void freetronicsLCDShield::setBackLight (float blIntensity) {
    // Switch the backlight on (true) or off (false)
    _bl = blIntensity;
}

void freetronicsLCDShield::setCursor (bool cStatus, bool blink) {
    int tmp = 0;
    
    if (blink) tmp = 0x01;
    if (cStatus) tmp |= 0x02; 
    writeCommand(0x0C + tmp);
}

void freetronicsLCDShield::shift(bool direction) {
    if(direction == LEFT) shiftLeft();
    else shiftRight();
}

void freetronicsLCDShield::shiftLeft(void) {
    writeCommand(0x18 + 0x04);
}

void freetronicsLCDShield::shiftRight(void) {
    writeCommand(0x18);
}

void freetronicsLCDShield::cls(void) {
    // Clear the display and place the cursor at 0, 0
    writeCommand(0x01);
    wait(0.00164f);
}

void freetronicsLCDShield::home(void) {
    // Undo shift operations and place cursor at 0,0
    writeCommand(0x02);
    wait(0.00164f);
}

void freetronicsLCDShield::writeCGRAM (char address, const char *ptr, char nbytes) {
    // Write the address only once, it is autoincremented 
    writeCommand(0x40 | address);

    // Write the data
    for(int i = 0; i < nbytes; i++) {
        writeData(*ptr++);
    }
}

// Low level output functions
void freetronicsLCDShield::writeByte (int byte) {
    // Split the byte in high and low nibble, write high nibble first
    _d = byte >> 4;
    PULSE_E;
    _d = byte >> 0;
    PULSE_E;
    // Most instructions take 40us
    wait(0.000040f);  
}

void freetronicsLCDShield::writeData (int data) {
    _rs = true;
    writeByte(data);
} 

void freetronicsLCDShield::writeCommand (int command) {
    _rs = false;
    writeByte(command);
} 

float freetronicsLCDShield::readButton(void) {
    return(_a0.read());
}

// Virtual functions for stream
int freetronicsLCDShield::_putc(int value) {   
    writeData(value);
    return value;
}

int freetronicsLCDShield::_getc() {
    return -1;
}