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.
Quick and versatile LCD screen control. Works as a great alternative to serial consoles!
---------
Inverted-color text function makes easy menus and borders
Uses a simple SetXY(char, char) function to draw data-plots or patterns
- Wiring -
LCD's connections from left to right:
1) +3.3v (V_lcd) - mbed's VOUT
2) GND - mbed's GND
3) SCE (chip select) - mbed's p8
4) RST (reset) - mbed's p9
5) DC (data/command) - mbed's p10
6) MOSI - mbed's p11
7) SCLK (serial clock) - mbed's p13
8) +3.3v (V_backlight) - mbed's VOUT
The code for this setup would be...
LcdPins myPins;
myPins.sce = p8;
myPins.rst = p9;
myPins.dc = p10;
myPins.mosi = p11;
myPins.miso = NC;
myPins.sclk = p13;
or more easily...
LcdPins myPins = { p11, NC, p13, p10, p8, p9 };
Init the NokiaLcd class using the above struct...
NokiaLcd myLcd( myPins );
then start the LCD using...
myLcd.InitLcd();
Simple text output is achieved with either of these functions:
void DrawString(char* str);
void DrawChar(char character);
------
Better documentation, pre/post conditions, and extended draw functions are coming soon :)
NOKIA_5110.cpp@3:41063eb2a040, 2014-01-10 (annotated)
- Committer:
- Fuzball
- Date:
- Fri Jan 10 22:22:17 2014 +0000
- Revision:
- 3:41063eb2a040
- Parent:
- 1:e25ab356dc9b
Cleaned up the code a little, added more draw commands, and threw in some functions for drawing strings
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Fuzball |
1:e25ab356dc9b | 1 | // Project: Nokia5110 - Controlling a NK5110 display from an NXP LPC1768 |
Fuzball |
1:e25ab356dc9b | 2 | // File: NOKIA_5110.cpp |
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: Supporting code for the NokiaLcd class |
Fuzball |
1:e25ab356dc9b | 7 | |
Fuzball |
1:e25ab356dc9b | 8 | #include "NOKIA_5110.h" |
Fuzball |
1:e25ab356dc9b | 9 | #include "mbed.h" |
Fuzball |
1:e25ab356dc9b | 10 | |
Fuzball |
1:e25ab356dc9b | 11 | NokiaLcd::NokiaLcd(LcdPins pinout) |
Fuzball |
1:e25ab356dc9b | 12 | { |
Fuzball |
1:e25ab356dc9b | 13 | // SPI |
Fuzball |
1:e25ab356dc9b | 14 | LcdSpi = new SPI(pinout.mosi, pinout.miso, pinout.sclk); |
Fuzball |
1:e25ab356dc9b | 15 | LcdSpi->format(LCD_SPI_BITS, LCD_SPI_MODE); |
Fuzball |
1:e25ab356dc9b | 16 | LcdSpi->frequency(LCD_FREQ); |
Fuzball |
1:e25ab356dc9b | 17 | |
Fuzball |
1:e25ab356dc9b | 18 | // Control Pins |
Fuzball |
1:e25ab356dc9b | 19 | Pins = new DigitalOut*[3]; |
Fuzball |
1:e25ab356dc9b | 20 | Pins[PIN_RST] = new DigitalOut(pinout.rst); |
Fuzball |
1:e25ab356dc9b | 21 | Pins[PIN_SCE] = new DigitalOut(pinout.sce); |
Fuzball |
1:e25ab356dc9b | 22 | Pins[PIN_DC] = new DigitalOut(pinout.dc); |
Fuzball |
1:e25ab356dc9b | 23 | |
Fuzball |
1:e25ab356dc9b | 24 | // Initial Command Instructions, note the initial command mode |
Fuzball |
1:e25ab356dc9b | 25 | FunctionSet.V = CMD_FS_HORIZONTAL_MODE; |
Fuzball |
1:e25ab356dc9b | 26 | FunctionSet.H = CMD_FS_EXTENDED_MODE; |
Fuzball |
1:e25ab356dc9b | 27 | FunctionSet.PD = CMD_FS_ACTIVE_MODE; |
Fuzball |
1:e25ab356dc9b | 28 | FunctionChar = CreateFunctionChar(); |
Fuzball |
1:e25ab356dc9b | 29 | |
Fuzball |
1:e25ab356dc9b | 30 | TempControlChar = CMD_TC_TEMP_2; |
Fuzball |
1:e25ab356dc9b | 31 | DispControlChar = CMD_DC_NORMAL_MODE; |
Fuzball |
1:e25ab356dc9b | 32 | BiasChar = CMD_BI_MUX_48; |
Fuzball |
1:e25ab356dc9b | 33 | VopChar = CMD_VOP_7V38; |
Fuzball |
1:e25ab356dc9b | 34 | } |
Fuzball |
1:e25ab356dc9b | 35 | |
Fuzball |
1:e25ab356dc9b | 36 | void NokiaLcd::ShutdownLcd() |
Fuzball |
1:e25ab356dc9b | 37 | { |
Fuzball |
1:e25ab356dc9b | 38 | FunctionSet.PD = CMD_FS_POWER_DOWN_MODE; |
Fuzball |
1:e25ab356dc9b | 39 | |
Fuzball |
1:e25ab356dc9b | 40 | ClearLcdMem(); |
Fuzball |
1:e25ab356dc9b | 41 | SendFunction( CMD_DC_CLEAR_DISPLAY ); |
Fuzball |
1:e25ab356dc9b | 42 | SendFunction( CreateFunctionChar() ); |
Fuzball |
1:e25ab356dc9b | 43 | } |
Fuzball |
1:e25ab356dc9b | 44 | |
Fuzball |
1:e25ab356dc9b | 45 | void NokiaLcd::ClearLcdMem() |
Fuzball |
1:e25ab356dc9b | 46 | { |
Fuzball |
1:e25ab356dc9b | 47 | for(int tick = 0; tick <= 503; tick++) |
Fuzball |
1:e25ab356dc9b | 48 | LcdSpi->write(0x00); |
Fuzball |
1:e25ab356dc9b | 49 | } |
Fuzball |
1:e25ab356dc9b | 50 | |
Fuzball |
1:e25ab356dc9b | 51 | void NokiaLcd::TestLcd(char test_pattern) |
Fuzball |
1:e25ab356dc9b | 52 | { |
Fuzball |
1:e25ab356dc9b | 53 | for(int tick = 0; tick <= 503; tick++) |
Fuzball |
1:e25ab356dc9b | 54 | LcdSpi->write(test_pattern); // Command gets sent |
Fuzball |
1:e25ab356dc9b | 55 | } |
Fuzball |
1:e25ab356dc9b | 56 | |
Fuzball |
1:e25ab356dc9b | 57 | void NokiaLcd::InitLcd() |
Fuzball |
1:e25ab356dc9b | 58 | { |
Fuzball |
1:e25ab356dc9b | 59 | ResetLcd(); |
Fuzball | 3:41063eb2a040 | 60 | Pins[PIN_SCE]->write(0); // Chip Select goes low |
Fuzball |
1:e25ab356dc9b | 61 | |
Fuzball |
1:e25ab356dc9b | 62 | // Redefine the FunctionChar in case it has changed |
Fuzball |
1:e25ab356dc9b | 63 | FunctionSet.V = CMD_FS_HORIZONTAL_MODE; |
Fuzball |
1:e25ab356dc9b | 64 | FunctionSet.H = CMD_FS_EXTENDED_MODE; |
Fuzball |
1:e25ab356dc9b | 65 | FunctionSet.PD = CMD_FS_ACTIVE_MODE; |
Fuzball |
1:e25ab356dc9b | 66 | SendFunction( CreateFunctionChar() ); // Extended CMD set |
Fuzball |
1:e25ab356dc9b | 67 | SendFunction( VopChar ); // | Vop |
Fuzball |
1:e25ab356dc9b | 68 | SendFunction( TempControlChar ); // | Temp |
Fuzball |
1:e25ab356dc9b | 69 | SendFunction( BiasChar ); // | Bias |
Fuzball |
1:e25ab356dc9b | 70 | |
Fuzball |
1:e25ab356dc9b | 71 | FunctionSet.H = CMD_FS_BASIC_MODE; |
Fuzball |
1:e25ab356dc9b | 72 | SendFunction( CreateFunctionChar() ); // Basic CMD set |
Fuzball |
1:e25ab356dc9b | 73 | SendFunction( DispControlChar ); // | Display Mode |
Fuzball |
1:e25ab356dc9b | 74 | |
Fuzball |
1:e25ab356dc9b | 75 | ClearLcdMem(); |
Fuzball | 3:41063eb2a040 | 76 | Pins[PIN_DC]->write(1); // Data/CMD goes back to Data mode |
Fuzball |
1:e25ab356dc9b | 77 | } |
Fuzball |
1:e25ab356dc9b | 78 | |
Fuzball |
1:e25ab356dc9b | 79 | void NokiaLcd::ResetLcd() |
Fuzball |
1:e25ab356dc9b | 80 | { |
Fuzball |
1:e25ab356dc9b | 81 | Pins[PIN_RST]->write(0); // Reset goes low |
Fuzball |
1:e25ab356dc9b | 82 | Pins[PIN_RST]->write(1); // Reset goes high |
Fuzball |
1:e25ab356dc9b | 83 | } |
Fuzball |
1:e25ab356dc9b | 84 | |
Fuzball |
1:e25ab356dc9b | 85 | char NokiaLcd::CreateFunctionChar() |
Fuzball |
1:e25ab356dc9b | 86 | { |
Fuzball |
1:e25ab356dc9b | 87 | return ( 0x20 | FunctionSet.PD | FunctionSet.V | FunctionSet.H ); |
Fuzball |
1:e25ab356dc9b | 88 | } |
Fuzball |
1:e25ab356dc9b | 89 | |
Fuzball |
1:e25ab356dc9b | 90 | void NokiaLcd::SendDrawData(char data) |
Fuzball |
1:e25ab356dc9b | 91 | { |
Fuzball |
1:e25ab356dc9b | 92 | LcdSpi->write(data); // Command gets sent |
Fuzball |
1:e25ab356dc9b | 93 | } |
Fuzball |
1:e25ab356dc9b | 94 | |
Fuzball |
1:e25ab356dc9b | 95 | void NokiaLcd::DrawChar(char character) |
Fuzball |
1:e25ab356dc9b | 96 | { |
Fuzball |
1:e25ab356dc9b | 97 | for( int i = 0; i < 6; i++) |
Fuzball |
1:e25ab356dc9b | 98 | SendDrawData( FONT_6x6[ ((character - 32)*6) + i] ); |
Fuzball |
1:e25ab356dc9b | 99 | } |
Fuzball |
1:e25ab356dc9b | 100 | |
Fuzball | 3:41063eb2a040 | 101 | void NokiaLcd::DrawString(char* s) |
Fuzball | 3:41063eb2a040 | 102 | { |
Fuzball | 3:41063eb2a040 | 103 | char len = strlen(s); |
Fuzball | 3:41063eb2a040 | 104 | for( int idx = 0; idx < len; idx++ ) |
Fuzball | 3:41063eb2a040 | 105 | { |
Fuzball | 3:41063eb2a040 | 106 | for( int i = 0; i < 6; i++) |
Fuzball | 3:41063eb2a040 | 107 | SendDrawData( FONT_6x6[ ((s[idx] - 32)*6) + i] ); |
Fuzball | 3:41063eb2a040 | 108 | } |
Fuzball | 3:41063eb2a040 | 109 | } |
Fuzball | 3:41063eb2a040 | 110 | |
Fuzball | 3:41063eb2a040 | 111 | void NokiaLcd::DrawFrameChar(char character) |
Fuzball | 3:41063eb2a040 | 112 | { |
Fuzball | 3:41063eb2a040 | 113 | for( int i = 0; i < 6; i++) |
Fuzball | 3:41063eb2a040 | 114 | SendDrawData((( FONT_6x6[ ((character - 32)*6) + i] ) << 1 ) | 0x81); |
Fuzball | 3:41063eb2a040 | 115 | } |
Fuzball | 3:41063eb2a040 | 116 | |
Fuzball | 3:41063eb2a040 | 117 | void NokiaLcd::DrawNegFrameChar(char character) |
Fuzball | 3:41063eb2a040 | 118 | { |
Fuzball | 3:41063eb2a040 | 119 | for( int i = 0; i < 6; i++) |
Fuzball | 3:41063eb2a040 | 120 | SendDrawData(~(( FONT_6x6[ ((character - 32)*6) + i] ) << 1 ) | 0x81); |
Fuzball | 3:41063eb2a040 | 121 | } |
Fuzball | 3:41063eb2a040 | 122 | |
Fuzball | 3:41063eb2a040 | 123 | char* NokiaLcd::NumToStr(int num) |
Fuzball | 3:41063eb2a040 | 124 | { |
Fuzball | 3:41063eb2a040 | 125 | if(num <= 0) |
Fuzball | 3:41063eb2a040 | 126 | return "0"; |
Fuzball | 3:41063eb2a040 | 127 | |
Fuzball | 3:41063eb2a040 | 128 | double length = 0; |
Fuzball | 3:41063eb2a040 | 129 | int tlen = 0; |
Fuzball | 3:41063eb2a040 | 130 | int temp = 1; |
Fuzball | 3:41063eb2a040 | 131 | char c; |
Fuzball | 3:41063eb2a040 | 132 | |
Fuzball | 3:41063eb2a040 | 133 | // Get number of digits |
Fuzball | 3:41063eb2a040 | 134 | while( temp <= num ) |
Fuzball | 3:41063eb2a040 | 135 | { |
Fuzball | 3:41063eb2a040 | 136 | temp *= 10; |
Fuzball | 3:41063eb2a040 | 137 | length++; |
Fuzball | 3:41063eb2a040 | 138 | } |
Fuzball | 3:41063eb2a040 | 139 | tlen = length; |
Fuzball | 3:41063eb2a040 | 140 | char* numString = new char[tlen+1]; |
Fuzball | 3:41063eb2a040 | 141 | |
Fuzball | 3:41063eb2a040 | 142 | // Convert each place in number to a stand-alone representative number |
Fuzball | 3:41063eb2a040 | 143 | temp = 0; |
Fuzball | 3:41063eb2a040 | 144 | for(int idx = pow(10, length); idx>1; idx = (idx/10)) |
Fuzball | 3:41063eb2a040 | 145 | { |
Fuzball | 3:41063eb2a040 | 146 | c = (char)( ((num % idx)-(num % (idx/10)))/(idx/10) + 48); |
Fuzball | 3:41063eb2a040 | 147 | numString[temp] = c; |
Fuzball | 3:41063eb2a040 | 148 | temp++; |
Fuzball | 3:41063eb2a040 | 149 | } |
Fuzball | 3:41063eb2a040 | 150 | numString[temp] = '\0'; |
Fuzball | 3:41063eb2a040 | 151 | return numString; |
Fuzball | 3:41063eb2a040 | 152 | } |
Fuzball | 3:41063eb2a040 | 153 | |
Fuzball | 3:41063eb2a040 | 154 | void NokiaLcd::SetXY(char x, char y) |
Fuzball | 3:41063eb2a040 | 155 | { |
Fuzball | 3:41063eb2a040 | 156 | if( (x > 83) || (y > 5) ) |
Fuzball | 3:41063eb2a040 | 157 | return; |
Fuzball | 3:41063eb2a040 | 158 | |
Fuzball | 3:41063eb2a040 | 159 | SendFunction( x | 0x80 ); |
Fuzball | 3:41063eb2a040 | 160 | SendFunction( y | 0x40 ); |
Fuzball | 3:41063eb2a040 | 161 | } |
Fuzball | 3:41063eb2a040 | 162 | |
Fuzball |
1:e25ab356dc9b | 163 | void NokiaLcd::SendFunction(char cmd) //TODO:Detection of what H should be |
Fuzball |
1:e25ab356dc9b | 164 | { |
Fuzball |
1:e25ab356dc9b | 165 | Pins[PIN_DC]->write(0); // Data/CMD goes low |
Fuzball |
1:e25ab356dc9b | 166 | LcdSpi->write(cmd); // Command gets sent |
Fuzball |
1:e25ab356dc9b | 167 | Pins[PIN_DC]->write(1); // Data/CMD goes back to Data mode |
Fuzball |
1:e25ab356dc9b | 168 | } |
Fuzball |
1:e25ab356dc9b | 169 | |
Fuzball |
1:e25ab356dc9b | 170 | NokiaLcd::~NokiaLcd() |
Fuzball |
1:e25ab356dc9b | 171 | { |
Fuzball |
1:e25ab356dc9b | 172 | ShutdownLcd(); |
Fuzball |
1:e25ab356dc9b | 173 | } |