Simple library for interfacing to Nokia 5110 LCD display (as found on the SparkFun website).
Dependents: LabyrinthOfTheMinotaur
Fork of N5110 by
Revision 14:520a02fc12aa, committed 2015-03-10
- Comitter:
- eencae
- Date:
- Tue Mar 10 19:21:47 2015 +0000
- Parent:
- 12:022993561fd8
- Child:
- 15:ee645611ff94
- Commit message:
- Expanded example and modified printChar().
Changed in this revision
N5110.cpp | Show annotated file Show diff for this revision Revisions of this file |
N5110.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/N5110.cpp Tue Mar 10 18:09:37 2015 +0000 +++ b/N5110.cpp Tue Mar 10 19:21:47 2015 +0000 @@ -10,10 +10,10 @@ N5110::N5110(PinName pwrPin, PinName scePin, PinName rstPin, PinName dcPin, PinName mosiPin, PinName sclkPin, PinName ledPin) { - + spi = new SPI(mosiPin,NC,sclkPin); // create new SPI instance and initialise - initSPI(); - + initSPI(); + // set up pins as required led = new PwmOut(ledPin); pwr = new DigitalOut(pwrPin); @@ -47,16 +47,18 @@ clearRAM(); } - -// sets normal video mode (black on white) -void N5110::normalMode() { - sendCommand(CMD_DC_NORMAL_MODE); - + +// sets normal video mode (black on white) +void N5110::normalMode() +{ + sendCommand(CMD_DC_NORMAL_MODE); + } -// sets normal video mode (white on black) -void N5110::inverseMode() { - sendCommand(CMD_DC_INVERT_VIDEO); +// sets normal video mode (white on black) +void N5110::inverseMode() +{ + sendCommand(CMD_DC_INVERT_VIDEO); } // function to power up the LCD and backlight @@ -144,7 +146,7 @@ } -// function to set the XY address in RAM for subsequenct data write +// function to set the XY address in RAM for subsequenct data write void N5110::setXYAddress(int x, int y) { // check whether address is in range @@ -187,11 +189,11 @@ void N5110::refresh() { int i,j; - + setXYAddress(0,0); // important to set address back to 0,0 before refreshing display // address auto increments after printing string, so buffer[0][0] will not coincide // with top-left pixel after priting string - + sce->write(0); //set CE low to begin frame for(j = 0; j < 6; j++) { // be careful to use correct order (j,i) for horizontal addressing @@ -217,17 +219,14 @@ } // function to print 5x7 font -void N5110::printChar(char c) +void N5110::printChar(char c,int x,int y) { - int i; - // loop through 5 columns - for (i = 0; i < 5 ; i++ ) { - sendData(font5x7[(c - 32)*5 + i]); - // array is offset by 32 relative to ASCII, each character is 5 pixels wide - // the X address is automatically incremented after each data write + for (int i = 0; i < 5 ; i++ ) { + buffer[x+i][y] = font5x7[(c - 32)*5 + i]; + // array is offset by 32 relative to ASCII, each character is 5 pixels wide } - sendData(0); // send an empty byte to introduce space between characters - + + refresh(); // this sends the buffer to the display and sets address (cursor) back to 0,0 } // function to print string at specified position @@ -237,20 +236,16 @@ // loop through string and print character while(*str) { - // This is the old version - strings are printed using the printChar function - //setXYAddress(x+6*n,y); // leave 1 pixel (6 = 5 + 1) between each character - //printChar(*str); // print the char - can probably do *str++ and remove next line - - // the new version writes the character bitmap data to the buffer, so that + // writes the character bitmap data to the buffer, so that // text and pixels can be displayed at the same time for (int i = 0; i < 5 ; i++ ) { buffer[x+i+n*6][y] = font5x7[(*str - 32)*5 + i]; } - + str++; // go to next character in string n++; // increment index } - + refresh(); // this sends the buffer to the display and sets address (cursor) back to 0,0 } @@ -258,7 +253,6 @@ // function to clear the screen void N5110::clear() { - clearRAM(); // clear on-board RAM clearBuffer(); // clear the buffer then call the refresh function refresh(); } @@ -275,18 +269,19 @@ } // function to plot array on display -void N5110::plotArray(float array[]) { - +void N5110::plotArray(float array[]) +{ + int i; - + for (i=0; i<84; i++) { // loop through array // elements are normalised from 0.0 to 1.0, so multiply // by 47 to convert to pixel range, and subtract from 47 // since top-left is 0,0 in the display geometry setPixel(i,47 - int(array[i]*47.0)); - } - + } + refresh(); - - + + } \ No newline at end of file
--- a/N5110.h Tue Mar 10 18:09:37 2015 +0000 +++ b/N5110.h Tue Mar 10 19:21:47 2015 +0000 @@ -32,6 +32,10 @@ #define CMD_VOP_6V06 0xB2 #define CMD_VOP_7V38 0xC8 +// number of pixels on display +#define WIDTH 84 +#define HEIGHT 48 + #include "mbed.h" /** @@ -50,45 +54,77 @@ * Example: * @code - #include "mbed.h" - #include "N5110.h" - - // VCC,SCE,RST,D/C,MOSI,SCLK,LED - N5110 lcd(p7,p8,p9,p10,p11,p13,p21); - - int main() { +#include "mbed.h" +#include "N5110.h" + +// VCC,SCE,RST,D/C,MOSI,SCLK,LED +N5110 lcd(p7,p8,p9,p10,p11,p13,p21); +// Can also power (VCC) directly from VOUT (3.3 V) - +// Can give better performance due to current limitation from GPIO pin + +int main() +{ + // first need to initialise display + lcd.init(); + + while(1) { + + char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) + // so can display a string of a maximum 14 characters in length + + lcd.clear(); // clear display + // these are default settings so not strictly needed + lcd.normalMode(); // normal colour mode + lcd.setBrightness(0.5); // put LED backlight on full + + // can directly print strings at specified co-ordinates + lcd.printString("Hello, World!",0,0); + + // or create formatted strings - ensure they aren't more than 14 characters long + int temperature = 27; + int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer + // it is important the format specifier ensures the length will fit in the buffer + if (length <= 14) // if string will fit on display + lcd.printString(buffer,0,1); // display on screen + + float pressure = 1012.3; // same idea with floats + length = sprintf(buffer,"P = %.2f mb",pressure); + if (length <= 14) + lcd.printString(buffer,0,2); - // initialise display - lcd.init(); - // print a string in top-left corner - lcd.printString("Hello, World!",0,0); - // move cursor to 4th row - lcd.setXYAddress(0,3); - // print character - lcd.printChar('X'); - - // data to be printed on display - int temperature = 27; - // print formatted data to buffer - char buffer[14]; // each character is 6 pixels, screen is 84 pixels (84/6 = 14) - int length = sprintf(buffer,"Temperatu = %2d",temperature); - // it is important the format specifier ensures the string will fit in the buffer - // if string will fit on display - if (length <= 14) - lcd.printString(buffer,0,4); // display on screen - else - lcd.printString("Too long",0,4); // else print error message + // can also print individual characters + lcd.printChar('X',5,3); + + // draw a line across the display at y = 40 pixels (origin top-left) + for (int i = 0; i < WIDTH; i++) { + lcd.setPixel(i,40); + } + // need to refresh display after setting pixels + lcd.refresh(); + + // can also check status of pixels using getPixel(x,y) + + wait(5.0); + + lcd.clear(); // clear display + lcd.inverseMode(); // invert colours + lcd.setBrightness(1.0); // put LED backlight on full + + float array[84]; + + for (int i = 0; i < 84; i++) { + array[i] = 0.5 + 0.5*sin(i*2*3.14/84); + } + + // can also plot graphs - 84 elements only + // values must be in range 0.0 - 1.0 + lcd.plotArray(array); + + wait(5.0); - // same idea with floats - float humidity = 9.45; - length = sprintf(buffer,"Humidit = %4.2f",humidity); - if (length <= 14) - lcd.printString(buffer,0,2); - else - lcd.printString("Too long",0,2); - - while(1); - } + } +} + * @endcode */ @@ -146,14 +182,6 @@ */ void setBrightness(float brightness); - /** Set XY Address - * - * Sets the X and Y address of where the next data sent to the displa will be written in RAM. - * @param x - the column number (0 to 83) - is automatically incremented after data is written - * @param y - the row number (0 to 5) - the diplay is split into 6 banks - each bank can be considered a row - */ - void setXYAddress(int x, int y); - /** Print String * * Prints a string of characters to the display. @@ -164,11 +192,12 @@ /** Print Character * - * Sends a character to the display. Will be printed at the current address. - * X address is autoincremented by 1 to leave a pixel between successive characters. + * Sends a character to the display. Printed at the specified location * @param c - the character to print. Can print ASCII as so printChar('C'). + * @param x - the column number (0 to 83) + * @param y - the row number (0 to 5) - the display is split into 6 banks - each bank can be considered a row */ - void printChar(char c); + void printChar(char c,int x,int y); /** Set a Pixel * @@ -224,6 +253,8 @@ void plotArray(float array[]); private: + + void setXYAddress(int x, int y); void initSPI(); void turnOn(); void reset(); @@ -344,4 +375,4 @@ 0x08, 0x1C, 0x2A, 0x08, 0x08 // <- }; -#endif \ No newline at end of file +#endif