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.cpp Source File

NOKIA_5110.cpp

00001 // Project: Nokia5110 - Controlling a NK5110 display from an NXP LPC1768
00002 // File: NOKIA_5110.cpp
00003 // Author: Krissi Yan
00004 // Created: January, 2016
00005 // Revised: January, 2017
00006 //  Desc: Supporting code for the NokiaLcd class
00007 
00008 #include "NOKIA_5110.h"
00009 #include "mbed.h"
00010 
00011 NokiaLcd::NokiaLcd(LcdPins pinout)
00012 {
00013     // SPI
00014     LcdSpi = new SPI(pinout.mosi, pinout.miso, pinout.sclk);
00015     LcdSpi->format(LCD_SPI_BITS, LCD_SPI_MODE);
00016     LcdSpi->frequency(LCD_FREQ);
00017     
00018     // Control Pins
00019     Pins = new DigitalOut*[3];
00020     Pins[PIN_RST]   = new DigitalOut(pinout.rst);
00021     Pins[PIN_SCE]   = new DigitalOut(pinout.sce);
00022     Pins[PIN_DC]    = new DigitalOut(pinout.dc);
00023     
00024     // Initial Command Instructions, note the initial command mode
00025     FunctionSet.V   = CMD_FS_HORIZONTAL_MODE;
00026     FunctionSet.H   = CMD_FS_EXTENDED_MODE;
00027     FunctionSet.PD  = CMD_FS_ACTIVE_MODE;
00028     FunctionChar    = CreateFunctionChar();
00029     
00030     TempControlChar = CMD_TC_TEMP_2;
00031     DispControlChar = CMD_DC_NORMAL_MODE;
00032     BiasChar        = CMD_BI_MUX_48;
00033     VopChar         = CMD_VOP_7V38;
00034 }
00035 
00036 void NokiaLcd::ShutdownLcd()
00037 {
00038     FunctionSet.PD  = CMD_FS_POWER_DOWN_MODE;
00039 
00040     ClearLcdMem();
00041     SendFunction( CMD_DC_CLEAR_DISPLAY );
00042     SendFunction( CreateFunctionChar() );
00043 }
00044 
00045 void NokiaLcd::ClearLcdMem()
00046 {
00047     for(int tick = 0; tick <= 503; tick++)
00048         LcdSpi->write(0x00);
00049 }
00050 
00051 void NokiaLcd::TestLcd(char test_pattern)
00052 {
00053     for(int tick = 0; tick <= 503; tick++)
00054         LcdSpi->write(test_pattern);         // Command gets sent
00055 }
00056 
00057 void NokiaLcd::InitLcd()
00058 {
00059     ResetLcd();
00060     Pins[PIN_SCE]->write(0);     // Chip Select goes low
00061     
00062     // Redefine the FunctionChar in case it has changed
00063     FunctionSet.V   = CMD_FS_HORIZONTAL_MODE;
00064     FunctionSet.H   = CMD_FS_EXTENDED_MODE;
00065     FunctionSet.PD  = CMD_FS_ACTIVE_MODE;
00066     SendFunction( CreateFunctionChar() );   // Extended CMD set
00067     SendFunction( VopChar );                // | Vop
00068     SendFunction( TempControlChar );        // | Temp
00069     SendFunction( BiasChar );               // | Bias
00070 
00071     FunctionSet.H   = CMD_FS_BASIC_MODE;
00072     SendFunction( CreateFunctionChar() );   // Basic CMD set
00073     SendFunction( DispControlChar );        // | Display Mode
00074     
00075     ClearLcdMem();
00076     Pins[PIN_DC]->write(1);     // Data/CMD goes back to Data mode
00077 }
00078 
00079 void NokiaLcd::ResetLcd()
00080 {
00081     Pins[PIN_RST]->write(0);    // Reset goes low
00082     Pins[PIN_RST]->write(1);    // Reset goes high
00083 }
00084 
00085 char NokiaLcd::CreateFunctionChar()
00086 {
00087     return ( 0x20 | FunctionSet.PD | FunctionSet.V | FunctionSet.H );
00088 }
00089 
00090 void NokiaLcd::SendDrawData(char data)
00091 {
00092     LcdSpi->write(data);         // Command gets sent
00093 }
00094 
00095 void NokiaLcd::DrawChar(char character)
00096 {
00097     for( int i = 0; i < 6; i++)
00098         SendDrawData( FONT_6x6[ ((character - 32)*6) + i] );
00099 }
00100 
00101 void NokiaLcd::DrawString(char* s)
00102 {
00103     char len = strlen(s);
00104     for( int idx = 0; idx < len; idx++ )
00105     {
00106         for( int i = 0; i < 6; i++)
00107             SendDrawData( FONT_6x6[ ((s[idx] - 32)*6) + i] );
00108     }
00109 }
00110 
00111 void NokiaLcd::DrawFrameChar(char character)
00112 {
00113     for( int i = 0; i < 6; i++)
00114         SendDrawData((( FONT_6x6[ ((character - 32)*6) + i]  ) << 1 ) | 0x81);
00115 }
00116 
00117 void NokiaLcd::DrawNegFrameChar(char character)
00118 {
00119     for( int i = 0; i < 6; i++)
00120         SendDrawData(~(( FONT_6x6[ ((character - 32)*6) + i]  ) << 1 ) | 0x81);
00121 }
00122 
00123 char* NokiaLcd::NumToStr(int num)
00124 {
00125     if(num <= 0)
00126         return "0";
00127 
00128     double length = 0;
00129     int tlen = 0;
00130     int temp = 1;
00131     char c;
00132     
00133     // Get number of digits
00134     while( temp <= num )
00135     {
00136         temp *= 10;
00137         length++;
00138     }
00139     tlen = length;
00140     char* numString = new char[tlen+1];
00141     
00142     // Convert each place in number to a stand-alone representative number
00143     temp = 0;
00144     for(int idx = pow(10, length); idx>1; idx = (idx/10))
00145     {
00146         c = (char)( ((num % idx)-(num % (idx/10)))/(idx/10) + 48);
00147         numString[temp] = c;
00148         temp++;
00149     }
00150     numString[temp] = '\0';
00151     return numString;
00152 }
00153 
00154 void NokiaLcd::SetXY(char x, char y)
00155 {
00156     if( (x > 83) || (y > 5) )
00157         return;
00158 
00159     SendFunction( x | 0x80 );
00160     SendFunction( y | 0x40 );
00161 }
00162 
00163 void NokiaLcd::SendFunction(char cmd) //TODO:Detection of what H should be
00164 {
00165     Pins[PIN_DC]->write(0);     // Data/CMD goes low
00166     LcdSpi->write(cmd);         // Command gets sent
00167     Pins[PIN_DC]->write(1);     // Data/CMD goes back to Data mode
00168 }
00169 
00170 NokiaLcd::~NokiaLcd()
00171 {
00172     ShutdownLcd();
00173 }