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