Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
N5110.h
00001 /* * Print String 00002 * 00003 * Prints a string of characters to the screen buffer, string is cut off after the 83rd pixel. 00004 * @param x - the column number (0 to 83) 00005 * @param y - the row number (0-5) - the display is split into 6 banks - each bank can be considered a row 00006 * @author - David Leaming - 25574043 00007 * @ Date - December 2021 00008 * 00009 * Acknowledgements 00010 * Craig A. Evans, University of Leeds, TMP102 Library ,Feb 2016 00011 * Dr Edmond Nurellari, University of Lincoln, Joystick & N5110 Libraries 00012 * 00013 */ 00014 00015 #ifndef N5110_H 00016 #define N5110_H 00017 00018 #include "mbed.h" 00019 00020 // number of pixels on display 00021 #define WIDTH 84 00022 #define HEIGHT 48 00023 #define BANKS 6 00024 00025 /// Fill types for 2D shapes 00026 enum FillType { 00027 FILL_TRANSPARENT, ///< Transparent with outline 00028 FILL_BLACK, ///< Filled black 00029 FILL_WHITE, ///< Filled white (no outline) 00030 }; 00031 00032 /** N5110 Class 00033 @brief Library for interfacing with Nokia 5110 LCD display (https://www.sparkfun.com/products/10168) using the hardware SPI on the mbed. 00034 @brief The display is powered from a GPIO pin meaning it can be controlled via software. The LED backlight is also software-controllable (via PWM pin). 00035 @brief Can print characters and strings to the display using the included 5x7 font. 00036 @brief The library also implements a screen buffer so that individual pixels on the display (84 x 48) can be set, cleared and read. 00037 @brief The library can print primitive shapes (lines, circles, rectangles) 00038 @brief Acknowledgements to Chris Yan's Nokia_5110 Library. 00039 00040 @brief Revision 1.4 00041 00042 @upgraded Dr. Edmond Nurellari 00043 @date 1st December 2021 00044 00045 @code 00046 00047 #include "mbed.h" 00048 #include "N5110.h" 00049 00050 // rows,cols 00051 int sprite[8][5] = { 00052 { 0,0,1,0,0 }, 00053 { 0,1,1,1,0 }, 00054 { 0,0,1,0,0 }, 00055 { 0,1,1,1,0 }, 00056 { 1,1,1,1,1 }, 00057 { 1,1,1,1,1 }, 00058 { 1,1,0,1,1 }, 00059 { 1,1,0,1,1 }, 00060 }; 00061 00062 // VCC,SCE,RST,D/C,MOSI,SCLK,LED 00063 //N5110 lcd(p7,p8,p9,p10,p11,p13,p21); // LPC1768 - pwr from GPIO 00064 N5110 lcd(p8,p9,p10,p11,p13,p21); // LPC1768 - powered from +3V3 - JP1 in 2/3 position 00065 //N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); // K64F - pwr from 3V3 00066 00067 int main() 00068 { 00069 // first need to initialise display 00070 lcd.init(); 00071 00072 // change set contrast in range 0.0 to 1.0 00073 // 0.4 appears to be a good starting point 00074 lcd.setContrast(0.4); 00075 00076 while(1) { 00077 00078 // these are default settings so not strictly needed 00079 lcd.normalMode(); // normal colour mode 00080 lcd.setBrightness(0.5); // put LED backlight on 50% 00081 00082 lcd.clear(); 00083 // x origin, y origin, rows, cols, sprite 00084 lcd.drawSprite(20,6,8,5,(int *)sprite); 00085 lcd.refresh(); 00086 wait(5.0); 00087 00088 lcd.clear(); // clear buffer at start of every loop 00089 // can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display) 00090 lcd.printString("Hello, World!",0,0); 00091 00092 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) 00093 // so can display a string of a maximum 14 characters in length 00094 // or create formatted strings - ensure they aren't more than 14 characters long 00095 int temperature = 27; 00096 int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer 00097 // it is important the format specifier ensures the length will fit in the buffer 00098 if (length <= 14) // if string will fit on display (assuming printing at x=0) 00099 lcd.printString(buffer,0,1); // display on screen 00100 00101 float pressure = 1012.3; // same idea with floats 00102 length = sprintf(buffer,"P = %.2f mb",pressure); 00103 if (length <= 14) 00104 lcd.printString(buffer,0,2); 00105 00106 // can also print individual characters at specified place 00107 lcd.printChar('X',5,3); 00108 00109 // draw a line across the display at y = 40 pixels (origin top-left) 00110 for (int i = 0; i < WIDTH; i++) { 00111 lcd.setPixel(i,40,true); 00112 } 00113 // need to refresh display after setting pixels or writing strings 00114 lcd.refresh(); 00115 wait(5.0); 00116 00117 // can check status of pixel using getPixel(x,y); 00118 lcd.clear(); // clear buffer 00119 lcd.setPixel(2,2,true); // set random pixel in buffer 00120 lcd.refresh(); 00121 wait(1.0); 00122 00123 int pixel_to_test = lcd.getPixel(2,2); 00124 00125 if ( pixel_to_test ) { 00126 lcd.printString("2,2 is set",0,4); 00127 } 00128 00129 // this one shouldn't be set 00130 lcd.setPixel(3,3,false); // clear random pixel in buffer 00131 lcd.refresh(); 00132 pixel_to_test = lcd.getPixel(3,3); 00133 00134 if ( pixel_to_test == 0 ) { 00135 lcd.printString("3,3 is clear",0,5); 00136 } 00137 00138 lcd.refresh(); 00139 wait(4.0); 00140 00141 lcd.clear(); // clear buffer 00142 lcd.inverseMode(); // invert colours 00143 lcd.setBrightness(1.0); // put LED backlight on full 00144 00145 float array[84]; 00146 00147 for (int i = 0; i < 84; i++) { 00148 array[i] = 0.5 + 0.5*sin(i*2*3.14/84); 00149 } 00150 00151 // can also plot graphs - 84 elements only 00152 // values must be in range 0.0 - 1.0 00153 lcd.plotArray(array); 00154 lcd.refresh(); 00155 wait(5.0); 00156 00157 lcd.clear(); 00158 lcd.normalMode(); // normal colour mode back 00159 lcd.setBrightness(0.5); // put LED backlight on 50% 00160 00161 // example of drawing lines 00162 for (int x = 0; x < WIDTH ; x+=10) { 00163 // x0,y0,x1,y1,type 0-white,1-black,2-dotted 00164 lcd.drawLine(0,0,x,HEIGHT,2); 00165 } 00166 lcd.refresh(); // refresh after drawing shapes 00167 wait(5.0); 00168 00169 00170 lcd.clear(); 00171 // example of how to draw circles 00172 lcd.drawCircle(WIDTH/2,HEIGHT/2,20,FILL_BLACK); // x,y,radius,black fill 00173 lcd.drawCircle(WIDTH/2,HEIGHT/2,10,FILL_WHITE); // x,y,radius,white fill 00174 lcd.drawCircle(WIDTH/2,HEIGHT/2,30,FILL_TRANSPARENT); // x,y,radius,transparent with outline 00175 lcd.refresh(); // refresh after drawing shapes 00176 wait(5.0); 00177 00178 lcd.clear(); 00179 // example of how to draw rectangles 00180 // origin x,y,width,height,type 00181 lcd.drawRect(10,10,50,30,FILL_BLACK); // filled black rectangle 00182 lcd.drawRect(15,15,20,10,FILL_WHITE); // filled white rectange (no outline) 00183 lcd.drawRect(2,2,70,40,FILL_TRANSPARENT); // transparent, just outline 00184 lcd.refresh(); // refresh after drawing shapes 00185 wait(5.0); 00186 00187 } 00188 } 00189 00190 00191 @endcode 00192 */ 00193 class N5110 00194 { 00195 private: 00196 // objects 00197 SPI *_spi; 00198 PwmOut *_led; 00199 DigitalOut *_pwr; 00200 DigitalOut *_sce; 00201 DigitalOut *_rst; 00202 DigitalOut *_dc; 00203 00204 // variables 00205 unsigned char buffer[84][6]; // screen buffer - the 6 is for the banks - each one is 8 bits; 00206 00207 public: 00208 /** Create a N5110 object connected to the specified pins 00209 * 00210 * @param pwr Pin connected to Vcc on the LCD display (pin 1) 00211 * @param sce Pin connected to chip enable (pin 3) 00212 * @param rst Pin connected to reset (pin 4) 00213 * @param dc Pin connected to data/command select (pin 5) 00214 * @param mosi Pin connected to data input (MOSI) (pin 6) 00215 * @param sclk Pin connected to serial clock (SCLK) (pin 7) 00216 * @param led Pin connected to LED backlight (must be PWM) (pin 8) 00217 * 00218 */ 00219 N5110(PinName const pwrPin, 00220 PinName const scePin, 00221 PinName const rstPin, 00222 PinName const dcPin, 00223 PinName const mosiPin, 00224 PinName const sclkPin, 00225 PinName const ledPin); 00226 00227 /** Create a N5110 object connected to the specified pins (Vcc to +3V3) 00228 * 00229 * @param sce Pin connected to chip enable (pin 3) 00230 * @param rst Pin connected to reset (pin 4) 00231 * @param dc Pin connected to data/command select (pin 5) 00232 * @param mosi Pin connected to data input (MOSI) (pin 6) 00233 * @param sclk Pin connected to serial clock (SCLK) (pin 7) 00234 * @param led Pin connected to LED backlight (must be PWM) (pin 8) 00235 * 00236 */ 00237 N5110(PinName const scePin, 00238 PinName const rstPin, 00239 PinName const dcPin, 00240 PinName const mosiPin, 00241 PinName const sclkPin, 00242 PinName const ledPin); 00243 00244 /** 00245 * Free allocated memory when object goes out of scope 00246 */ 00247 ~N5110(); 00248 00249 /** Initialise display 00250 * 00251 * Powers up the display and turns on backlight (50% brightness default). 00252 * Sets the display up in horizontal addressing mode and with normal video mode. 00253 */ 00254 void init(); 00255 00256 /** Turn off 00257 * 00258 * Powers down the display and turns of the backlight. 00259 * Needs to be reinitialised before being re-used. 00260 */ 00261 void turnOff(); 00262 00263 /** Clear 00264 * 00265 * Clears the screen buffer. 00266 */ 00267 void clear(); 00268 00269 /** Set screen constrast 00270 * @param constrast - float in range 0.0 to 1.0 (0.40 to 0.60 is usually a good value) 00271 */ 00272 void setContrast(float contrast); 00273 00274 /** Turn on normal video mode (default) 00275 * Black on white 00276 */ 00277 void normalMode(); 00278 00279 /** Turn on inverse video mode (default) 00280 * White on black 00281 */ 00282 void inverseMode(); 00283 00284 /** Set Brightness 00285 * 00286 * Sets brightness of LED backlight. 00287 * @param brightness - float in range 0.0 to 1.0 00288 */ 00289 void setBrightness(float const brightness); 00290 00291 /** Print String 00292 * 00293 * Prints a string of characters to the screen buffer. String is cut-off after the 83rd pixel. 00294 * @param x - the column number (0 to 83) 00295 * @param y - the row number (0 to 5) - the display is split into 6 banks - each bank can be considered a row 00296 */ 00297 void printString(char const *str, 00298 unsigned int const x, 00299 unsigned int const y); 00300 00301 /** Print Character 00302 * 00303 * Sends a character to the screen buffer. Printed at the specified location. Character is cut-off after the 83rd pixel. 00304 * @param c - the character to print. Can print ASCII as so printChar('C'). 00305 * @param x - the column number (0 to 83) 00306 * @param y - the row number (0 to 5) - the display is split into 6 banks - each bank can be considered a row 00307 */ 00308 void printChar(char const c, 00309 unsigned int const x, 00310 unsigned int const y); 00311 00312 /** 00313 * @brief Set a Pixel 00314 * 00315 * @param x The x co-ordinate of the pixel (0 to 83) 00316 * @param y The y co-ordinate of the pixel (0 to 47) 00317 * @param state The state of the pixel [true=black (default), false=white] 00318 * 00319 * @details This function sets the state of a pixel in the screen buffer. 00320 * The third parameter can be omitted, 00321 */ 00322 void setPixel(unsigned int const x, 00323 unsigned int const y, 00324 bool const state = true); 00325 00326 /** 00327 * @brief Clear a Pixel 00328 * 00329 * @param x - the x co-ordinate of the pixel (0 to 83) 00330 * @param y - the y co-ordinate of the pixel (0 to 47) 00331 * 00332 * @details This function clears pixel in the screen buffer 00333 * 00334 * @deprecated Use setPixel(x, y, false) instead 00335 */ 00336 void clearPixel(unsigned int const x, 00337 unsigned int const y) 00338 __attribute__((deprecated("Use setPixel(x,y,false) instead"))); 00339 00340 /** Get a Pixel 00341 * 00342 * This function gets the status of a pixel in the screen buffer. 00343 * @param x - the x co-ordinate of the pixel (0 to 83) 00344 * @param y - the y co-ordinate of the pixel (0 to 47) 00345 * @returns 00346 * 0 - pixel is clear 00347 * 1 - pixel is set 00348 */ 00349 int getPixel(unsigned int const x, 00350 unsigned int const y) const; 00351 00352 /** Refresh display 00353 * 00354 * This functions sends the screen buffer to the display. 00355 */ 00356 void refresh(); 00357 00358 /** Randomise buffer 00359 * 00360 * This function fills the buffer with random data. Can be used to test the display. 00361 * A call to refresh() must be made to update the display to reflect the change in pixels. 00362 * The seed is not set and so the generated pattern will probably be the same each time. 00363 * TODO: Randomise the seed - maybe using the noise on the AnalogIn pins. 00364 */ 00365 void randomiseBuffer(); 00366 00367 /** Plot Array 00368 * 00369 * This function plots a one-dimensional array in the buffer. 00370 * @param array[] - y values of the plot. Values should be normalised in the range 0.0 to 1.0. First 84 plotted. 00371 */ 00372 void plotArray(float const array[]); 00373 00374 /** Draw Circle 00375 * 00376 * This function draws a circle at the specified origin with specified radius in the screen buffer 00377 * Uses the midpoint circle algorithm. 00378 * @see http://en.wikipedia.org/wiki/Midpoint_circle_algorithm 00379 * @param x0 - x-coordinate of centre 00380 * @param y0 - y-coordinate of centre 00381 * @param radius - radius of circle in pixels 00382 * @param fill - fill-type for the shape 00383 */ 00384 void drawCircle(unsigned int const x0, 00385 unsigned int const y0, 00386 unsigned int const radius, 00387 FillType const fill); 00388 00389 /** Draw Line 00390 * 00391 * This function draws a line between the specified points using linear interpolation. 00392 * @param x0 - x-coordinate of first point 00393 * @param y0 - y-coordinate of first point 00394 * @param x1 - x-coordinate of last point 00395 * @param y1 - y-coordinate of last point 00396 * @param type - 0 white,1 black,2 dotted 00397 */ 00398 void drawLine(unsigned int const x0, 00399 unsigned int const y0, 00400 unsigned int const x1, 00401 unsigned int const y1, 00402 unsigned int const type); 00403 00404 /** Draw Rectangle 00405 * 00406 * This function draws a rectangle. 00407 * @param x0 - x-coordinate of origin (top-left) 00408 * @param y0 - y-coordinate of origin (top-left) 00409 * @param width - width of rectangle 00410 * @param height - height of rectangle 00411 * @param fill - fill-type for the shape 00412 */ 00413 void drawRect(unsigned int const x0, 00414 unsigned int const y0, 00415 unsigned int const width, 00416 unsigned int const height, 00417 FillType const fill); 00418 00419 /** Draw Sprite 00420 * 00421 * This function draws a sprite as defined in a 2D array 00422 * @param x0 - x-coordinate of origin (top-left) 00423 * @param y0 - y-coordinate of origin (top-left) 00424 * @param nrows - number of rows in sprite 00425 * @param ncols - number of columns in sprite 00426 * @param sprite - 2D array representing the sprite 00427 */ 00428 void drawSprite(int x0, 00429 int y0, 00430 int nrows, 00431 int ncols, 00432 int *sprite); 00433 00434 00435 private: 00436 // methods 00437 void setXYAddress(unsigned int const x, 00438 unsigned int const y); 00439 void initSPI(); 00440 void turnOn(); 00441 void reset(); 00442 void clearRAM(); 00443 void sendCommand(unsigned char command); 00444 void sendData(unsigned char data); 00445 void setTempCoefficient(char tc); // 0 to 3 00446 void setBias(char bias); // 0 to 7 00447 }; 00448 00449 const unsigned char font5x7[480] = { 00450 0x00, 0x00, 0x00, 0x00, 0x00,// (space) 00451 0x00, 0x00, 0x5F, 0x00, 0x00,// ! 00452 0x00, 0x07, 0x00, 0x07, 0x00,// " 00453 0x14, 0x7F, 0x14, 0x7F, 0x14,// # 00454 0x24, 0x2A, 0x7F, 0x2A, 0x12,// $ 00455 0x23, 0x13, 0x08, 0x64, 0x62,// % 00456 0x36, 0x49, 0x55, 0x22, 0x50,// & 00457 0x00, 0x05, 0x03, 0x00, 0x00,// ' 00458 0x00, 0x1C, 0x22, 0x41, 0x00,// ( 00459 0x00, 0x41, 0x22, 0x1C, 0x00,// ) 00460 0x08, 0x2A, 0x1C, 0x2A, 0x08,// * 00461 0x08, 0x08, 0x3E, 0x08, 0x08,// + 00462 0x00, 0x50, 0x30, 0x00, 0x00,// , 00463 0x08, 0x08, 0x08, 0x08, 0x08,// - 00464 0x00, 0x60, 0x60, 0x00, 0x00,// . 00465 0x20, 0x10, 0x08, 0x04, 0x02,// / 00466 0x3E, 0x51, 0x49, 0x45, 0x3E,// 0 00467 0x00, 0x42, 0x7F, 0x40, 0x00,// 1 00468 0x42, 0x61, 0x51, 0x49, 0x46,// 2 00469 0x21, 0x41, 0x45, 0x4B, 0x31,// 3 00470 0x18, 0x14, 0x12, 0x7F, 0x10,// 4 00471 0x27, 0x45, 0x45, 0x45, 0x39,// 5 00472 0x3C, 0x4A, 0x49, 0x49, 0x30,// 6 00473 0x01, 0x71, 0x09, 0x05, 0x03,// 7 00474 0x36, 0x49, 0x49, 0x49, 0x36,// 8 00475 0x06, 0x49, 0x49, 0x29, 0x1E,// 9 00476 0x00, 0x36, 0x36, 0x00, 0x00,// : 00477 0x00, 0x56, 0x36, 0x00, 0x00,// ; 00478 0x00, 0x08, 0x14, 0x22, 0x41,// < 00479 0x14, 0x14, 0x14, 0x14, 0x14,// = 00480 0x41, 0x22, 0x14, 0x08, 0x00,// > 00481 0x02, 0x01, 0x51, 0x09, 0x06,// ? 00482 0x32, 0x49, 0x79, 0x41, 0x3E,// @ 00483 0x7E, 0x11, 0x11, 0x11, 0x7E,// A 00484 0x7F, 0x49, 0x49, 0x49, 0x36,// B 00485 0x3E, 0x41, 0x41, 0x41, 0x22,// C 00486 0x7F, 0x41, 0x41, 0x22, 0x1C,// D 00487 0x7F, 0x49, 0x49, 0x49, 0x41,// E 00488 0x7F, 0x09, 0x09, 0x01, 0x01,// F 00489 0x3E, 0x41, 0x41, 0x51, 0x32,// G 00490 0x7F, 0x08, 0x08, 0x08, 0x7F,// H 00491 0x00, 0x41, 0x7F, 0x41, 0x00,// I 00492 0x20, 0x40, 0x41, 0x3F, 0x01,// J 00493 0x7F, 0x08, 0x14, 0x22, 0x41,// K 00494 0x7F, 0x40, 0x40, 0x40, 0x40,// L 00495 0x7F, 0x02, 0x04, 0x02, 0x7F,// M 00496 0x7F, 0x04, 0x08, 0x10, 0x7F,// N 00497 0x3E, 0x41, 0x41, 0x41, 0x3E,// O 00498 0x7F, 0x09, 0x09, 0x09, 0x06,// P 00499 0x3E, 0x41, 0x51, 0x21, 0x5E,// Q 00500 0x7F, 0x09, 0x19, 0x29, 0x46,// R 00501 0x46, 0x49, 0x49, 0x49, 0x31,// S 00502 0x01, 0x01, 0x7F, 0x01, 0x01,// T 00503 0x3F, 0x40, 0x40, 0x40, 0x3F,// U 00504 0x1F, 0x20, 0x40, 0x20, 0x1F,// V 00505 0x7F, 0x20, 0x18, 0x20, 0x7F,// W 00506 0x63, 0x14, 0x08, 0x14, 0x63,// X 00507 0x03, 0x04, 0x78, 0x04, 0x03,// Y 00508 0x61, 0x51, 0x49, 0x45, 0x43,// Z 00509 0x00, 0x00, 0x7F, 0x41, 0x41,// [ 00510 0x02, 0x04, 0x08, 0x10, 0x20,// "\" 00511 0x41, 0x41, 0x7F, 0x00, 0x00,// ] 00512 0x04, 0x02, 0x01, 0x02, 0x04,// ^ 00513 0x40, 0x40, 0x40, 0x40, 0x40,// _ 00514 0x00, 0x01, 0x02, 0x04, 0x00,// ` 00515 0x20, 0x54, 0x54, 0x54, 0x78,// a 00516 0x7F, 0x48, 0x44, 0x44, 0x38,// b 00517 0x38, 0x44, 0x44, 0x44, 0x20,// c 00518 0x38, 0x44, 0x44, 0x48, 0x7F,// d 00519 0x38, 0x54, 0x54, 0x54, 0x18,// e 00520 0x08, 0x7E, 0x09, 0x01, 0x02,// f 00521 0x08, 0x14, 0x54, 0x54, 0x3C,// g 00522 0x7F, 0x08, 0x04, 0x04, 0x78,// h 00523 0x00, 0x44, 0x7D, 0x40, 0x00,// i 00524 0x20, 0x40, 0x44, 0x3D, 0x00,// j 00525 0x00, 0x7F, 0x10, 0x28, 0x44,// k 00526 0x00, 0x41, 0x7F, 0x40, 0x00,// l 00527 0x7C, 0x04, 0x18, 0x04, 0x78,// m 00528 0x7C, 0x08, 0x04, 0x04, 0x78,// n 00529 0x38, 0x44, 0x44, 0x44, 0x38,// o 00530 0x7C, 0x14, 0x14, 0x14, 0x08,// p 00531 0x08, 0x14, 0x14, 0x18, 0x7C,// q 00532 0x7C, 0x08, 0x04, 0x04, 0x08,// r 00533 0x48, 0x54, 0x54, 0x54, 0x20,// s 00534 0x04, 0x3F, 0x44, 0x40, 0x20,// t 00535 0x3C, 0x40, 0x40, 0x20, 0x7C,// u 00536 0x1C, 0x20, 0x40, 0x20, 0x1C,// v 00537 0x3C, 0x40, 0x30, 0x40, 0x3C,// w 00538 0x44, 0x28, 0x10, 0x28, 0x44,// x 00539 0x0C, 0x50, 0x50, 0x50, 0x3C,// y 00540 0x44, 0x64, 0x54, 0x4C, 0x44,// z 00541 0x00, 0x08, 0x36, 0x41, 0x00,// { 00542 0x00, 0x00, 0x7F, 0x00, 0x00,// | 00543 0x00, 0x41, 0x36, 0x08, 0x00,// } 00544 0x08, 0x08, 0x2A, 0x1C, 0x08,// -> 00545 0x08, 0x1C, 0x2A, 0x08, 0x08 // <- 00546 }; 00547 00548 #endif
Generated on Sun Jul 31 2022 21:19:10 by
