A basic LCD output test which uses the NXP LPC1768\'s SPI interface to display pixels, characters, and numbers on the Nokia 5110 or Nokia 3310 LCD.

Dependencies:   mbed

Fork of Nokia5110 by Krissi Yan

Committer:
gonzo1964
Date:
Tue Dec 23 22:17:07 2014 +0000
Revision:
4:f2e807b8fecc
Parent:
3:41063eb2a040
zbyszekgonda1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Fuzball 1:e25ab356dc9b 1 // Project: Nokia5110 - Controlling a NK5110 display from an NXP LPC1768
Fuzball 1:e25ab356dc9b 2 // File: NOKIA_5110.h
Fuzball 1:e25ab356dc9b 3 // Author: Chris Yan
Fuzball 1:e25ab356dc9b 4 // Created: January, 2012
Fuzball 3:41063eb2a040 5 // Revised: January, 2014
Fuzball 1:e25ab356dc9b 6 // Desc: Commands, fonts, and class for using a
Fuzball 1:e25ab356dc9b 7 // Nokia 5110 LCD via the Phillips 8554 LCD driver.
Fuzball 1:e25ab356dc9b 8 //
Fuzball 1:e25ab356dc9b 9 // Typical Usage: User must fill the LcdPins struct with the pinout used to control the LCD and
Fuzball 1:e25ab356dc9b 10 // instantiate the NokiaLcd class - passing the created LcdPins struct to the constructor.
Fuzball 1:e25ab356dc9b 11 // The class function NokiaLcd::InitLcd may then be called to reset and start the LCD driver.
Fuzball 1:e25ab356dc9b 12 // A simple 6x6 font (6x8 in LCD space and ~5x5 character space) is included to facilitate
Fuzball 1:e25ab356dc9b 13 // the NokiaLcd::DrawChar( char character ) function, which will copy the character 8 bits
Fuzball 1:e25ab356dc9b 14 // at a time for 6 clock cycles.
Fuzball 1:e25ab356dc9b 15 // Commands may be sent to the LCD via the NokiaLcd::SendFunction(char cmd)
Fuzball 1:e25ab356dc9b 16 // function, but be aware that certain commands require the Function Set register's H-value
Fuzball 1:e25ab356dc9b 17 // to be either 1 or 0, depending on the command. This class does not check to see whether
Fuzball 1:e25ab356dc9b 18 // the H-value is of proper status. The Function Set register /may/ be changed via the
Fuzball 1:e25ab356dc9b 19 // NokiaLcd::SendFunction(char cmd), but the code uses this internally and expects that
Fuzball 1:e25ab356dc9b 20 // most function registers have not been changed by the user.
Fuzball 1:e25ab356dc9b 21 //
Fuzball 1:e25ab356dc9b 22 // Example:
Fuzball 1:e25ab356dc9b 23 // #include "mbed.h"
Fuzball 1:e25ab356dc9b 24 // #include "NOKIA_5110.h"
Fuzball 1:e25ab356dc9b 25 //
Fuzball 1:e25ab356dc9b 26 // int main() {
Fuzball 1:e25ab356dc9b 27 // LcdPins myLcdPins = { p11, NC, p13, p10, p8, p9 };
Fuzball 1:e25ab356dc9b 28 // NokiaLcd myLcd( myLcdPins ); // SPI is started here (8-bits, mode 1)
Fuzball 1:e25ab356dc9b 29 // myLcd.InitLcd(); // LCD is reset and DDRAM is cleared
Fuzball 1:e25ab356dc9b 30 // myLcd.TestLcd( 0xAA ); // Draws a vertical pattern where every other pixel is on
Fuzball 1:e25ab356dc9b 31 // wait(10);
Fuzball 1:e25ab356dc9b 32 // myLcd.ShutdownLcd(); // Clears the LCD's DDRAM and powers it down via CMD_FS_POWER_DOWN_MODE, H=0
Fuzball 1:e25ab356dc9b 33 // while(1)
Fuzball 1:e25ab356dc9b 34 // { };
Fuzball 1:e25ab356dc9b 35 // }
Fuzball 1:e25ab356dc9b 36
Fuzball 1:e25ab356dc9b 37 // Command Instructions
Fuzball 1:e25ab356dc9b 38 // H = 0
Fuzball 3:41063eb2a040 39 #ifndef __NOKIA_5110_H__
Fuzball 3:41063eb2a040 40 #define __NOKIA_5110_H__
Fuzball 3:41063eb2a040 41
Fuzball 3:41063eb2a040 42 // Command Instructions
Fuzball 3:41063eb2a040 43 // H = 0
Fuzball 1:e25ab356dc9b 44 #define CMD_DC_CLEAR_DISPLAY 0x08
Fuzball 1:e25ab356dc9b 45 #define CMD_DC_NORMAL_MODE 0x0C
Fuzball 1:e25ab356dc9b 46 #define CMD_DC_FILL_DISPLAY 0x09
Fuzball 1:e25ab356dc9b 47 #define CMD_DC_INVERT_VIDEO 0x0D
Fuzball 1:e25ab356dc9b 48 #define CMD_FS_HORIZONTAL_MODE 0x00
Fuzball 1:e25ab356dc9b 49 #define CMD_FS_VERTICAL_MODE 0x02
Fuzball 1:e25ab356dc9b 50 #define CMD_FS_BASIC_MODE 0x00
Fuzball 1:e25ab356dc9b 51 #define CMD_FS_EXTENDED_MODE 0x01
Fuzball 1:e25ab356dc9b 52 #define CMD_FS_ACTIVE_MODE 0x00
Fuzball 1:e25ab356dc9b 53 #define CMD_FS_POWER_DOWN_MODE 0x04
Fuzball 1:e25ab356dc9b 54 // H = 1
Fuzball 1:e25ab356dc9b 55 #define CMD_TC_TEMP_0 0x04
Fuzball 1:e25ab356dc9b 56 #define CMD_TC_TEMP_1 0x05
Fuzball 1:e25ab356dc9b 57 #define CMD_TC_TEMP_2 0x06
Fuzball 1:e25ab356dc9b 58 #define CMD_TC_TEMP_3 0x07
Fuzball 1:e25ab356dc9b 59 #define CMD_BI_MUX_24 0x15
Fuzball 1:e25ab356dc9b 60 #define CMD_BI_MUX_48 0x13
Fuzball 1:e25ab356dc9b 61 #define CMD_BI_MUX_100 0x10
Fuzball 1:e25ab356dc9b 62 #define CMD_VOP_6V06 0xB2
Fuzball 1:e25ab356dc9b 63 #define CMD_VOP_7V38 0xC8
Fuzball 1:e25ab356dc9b 64
Fuzball 1:e25ab356dc9b 65 // LCD Characteristics
Fuzball 3:41063eb2a040 66 #define LCD_FREQ 2000000
Fuzball 1:e25ab356dc9b 67 #define LCD_SPI_MODE 0x01
Fuzball 1:e25ab356dc9b 68 #define LCD_SPI_BITS 0x08
Fuzball 3:41063eb2a040 69 #define LCD_X_MAX 84
Fuzball 3:41063eb2a040 70 #define LCD_Y_MAX 48
Fuzball 1:e25ab356dc9b 71
Fuzball 1:e25ab356dc9b 72 #define PIN_RST 0x00
Fuzball 1:e25ab356dc9b 73 #define PIN_SCE 0x01
Fuzball 1:e25ab356dc9b 74 #define PIN_DC 0x02
Fuzball 1:e25ab356dc9b 75
Fuzball 1:e25ab356dc9b 76 #include "mbed.h"
Fuzball 1:e25ab356dc9b 77
Fuzball 1:e25ab356dc9b 78 struct LcdPins
Fuzball 1:e25ab356dc9b 79 {
Fuzball 1:e25ab356dc9b 80 PinName mosi;
Fuzball 1:e25ab356dc9b 81 PinName miso;
Fuzball 1:e25ab356dc9b 82 PinName sclk;
Fuzball 1:e25ab356dc9b 83 PinName dc;
Fuzball 1:e25ab356dc9b 84 PinName sce;
Fuzball 1:e25ab356dc9b 85 PinName rst;
Fuzball 1:e25ab356dc9b 86 };
Fuzball 1:e25ab356dc9b 87
Fuzball 1:e25ab356dc9b 88 struct LcdFunctionSet
Fuzball 1:e25ab356dc9b 89 {
Fuzball 1:e25ab356dc9b 90 char PD;
Fuzball 1:e25ab356dc9b 91 char V;
Fuzball 1:e25ab356dc9b 92 char H;
Fuzball 1:e25ab356dc9b 93 };
Fuzball 1:e25ab356dc9b 94
Fuzball 1:e25ab356dc9b 95 typedef char LcdFunctionChar;
Fuzball 1:e25ab356dc9b 96 typedef char LcdTempControl;
Fuzball 1:e25ab356dc9b 97 typedef char LcdDispControl;
Fuzball 1:e25ab356dc9b 98 typedef char LcdBiasChar;
Fuzball 1:e25ab356dc9b 99 typedef char LcdVopChar;
Fuzball 1:e25ab356dc9b 100
Fuzball 1:e25ab356dc9b 101 class NokiaLcd
Fuzball 1:e25ab356dc9b 102 {
Fuzball 1:e25ab356dc9b 103 public:
Fuzball 1:e25ab356dc9b 104 NokiaLcd(LcdPins lcd_pinout);
Fuzball 1:e25ab356dc9b 105 ~NokiaLcd();
Fuzball 1:e25ab356dc9b 106
Fuzball 1:e25ab356dc9b 107 public:
Fuzball 1:e25ab356dc9b 108 void InitLcd();
Fuzball 1:e25ab356dc9b 109 void ClearLcdMem();
Fuzball 1:e25ab356dc9b 110 void ShutdownLcd();
Fuzball 1:e25ab356dc9b 111 void SendFunction(char cmd);
Fuzball 1:e25ab356dc9b 112 void TestLcd(char test_pattern);
Fuzball 3:41063eb2a040 113 void SendDrawData(char data);
Fuzball 3:41063eb2a040 114
Fuzball 3:41063eb2a040 115 public:
Fuzball 3:41063eb2a040 116 void DrawString(char* str);
Fuzball 1:e25ab356dc9b 117 void DrawChar(char character);
Fuzball 3:41063eb2a040 118 void SetXY(char x, char y);
Fuzball 3:41063eb2a040 119 void DrawFrameChar(char character);
Fuzball 3:41063eb2a040 120 void DrawNegFrameChar(char character);
Fuzball 3:41063eb2a040 121 char* NumToStr(int num);
Fuzball 3:41063eb2a040 122
Fuzball 1:e25ab356dc9b 123 private:
Fuzball 1:e25ab356dc9b 124 char CreateFunctionChar();
Fuzball 1:e25ab356dc9b 125 void ResetLcd();
Fuzball 1:e25ab356dc9b 126
Fuzball 1:e25ab356dc9b 127 private:
Fuzball 1:e25ab356dc9b 128 LcdFunctionChar FunctionChar;
Fuzball 1:e25ab356dc9b 129 LcdTempControl TempControlChar;
Fuzball 1:e25ab356dc9b 130 LcdDispControl DispControlChar;
Fuzball 1:e25ab356dc9b 131 LcdFunctionSet FunctionSet;
Fuzball 1:e25ab356dc9b 132 LcdBiasChar BiasChar;
Fuzball 1:e25ab356dc9b 133 LcdVopChar VopChar;
Fuzball 1:e25ab356dc9b 134 DigitalOut** Pins;
Fuzball 1:e25ab356dc9b 135 SPI* LcdSpi;
Fuzball 1:e25ab356dc9b 136
Fuzball 1:e25ab356dc9b 137 };
Fuzball 1:e25ab356dc9b 138
Fuzball 1:e25ab356dc9b 139 const char FONT_6x6[570] = //should be 564 total char
Fuzball 1:e25ab356dc9b 140 {
Fuzball 1:e25ab356dc9b 141 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SPACE 1
Fuzball 1:e25ab356dc9b 142 0x00, 0x06, 0x2F, 0x06, 0x00, 0x00, // ! 2
Fuzball 1:e25ab356dc9b 143 0x00, 0x06, 0x00, 0x06, 0x00, 0x00, // " 3
Fuzball 1:e25ab356dc9b 144 0x14, 0x3E, 0x14, 0x3E, 0x14, 0x00, // # 4
Fuzball 1:e25ab356dc9b 145 0x2E, 0x2A, 0x3F, 0x2A, 0x3A, 0x00, // $ 5
Fuzball 1:e25ab356dc9b 146 0x26, 0x16, 0x08, 0x34, 0x32, 0x00, // % 6
Fuzball 1:e25ab356dc9b 147 0x34, 0x2A, 0x3C, 0x18, 0x28, 0x00, // & 7
Fuzball 1:e25ab356dc9b 148 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, // ' 8
Fuzball 1:e25ab356dc9b 149 0x00, 0x00, 0x1C, 0x36, 0x22, 0x00, // ( 9
Fuzball 1:e25ab356dc9b 150 0x22, 0x36, 0x1C, 0x00, 0x00, 0x00, // ) 10
Fuzball 1:e25ab356dc9b 151 0x24, 0x18, 0x0E, 0x18, 0x24, 0x00, // * 11
Fuzball 1:e25ab356dc9b 152 0x08, 0x08, 0x3E, 0x08, 0x08, 0x00, // + 12
Fuzball 1:e25ab356dc9b 153 0x20, 0x30, 0x00, 0x00, 0x00, 0x00, // , 13
Fuzball 1:e25ab356dc9b 154 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, // - 14
Fuzball 1:e25ab356dc9b 155 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, // . 15
Fuzball 1:e25ab356dc9b 156 0x30, 0x18, 0x0C, 0x06, 0x00, 0x00, // / 16
Fuzball 1:e25ab356dc9b 157 0x00, 0x1C, 0x22, 0x22, 0x1C, 0x00, // 0 17
Fuzball 1:e25ab356dc9b 158 0x00, 0x24, 0x3E, 0x20, 0x00, 0x00, // 1 18
Fuzball 1:e25ab356dc9b 159 0x3A, 0x2A, 0x2A, 0x2A, 0x2E, 0x00, // 2 19
Fuzball 1:e25ab356dc9b 160 0x22, 0x2A, 0x2A, 0x2A, 0x3E, 0x00, // 3 20
Fuzball 1:e25ab356dc9b 161 0x0E, 0x08, 0x08, 0x3E, 0x08, 0x00, // 4 21
Fuzball 1:e25ab356dc9b 162 0x2E, 0x2A, 0x2A, 0x2A, 0x3A, 0x00, // 5 22
Fuzball 1:e25ab356dc9b 163 0x3E, 0x2A, 0x2A, 0x2A, 0x3A, 0x00, // 6 23
Fuzball 1:e25ab356dc9b 164 0x22, 0x12, 0x0A, 0x06, 0x02, 0x00, // 7 24
Fuzball 1:e25ab356dc9b 165 0x3E, 0x2A, 0x2A, 0x2A, 0x3E, 0x00, // 8 25
Fuzball 1:e25ab356dc9b 166 0x00, 0x2E, 0x2A, 0x2A, 0x3E, 0x00, // 9 26
Fuzball 1:e25ab356dc9b 167 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, // : 27
Fuzball 1:e25ab356dc9b 168 0x00, 0x20, 0x14, 0x00, 0x00, 0x00, // ; 28
Fuzball 1:e25ab356dc9b 169 0x00, 0x00, 0x08, 0x14, 0x22, 0x00, // < 29
Fuzball 1:e25ab356dc9b 170 0x14, 0x14, 0x14, 0x14, 0x14, 0x00, // = 30
Fuzball 1:e25ab356dc9b 171 0x22, 0x14, 0x08, 0x00, 0x00, 0x00, // > 31
Fuzball 1:e25ab356dc9b 172 0x06, 0x01, 0x2D, 0x06, 0x00, 0x00, // ? 32
Fuzball 1:e25ab356dc9b 173 0x1E, 0x23, 0x19, 0x35, 0x3E, 0x00, // @ 33
Fuzball 1:e25ab356dc9b 174 0x3C, 0x0A, 0x0A, 0x0A, 0x3C, 0x00, // A 34
Fuzball 1:e25ab356dc9b 175 0x3E, 0x2A, 0x2A, 0x2A, 0x1C, 0x00, // B 35
Fuzball 1:e25ab356dc9b 176 0x1C, 0x22, 0x22, 0x22, 0x22, 0x00, // C 36
Fuzball 1:e25ab356dc9b 177 0x3E, 0x22, 0x22, 0x22, 0x1C, 0x00, // D 37
Fuzball 1:e25ab356dc9b 178 0x3E, 0x2A, 0x2A, 0x2A, 0x22, 0x00, // E 38
Fuzball 1:e25ab356dc9b 179 0x3E, 0x0A, 0x0A, 0x0A, 0x02, 0x00, // F 39
Fuzball 1:e25ab356dc9b 180 0x1C, 0x22, 0x2A, 0x2A, 0x18, 0x00, // G 40
Fuzball 1:e25ab356dc9b 181 0x3E, 0x08, 0x08, 0x08, 0x3E, 0x00, // H
Fuzball 1:e25ab356dc9b 182 0x22, 0x22, 0x3E, 0x22, 0x22, 0x00, // I
Fuzball 1:e25ab356dc9b 183 0x10, 0x22, 0x22, 0x1E, 0x02, 0x00, // J
Fuzball 1:e25ab356dc9b 184 0x3E, 0x08, 0x14, 0x22, 0x00, 0x00, // K
Fuzball 1:e25ab356dc9b 185 0x00, 0x3E, 0x20, 0x20, 0x20, 0x00, // L 45
Fuzball 1:e25ab356dc9b 186 0x3E, 0x04, 0x08, 0x04, 0x3E, 0x00, // M
Fuzball 1:e25ab356dc9b 187 0x3C, 0x02, 0x02, 0x02, 0x3C, 0x00, // N
Fuzball 1:e25ab356dc9b 188 0x1C, 0x22, 0x22, 0x22, 0x1C, 0x00, // O
Fuzball 1:e25ab356dc9b 189 0x3E, 0x0A, 0x0A, 0x04, 0x00, 0x00, // P
Fuzball 1:e25ab356dc9b 190 0x1C, 0x22, 0x32, 0x3C, 0x20, 0x00, // Q 50
Fuzball 1:e25ab356dc9b 191 0x3E, 0x0A, 0x0A, 0x1A, 0x24, 0x00, // R
Fuzball 1:e25ab356dc9b 192 0x24, 0x2A, 0x2A, 0x2A, 0x12, 0x00, // S
Fuzball 1:e25ab356dc9b 193 0x02, 0x02, 0x3E, 0x02, 0x02, 0x00, // T
Fuzball 1:e25ab356dc9b 194 0x1E, 0x20, 0x20, 0x20, 0x1E, 0x00, // U
Fuzball 1:e25ab356dc9b 195 0x06, 0x18, 0x20, 0x18, 0x06, 0x00, // V 55
Fuzball 1:e25ab356dc9b 196 0x0E, 0x30, 0x18, 0x30, 0x0E, 0x00, // W
Fuzball 1:e25ab356dc9b 197 0x22, 0x14, 0x08, 0x14, 0x22, 0x00, // X
Fuzball 1:e25ab356dc9b 198 0x02, 0x04, 0x38, 0x04, 0x02, 0x00, // Y
Fuzball 1:e25ab356dc9b 199 0x22, 0x32, 0x2A, 0x26, 0x22, 0x00, // Z
Fuzball 1:e25ab356dc9b 200 0x00, 0x00, 0x00, 0x3E, 0x22, 0x00, // [ 60
Fuzball 1:e25ab356dc9b 201 0x06, 0x0C, 0x18, 0x30, 0x00, 0x00, // backslash
Fuzball 1:e25ab356dc9b 202 0x22, 0x3E, 0x00, 0x00, 0x00, 0x00, // ]
Fuzball 1:e25ab356dc9b 203 0x00, 0x04, 0x02, 0x02, 0x04, 0x00, // ^
Fuzball 1:e25ab356dc9b 204 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, // _
Fuzball 1:e25ab356dc9b 205 0x00, 0x00, 0x04, 0x08, 0x00, 0x00, // ` 65
Fuzball 1:e25ab356dc9b 206 0x18, 0x24, 0x14, 0x38, 0x00, 0x00, // a
Fuzball 1:e25ab356dc9b 207 0x1E, 0x28, 0x28, 0x10, 0x00, 0x00, // b
Fuzball 1:e25ab356dc9b 208 0x18, 0x24, 0x24, 0x00, 0x00, 0x00, // c
Fuzball 1:e25ab356dc9b 209 0x10, 0x28, 0x28, 0x1E, 0x00, 0x00, // d
Fuzball 1:e25ab356dc9b 210 0x18, 0x2C, 0x2C, 0x08, 0x00, 0x00, // e 70
Fuzball 1:e25ab356dc9b 211 0x00, 0x3C, 0x12, 0x04, 0x00, 0x00, // f
Fuzball 1:e25ab356dc9b 212 0x24, 0x2A, 0x1E, 0x00, 0x00, 0x00, // g
Fuzball 1:e25ab356dc9b 213 0x3E, 0x08, 0x30, 0x00, 0x00, 0x00, // h
Fuzball 1:e25ab356dc9b 214 0x00, 0x3A, 0x00, 0x00, 0x00, 0x00, // i
Fuzball 1:e25ab356dc9b 215 0x10, 0x20, 0x1A, 0x00, 0x00, 0x00, // j 75
Fuzball 1:e25ab356dc9b 216 0x3E, 0x10, 0x2C, 0x20, 0x00, 0x00, // k
Fuzball 1:e25ab356dc9b 217 0x00, 0x3E, 0x00, 0x00, 0x00, 0x00, // l
Fuzball 1:e25ab356dc9b 218 0x38, 0x08, 0x18, 0x08, 0x30, 0x00, // m
Fuzball 1:e25ab356dc9b 219 0x30, 0x08, 0x08, 0x30, 0x00, 0x00, // n
Fuzball 1:e25ab356dc9b 220 0x10, 0x28, 0x28, 0x10, 0x00, 0x00, // o 80
Fuzball 1:e25ab356dc9b 221 0x38, 0x14, 0x14, 0x08, 0x00, 0x00, // p
Fuzball 1:e25ab356dc9b 222 0x08, 0x14, 0x14, 0x38, 0x00, 0x00, // q
Fuzball 1:e25ab356dc9b 223 0x3C, 0x08, 0x04, 0x00, 0x00, 0x00, // r
Fuzball 1:e25ab356dc9b 224 0x2C, 0x34, 0x00, 0x00, 0x00, 0x00, // s
Fuzball 1:e25ab356dc9b 225 0x08, 0x3C, 0x28, 0x00, 0x00, 0x00, // t 85
Fuzball 1:e25ab356dc9b 226 0x18, 0x20, 0x20, 0x18, 0x00, 0x00, // u
Fuzball 1:e25ab356dc9b 227 0x08, 0x10, 0x20, 0x10, 0x08, 0x00, // v
Fuzball 1:e25ab356dc9b 228 0x18, 0x20, 0x10, 0x20, 0x18, 0x00, // w
Fuzball 1:e25ab356dc9b 229 0x28, 0x10, 0x28, 0x00, 0x00, 0x00, // x
Fuzball 1:e25ab356dc9b 230 0x2C, 0x30, 0x1C, 0x00, 0x00, 0x00, // y 90
Fuzball 1:e25ab356dc9b 231 0x24, 0x34, 0x2C, 0x24, 0x00, 0x00, // z
Fuzball 1:e25ab356dc9b 232 0x00, 0x00, 0x08, 0x3E, 0x22, 0x00, // {
Fuzball 3:41063eb2a040 233 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, // |
Fuzball 1:e25ab356dc9b 234 0x22, 0x3E, 0x08, 0x00, 0x00, 0x00, // }
Fuzball 1:e25ab356dc9b 235 0x10, 0x08, 0x18, 0x10, 0x08, 0x00, // ~ 95
Fuzball 1:e25ab356dc9b 236 };
Fuzball 3:41063eb2a040 237
Fuzball 3:41063eb2a040 238 #endif