OSDN Git Service

[SCHEMATIC] Modify SW/MW/LW Preamp, insert galbanic-isolator replace of common-mode...
[openi2cradio/OpenI2CRadio.git] / lcd_acm1602.c
index fbf212d..d33af14 100644 (file)
 #include "lcd_acm1602.h"
 
 #ifdef _USE_I2C_1602
-static unsigned char lcd_busychk(void)
+static unsigned char lcd_busychk(addr)
 {
     unsigned char b, d;
-    // Under development.
-    d = 0xff;
+    i2c_idle();
+    OPENASMASTER();
+    i2c_writechar(addr | 0x01); // Read
+    i2c_writechar(0x00);
+    d = i2c_readchar();
+    CLOSEASMASTER();
+    _ACM1602_I2C_WAIT(); // 5.5ms
     if((d & _LCDPORT_BUSYMASK) != 0){
         d = 0xff; // BUSY
     } else {
@@ -43,13 +48,13 @@ static unsigned char lcd_busychk(void)
     return d;
 }
 
-static void lcd_waitbusy(unsigned char flag)
+void lcd_waitbusy(unsigned char addr, unsigned char flag)
 {
     unsigned char f,ff;
     ff = flag;
     if(ff != 0) ff = 0xff;
     do {
-        f = lcd_busychk();
+        f = lcd_busychk(addr);
         _ACM1602_SHORT_WAIT(); // Wait 0.1ms
     } while(f != ff);
 }
@@ -59,11 +64,11 @@ static void sendcmd(unsigned char addr, unsigned char cmd, unsigned char busyfla
 {
     i2c_idle();
     OPENASMASTER();
-    i2c_writechar(addr);
+    i2c_writechar(addr & 0xfe);
     i2c_writechar(0x00);
     i2c_writechar(cmd);
     CLOSEASMASTER();
-    _ACM1602_I2C_WAIT(); // 0.2ms
+    _ACM1602_I2C_WAIT(); // 5.5ms
 }
 
 static void sendonce(unsigned char addr, unsigned char cmd)
@@ -81,20 +86,37 @@ void acm1602_putchar(unsigned char addr, unsigned char c)
 {
     i2c_idle();
     OPENASMASTER();
-    i2c_writechar(addr);
+    i2c_writechar(addr & 0xfe);
     i2c_writechar(0x80); //Data
     i2c_writechar(c); // Putchar
     CLOSEASMASTER();
-    _ACM1602_SHORT_WAIT(); // Wait 50us
+    _ACM1602_I2C_WAIT(); // Wait 50us
 }
 
+
 unsigned char acm1602_getchar(unsigned char addr)
 {
-    // Under development
-    return 0x00;
+    unsigned char d;
+    i2c_idle();
+    OPENASMASTER();
+    i2c_writechar(addr | 0x01); // Read
+    i2c_writechar(0x80);
+    d = i2c_readchar();
+    CLOSEASMASTER();
+    _ACM1602_I2C_WAIT(); // 5.5ms
+    return d;
 }
 #else // Parallel
-static unsigned char lcd_busychk(void)
+static void _ACM1602_TC_WAIT(void)
+{
+#ifdef __SDCC
+   delay10tcy(4);
+#else
+   __delay_us(5);
+#endif
+}
+
+static unsigned char lcd_busychk(unsigned char addr)
 {
     unsigned char b, d;
 
@@ -103,20 +125,35 @@ static unsigned char lcd_busychk(void)
     b |= _LCDPORT_READMASK;// Set mask for Tristate, direction = read.
     _LCDPORT_TRIS_DATA = b;
 
-    b = _LCDPORT_CONT_LATCH;
-    b &= ~_LCDPORT_CONT_RS;
-    b |= _LCDPORT_CONT_RW;
-    _LCDPORT_CONT_LATCH = b;
-    b |= _LCDPORT_CONT_EN; // Send CMD
-    _LCDPORT_CONT_LATCH = b;
+//    b = _LCDPORT_CONT_LATCH;
+    _LCDPORT_CONT_RS = 0;
+    _LCDPORT_CONT_RW = 1;
+//    _LCDPORT_CONT_LATCH = b;
+    _ACM1602_TC_WAIT(); // Wait 2.5?us
+
+    _LCDPORT_CONT_EN = 1; // Send CMD
+//    _LCDPORT_CONT_LATCH = b;
     _ACM1602_TC_WAIT(); // Wait 2.5?us
+
     d = _LCDPORT_DATA;
-    b &= ~_LCDPORT_CONT_EN; // Disable EN
-    _LCDPORT_CONT_LATCH = b;
+    _ACM1602_TC_WAIT(); // Wait 2.5?us
+
+    _LCDPORT_CONT_EN = 0; // Disable EN
+//    _LCDPORT_CONT_LATCH = b;
+    _ACM1602_TC_WAIT(); // Wait 2.5?us
+
+    _LCDPORT_CONT_EN = 1; // Disable EN
+//    _LCDPORT_CONT_LATCH = b;
+    _ACM1602_TC_WAIT(); // Wait 2.5?us
+
+    _LCDPORT_CONT_EN = 0;; // Disable EN
+//    _LCDPORT_CONT_LATCH = b;
+    _ACM1602_TC_WAIT(); // Wait 2.5?us
 
     b = _LCDPORT_TRIS_DATA;
     b &= ~_LCDPORT_READMASK; // Clear mask for Tristate, direction = write.
     _LCDPORT_TRIS_DATA = b;
+    _ACM1602_TC_WAIT(); // Wait 2.5?us
 
     if((d & _LCDPORT_BUSYMASK) != 0){
         d = 0xff; // BUSY
@@ -126,13 +163,13 @@ static unsigned char lcd_busychk(void)
     return d;
 }
 
-static void lcd_waitbusy(unsigned char flag)
+void lcd_waitbusy(unsigned char addr, unsigned char flag)
 {
     unsigned char f,ff;
     ff = flag;
     if(ff != 0) ff = 0xff;
     do {
-        f = lcd_busychk();
+        f = lcd_busychk(addr);
         _ACM1602_SHORT_WAIT(); // Wait 0.1ms
     } while(f != ff);
 }
@@ -146,48 +183,56 @@ static void sendcmd(unsigned char addr, unsigned char cmd,unsigned char busyflag
     h = cmd & 0xf0; // Higher bit is used.
     l = (cmd & 0x0f) << 4; // Higher bit is used.
     // Higher NIBBLE
-    if(busyflag != 0) lcd_waitbusy(0);
+    if(busyflag != 0) lcd_waitbusy(addr, 0);
     d = _LCDPORT_TRIS_CONT;
     d &= ~_LCDPORT_READMASK; // Clear mask for Tristate, direction = write.
     _LCDPORT_TRIS_CONT = d;
 
-
-    b = _LCDPORT_CONT_LATCH;
-    b &= ~(_LCDPORT_CONT_RW | 0xf0 | _LCDPORT_CONT_EN | _LCDPORT_CONT_RS); // DATA=blank,RW='0',RS='0"
-    b |= h;
-    _LCDPORT_CONT_LATCH = b;
+    _LCDPORT_CONT_RW = 0;
+    _LCDPORT_CONT_EN = 0;
+    _LCDPORT_CONT_RS = 0;
+    _LCDPORT_CONT_LATCH &= 0x0f; // Upper
+//    b &= ~(_LCDPORT_CONT_RW | 0xf0 | _LCDPORT_CONT_EN | _LCDPORT_CONT_RS); // DATA=blank,RW='0',RS='0"
+    _LCDPORT_CONT_LATCH |= h;
+//    _LCDPORT_CONT_LATCH = b;
     _ACM1602_TC_WAIT(); // Wait 2.5?us
-    b |= _LCDPORT_CONT_EN;
-    _LCDPORT_CONT_LATCH = b;
+
+    _LCDPORT_CONT_EN = 1;
+    //_LCDPORT_CONT_LATCH = b;
     _ACM1602_TC_WAIT(); // Wait 2.5?us
-    b &= ~_LCDPORT_CONT_EN; // Disable EN
-    _LCDPORT_CONT_LATCH = b;
+
+    _LCDPORT_CONT_EN = 0; // Disable EN
+//    _LCDPORT_CONT_LATCH = b;
     _ACM1602_TC_WAIT();
 
     // Lower NIBBLE
-    if(busyflag != 0) lcd_waitbusy(0);
-    d = _LCDPORT_TRIS_CONT;
-    d &= ~_LCDPORT_READMASK; // Clear mask for Tristate, direction = write.
-    _LCDPORT_TRIS_CONT = d;
-    b = _LCDPORT_CONT_LATCH;
-    b &= ~(_LCDPORT_CONT_RW | 0xf0 | _LCDPORT_CONT_EN | _LCDPORT_CONT_RS); // DATA=blank,RW='0',RS='0"
-    b |= l;
-    _LCDPORT_CONT_LATCH = b;
+//    if(busyflag != 0) lcd_waitbusy(addr, 0);
+//    d = _LCDPORT_TRIS_CONT;
+//    d &= ~_LCDPORT_READMASK; // Clear mask for Tristate, direction = write.
+//    _LCDPORT_TRIS_CONT = d;
+
+    _LCDPORT_CONT_RW = 0;
+    _LCDPORT_CONT_EN = 0;
+    _LCDPORT_CONT_RS = 0;
+    _LCDPORT_CONT_LATCH &= 0x0f; // Lower
+
+    _LCDPORT_CONT_LATCH |= l;
     _ACM1602_TC_WAIT(); // Wait 2.5?us
-    b |= _LCDPORT_CONT_EN;
-    _LCDPORT_CONT_LATCH = b;
+    _LCDPORT_CONT_EN = 1;
+//    _LCDPORT_CONT_LATCH = b;
+
     _ACM1602_TC_WAIT(); // Wait 2.5?us
-    b &= ~_LCDPORT_CONT_EN; // Disable EN
-    _LCDPORT_CONT_LATCH = b;
+    _LCDPORT_CONT_EN = 0; // Disable EN
+//    _LCDPORT_CONT_LATCH = b;
 
-    d = _LCDPORT_TRIS_CONT;
-    d &= ~_LCDPORT_READMASK; // Clear mask for Tristate, direction = write.
-    _LCDPORT_TRIS_CONT = d;
+//    d = _LCDPORT_TRIS_CONT;
//   d &= ~_LCDPORT_READMASK; // Clear mask for Tristate, direction = write.
+  //  _LCDPORT_TRIS_CONT = d;
 
 #else
     unsigned char b,d;
 
-    if(busyflag != 0) lcd_waitbusy(0);
+    if(busyflag != 0) lcd_waitbusy(addr, 0);
     d = _LCDPORT_TRIS_DATA;
     d &= ~_LCDPORT_READMASK; // Clear mask for Tristate, direction = write.
     _LCDPORT_TRIS_DATA = d;
@@ -222,26 +267,29 @@ static void sendonce(unsigned char addr, unsigned char cmd)
 
     h = cmd & 0xf0; // Higher bit is used.
     // Higher NIBBLE
+    //d = _LCDPORT_TRIS_CONT;
+//    d = TRIS_D_VAL; // Clear mask for Tristate, direction = write.
+//    _LCDPORT_TRIS_CONT = d;
     d = _LCDPORT_TRIS_CONT;
     d &= ~_LCDPORT_READMASK; // Clear mask for Tristate, direction = write.
     _LCDPORT_TRIS_CONT = d;
 
-    b = _LCDPORT_CONT_LATCH;
-    b &= ~(_LCDPORT_CONT_RS | _LCDPORT_CONT_RW | _LCDPORT_CONT_EN); // RS='0',RW='0'
-    b |= h;
-    _LCDPORT_CONT_LATCH = b;
+    _LCDPORT_CONT_RS = 0;
+    _LCDPORT_CONT_RW = 0;
+    _LCDPORT_CONT_EN = 0; // RS='0',RW='0'
+    _LCDPORT_CONT_LATCH &= 0x0f;
+    _LCDPORT_CONT_LATCH |= h;
+    _ACM1602_TC_WAIT(); // Wait 2.5?us
 
-    b |= _LCDPORT_CONT_EN; // Send CMD
-    _LCDPORT_CONT_LATCH = b;
+    _LCDPORT_CONT_EN = 1; // Send CMD
     _ACM1602_TC_WAIT(); // Wait 2.5?us
 
-    b &= ~_LCDPORT_CONT_EN; // Disable EN
-    _LCDPORT_CONT_LATCH = b;
+    _LCDPORT_CONT_EN = 0; // Disable EN
     _ACM1602_TC_WAIT();
 
-    d = _LCDPORT_TRIS_CONT;
-    d &= ~_LCDPORT_READMASK; // Clear mask for Tristate, direction = write.
-    _LCDPORT_TRIS_CONT = d;
+//    d = _LCDPORT_TRIS_CONT;
+//    d &= ~_LCDPORT_READMASK; // Clear mask for Tristate, direction = write.
+//    _LCDPORT_TRIS_CONT = d;
 
 #else
     unsigned char b, d;
@@ -274,16 +322,15 @@ static void acm1602_ioinit(unsigned char addr)
 //    lcd_waitbusy(0);
     _ACM1602_LONG_LONG_WAIT();
 #ifdef _LCD_IF_4BIT
-    b = _LCDPORT_TRIS_CONT;
-    b &= ~(_LCDPORT_CONT_RS | _LCDPORT_CONT_RW | _LCDPORT_CONT_EN | 0xf0);
-    _LCDPORT_TRIS_CONT = b; // Clear Tristate bits, output.
-
-    b = _LCDPORT_CONT_LATCH;
-    b &= ~(_LCDPORT_CONT_RS | _LCDPORT_CONT_RW | _LCDPORT_CONT_EN | 0xf0);
-    _LCDPORT_CONT_LATCH = b;
-
-    //_LCDPORT_TRIS_DATA = 0x00; // OUT
-    //_LCDPORT_LATCH_DATA = 0x00;
+    _LCDPORT_TRIS_CONT = TRIS_D_VAL;
+
+    _LCDPORT_CONT_RS = 0;
+    _LCDPORT_CONT_RW = 0;
+    _LCDPORT_CONT_EN = 0;
+    _LCDPORT_CONT_LATCH &= ~0xf0;
+//    _LCDPORT_CONT_LATCH = b;
+  //  _LCDPORT_TRIS_DATA = 0x00; // OUT
+  //  _LCDPORT_LATCH_DATA = 0x00;
 #else
     b = _LCDPORT_TRIS_CONT;
     b &= ~(_LCDPORT_CONT_RS | _LCDPORT_CONT_RW | _LCDPORT_CONT_EN);
@@ -305,49 +352,46 @@ void acm1602_putchar(unsigned char addr, unsigned char c)
     h = c & 0xf0;
     l = (c & 0x0f)<<4;
 
-    lcd_waitbusy(0);
-    d = _LCDPORT_TRIS_CONT;
-    d &= ~_LCDPORT_READMASK; // Clear mask for Tristate, direction = write.
+    lcd_waitbusy(addr, 0);
+//    d = _LCDPORT_TRIS_CONT;
+//    d &= 0x00; // Clear mask for Tristate, direction = write.
+    d = TRIS_D_VAL; // ALL OUT
     _LCDPORT_TRIS_CONT = d;
+    _ACM1602_TC_WAIT(); // Wait 2.5?us
 
     // Send high nibble
-    b = _LCDPORT_CONT_LATCH;
-    b &= ~(_LCDPORT_CONT_RW | 0xf0 | _LCDPORT_CONT_EN); // DATA=blank,RW='0'
-    b |= _LCDPORT_CONT_RS; // RS='1"
-    b |= h;
-    _LCDPORT_CONT_LATCH = b;
+    //b = _LCDPORT_CONT_LATCH;
+    _LCDPORT_CONT_RW = 0;
+    _LCDPORT_CONT_EN = 0; // DATA=blank,RW='0'
+    _LCDPORT_CONT_LATCH &= ~0xf0;
+    _LCDPORT_CONT_RS = 1; // RS='1"
+//    _LCDPORT_CONT_LATCH = b;
     _ACM1602_TC_WAIT(); // Wait 2.5?us
 
-    b |= _LCDPORT_CONT_EN; // Do high write.
-    _LCDPORT_CONT_LATCH = b;
+    _LCDPORT_CONT_EN = 1; // Do high write.
+//    b |= h;
+    _LCDPORT_CONT_LATCH |= h;
     _ACM1602_TC_WAIT(); // Wait 2.5?us
 
-    b &= ~_LCDPORT_CONT_EN; // Disable EN
-    _LCDPORT_CONT_LATCH = b;
-    _ACM1602_TC_WAIT();
-
-    lcd_waitbusy(0);
-    d = _LCDPORT_TRIS_CONT;
-    d &= ~_LCDPORT_READMASK; // Clear mask for Tristate, direction = write.
-    _LCDPORT_TRIS_CONT = d;
+    _LCDPORT_CONT_EN = 0; // Disable EN
+//    _LCDPORT_CONT_LATCH = b;
+    _ACM1602_SHORT_WAIT();
 
-    b = _LCDPORT_CONT_LATCH;
-    b &= ~(_LCDPORT_CONT_RW | 0xf0 | _LCDPORT_CONT_EN); // DATA=blank,RW='0'
-    b |= _LCDPORT_CONT_RS; // RS='1"
-    b |= l;
-    _LCDPORT_CONT_LATCH = b;
+//    lcd_waitbusy(addr, 0);
+    _LCDPORT_CONT_LATCH &= ~0xf0;
+    _LCDPORT_CONT_RS = 1; // RS='1"
     _ACM1602_TC_WAIT(); // Wait 2.5?us
 
-    b |= _LCDPORT_CONT_EN;
-    _LCDPORT_CONT_LATCH = b; // Do low write.
+    _LCDPORT_CONT_LATCH |= l; // Do low write.
+    _LCDPORT_CONT_EN = 1;
     _ACM1602_TC_WAIT(); // Wait 2.5?us
 
-    b &= ~_LCDPORT_CONT_EN; // Disable EN
-    _LCDPORT_CONT_LATCH = b;
+    _LCDPORT_CONT_EN = 0; // Disable EN
     _ACM1602_SHORT_WAIT();
 
+
 #else
-    lcd_waitbusy(0);
+    lcd_waitbusy(addr, 0);
     d = _LCDPORT_TRIS_DATA;
     d &= ~_LCDPORT_READMASK; // Clear mask for Tristate, direction = write.
     _LCDPORT_TRIS_DATA = d;
@@ -369,98 +413,6 @@ void acm1602_putchar(unsigned char addr, unsigned char c)
 #endif
 }
 
-unsigned char acm1602_getchar(unsigned char addr)
-{
-    unsigned char b, d;
-#ifdef _LCD_IF_4BIT
-    unsigned char h, l;
-
-    lcd_waitbusy(0);
-    d = _LCDPORT_TRIS_CONT;
-    d |= _LCDPORT_READMASK; // Set mask for Tristate, direction = read.
-    _LCDPORT_TRIS_CONT = d;
-
-    // Recv high nibble
-    b = _LCDPORT_CONT_LATCH;
-    b &= ~(0xf0 | _LCDPORT_CONT_EN); // DATA=blank
-    b |= (_LCDPORT_CONT_RS | _LCDPORT_CONT_RW); // RS='1", RW='1'
-    _LCDPORT_CONT_LATCH = b;
-    _ACM1602_TC_WAIT(); // Wait 2.5?us
-
-    b |= _LCDPORT_CONT_EN;
-    _LCDPORT_CONT_LATCH = b;
-    _ACM1602_TC_WAIT(); // Wait 2.5?us
-
-    h = _LCDPORT_DATA & 0xf0;
-    _ACM1602_TC_WAIT(); // Wait 2.5?us
-
-    b &= ~_LCDPORT_CONT_EN; // Disable EN
-    _LCDPORT_CONT_LATCH = b;
-    _ACM1602_TC_WAIT();
-
-    lcd_waitbusy(0);
-    // Recv Low nibble.
-    d = _LCDPORT_TRIS_CONT;
-    d |= _LCDPORT_READMASK; // Set mask for Tristate, direction = read.
-    _LCDPORT_TRIS_CONT = d;
-
-    b = _LCDPORT_CONT_LATCH;
-    b &= ~(0xf0 | _LCDPORT_CONT_EN); // DATA=blank
-    b |= (_LCDPORT_CONT_RS | _LCDPORT_CONT_RW); // RS='1", RW='1'
-    _LCDPORT_CONT_LATCH = b;
-    _ACM1602_TC_WAIT(); // Wait 2.5?us
-
-    b |= _LCDPORT_CONT_EN;
-    _LCDPORT_CONT_LATCH = b;
-    _ACM1602_TC_WAIT(); // Wait 2.5?us
-
-    l = _LCDPORT_DATA & 0xf0;
-    _ACM1602_TC_WAIT(); // Wait 2.5?us
-
-    b &= ~_LCDPORT_CONT_EN; // Disable EN
-    _LCDPORT_CONT_LATCH = b;
-    _ACM1602_TC_WAIT(); // Wait 2.5?us
-
-    d = _LCDPORT_TRIS_CONT;
-    d &= ~_LCDPORT_READMASK; // Set mask for Tristate, direction = write.
-    _LCDPORT_TRIS_CONT = d;
-
-    _ACM1602_SHORT_WAIT();
-
-    d = h | (l >> 4);
-    return d;
-#else
-    unsigned char e;
-    lcd_waitbusy(0);
-    d = _LCDPORT_TRIS_DATA;
-    d |= _LCDPORT_READMASK; // Set mask for Tristate, direction = read.
-    _LCDPORT_TRIS_DATA = d;
-
-    b = _LCDPORT_CONT_LATCH;
-    b |= (_LCDPORT_CONT_RS | _LCDPORT_CONT_RW); // RW=1, RS=1
-    b &= ~_LCDPORT_CONT_EN; // Clear Enable
-    _LCDPORT_CONT_LATCH = b;
-    _ACM1602_TC_WAIT();
-
-    b |= _LCDPORT_CONT_EN; // Send character
-    _LCDPORT_CONT_LATCH = b;
-    _ACM1602_TC_WAIT(); // Wait 2.5?us
-
-    e = _LCDPORT_DATA; // Read BYTE
-    _ACM1602_TC_WAIT();
-
-    b &= ~_LCDPORT_CONT_EN; // Disable EN
-    _LCDPORT_CONT_LATCH = b;
-
-    d = _LCDPORT_TRIS_DATA;
-    d &= ~_LCDPORT_READMASK; // Clear mask for Tristate, direction = read.
-    _LCDPORT_TRIS_DATA = d;
-
-    _ACM1602_SHORT_WAIT(); // Wait 0.1ms
-    return e;
-#endif
-}
-
 #endif
 
 void acm1602_cls(unsigned char addr)
@@ -469,17 +421,32 @@ void acm1602_cls(unsigned char addr)
     _ACM1602_LONG_WAIT(); // Wait 5ms
 }
 
-void acm1602_locate(unsigned char addr, char x, char y)
+void acm1602_setdataramaddress(unsigned char addr, unsigned char pos)
+{
+    unsigned char cmd = (pos & 0x7f) | 0x80;
+    sendcmd(addr, cmd, 0xff);
+}
+
+
+void acm1602_locate_16x2(unsigned char addr, unsigned char x, unsigned char y)
 {
     unsigned char ramaddr;
-    if((x < 0) || (x > 15)) return;
-    if((y < 0) || (y > 1)) return;
-    if(y == 0){
-        ramaddr = 0x80 | (x & 0x0f);
-    } else {
-        ramaddr = 0xc0 | (x & 0x0f);
-    }
-    sendcmd(addr, ramaddr, 0xff);
+    if(x > 16) return;
+    if(y > 1) return;
+    ramaddr = (y == 0)? x  : (x  | 0x40);
+    acm1602_setdataramaddress(addr, ramaddr);
+}
+
+/*
+ * For using
+ */
+void acm1602_locate_8x2(unsigned char addr, unsigned char x, unsigned char y)
+{
+    unsigned char ramaddr;
+    if(x > 8) return;
+    if(y > 1) return;
+    ramaddr = (y == 0)? x : x | 0x40;
+    acm1602_setdataramaddress(addr, ramaddr);
 }
 
 void acm1602_home(unsigned char addr)
@@ -488,65 +455,108 @@ void acm1602_home(unsigned char addr)
     _ACM1602_LONG_WAIT(); // Wait 5ms
 }
 
-void acm1602_printf(unsigned char addr, const char *fmt, ...)
+
+void acm1602_cursordir(unsigned char addr, unsigned char right)
 {
-    char strbuf[32]; // Max 32 bytes.
-    va_list args;
-    int i;
-    int l;
-
-    strbuf[0] = '\0';
-    va_start(args, fmt );
-    sprintf(strbuf, args);
-    l = strlen(strbuf);
-    if((l >= 32) || (l <= 0))return;
-    for(i = 0; i < l; i++) {
-        acm1602_putchar(addr, strbuf[i]);
+    unsigned char cmd;
+
+    cmd = 0x14;
+    if(right == 0)  {
+        cmd = 0x10;
     }
+    sendcmd(addr, cmd, 0xff);
 }
 
-void acm1602_cursordir(unsigned char addr, unsigned char right)
+void acm1602_dispcursor(unsigned char addr, unsigned char flag)
 {
     unsigned char cmd;
-    if(right == 0) {
-        cmd = 0x18;
-    } else {
-        cmd = 0x1c;
+    cmd = 0x0d; // Cursor ON
+    if(flag == 0x00) {
+        cmd = 0x0c; // Cursor off
     }
     sendcmd(addr, cmd, 0xff);
 }
 
+void acm1602_setcgramaddress(unsigned char addr, unsigned char pos)
+{
+    unsigned char cmd = (pos & 0x3f) | 0x40;
+    sendcmd(addr, cmd, 0xff);
+}
+
 void acm1602_init(unsigned char addr, unsigned char cls)
 {
     acm1602_ioinit(addr);
 #ifdef _USE_I2C_1602
-    sendonce(addr, 0x38, 0x00); // 2lines, 8x10dot fonts.
-    _ACM1602_SHORT_WAIT();
+    sendcmd(addr,0x01,0x00);
     sendcmd(addr, 0x38, 0x00); // 2lines, 8x10dot fonts.
-    _ACM1602_SHORT_WAIT();
+//    sendcmd(addr, 0x38, 0x00); // 2lines, 8x10dot fonts.
 #else
   #ifndef _LCD_IF_4BIT
     sendonce(addr, 0x38); // 2lines, 8x10dot fonts.
-    _ACM1602_SHORT_WAIT();
     sendcmd(addr, 0x38, 0x00); // 2lines, 8x10dot fonts.
-    _ACM1602_SHORT_WAIT();
-  #else
+  #else // 4Bit
+    _ACM1602_LONG_LONG_WAIT();
     sendonce(addr, 0x30); // 2lines, 8x10dot fonts.
-    _ACM1602_SHORT_WAIT();
+//    _ACM1602_SHORT_WAIT();
+    sendonce(addr, 0x30); // 2lines, 8x10dot fonts.
+//    _ACM1602_SHORT_WAIT();
+    sendonce(addr, 0x30); // 2lines, 8x10dot fonts.
+//    _ACM1602_SHORT_WAIT();
+    sendonce(addr, 0x20); // 2lines, 8x10dot fonts.
+//    _ACM1602_SHORT_WAIT();
     // Send twice on 4Bit Mode.
-    sendcmd(addr, 0x28, 0x00); // 2lines, 8x10dot fonts.
-    _ACM1602_SHORT_WAIT();
-    sendcmd(addr, 0x28, 0x00); // 2lines, 8x10dot fonts.
-    _ACM1602_SHORT_WAIT();
+//    sendonce(addr, 0x30); // 2lines, 8x10dot fonts.
+    sendcmd(addr, 0x28, 0xff); // 2lines, 8x5dot fonts.
   #endif
 #endif
-    sendcmd(addr, 0x0f, 0xff); // Display ON.
-    _ACM1602_SHORT_WAIT();
-    if(cls == 0){
+//    sendcmd(addr, 0x08, 0xff); // Display OFF.
+    sendcmd(addr, 0x0c, 0xff); // Display ON.
+//    sendcmd(addr, 0x06, 0xff); // Cursor increment,not shift.
+   if(cls == 0){
         acm1602_home(addr);
     } else {
         acm1602_cls(addr);
     }
-    sendcmd(addr, 0b00000110, 0xff); // Cursor increment,not shift.
-    _ACM1602_SHORT_WAIT();
-}
\ No newline at end of file
+//    _ACM1602_LONG_WAIT();
+    sendcmd(addr, 0x06, 0xff);
+}
+
+/*
+ * Suspend lcd
+ */
+void acm1602_suspend(unsigned char addr)
+{
+    sendcmd(addr, 0x08, 0xff);
+    _ACM1602_LONG_WAIT();
+}
+
+void acm1602_resume(unsigned char addr)
+{
+    sendcmd(addr, 0x0c, 0xff);
+    _ACM1602_LONG_WAIT();
+}
+
+
+#ifdef _USE_I2C_1602
+/*
+ * st7032 extensions.
+ */
+void st7032_extcmd(unsigned char addr, unsigned char mode, 
+        unsigned char cmd, unsigned char busyf)
+{
+    unsigned char m = mode & 0x1c;
+    m = m | 0x21;
+    sendcmd(addr, m, busyf); // Shift to IS1
+    sendcmd(addr, cmd, busyf);
+    m &= 0xfe; // Shift to IS0
+    sendcmd(addr, m, busyf);
+}
+
+void st7032_setcontrast(unsigned char addr, unsigned char value,
+        unsigned char busyf)
+{
+    unsigned char val = value & 0x0f;
+    val |= 0x70;
+    st7032_extcmd(addr, 0x38, val, busyf);
+}
+#endif