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

Committer:
Fuzball
Date:
Fri Oct 15 20:36:13 2021 +0000
Revision:
1:5ec993784d65
Parent:
0:9cfce382e741
.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Fuzball 0:9cfce382e741 1 // Project: Nokia5110 - Controlling a NK5110 display from an NXP LPC1768
Fuzball 0:9cfce382e741 2 // File: NOKIA_5110.cpp
Fuzball 1:5ec993784d65 3 // Author: Krissi Yan
Fuzball 1:5ec993784d65 4 // Created: January, 2016
Fuzball 1:5ec993784d65 5 // Revised: January, 2017
Fuzball 0:9cfce382e741 6 // Desc: Supporting code for the NokiaLcd class
Fuzball 0:9cfce382e741 7
Fuzball 0:9cfce382e741 8 #include "NOKIA_5110.h"
Fuzball 0:9cfce382e741 9 #include "mbed.h"
Fuzball 0:9cfce382e741 10
Fuzball 0:9cfce382e741 11 NokiaLcd::NokiaLcd(LcdPins pinout)
Fuzball 0:9cfce382e741 12 {
Fuzball 0:9cfce382e741 13 // SPI
Fuzball 0:9cfce382e741 14 LcdSpi = new SPI(pinout.mosi, pinout.miso, pinout.sclk);
Fuzball 0:9cfce382e741 15 LcdSpi->format(LCD_SPI_BITS, LCD_SPI_MODE);
Fuzball 0:9cfce382e741 16 LcdSpi->frequency(LCD_FREQ);
Fuzball 0:9cfce382e741 17
Fuzball 0:9cfce382e741 18 // Control Pins
Fuzball 0:9cfce382e741 19 Pins = new DigitalOut*[3];
Fuzball 0:9cfce382e741 20 Pins[PIN_RST] = new DigitalOut(pinout.rst);
Fuzball 0:9cfce382e741 21 Pins[PIN_SCE] = new DigitalOut(pinout.sce);
Fuzball 0:9cfce382e741 22 Pins[PIN_DC] = new DigitalOut(pinout.dc);
Fuzball 0:9cfce382e741 23
Fuzball 0:9cfce382e741 24 // Initial Command Instructions, note the initial command mode
Fuzball 0:9cfce382e741 25 FunctionSet.V = CMD_FS_HORIZONTAL_MODE;
Fuzball 0:9cfce382e741 26 FunctionSet.H = CMD_FS_EXTENDED_MODE;
Fuzball 0:9cfce382e741 27 FunctionSet.PD = CMD_FS_ACTIVE_MODE;
Fuzball 0:9cfce382e741 28 FunctionChar = CreateFunctionChar();
Fuzball 0:9cfce382e741 29
Fuzball 0:9cfce382e741 30 TempControlChar = CMD_TC_TEMP_2;
Fuzball 0:9cfce382e741 31 DispControlChar = CMD_DC_NORMAL_MODE;
Fuzball 0:9cfce382e741 32 BiasChar = CMD_BI_MUX_48;
Fuzball 0:9cfce382e741 33 VopChar = CMD_VOP_7V38;
Fuzball 0:9cfce382e741 34 }
Fuzball 0:9cfce382e741 35
Fuzball 0:9cfce382e741 36 void NokiaLcd::ShutdownLcd()
Fuzball 0:9cfce382e741 37 {
Fuzball 0:9cfce382e741 38 FunctionSet.PD = CMD_FS_POWER_DOWN_MODE;
Fuzball 0:9cfce382e741 39
Fuzball 0:9cfce382e741 40 ClearLcdMem();
Fuzball 0:9cfce382e741 41 SendFunction( CMD_DC_CLEAR_DISPLAY );
Fuzball 0:9cfce382e741 42 SendFunction( CreateFunctionChar() );
Fuzball 0:9cfce382e741 43 }
Fuzball 0:9cfce382e741 44
Fuzball 0:9cfce382e741 45 void NokiaLcd::ClearLcdMem()
Fuzball 0:9cfce382e741 46 {
Fuzball 0:9cfce382e741 47 for(int tick = 0; tick <= 503; tick++)
Fuzball 0:9cfce382e741 48 LcdSpi->write(0x00);
Fuzball 0:9cfce382e741 49 }
Fuzball 0:9cfce382e741 50
Fuzball 0:9cfce382e741 51 void NokiaLcd::TestLcd(char test_pattern)
Fuzball 0:9cfce382e741 52 {
Fuzball 0:9cfce382e741 53 for(int tick = 0; tick <= 503; tick++)
Fuzball 0:9cfce382e741 54 LcdSpi->write(test_pattern); // Command gets sent
Fuzball 0:9cfce382e741 55 }
Fuzball 0:9cfce382e741 56
Fuzball 0:9cfce382e741 57 void NokiaLcd::InitLcd()
Fuzball 0:9cfce382e741 58 {
Fuzball 0:9cfce382e741 59 ResetLcd();
Fuzball 0:9cfce382e741 60 Pins[PIN_SCE]->write(0); // Chip Select goes low
Fuzball 0:9cfce382e741 61
Fuzball 0:9cfce382e741 62 // Redefine the FunctionChar in case it has changed
Fuzball 0:9cfce382e741 63 FunctionSet.V = CMD_FS_HORIZONTAL_MODE;
Fuzball 0:9cfce382e741 64 FunctionSet.H = CMD_FS_EXTENDED_MODE;
Fuzball 0:9cfce382e741 65 FunctionSet.PD = CMD_FS_ACTIVE_MODE;
Fuzball 0:9cfce382e741 66 SendFunction( CreateFunctionChar() ); // Extended CMD set
Fuzball 0:9cfce382e741 67 SendFunction( VopChar ); // | Vop
Fuzball 0:9cfce382e741 68 SendFunction( TempControlChar ); // | Temp
Fuzball 0:9cfce382e741 69 SendFunction( BiasChar ); // | Bias
Fuzball 0:9cfce382e741 70
Fuzball 0:9cfce382e741 71 FunctionSet.H = CMD_FS_BASIC_MODE;
Fuzball 0:9cfce382e741 72 SendFunction( CreateFunctionChar() ); // Basic CMD set
Fuzball 0:9cfce382e741 73 SendFunction( DispControlChar ); // | Display Mode
Fuzball 0:9cfce382e741 74
Fuzball 0:9cfce382e741 75 ClearLcdMem();
Fuzball 0:9cfce382e741 76 Pins[PIN_DC]->write(1); // Data/CMD goes back to Data mode
Fuzball 0:9cfce382e741 77 }
Fuzball 0:9cfce382e741 78
Fuzball 0:9cfce382e741 79 void NokiaLcd::ResetLcd()
Fuzball 0:9cfce382e741 80 {
Fuzball 0:9cfce382e741 81 Pins[PIN_RST]->write(0); // Reset goes low
Fuzball 0:9cfce382e741 82 Pins[PIN_RST]->write(1); // Reset goes high
Fuzball 0:9cfce382e741 83 }
Fuzball 0:9cfce382e741 84
Fuzball 0:9cfce382e741 85 char NokiaLcd::CreateFunctionChar()
Fuzball 0:9cfce382e741 86 {
Fuzball 0:9cfce382e741 87 return ( 0x20 | FunctionSet.PD | FunctionSet.V | FunctionSet.H );
Fuzball 0:9cfce382e741 88 }
Fuzball 0:9cfce382e741 89
Fuzball 0:9cfce382e741 90 void NokiaLcd::SendDrawData(char data)
Fuzball 0:9cfce382e741 91 {
Fuzball 0:9cfce382e741 92 LcdSpi->write(data); // Command gets sent
Fuzball 0:9cfce382e741 93 }
Fuzball 0:9cfce382e741 94
Fuzball 0:9cfce382e741 95 void NokiaLcd::DrawChar(char character)
Fuzball 0:9cfce382e741 96 {
Fuzball 0:9cfce382e741 97 for( int i = 0; i < 6; i++)
Fuzball 0:9cfce382e741 98 SendDrawData( FONT_6x6[ ((character - 32)*6) + i] );
Fuzball 0:9cfce382e741 99 }
Fuzball 0:9cfce382e741 100
Fuzball 0:9cfce382e741 101 void NokiaLcd::DrawString(char* s)
Fuzball 0:9cfce382e741 102 {
Fuzball 0:9cfce382e741 103 char len = strlen(s);
Fuzball 0:9cfce382e741 104 for( int idx = 0; idx < len; idx++ )
Fuzball 0:9cfce382e741 105 {
Fuzball 0:9cfce382e741 106 for( int i = 0; i < 6; i++)
Fuzball 0:9cfce382e741 107 SendDrawData( FONT_6x6[ ((s[idx] - 32)*6) + i] );
Fuzball 0:9cfce382e741 108 }
Fuzball 0:9cfce382e741 109 }
Fuzball 0:9cfce382e741 110
Fuzball 0:9cfce382e741 111 void NokiaLcd::DrawFrameChar(char character)
Fuzball 0:9cfce382e741 112 {
Fuzball 0:9cfce382e741 113 for( int i = 0; i < 6; i++)
Fuzball 0:9cfce382e741 114 SendDrawData((( FONT_6x6[ ((character - 32)*6) + i] ) << 1 ) | 0x81);
Fuzball 0:9cfce382e741 115 }
Fuzball 0:9cfce382e741 116
Fuzball 0:9cfce382e741 117 void NokiaLcd::DrawNegFrameChar(char character)
Fuzball 0:9cfce382e741 118 {
Fuzball 0:9cfce382e741 119 for( int i = 0; i < 6; i++)
Fuzball 0:9cfce382e741 120 SendDrawData(~(( FONT_6x6[ ((character - 32)*6) + i] ) << 1 ) | 0x81);
Fuzball 0:9cfce382e741 121 }
Fuzball 0:9cfce382e741 122
Fuzball 0:9cfce382e741 123 char* NokiaLcd::NumToStr(int num)
Fuzball 0:9cfce382e741 124 {
Fuzball 0:9cfce382e741 125 if(num <= 0)
Fuzball 0:9cfce382e741 126 return "0";
Fuzball 0:9cfce382e741 127
Fuzball 0:9cfce382e741 128 double length = 0;
Fuzball 0:9cfce382e741 129 int tlen = 0;
Fuzball 0:9cfce382e741 130 int temp = 1;
Fuzball 0:9cfce382e741 131 char c;
Fuzball 0:9cfce382e741 132
Fuzball 0:9cfce382e741 133 // Get number of digits
Fuzball 0:9cfce382e741 134 while( temp <= num )
Fuzball 0:9cfce382e741 135 {
Fuzball 0:9cfce382e741 136 temp *= 10;
Fuzball 0:9cfce382e741 137 length++;
Fuzball 0:9cfce382e741 138 }
Fuzball 0:9cfce382e741 139 tlen = length;
Fuzball 0:9cfce382e741 140 char* numString = new char[tlen+1];
Fuzball 0:9cfce382e741 141
Fuzball 0:9cfce382e741 142 // Convert each place in number to a stand-alone representative number
Fuzball 0:9cfce382e741 143 temp = 0;
Fuzball 0:9cfce382e741 144 for(int idx = pow(10, length); idx>1; idx = (idx/10))
Fuzball 0:9cfce382e741 145 {
Fuzball 0:9cfce382e741 146 c = (char)( ((num % idx)-(num % (idx/10)))/(idx/10) + 48);
Fuzball 0:9cfce382e741 147 numString[temp] = c;
Fuzball 0:9cfce382e741 148 temp++;
Fuzball 0:9cfce382e741 149 }
Fuzball 0:9cfce382e741 150 numString[temp] = '\0';
Fuzball 0:9cfce382e741 151 return numString;
Fuzball 0:9cfce382e741 152 }
Fuzball 0:9cfce382e741 153
Fuzball 0:9cfce382e741 154 void NokiaLcd::SetXY(char x, char y)
Fuzball 0:9cfce382e741 155 {
Fuzball 0:9cfce382e741 156 if( (x > 83) || (y > 5) )
Fuzball 0:9cfce382e741 157 return;
Fuzball 0:9cfce382e741 158
Fuzball 0:9cfce382e741 159 SendFunction( x | 0x80 );
Fuzball 0:9cfce382e741 160 SendFunction( y | 0x40 );
Fuzball 0:9cfce382e741 161 }
Fuzball 0:9cfce382e741 162
Fuzball 0:9cfce382e741 163 void NokiaLcd::SendFunction(char cmd) //TODO:Detection of what H should be
Fuzball 0:9cfce382e741 164 {
Fuzball 0:9cfce382e741 165 Pins[PIN_DC]->write(0); // Data/CMD goes low
Fuzball 0:9cfce382e741 166 LcdSpi->write(cmd); // Command gets sent
Fuzball 0:9cfce382e741 167 Pins[PIN_DC]->write(1); // Data/CMD goes back to Data mode
Fuzball 0:9cfce382e741 168 }
Fuzball 0:9cfce382e741 169
Fuzball 0:9cfce382e741 170 NokiaLcd::~NokiaLcd()
Fuzball 0:9cfce382e741 171 {
Fuzball 0:9cfce382e741 172 ShutdownLcd();
Fuzball 0:9cfce382e741 173 }