Craig Evans / N5110

Dependents:   LV7_LCDtest LV7_Grupa5_Tim003_Zadatak1 lv7_Grupa5_Tim008_zad1 LV7_PAI_Grupa5_tim10_Zadatak1 ... more

Files at this revision

API Documentation at this revision

Comitter:
eencae
Date:
Wed Nov 01 20:48:05 2017 +0000
Parent:
43:c2598020fcac
Child:
45:97e54ea40dac
Commit message:
Separated out contrast, bias and temp coefficient methods. Modified init method to take this into account. Can now dynamically control contrast at run-time which makes it easier to account for natural variation between individual LCD displays.

Changed in this revision

N5110.cpp Show annotated file Show diff for this revision Revisions of this file
N5110.h Show annotated file Show diff for this revision Revisions of this file
--- a/N5110.cpp	Tue Mar 21 11:46:14 2017 +0000
+++ b/N5110.cpp	Wed Nov 01 20:48:05 2017 +0000
@@ -57,22 +57,14 @@
 {
     turnOn();     // power up
     reset();      // reset LCD - must be done within 100 ms
-
-    initSPI();
-    // function set - extended
-    sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE);
-    // Don't completely understand these parameters - they seem to work as they are
-    // Consult the datasheet if you need to change them
-    sendCommand(CMD_VOP_7V38);    // operating voltage - these values are from Chris Yan's Library
-    sendCommand(CMD_TC_TEMP_2);   // temperature control
-    sendCommand(CMD_BI_MUX_48);   // changing this can sometimes improve the contrast on some displays
-
-    // function set - basic
-    sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE);
+    initSPI();    
+    
+    setContrast(0.40);  // this may need tuning (say 0.4 to 0.6)
+    setBias(3);   // datasheet - 48:1 mux - don't mess with if you don't know what you're doing! 
+    setTempCoefficient(0); // datasheet - may need increasing (range 0 to 3) at very low temperatures
     normalMode();  // normal video mode by default
-    sendCommand(CMD_DC_NORMAL_MODE);  // black on white
-
-    clearRAM();      // RAM is undefined at power-up so clear
+    
+    clearRAM();      // RAM is undefined at power-up so clear to be sure
     clear();   // clear buffer
     setBrightness(0.5);
 }
@@ -80,13 +72,15 @@
 // sets normal video mode (black on white)
 void N5110::normalMode()
 {
-    sendCommand(CMD_DC_NORMAL_MODE);
+    sendCommand(0b00100000);   // basic instruction
+    sendCommand(0b00001100);  // normal video mode- datasheet
 }
 
 // sets normal video mode (white on black)
 void N5110::inverseMode()
 {
-    sendCommand(CMD_DC_INVERT_VIDEO);
+    sendCommand(0b00100000);   // basic instruction
+    sendCommand(0b00001101);   // inverse video mode - datasheet
 }
 
 // function to power up the LCD and backlight - only works when using GPIO to power
@@ -105,16 +99,15 @@
     setBrightness(0.0);  // turn backlight off
     clearRAM();   // clear RAM to ensure specified current consumption
     // send command to ensure we are in basic mode
-    sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE);
-    // clear the display
-    sendCommand(CMD_DC_CLEAR_DISPLAY);
-    // enter the extended mode and power down
-    sendCommand(0x20 | CMD_FS_POWER_DOWN_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE);
-    // small delay and then turn off the power pin
-    wait_ms(10);
-
+    
+    sendCommand(0b00100000); // basic mode
+    sendCommand(0b00001000); // clear display
+    sendCommand(0b00100001); // extended mode
+    sendCommand(0b00100100); // power down
+    
     // if we are powering the LCD using the GPIO then make it low to turn off
     if (_pwr != NULL) {
+        wait_ms(10);  // small delay and then turn off the power pin
         _pwr->write(0);  // turn off power
     }
 
@@ -132,6 +125,58 @@
     _led->write(brightness);
 }
 
+void N5110::setContrast(float contrast) {
+    
+    // enforce limits
+    if (contrast > 1.0)
+        contrast = 1.0;
+    else if (contrast < 0.0)
+        contrast = 0.0;
+    
+    // convert to char in range 0 to 127 (i.e. 6 bits)
+    char ic = char(contrast*127.0);
+    
+    sendCommand(0b00100001);  // extended instruction set
+    sendCommand(0b10000000 | ic);   // set Vop (which controls contrast)
+    sendCommand(0b00100000);  // back to basic instruction set
+}
+
+void N5110::setTempCoefficient(char tc) {
+    
+    // enforce limits
+    if (tc>3) {
+        tc=3;
+    }
+    
+    // temperature coefficient may need increasing at low temperatures
+
+    sendCommand(0b00100001);  // extended instruction set
+    sendCommand(0b00000100 | tc);
+    sendCommand(0b00100000);  // back to basic instruction set
+}
+    
+void N5110::setBias(char bias) {
+    
+    // from data sheet
+    // bias      mux rate
+    // 0        1:100
+    // 1        1:80
+    // 2        1:65
+    // 3        1:48   (default)
+    // 4        1:40/1:34
+    // 5        1:24
+    // 6        1:18/1:16
+    // 7        1:10/1:9/1:8
+    
+    // enforce limits
+    if (bias>7) {
+        bias=7;
+    }
+        
+    sendCommand(0b00100001);  // extended mode instruction
+    sendCommand(0b00010000 | bias);  
+    sendCommand(0b00100000); // end of extended mode instruction
+}
 
 // pulse the active low reset line
 void N5110::reset()
@@ -182,8 +227,9 @@
                          unsigned int const y)
 {
     if (x<WIDTH && y<HEIGHT) {  // check within range
-        sendCommand(0x80 | x);  // send addresses to display with relevant mask
-        sendCommand(0x40 | y);
+        sendCommand(0b00100000);  // basic instruction
+        sendCommand(0b10000000 | x);  // send addresses to display with relevant mask
+        sendCommand(0b01000000 | y);
     }
 }
 
--- a/N5110.h	Tue Mar 21 11:46:14 2017 +0000
+++ b/N5110.h	Wed Nov 01 20:48:05 2017 +0000
@@ -3,42 +3,6 @@
 
 #include "mbed.h"
 
-// Command Bytes - taken from Chris Yan's library
-// More information can be found in the display datasheet
-// H = 0 - Basic instructions
-#define CMD_DC_CLEAR_DISPLAY   0x08
-#define CMD_DC_NORMAL_MODE     0x0C
-#define CMD_DC_FILL_DISPLAY    0x09
-#define CMD_DC_INVERT_VIDEO    0x0D
-#define CMD_FS_HORIZONTAL_MODE 0x00
-#define CMD_FS_VERTICAL_MODE   0x02
-#define CMD_FS_BASIC_MODE      0x00
-#define CMD_FS_EXTENDED_MODE   0x01
-#define CMD_FS_ACTIVE_MODE     0x00
-#define CMD_FS_POWER_DOWN_MODE 0x04
-// H = 1 - Extended instructions
-#define CMD_TC_TEMP_0          0x04
-#define CMD_TC_TEMP_1          0x05
-#define CMD_TC_TEMP_2          0x06
-#define CMD_TC_TEMP_3          0x07
-#define CMD_BI_MUX_24          0x15
-#define CMD_BI_MUX_48          0x13
-#define CMD_BI_MUX_100         0x10
-#define CMD_VOP_6V06           0xB2
-#define CMD_VOP_7V38           0xC8
-
-// number of pixels on display
-#define WIDTH 84
-#define HEIGHT 48
-#define BANKS 6
-
-/// Fill types for 2D shapes
-enum FillType {
-    FILL_TRANSPARENT, ///< Transparent with outline
-    FILL_BLACK,       ///< Filled black
-    FILL_WHITE,       ///< Filled white (no outline)
-};
-
 /** N5110 Class
 @brief Library for interfacing with Nokia 5110 LCD display (https://www.sparkfun.com/products/10168) using the hardware SPI on the mbed.
 @brief The display is powered from a GPIO pin meaning it can be controlled via software.  The LED backlight is also software-controllable (via PWM pin).
@@ -202,6 +166,19 @@
 
 @endcode
 */
+
+// number of pixels on display
+#define WIDTH 84
+#define HEIGHT 48
+#define BANKS 6
+
+/// Fill types for 2D shapes
+enum FillType {
+    FILL_TRANSPARENT, ///< Transparent with outline
+    FILL_BLACK,       ///< Filled black
+    FILL_WHITE,       ///< Filled white (no outline)
+};
+
 class N5110
 {
 private:
@@ -278,6 +255,11 @@
     */
     void clear();
 
+    /** Set screen constrast
+    *   @param constrast - float in range 0.0 to 1.0 (0.40 to 0.60 is usually a good value)
+    */
+    void setContrast(float contrast);
+    
     /** Turn on normal video mode (default)
     *  Black on white
     */
@@ -450,6 +432,8 @@
     void clearRAM();
     void sendCommand(unsigned char command);
     void sendData(unsigned char data);
+    void setTempCoefficient(char tc);  // 0 to 3
+    void setBias(char bias);  // 0 to 7
 };
 
 const unsigned char font5x7[480] = {