A small and easy-to-use library for the Nokia 5110, 3310, and LCD-10168 / PCD8544 LCD controller. Draw support includes strings, patterns, and pixel-by-pixel methods.

Dependents:   TextLCD_NOKIA_5110 Nokia5110_KL25Z Nokia5110_test_nucleo LPC1114_5110_PIR ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NOKIA_5110.h Source File

NOKIA_5110.h

00001 // Project: Nokia5110 - Controlling a NK5110 display from an NXP LPC1768
00002 // File: NOKIA_5110.h
00003 // Author: Krissi Yan
00004 // Created: January, 2015
00005 // Revised: January, 2016
00006 //  Desc: Commands, fonts, and class for using a
00007 //      Nokia 5110 LCD via the Phillips 8554 LCD driver.
00008 // 
00009 //  Typical Usage: User must fill the LcdPins struct with the pinout used to control the LCD and
00010 //      instantiate the NokiaLcd class - passing the created LcdPins struct to the constructor.
00011 //      The class function NokiaLcd::InitLcd may then be called to reset and start the LCD driver.
00012 //      A simple 6x6 font (6x8 in LCD space and ~5x5 character space) is included to facilitate 
00013 //      the NokiaLcd::DrawChar( char character ) function, which will copy the character 8 bits 
00014 //      at a time for 6 clock cycles.
00015 //                 Commands may be sent to the LCD via the NokiaLcd::SendFunction(char cmd) 
00016 //      function, but be aware that certain commands require the Function Set register's H-value
00017 //      to be either 1 or 0, depending on the command. This class does not check to see whether
00018 //      the H-value is of proper status. The Function Set register /may/ be changed via the 
00019 //      NokiaLcd::SendFunction(char cmd), but the code uses this internally and expects that
00020 //      most function registers have not been changed by the user.
00021 //
00022 //      Example:
00023 //          #include "mbed.h"
00024 //          #include "NOKIA_5110.h"
00025 //
00026 //          int main() {
00027 //              LcdPins myLcdPins = { p11, NC, p13, p10, p8, p9 };
00028 //              NokiaLcd myLcd( myLcdPins );    // SPI is started here (8-bits, mode 1)
00029 //              myLcd.InitLcd();                // LCD is reset and DDRAM is cleared
00030 //              myLcd.TestLcd( 0xAA );          // Draws a vertical pattern where every other pixel is on 
00031 //              wait(10);                       
00032 //              myLcd.ShutdownLcd();            // Clears the LCD's DDRAM and powers it down via CMD_FS_POWER_DOWN_MODE, H=0
00033 //              while(1)
00034 //              {   };
00035 //          }
00036 
00037 // Command Instructions
00038 //       H = 0
00039 #ifndef __NOKIA_5110_H__
00040 #define __NOKIA_5110_H__
00041 
00042 // Command Instructions
00043 //       H = 0
00044 #define CMD_DC_CLEAR_DISPLAY   0x08
00045 #define CMD_DC_NORMAL_MODE     0x0C
00046 #define CMD_DC_FILL_DISPLAY    0x09
00047 #define CMD_DC_INVERT_VIDEO    0x0D
00048 #define CMD_FS_HORIZONTAL_MODE 0x00
00049 #define CMD_FS_VERTICAL_MODE   0x02
00050 #define CMD_FS_BASIC_MODE      0x00
00051 #define CMD_FS_EXTENDED_MODE   0x01
00052 #define CMD_FS_ACTIVE_MODE     0x00
00053 #define CMD_FS_POWER_DOWN_MODE 0x04
00054 //       H = 1
00055 #define CMD_TC_TEMP_0          0x04
00056 #define CMD_TC_TEMP_1          0x05
00057 #define CMD_TC_TEMP_2          0x06
00058 #define CMD_TC_TEMP_3          0x07
00059 #define CMD_BI_MUX_24          0x15
00060 #define CMD_BI_MUX_48          0x13
00061 #define CMD_BI_MUX_100         0x10
00062 #define CMD_VOP_6V06           0xB2
00063 #define CMD_VOP_7V38           0xC8
00064 
00065 // LCD Characteristics
00066 #define LCD_FREQ 2000000
00067 #define LCD_SPI_MODE 0x01
00068 #define LCD_SPI_BITS 0x08
00069 #define LCD_X_MAX 84
00070 #define LCD_Y_MAX 48
00071 
00072 #define PIN_RST  0x00
00073 #define PIN_SCE  0x01
00074 #define PIN_DC   0x02
00075 
00076 #include "mbed.h"
00077 
00078 // Struct to hold the serial pinout
00079 struct LcdPins
00080 {
00081     PinName mosi;
00082     PinName miso;
00083     PinName sclk;
00084     PinName dc;
00085     PinName sce;
00086     PinName rst;
00087 };
00088 
00089 // Struct to hold the basic settings for the LCD
00090 struct LcdFunctionSet
00091 {
00092     char PD;
00093     char V;
00094     char H;
00095 };
00096 
00097 // Typedefs to make the code more readable
00098 typedef char LcdFunctionChar;
00099 typedef char LcdTempControl;
00100 typedef char LcdDispControl;
00101 typedef char LcdBiasChar;
00102 typedef char LcdVopChar;
00103 
00104 class NokiaLcd
00105 {
00106     public:
00107         NokiaLcd(LcdPins lcd_pinout);
00108         ~NokiaLcd();
00109         
00110     public:
00111     // Setup and Modification Functions 
00112         void InitLcd();
00113         void ClearLcdMem();
00114         void ShutdownLcd();
00115         void SendFunction(char cmd);
00116         void TestLcd(char test_pattern);
00117         void SendDrawData(char data);
00118         
00119     public:
00120     // Draw Functions
00121         void DrawString(char* str);
00122         void DrawChar(char character);
00123         void SetXY(char x, char y);
00124         void DrawFrameChar(char character);
00125         void DrawNegFrameChar(char character);
00126         char* NumToStr(int num);
00127         
00128     private:
00129         char CreateFunctionChar();
00130         void ResetLcd();
00131         
00132     private:
00133         LcdFunctionChar FunctionChar;
00134         LcdTempControl  TempControlChar;
00135         LcdDispControl  DispControlChar;
00136         LcdFunctionSet  FunctionSet;
00137         LcdBiasChar     BiasChar;
00138         LcdVopChar      VopChar;
00139         DigitalOut**    Pins;
00140         SPI*            LcdSpi;
00141         
00142 };
00143 
00144 //////////////////////////////////////////////////////
00145 // Rotated font built for this screen's display memory
00146 //////////////////////////////////////////////////////
00147 const char FONT_6x6[570] =           //should be 564 total char
00148 {                                    //
00149  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SPACE   1   
00150  0x00, 0x06, 0x2F, 0x06, 0x00, 0x00, // !   2
00151  0x00, 0x06, 0x00, 0x06, 0x00, 0x00, // "   3
00152  0x14, 0x3E, 0x14, 0x3E, 0x14, 0x00, // #   4
00153  0x2E, 0x2A, 0x3F, 0x2A, 0x3A, 0x00, // $   5
00154  0x26, 0x16, 0x08, 0x34, 0x32, 0x00, // %   6
00155  0x34, 0x2A, 0x3C, 0x18, 0x28, 0x00, // &   7
00156  0x00, 0x06, 0x00, 0x00, 0x00, 0x00, // '   8
00157  0x00, 0x00, 0x1C, 0x36, 0x22, 0x00, // (   9
00158  0x22, 0x36, 0x1C, 0x00, 0x00, 0x00, // )   10
00159  0x24, 0x18, 0x0E, 0x18, 0x24, 0x00, // *   11
00160  0x08, 0x08, 0x3E, 0x08, 0x08, 0x00, // +   12
00161  0x20, 0x30, 0x00, 0x00, 0x00, 0x00, // ,   13
00162  0x08, 0x08, 0x08, 0x08, 0x08, 0x00, // -   14
00163  0x20, 0x00, 0x00, 0x00, 0x00, 0x00, // .   15
00164  0x30, 0x18, 0x0C, 0x06, 0x00, 0x00, // /   16
00165  0x00, 0x1C, 0x22, 0x22, 0x1C, 0x00, // 0   17
00166  0x00, 0x24, 0x3E, 0x20, 0x00, 0x00, // 1   18
00167  0x3A, 0x2A, 0x2A, 0x2A, 0x2E, 0x00, // 2   19
00168  0x22, 0x2A, 0x2A, 0x2A, 0x3E, 0x00, // 3   20
00169  0x0E, 0x08, 0x08, 0x3E, 0x08, 0x00, // 4   21
00170  0x2E, 0x2A, 0x2A, 0x2A, 0x3A, 0x00, // 5   22
00171  0x3E, 0x2A, 0x2A, 0x2A, 0x3A, 0x00, // 6   23
00172  0x22, 0x12, 0x0A, 0x06, 0x02, 0x00, // 7   24
00173  0x3E, 0x2A, 0x2A, 0x2A, 0x3E, 0x00, // 8   25
00174  0x00, 0x2E, 0x2A, 0x2A, 0x3E, 0x00, // 9   26
00175  0x00, 0x00, 0x14, 0x00, 0x00, 0x00, // :   27
00176  0x00, 0x20, 0x14, 0x00, 0x00, 0x00, // ;   28
00177  0x00, 0x00, 0x08, 0x14, 0x22, 0x00, // <   29
00178  0x14, 0x14, 0x14, 0x14, 0x14, 0x00, // =   30
00179  0x22, 0x14, 0x08, 0x00, 0x00, 0x00, // >   31
00180  0x06, 0x01, 0x2D, 0x06, 0x00, 0x00, // ?   32
00181  0x1E, 0x23, 0x19, 0x35, 0x3E, 0x00, // @   33
00182  0x3C, 0x0A, 0x0A, 0x0A, 0x3C, 0x00, // A   34
00183  0x3E, 0x2A, 0x2A, 0x2A, 0x1C, 0x00, // B   35
00184  0x1C, 0x22, 0x22, 0x22, 0x22, 0x00, // C   36
00185  0x3E, 0x22, 0x22, 0x22, 0x1C, 0x00, // D   37
00186  0x3E, 0x2A, 0x2A, 0x2A, 0x22, 0x00, // E   38
00187  0x3E, 0x0A, 0x0A, 0x0A, 0x02, 0x00, // F   39
00188  0x1C, 0x22, 0x2A, 0x2A, 0x18, 0x00, // G   40
00189  0x3E, 0x08, 0x08, 0x08, 0x3E, 0x00, // H
00190  0x22, 0x22, 0x3E, 0x22, 0x22, 0x00, // I
00191  0x10, 0x22, 0x22, 0x1E, 0x02, 0x00, // J
00192  0x3E, 0x08, 0x14, 0x22, 0x00, 0x00, // K
00193  0x00, 0x3E, 0x20, 0x20, 0x20, 0x00, // L   45
00194  0x3E, 0x04, 0x08, 0x04, 0x3E, 0x00, // M
00195  0x3C, 0x02, 0x02, 0x02, 0x3C, 0x00, // N
00196  0x1C, 0x22, 0x22, 0x22, 0x1C, 0x00, // O
00197  0x3E, 0x0A, 0x0A, 0x04, 0x00, 0x00, // P
00198  0x1C, 0x22, 0x32, 0x3C, 0x20, 0x00, // Q   50
00199  0x3E, 0x0A, 0x0A, 0x1A, 0x24, 0x00, // R
00200  0x24, 0x2A, 0x2A, 0x2A, 0x12, 0x00, // S
00201  0x02, 0x02, 0x3E, 0x02, 0x02, 0x00, // T
00202  0x1E, 0x20, 0x20, 0x20, 0x1E, 0x00, // U
00203  0x06, 0x18, 0x20, 0x18, 0x06, 0x00, // V   55
00204  0x0E, 0x30, 0x18, 0x30, 0x0E, 0x00, // W
00205  0x22, 0x14, 0x08, 0x14, 0x22, 0x00, // X
00206  0x02, 0x04, 0x38, 0x04, 0x02, 0x00, // Y
00207  0x22, 0x32, 0x2A, 0x26, 0x22, 0x00, // Z
00208  0x00, 0x00, 0x00, 0x3E, 0x22, 0x00, // [   60
00209  0x06, 0x0C, 0x18, 0x30, 0x00, 0x00, // backslash
00210  0x22, 0x3E, 0x00, 0x00, 0x00, 0x00, // ]
00211  0x00, 0x04, 0x02, 0x02, 0x04, 0x00, // ^
00212  0x20, 0x20, 0x20, 0x20, 0x20, 0x00, // _
00213  0x00, 0x00, 0x04, 0x08, 0x00, 0x00, // `   65
00214  0x18, 0x24, 0x14, 0x38, 0x00, 0x00, // a
00215  0x1E, 0x28, 0x28, 0x10, 0x00, 0x00, // b
00216  0x18, 0x24, 0x24, 0x00, 0x00, 0x00, // c
00217  0x10, 0x28, 0x28, 0x1E, 0x00, 0x00, // d
00218  0x18, 0x2C, 0x2C, 0x08, 0x00, 0x00, // e   70
00219  0x00, 0x3C, 0x12, 0x04, 0x00, 0x00, // f
00220  0x24, 0x2A, 0x1E, 0x00, 0x00, 0x00, // g
00221  0x3E, 0x08, 0x30, 0x00, 0x00, 0x00, // h
00222  0x00, 0x3A, 0x00, 0x00, 0x00, 0x00, // i
00223  0x10, 0x20, 0x1A, 0x00, 0x00, 0x00, // j   75
00224  0x3E, 0x10, 0x2C, 0x20, 0x00, 0x00, // k
00225  0x00, 0x3E, 0x00, 0x00, 0x00, 0x00, // l
00226  0x38, 0x08, 0x18, 0x08, 0x30, 0x00, // m
00227  0x30, 0x08, 0x08, 0x30, 0x00, 0x00, // n
00228  0x10, 0x28, 0x28, 0x10, 0x00, 0x00, // o   80
00229  0x38, 0x14, 0x14, 0x08, 0x00, 0x00, // p
00230  0x08, 0x14, 0x14, 0x38, 0x00, 0x00, // q
00231  0x3C, 0x08, 0x04, 0x00, 0x00, 0x00, // r
00232  0x2C, 0x34, 0x00, 0x00, 0x00, 0x00, // s
00233  0x08, 0x3C, 0x28, 0x00, 0x00, 0x00, // t   85
00234  0x18, 0x20, 0x20, 0x18, 0x00, 0x00, // u
00235  0x08, 0x10, 0x20, 0x10, 0x08, 0x00, // v
00236  0x18, 0x20, 0x10, 0x20, 0x18, 0x00, // w
00237  0x28, 0x10, 0x28, 0x00, 0x00, 0x00, // x
00238  0x2C, 0x30, 0x1C, 0x00, 0x00, 0x00, // y   90
00239  0x24, 0x34, 0x2C, 0x24, 0x00, 0x00, // z
00240  0x00, 0x00, 0x08, 0x3E, 0x22, 0x00, // {
00241  0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, // |
00242  0x22, 0x3E, 0x08, 0x00, 0x00, 0x00, // }
00243  0x10, 0x08, 0x18, 0x10, 0x08, 0x00, // ~   95
00244 };
00245 
00246 #endif