Minh Nguyen / LCD
Revision:
2:e3d3eaa27040
Parent:
1:707fccd6c949
Child:
3:a65abeda9231
diff -r 707fccd6c949 -r e3d3eaa27040 LCD.cpp
--- a/LCD.cpp	Tue Mar 02 03:32:43 2021 +0000
+++ b/LCD.cpp	Mon Mar 08 14:33:47 2021 +0000
@@ -5,13 +5,15 @@
 //Code for Newhaven NHD‐0216K3Z‐FL‐GBW LCD interfaced to FRDM-K64F via I2C
 LCD::LCD() : i2c(PIN_SCL, PIN_SDA){
     LCD_init();
+    clearLCD();
+    wait_us(LCD_DELAY); //wait for stabilization
 }
 
 void LCD::LCD_init(void) {
     
     //LCD Initiations---------------------------------------
 
-    i2c.frequency(LCD_FREQ); //Corresponds to 50KHz    
+    i2c.frequency(LCD_FREQ);
     
     char cmd[3]; 
   
@@ -50,16 +52,18 @@
 
 void LCD::LCD_display(string topLine, string bottomLine) {
     
-    char cmd[] = "                "; //Long enough to send a complete line of text
+    int i;
+    
+    char cmd[16]; //Long enough to send a complete line of text
+    for(i = 0; i < 16; i++)
+        cmd[i] = ' ';
 
 
     clearLCD(); //Clear before display
 
     wait_us(LCD_DELAY); //Short Delay 
-
-    int i;
   
-    for(i=0;(i<16 && i < topLine.length());i++) { //Display top line
+    for(i=0;i < topLine.length();i++) { //Display top line
         cmd[i]=topLine[i];
     }
     i2c.write(addr,cmd,16);
@@ -70,8 +74,10 @@
     i2c.write(addr, cmd, 3);
        
     wait_us(LCD_DELAY); //Delay between writing lines
-        
-    for(i=0;(i<16 && i < topLine.length());i++) { //Display bottom line
+    
+    for(i = 0; i < 16; i++)
+        cmd[i] = ' ';
+    for(i=0;i < bottomLine.length();i++) { //Display bottom line
         cmd[i]=bottomLine[i];
     }
     i2c.write(addr,cmd,16);