character LCD module to I2C adapter

Dependencies:   mbed I2CLCD

Files at this revision

API Documentation at this revision

Comitter:
okini3939
Date:
Thu Oct 14 14:45:16 2010 +0000
Child:
1:16e014cddf29
Commit message:

Changed in this revision

I2CLCD/I2CLCD.cpp Show annotated file Show diff for this revision Revisions of this file
I2CLCD/I2CLCD.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/I2CLCD/I2CLCD.cpp	Thu Oct 14 14:45:16 2010 +0000
@@ -0,0 +1,174 @@
+/*
+ * mbed library to use a I2C LCD
+ * Copyright (c) 2010 Hiroshi Suga
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
+
+#include "mbed.h"
+#include "I2CLCD.h"
+
+#define I2CLCD_ADDR 0x7c
+
+
+int I2CLCD::_putc (int value) {
+
+    if (value == '\n') {
+        x = 0;
+        y ++;
+        if (y >= rows()) {
+            y = 0;
+            lcd_out(address(x, y), 0);
+        }
+
+    } else {
+
+        lcd_out(address(x, y), 0);
+        lcd_out(value, 1);
+        x ++;
+        if (x >= cols()) {
+            x = 0;
+            y ++;
+            if (y >= rows()) {
+                y = 0;
+                lcd_out(address(x, y), 0);
+            }
+        }
+    }
+
+    return value;
+}
+
+int I2CLCD::_getc() {
+    return lcd_in(0);
+}
+
+
+I2CLCD::I2CLCD (PinName p_sda, PinName p_scl, I2CLCDType p_type, I2CLCDConfig p_config) : i2c(p_sda, p_scl) {
+    init(p_type, p_config);
+}
+
+I2CLCD::I2CLCD (I2C& p_i2c, I2CLCDType p_type, I2CLCDConfig p_config) : i2c(p_i2c) {
+    init(p_type, p_config);
+}
+
+void I2CLCD::init (I2CLCDType p_type, I2CLCDConfig p_config) {
+
+    type = p_type;
+
+    lcd_cfg(p_config);
+
+    wait(0.5);
+    lcd_out(0x30, 0);
+    wait(0.005);
+    lcd_out(0x30, 0);
+    wait(0.002);
+    lcd_out(0x30, 0);
+
+    lcd_out(0x38, 0); // func 
+    lcd_out(0x10, 0); // shift
+    lcd_out(0x0c, 0); // display
+    lcd_out(0x06, 0); // entry mode
+    cls();
+}
+
+void I2CLCD::cls() {
+    lcd_out(0x01, 0); // clear
+    wait(0.002);
+    lcd_out(0x02, 0); // home
+    wait(0.002);
+    locate(0, 0);
+}
+
+void I2CLCD::locate(int col, int row) {
+    x = col;
+    y = row;
+    lcd_out(address(x, y), 0);
+}
+
+int I2CLCD::address(int col, int row) {
+    switch (type) {
+        case LCD16x1:
+            return (col < 8 ? 0x80 : 0xc0) + (col & 0x03);
+        case LCD16x4:
+        case LCD20x4:
+            switch (row) {
+                case 0:
+                    return 0x80 + col;
+                case 1:
+                    return 0xc0 + col;
+                case 2:
+                    return 0x94 + col;
+                case 3:
+                    return 0xd4 + col;
+            }
+        case LCD16x2B:
+            return 0x80 + (row * 40) + col;
+        case LCD8x2:
+        case LCD16x2:
+        case LCD20x2:
+        default:
+            return 0x80 + (row * 0x40) + col;
+    }
+}
+
+int I2CLCD::cols() {
+    switch (type) {
+        case LCD8x2:
+            return 8;
+        case LCD20x4:
+        case LCD20x2:
+            return 20;
+        case LCD16x1:
+        case LCD16x2:
+        case LCD16x2B:
+        case LCD16x4:
+        default:
+            return 16;
+    }
+}
+
+int I2CLCD::rows() {
+    switch (type) {
+        case LCD16x1:
+            return 1;
+        case LCD16x4:
+        case LCD20x4:
+            return 4;
+        case LCD8x2:
+        case LCD16x2:
+        case LCD16x2B:
+        case LCD20x2:
+        default:
+            return 2;
+    }
+}
+
+void I2CLCD::lcd_cfg (I2CLCDConfig cfg) {
+    i2c.start();
+    i2c.write(I2CLCD_ADDR);
+    i2c.write(LCDCFG_ENABLE | (cfg & 0x1f));
+    i2c.stop();
+}
+
+void I2CLCD::lcd_out (int dat, int rs) {
+    i2c.start();
+    i2c.write(I2CLCD_ADDR);
+    i2c.write(rs ? 0x40 : 0);
+    i2c.write(dat);
+    i2c.stop();
+}
+
+int I2CLCD::lcd_in (int rs) {
+    int i;
+
+    i2c.start();
+    i2c.write(I2CLCD_ADDR);
+    i2c.write(rs ? 0x40 : 0);
+
+    i2c.start();
+    i2c.write(I2CLCD_ADDR | 0x01);
+    i = i2c.read(0);
+    i2c.stop();
+
+    return i;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/I2CLCD/I2CLCD.h	Thu Oct 14 14:45:16 2010 +0000
@@ -0,0 +1,56 @@
+/*
+ * mbed library to use a I2C LCD
+ * Copyright (c) 2010 Hiroshi Suga
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
+ 
+#ifndef I2CLCD_H
+#define I2CLCD_H
+
+#include "mbed.h"
+
+enum I2CLCDType {
+    LCD8x2,
+    LCD16x1,
+    LCD16x2,
+    LCD16x2B,
+    LCD16x4,
+    LCD20x2,
+    LCD20x4
+};
+
+enum I2CLCDConfig {
+    LCDCFG_ENABLE   = 0x20,
+    LCDCFG_PWMCOUNT = 0x10,
+    LCDCFG_LED      = 0x08,
+    LCDCFG_3V       = 0x04,
+    LCDCFG_ADDR     = 0x02,
+    LCDCFG_INIT     = 0x01
+};
+
+class I2CLCD : public Stream {
+public:
+    I2CLCD (PinName p_sda, PinName p_scl, I2CLCDType p_type = LCD16x2, I2CLCDConfig p_config = LCDCFG_3V);
+    I2CLCD (I2C& p_i2c, I2CLCDType p_type = LCD16x2, I2CLCDConfig p_config = LCDCFG_3V);
+
+    void locate (int, int);
+    void cls ();
+    void lcd_cfg (I2CLCDConfig);
+
+protected:
+    virtual int _putc (int);
+    virtual int _getc ();
+
+    int address (int, int);
+    int rows ();
+    int cols ();
+    void init (I2CLCDType, I2CLCDConfig);
+    void lcd_out (int, int);
+    int lcd_in (int);
+
+    I2C i2c;
+    I2CLCDType type;
+    int x, y;
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Oct 14 14:45:16 2010 +0000
@@ -0,0 +1,26 @@
+#include "mbed.h"
+#include "I2CLCD.h"
+
+I2C i2c(p9, p10);
+I2CLCD i2clcd(i2c);
+/*
+DigitalOut pullup1(p19);
+DigitalOut pullup2(p20);
+*/
+//I2CLCD i2clcd(p9, p10);
+
+DigitalOut myled(LED1);
+
+int main() {
+    int i = 1;
+   
+//    pullup1 = pullup2 = 1;
+
+    myled = 1;
+    i2clcd.putc('0' + i);
+    i2clcd.putc('A' + i);
+    i2clcd.putc('a' + i);
+    myled = 0;
+
+    for (;;);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Oct 14 14:45:16 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e