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
- Committer:
- Fuzball
- Date:
- 2012-01-16
- Revision:
- 0:fbba596f1039
File content as of revision 0:fbba596f1039:
// Project: Fuzzy LCD Voltmeter // File: NOKIA_5110.cpp // Author: Chris Yan // Created: January, 2012 // Revised: // Desc: Supporting code for the NokiaLcd class #include "NOKIA_5110.h" #include "mbed.h" NokiaLcd::NokiaLcd(LcdPins pinout) { // SPI LcdSpi = new SPI(pinout.mosi, pinout.miso, pinout.sclk); LcdSpi->format(LCD_SPI_BITS, LCD_SPI_MODE); LcdSpi->frequency(LCD_FREQ); // Control Pins Pins = new DigitalOut*[3]; Pins[PIN_RST] = new DigitalOut(pinout.rst); Pins[PIN_SCE] = new DigitalOut(pinout.sce); Pins[PIN_DC] = new DigitalOut(pinout.dc); // Initial Command Instructions, note the initial command mode FunctionSet.V = CMD_FS_HORIZONTAL_MODE; FunctionSet.H = CMD_FS_EXTENDED_MODE; FunctionSet.PD = CMD_FS_ACTIVE_MODE; FunctionChar = CreateFunctionChar(); TempControlChar = CMD_TC_TEMP_2; DispControlChar = CMD_DC_NORMAL_MODE; BiasChar = CMD_BI_MUX_48; VopChar = CMD_VOP_7V38; } void NokiaLcd::ShutdownLcd() { FunctionSet.PD = CMD_FS_POWER_DOWN_MODE; ClearLcdMem(); SendFunction( CMD_DC_CLEAR_DISPLAY ); SendFunction( CreateFunctionChar() ); } void NokiaLcd::ClearLcdMem() { Pins[PIN_SCE]->write(0); // Chip Select goes low Pins[PIN_DC]->write(1); // Data/CMD goes high for(int tick = 0; tick <= 503; tick++) LcdSpi->write(0x00); Pins[PIN_SCE]->write(1); // Chip Select goes high } void NokiaLcd::TestLcd(char test_pattern) { Pins[PIN_SCE]->write(0); // Chip Select goes low Pins[PIN_DC]->write(1); // Data/CMD goes high for(int tick = 0; tick <= 503; tick++) LcdSpi->write(test_pattern); // Command gets sent Pins[PIN_SCE]->write(1); // Chip Select goes high } void NokiaLcd::InitLcd() { ResetLcd(); // Redefine the FunctionChar in case it has changed FunctionSet.V = CMD_FS_HORIZONTAL_MODE; FunctionSet.H = CMD_FS_EXTENDED_MODE; FunctionSet.PD = CMD_FS_ACTIVE_MODE; SendFunction( CreateFunctionChar() ); // Extended CMD set SendFunction( VopChar ); // | Vop SendFunction( TempControlChar ); // | Temp SendFunction( BiasChar ); // | Bias FunctionSet.H = CMD_FS_BASIC_MODE; SendFunction( CreateFunctionChar() ); // Basic CMD set SendFunction( DispControlChar ); // | Display Mode ClearLcdMem(); } void NokiaLcd::ResetLcd() { Pins[PIN_RST]->write(0); // Reset goes low Pins[PIN_RST]->write(1); // Reset goes high } char NokiaLcd::CreateFunctionChar() { return ( 0x20 | FunctionSet.PD | FunctionSet.V | FunctionSet.H ); } void NokiaLcd::SendDrawData(char data) { Pins[PIN_SCE]->write(0); // Chip Select goes low Pins[PIN_DC]->write(1); // Data/CMD goes high LcdSpi->write(data); // Command gets sent Pins[PIN_SCE]->write(1); // Chip Select goes high } void NokiaLcd::DrawChar(char character) { for( int i = 0; i < 6; i++) SendDrawData( FONT_6x6[ ((character - 32)*6) + i] ); } void NokiaLcd::SendFunction(char cmd) //TODO:Detection of what H should be { Pins[PIN_SCE]->write(0); // Chip Select goes low Pins[PIN_DC]->write(0); // Data/CMD goes low LcdSpi->write(cmd); // Command gets sent Pins[PIN_DC]->write(1); // Data/CMD goes back to Data mode Pins[PIN_SCE]->write(1); // Chip Select goes high } NokiaLcd::~NokiaLcd() { ShutdownLcd(); }