Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
lcd_i2c.cpp
00001 #include "mbed.h" 00002 #include "lcd_i2c.h" 00003 /* 00004 LCD (Optrex DCM16230, DCM20261, etc.) via I2C using a PCF8574 00005 00006 Frequency (clks/sec) Frequency 00007 Transfer rate = ------------------------------------------------ = ---------------- (characters/sec) 00008 9 clks/write * 3 writes/nibble * 2 nibbles/character 54 00009 */ 00010 I2C i2c(p9, p10); // (sda, scl) 00011 Serial pc2(USBTX, USBRX); // tx, rx 00012 00013 // PCF8574 output bits Px 00014 #define RS BIT1 00015 #define RW BIT2 00016 #define E BIT3 00017 #define DB4 BIT4 00018 #define DB5 BIT5 00019 #define DB6 BIT6 00020 #define DB7 BIT7 00021 00022 char update[NUPDATE]; 00023 char cw, address; 00024 00025 int updateDisplay(char *str, int nchars) 00026 { 00027 char w, ch, *p; 00028 int n, k, rc; 00029 00030 if (nchars > 20) 00031 return 1; 00032 n = 0; 00033 p = update; 00034 for (k = 0; k < nchars; k++) 00035 { 00036 // write most significant nibble 00037 ch = *str++; 00038 w = (ch & 0xf0) | cw; 00039 *p++ = w; 00040 *p++ = w | E; 00041 *p++ = w; 00042 00043 // write least significant nibble 00044 w = (ch << 4) | cw; 00045 *p++ = w; 00046 *p++ = w | E; 00047 *p++ = w; 00048 n += 6; 00049 } 00050 rc = i2c.write(address, update, n); // rc == 0 ==> ACK 00051 return rc; 00052 } 00053 00054 int write4Bits(char data) 00055 { 00056 char w; 00057 int rc; 00058 00059 w = (data & 0xf0) | cw; // grab the left nibble 00060 update[0] = w; 00061 update[1] = w | E; 00062 update[2] = w & ~ E; 00063 rc = i2c.write(address, update, 3); 00064 return rc; 00065 } 00066 00067 int initLcd(int i2cAddress, int frequency) 00068 { 00069 typedef struct { 00070 char cmd; 00071 char numNibbles; 00072 char delayMs; 00073 //char testBF; 00074 } INIT; 00075 static const INIT initData[] = { 00076 {3, 1, 5}, 00077 {3, 1, 1}, 00078 {3, 1, 1}, 00079 {2, 1, 1}, // Function set: 4-bit interface 00080 {0x28, 2, 2}, // Function set: 2 lines, 5x7 00081 {0x08, 2, 2}, // Display off 00082 {0x01, 2, 16}, // Clear display 00083 {0x06, 2, 2}, // Entry mode set: No increment, shift cursor 00084 {0x0e, 2, 2}}; // Display on: cursor on, blink off 00085 #define NINITD (sizeof(initData)/sizeof(initData[0])) 00086 INIT *p; 00087 int k, rc; 00088 // Initialization must start at least 16 ms after power up. 00089 i2c.frequency(frequency); 00090 address = i2cAddress; 00091 cw = 0; 00092 p = (INIT *)initData; 00093 for (k = 0; k < NINITD; k++) 00094 { 00095 //p = (INIT *)&initData[k]; 00096 if (p->numNibbles == 1) 00097 rc = write4Bits(p->cmd << 4); 00098 else 00099 { 00100 rc = write4Bits(p->cmd); // output the most significant nibble 00101 wait_us(10); // this will need to be adjusted when printf stmts are removed 00102 rc |= write4Bits(p->cmd << 4); // output the least significant nibble 00103 } 00104 if (rc) 00105 break; 00106 wait_ms(p->delayMs); 00107 p++; 00108 } 00109 return rc; 00110 } 00111 00112 int lcdWriteMsg(char *msg) 00113 { // msg must be a null-terminated string 00114 char rc; 00115 cw |= RS; // data 00116 cw &= ~RW; // write 00117 rc = updateDisplay(msg, strlen(msg)); 00118 return rc; 00119 } 00120 00121 int lcdWriteCmd(char cmd, int delayMs) 00122 { 00123 int rc; 00124 cw &= ~RS; // command 00125 rc = write4Bits(cmd); 00126 wait_us(10); 00127 rc |= write4Bits(cmd << 4); 00128 if (delayMs) 00129 wait_ms(delayMs); 00130 return rc; 00131 } 00132 00133 int lcdPositionCursor(char row, char col) 00134 { 00135 // Code may not be correct for displays larger than 20 x 2 00136 int x, rc; 00137 x = 0x80 | ((row & 1)<<6) | (col & 0xf); 00138 rc = lcdWriteCmd(x, 1); 00139 return rc; 00140 } 00141 00142 int lcdClearDisplay(void) 00143 { 00144 return lcdWriteCmd(1, 16); 00145 } 00146 00147 00148
Generated on Tue Jul 12 2022 21:13:13 by
1.7.2