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.

Dependencies:   mbed NOKIA_5110

Quick and versatile LCD screen control. Works as a great alternative to serial consoles!

---------

/media/uploads/Fuzball/debug_gui_1.jpg

Inverted-color text function makes easy menus and borders

/media/uploads/Fuzball/oscil_1.jpg

Uses a simple SetXY(char, char) function to draw data-plots or patterns

- Wiring -

/media/uploads/Fuzball/debug_gui_2.jpg

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 :)

Committer:
Fuzball
Date:
Mon Jan 16 19:51:19 2012 +0000
Revision:
1:e25ab356dc9b
Child:
3:41063eb2a040
Forgot to change the project\'s title :3;

Who changed what in which revision?

UserRevisionLine numberNew 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 1:e25ab356dc9b 5 // Revised:
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 Pins[PIN_SCE]->write(0); // Chip Select goes low
Fuzball 1:e25ab356dc9b 48 Pins[PIN_DC]->write(1); // Data/CMD goes high
Fuzball 1:e25ab356dc9b 49
Fuzball 1:e25ab356dc9b 50 for(int tick = 0; tick <= 503; tick++)
Fuzball 1:e25ab356dc9b 51 LcdSpi->write(0x00);
Fuzball 1:e25ab356dc9b 52
Fuzball 1:e25ab356dc9b 53 Pins[PIN_SCE]->write(1); // Chip Select goes high
Fuzball 1:e25ab356dc9b 54 }
Fuzball 1:e25ab356dc9b 55
Fuzball 1:e25ab356dc9b 56 void NokiaLcd::TestLcd(char test_pattern)
Fuzball 1:e25ab356dc9b 57 {
Fuzball 1:e25ab356dc9b 58 Pins[PIN_SCE]->write(0); // Chip Select goes low
Fuzball 1:e25ab356dc9b 59 Pins[PIN_DC]->write(1); // Data/CMD goes high
Fuzball 1:e25ab356dc9b 60
Fuzball 1:e25ab356dc9b 61 for(int tick = 0; tick <= 503; tick++)
Fuzball 1:e25ab356dc9b 62 LcdSpi->write(test_pattern); // Command gets sent
Fuzball 1:e25ab356dc9b 63
Fuzball 1:e25ab356dc9b 64 Pins[PIN_SCE]->write(1); // Chip Select goes high
Fuzball 1:e25ab356dc9b 65 }
Fuzball 1:e25ab356dc9b 66
Fuzball 1:e25ab356dc9b 67 void NokiaLcd::InitLcd()
Fuzball 1:e25ab356dc9b 68 {
Fuzball 1:e25ab356dc9b 69 ResetLcd();
Fuzball 1:e25ab356dc9b 70
Fuzball 1:e25ab356dc9b 71 // Redefine the FunctionChar in case it has changed
Fuzball 1:e25ab356dc9b 72 FunctionSet.V = CMD_FS_HORIZONTAL_MODE;
Fuzball 1:e25ab356dc9b 73 FunctionSet.H = CMD_FS_EXTENDED_MODE;
Fuzball 1:e25ab356dc9b 74 FunctionSet.PD = CMD_FS_ACTIVE_MODE;
Fuzball 1:e25ab356dc9b 75 SendFunction( CreateFunctionChar() ); // Extended CMD set
Fuzball 1:e25ab356dc9b 76 SendFunction( VopChar ); // | Vop
Fuzball 1:e25ab356dc9b 77 SendFunction( TempControlChar ); // | Temp
Fuzball 1:e25ab356dc9b 78 SendFunction( BiasChar ); // | Bias
Fuzball 1:e25ab356dc9b 79
Fuzball 1:e25ab356dc9b 80 FunctionSet.H = CMD_FS_BASIC_MODE;
Fuzball 1:e25ab356dc9b 81 SendFunction( CreateFunctionChar() ); // Basic CMD set
Fuzball 1:e25ab356dc9b 82 SendFunction( DispControlChar ); // | Display Mode
Fuzball 1:e25ab356dc9b 83
Fuzball 1:e25ab356dc9b 84 ClearLcdMem();
Fuzball 1:e25ab356dc9b 85 }
Fuzball 1:e25ab356dc9b 86
Fuzball 1:e25ab356dc9b 87 void NokiaLcd::ResetLcd()
Fuzball 1:e25ab356dc9b 88 {
Fuzball 1:e25ab356dc9b 89 Pins[PIN_RST]->write(0); // Reset goes low
Fuzball 1:e25ab356dc9b 90 Pins[PIN_RST]->write(1); // Reset goes high
Fuzball 1:e25ab356dc9b 91 }
Fuzball 1:e25ab356dc9b 92
Fuzball 1:e25ab356dc9b 93 char NokiaLcd::CreateFunctionChar()
Fuzball 1:e25ab356dc9b 94 {
Fuzball 1:e25ab356dc9b 95 return ( 0x20 | FunctionSet.PD | FunctionSet.V | FunctionSet.H );
Fuzball 1:e25ab356dc9b 96 }
Fuzball 1:e25ab356dc9b 97
Fuzball 1:e25ab356dc9b 98 void NokiaLcd::SendDrawData(char data)
Fuzball 1:e25ab356dc9b 99 {
Fuzball 1:e25ab356dc9b 100 Pins[PIN_SCE]->write(0); // Chip Select goes low
Fuzball 1:e25ab356dc9b 101 Pins[PIN_DC]->write(1); // Data/CMD goes high
Fuzball 1:e25ab356dc9b 102
Fuzball 1:e25ab356dc9b 103 LcdSpi->write(data); // Command gets sent
Fuzball 1:e25ab356dc9b 104
Fuzball 1:e25ab356dc9b 105 Pins[PIN_SCE]->write(1); // Chip Select goes high
Fuzball 1:e25ab356dc9b 106 }
Fuzball 1:e25ab356dc9b 107
Fuzball 1:e25ab356dc9b 108 void NokiaLcd::DrawChar(char character)
Fuzball 1:e25ab356dc9b 109 {
Fuzball 1:e25ab356dc9b 110 for( int i = 0; i < 6; i++)
Fuzball 1:e25ab356dc9b 111 SendDrawData( FONT_6x6[ ((character - 32)*6) + i] );
Fuzball 1:e25ab356dc9b 112 }
Fuzball 1:e25ab356dc9b 113
Fuzball 1:e25ab356dc9b 114 void NokiaLcd::SendFunction(char cmd) //TODO:Detection of what H should be
Fuzball 1:e25ab356dc9b 115 {
Fuzball 1:e25ab356dc9b 116 Pins[PIN_SCE]->write(0); // Chip Select goes low
Fuzball 1:e25ab356dc9b 117 Pins[PIN_DC]->write(0); // Data/CMD goes low
Fuzball 1:e25ab356dc9b 118
Fuzball 1:e25ab356dc9b 119 LcdSpi->write(cmd); // Command gets sent
Fuzball 1:e25ab356dc9b 120
Fuzball 1:e25ab356dc9b 121 Pins[PIN_DC]->write(1); // Data/CMD goes back to Data mode
Fuzball 1:e25ab356dc9b 122 Pins[PIN_SCE]->write(1); // Chip Select goes high
Fuzball 1:e25ab356dc9b 123 }
Fuzball 1:e25ab356dc9b 124
Fuzball 1:e25ab356dc9b 125 NokiaLcd::~NokiaLcd()
Fuzball 1:e25ab356dc9b 126 {
Fuzball 1:e25ab356dc9b 127 ShutdownLcd();
Fuzball 1:e25ab356dc9b 128 }