Files at this revision

API Documentation at this revision

Comitter:
Wimpie
Date:
Sat Dec 18 20:06:14 2010 +0000
Child:
1:0eb3365ec819
Commit message:

Changed in this revision

I2CTextLCD.cpp Show annotated file Show diff for this revision Revisions of this file
I2CTextLCD.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/I2CTextLCD.cpp	Sat Dec 18 20:06:14 2010 +0000
@@ -0,0 +1,154 @@
+/* mbed I2CTextLCD Library
+ * Copyright (c) 2007-2009 sford
+ * Copyright (c) 2010 Wim De Roeve  changed to work with I2C PCF8575
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
+
+#include "I2CTextLCD.h"
+#include "mbed.h"
+#include "error.h"
+
+using namespace mbed;
+
+/*
+     * useful info found at http://www.a-netz.de/lcd.en.php
+     *
+     *
+     * Initialisation
+     * ==============
+     *
+     * After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state
+     *
+     * - wait approximately 15 ms so the display is ready to execute commands
+     * - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now).
+     * - The display is in 8 bit mode, so if you have only connected 4 data pins you should only transmit the higher nibble of each command.
+     * - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now.
+         * - Execute the "clear display" command
+     *
+     * Timing
+     * ======
+     *
+     * Nearly all commands transmitted to the display need 40us for execution.
+     * Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position"
+     * These commands need 1.64ms for execution. These timings are valid for all displays working with an
+     * internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you
+     * can use the busy flag to test if the display is ready to accept the next command.
+     *
+     */
+
+I2CTextLCD::I2CTextLCD(PinName sda, PinName scl, int i2cAddress , int columns, int rows) :
+        _i2c(sda, scl) {
+
+    _i2cAddress = i2cAddress;
+    _columns = columns;
+    _rows = rows;
+
+
+    // Should theoretically wait 15ms, but most things will be powered up pre-reset
+    // so i'll disable that for the minute. If implemented, could wait 15ms post reset
+    // instead
+    // wait(0.015);
+
+    // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
+
+    for (int i=0; i<3; i++) {
+        writeNibble(0x3,false);
+        wait(0.00164);      // this command takes 1.64ms, so wait for it
+    }
+    writeNibble(0x2,false); // 4-bit mode
+
+    writeCommand(0x28);    // Function set 001 BW N F - -
+    writeCommand(0x0C);
+    writeCommand(0x6);  //  Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
+
+    cls();
+}
+
+int I2CTextLCD::_putc(int value) {
+    if (value == '\n') {
+        newline();
+    } else {
+        writeData(value);
+    }
+    return value;
+}
+
+int I2CTextLCD::_getc() {
+    return 0;
+}
+
+void I2CTextLCD::newline() {
+    _column = 0;
+    _row++;
+    if (_row >= _rows) {
+        _row = 0;
+    }
+    locate(_column, _row);
+}
+
+void I2CTextLCD::locate(int column, int row) {
+    if (column < 0 || column >= _columns || row < 0 || row >= _rows) {
+        error("locate(%d,%d) out of range on %dx%d display", column, row, _columns, _rows);
+        return;
+    }
+
+    _row = row;
+    _column = column;
+    int address = 0x80 + (_row * 40) + _column; // memory starts at 0x80, and is 40 chars long per row
+    // pc_LCD.traceOut("locate %dx%d\r\n", column, row);
+    writeCommand(address);
+}
+
+void I2CTextLCD::cls() {
+    writeCommand(0x01); // Clear Display
+    wait(0.00164f);     // This command takes 1.64 ms
+    locate(0, 0);
+}
+
+void I2CTextLCD::reset() {
+    cls();
+}
+
+void I2CTextLCD::writeNibble(int data, bool rs) {
+
+    data = (data & 0xF);
+
+    if (rs) {
+        data = data | RS_ON; // set rs bit
+    }
+
+    writeI2CByte(data | E_ON); // E=1
+
+    wait(0.000040f);
+    writeI2CByte(data );   // E=0
+
+    wait(0.000040f);
+    writeI2CByte(data | E_ON);  // E=1
+}
+
+void I2CTextLCD::writeByte(int data, bool rs) {
+    writeNibble(data >> 4 , rs);
+    writeNibble(data >> 0 , rs);
+}
+
+void I2CTextLCD::writeCommand(int command) {
+    //command ^= RS_ON; // RS = 0;
+    writeByte(command,false);
+}
+
+void I2CTextLCD::writeData(int data) {
+    //  data = data | RS_ON; //RS = 1
+    writeByte(data,true);
+
+    _column++;
+    if (_column >= _columns) {
+        newline();
+    }
+}
+
+void I2CTextLCD::writeI2CByte(int data) {
+    char cmd[2];
+    cmd[0] = (data & 0xFF);
+    cmd[1] = (data >> 8);
+    _i2c.write(_i2cAddress, cmd, 2);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/I2CTextLCD.h	Sat Dec 18 20:06:14 2010 +0000
@@ -0,0 +1,139 @@
+/* mbed I2CTextLCD Library
+ * Copyright (c) 2007-2009 sford
+ * Copyright (c) 2010 Wim De Roeve  changed to work with I2C PCF8575
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
+
+#ifndef MBED_I2CTextLCD_H
+#define MBED_I2CTextLCD_H
+
+#include "mbed.h"
+#include "Stream.h"
+
+/* The code assumes the following connections between the PCF8575
+* and the LCD
+*
+* nc  - D0
+* nc  - D1
+* nc  - D2
+* nc  - D3
+* P0  - D4
+* P1  - D5
+* P2  - D6
+* P3  - D7
+* P4  - E
+* P5  - nc
+* P6  - nc
+* P7  - RS
+* gnd - R/W
+
+*/
+
+#define E_ON  0x10  //P4
+#define RS_ON 0x80  //P7
+
+namespace mbed {
+
+/* Class: I2CTextLCD
+ * A 16x2 Text LCD controller
+ *
+ * Allows you to print to a Text LCD screen, and locate/cls. Could be
+ * turned in to a more generic libray.
+ *
+ * If you are connecting multiple displays, you can connect them all in
+ * parallel except for the enable (e) pin, which must be unique for each
+ * display.
+ *
+ * Example:
+ * > #include "mbed.h"
+ * > #include "I2CTextLCD.h"
+ * >
+ * > I2CTextLCD lcd(p9, P10, 0x40); // sda scl, address
+ * >
+ * > int main() {
+ * >     lcd.printf("Hello World!");
+ * > }
+ */
+class I2CTextLCD : public Stream {
+
+public:
+    /* Constructor: I2CTextLCD
+     * Create a I2CTextLCD object, connected to the specified pins
+     *
+     * All signals must be connected to DigitalIn compatible pins.
+     *
+     * Variables:
+     *  rs -  Used to specify data or command
+     *  rw - Used to determine read or write
+     *  e - enable
+     *  d0..d3 - The data lines
+     */
+
+    I2CTextLCD(PinName sda, PinName scl, int address, int columns = 16, int rows = 2);
+
+#if 0 // Inhereted from Stream, for documentation only
+    /* Function: putc
+     *  Write a character
+     *
+     * Variables:
+     *  c - The character to write to the serial port
+     */
+    int putc(int c);
+
+    /* Function: printf
+     *  Write a formated string
+     *
+     * Variables:
+     *  format - A printf-style format string, followed by the
+     *      variables to use in formating the string.
+     */
+    int printf(const char* format, ...);
+#endif
+
+    /* Function: locate
+     * Locate to a certian position
+     *
+     * Variables:
+     *  column - the column to locate to, from 0..15
+     *  row - the row to locate to, from 0..1
+     */
+    virtual void locate(int column, int row);
+
+    /* Function: cls
+     * Clear the screen, and locate to 0,0
+     */
+    virtual void cls();
+
+    virtual void reset();
+
+protected:
+
+    void clock();
+    void writeData(int data);
+    void writeCommand(int command);
+    void writeByte(int value, bool rs);
+    void writeNibble(int value, bool rs);
+    void writeI2CByte(int data);
+    virtual int _putc(int c);
+    virtual int _getc();
+    virtual void newline();
+
+    int _row;
+    int _column;
+    int _columns;
+    int _rows;
+
+    I2C _i2c;
+    int _i2cAddress;
+        
+private:
+
+   
+
+};
+
+}
+
+#endif
+
+