Demo for STM32F103C8T6 with a Nokia 5110 LCD.
Nokia 5110 LCD driven by STM32F103C8T6 board
This is a fork of Craig Evans' example program using his N5110 library.
Schematic
Wiring
STM32F103C8T6 | Nokia 5110 | |||
---|---|---|---|---|
GND | <=> | GND | ||
PA_10 | <R1> | LIGHT | ||
PA_9 | <=> | VCC | ||
PB_3 | <=> | CLK | ||
PB_5 | <=> | DIN | ||
PA_15 | <=> | DC | ||
PA_11 | <=> | CE | ||
PA_12 | <=> | RST |
main.cpp@2:2bab15094d3d, 2019-02-05 (annotated)
- Committer:
- hudakz
- Date:
- Tue Feb 05 13:53:22 2019 +0000
- Revision:
- 2:2bab15094d3d
- Parent:
- 1:a5480500307f
Updated.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:1be8870a132e | 1 | /* |
hudakz | 0:1be8870a132e | 2 | * Nokia 5110 LCD demo |
hudakz | 0:1be8870a132e | 3 | */ |
hudakz | 0:1be8870a132e | 4 | #include "mbed.h" |
hudakz | 0:1be8870a132e | 5 | #include "N5110.h" |
hudakz | 0:1be8870a132e | 6 | |
hudakz | 0:1be8870a132e | 7 | N5110 lcd(PA_9, PA_11, PA_12, PA_15, PB_5, PB_3, PA_10); // VCC, CE, RST, DC, DIN, CLK, LIGHT |
hudakz | 0:1be8870a132e | 8 | |
hudakz | 0:1be8870a132e | 9 | int main() |
hudakz | 0:1be8870a132e | 10 | { |
hudakz | 0:1be8870a132e | 11 | lcd.init(); // first need to initialise display |
hudakz | 0:1be8870a132e | 12 | |
hudakz | 0:1be8870a132e | 13 | while(1) { |
hudakz | 0:1be8870a132e | 14 | |
hudakz | 0:1be8870a132e | 15 | // these are default settings so not strictly needed |
hudakz | 0:1be8870a132e | 16 | lcd.normalMode(); // normal colour mode |
hudakz | 0:1be8870a132e | 17 | lcd.setBrightness(0.5); // put LED backlight on 50% |
hudakz | 0:1be8870a132e | 18 | |
hudakz | 0:1be8870a132e | 19 | // can directly print strings at specified co-ordinates |
hudakz | 0:1be8870a132e | 20 | lcd.printString("Hello, World!",0,0); |
hudakz | 0:1be8870a132e | 21 | |
hudakz | 0:1be8870a132e | 22 | char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) |
hudakz | 0:1be8870a132e | 23 | // so can display a string of a maximum 14 characters in length |
hudakz | 0:1be8870a132e | 24 | // or create formatted strings - ensure they aren't more than 14 characters long |
hudakz | 0:1be8870a132e | 25 | int temperature = 27; |
hudakz | 0:1be8870a132e | 26 | int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer |
hudakz | 0:1be8870a132e | 27 | // it is important the format specifier ensures the length will fit in the buffer |
hudakz | 0:1be8870a132e | 28 | if (length <= 14) // if string will fit on display |
hudakz | 0:1be8870a132e | 29 | lcd.printString(buffer,0,1); // display on screen |
hudakz | 0:1be8870a132e | 30 | |
hudakz | 0:1be8870a132e | 31 | float pressure = 1012.3; // same idea with floats |
hudakz | 0:1be8870a132e | 32 | length = sprintf(buffer,"P = %.2f mb",pressure); |
hudakz | 0:1be8870a132e | 33 | if (length <= 14) |
hudakz | 0:1be8870a132e | 34 | lcd.printString(buffer,0,2); |
hudakz | 0:1be8870a132e | 35 | |
hudakz | 0:1be8870a132e | 36 | // can also print individual characters at specified place |
hudakz | 0:1be8870a132e | 37 | lcd.printChar('X',5,3); |
hudakz | 0:1be8870a132e | 38 | |
hudakz | 0:1be8870a132e | 39 | // draw a line across the display at y = 40 pixels (origin top-left) |
hudakz | 0:1be8870a132e | 40 | for (int i = 0; i < WIDTH; i++) { |
hudakz | 0:1be8870a132e | 41 | lcd.setPixel(i,40); |
hudakz | 0:1be8870a132e | 42 | } |
hudakz | 0:1be8870a132e | 43 | // need to refresh display after setting pixels |
hudakz | 0:1be8870a132e | 44 | lcd.refresh(); |
hudakz | 0:1be8870a132e | 45 | |
hudakz | 0:1be8870a132e | 46 | // can also check status of pixels using getPixel(x,y) |
hudakz | 0:1be8870a132e | 47 | |
hudakz | 0:1be8870a132e | 48 | wait(5.0); |
hudakz | 0:1be8870a132e | 49 | lcd.clear(); // clear display |
hudakz | 0:1be8870a132e | 50 | lcd.inverseMode(); // invert colours |
hudakz | 0:1be8870a132e | 51 | lcd.setBrightness(1.0); // put LED backlight on full |
hudakz | 0:1be8870a132e | 52 | |
hudakz | 0:1be8870a132e | 53 | float array[84]; |
hudakz | 0:1be8870a132e | 54 | |
hudakz | 0:1be8870a132e | 55 | for (int i = 0; i < 84; i++) { |
hudakz | 0:1be8870a132e | 56 | array[i] = 0.5 + 0.5*sin(i*2*3.14/84); |
hudakz | 0:1be8870a132e | 57 | } |
hudakz | 0:1be8870a132e | 58 | |
hudakz | 0:1be8870a132e | 59 | // can also plot graphs - 84 elements only |
hudakz | 0:1be8870a132e | 60 | // values must be in range 0.0 - 1.0 |
hudakz | 0:1be8870a132e | 61 | lcd.plotArray(array); |
hudakz | 0:1be8870a132e | 62 | wait(5.0); |
hudakz | 0:1be8870a132e | 63 | lcd.clear(); |
hudakz | 0:1be8870a132e | 64 | lcd.normalMode(); // normal colour mode back |
hudakz | 0:1be8870a132e | 65 | lcd.setBrightness(0.5); // put LED backlight on 50% |
hudakz | 0:1be8870a132e | 66 | |
hudakz | 0:1be8870a132e | 67 | // example of drawing lines |
hudakz | 0:1be8870a132e | 68 | for (int x = 0; x < WIDTH ; x+=10) { |
hudakz | 0:1be8870a132e | 69 | // x0,y0,x1,y1,type 0-white,1-black,2-dotted |
hudakz | 0:1be8870a132e | 70 | lcd.drawLine(0,0,x,HEIGHT,2); |
hudakz | 0:1be8870a132e | 71 | } |
hudakz | 0:1be8870a132e | 72 | lcd.refresh(); // need to refresh screen after drawing lines |
hudakz | 0:1be8870a132e | 73 | |
hudakz | 0:1be8870a132e | 74 | wait(5.0); |
hudakz | 0:1be8870a132e | 75 | lcd.clear(); |
hudakz | 0:1be8870a132e | 76 | |
hudakz | 0:1be8870a132e | 77 | // example of how to draw circles |
hudakz | 1:a5480500307f | 78 | lcd.drawCircle(WIDTH/2,HEIGHT/2,20,FILL_BLACK); // x,y,radius,black fill |
hudakz | 1:a5480500307f | 79 | lcd.drawCircle(WIDTH/2,HEIGHT/2,10,FILL_WHITE); // x,y,radius,white fill |
hudakz | 1:a5480500307f | 80 | lcd.drawCircle(WIDTH/2,HEIGHT/2,30,FILL_TRANSPARENT); // x,y,radius,transparent with outline |
hudakz | 0:1be8870a132e | 81 | lcd.refresh(); // need to refresh screen after drawing circles |
hudakz | 0:1be8870a132e | 82 | |
hudakz | 0:1be8870a132e | 83 | wait(5.0); |
hudakz | 0:1be8870a132e | 84 | lcd.clear(); |
hudakz | 0:1be8870a132e | 85 | |
hudakz | 0:1be8870a132e | 86 | // example of how to draw rectangles |
hudakz | 0:1be8870a132e | 87 | // origin x,y,width,height,type |
hudakz | 1:a5480500307f | 88 | lcd.drawRect(10,10,50,30,FILL_BLACK); // filled black rectangle |
hudakz | 1:a5480500307f | 89 | lcd.drawRect(15,15,20,10,FILL_WHITE); // filled white rectange (no outline) |
hudakz | 1:a5480500307f | 90 | lcd.drawRect(2,2,70,40,FILL_TRANSPARENT); // transparent, just outline |
hudakz | 0:1be8870a132e | 91 | lcd.refresh(); // need to refresh screen after drawing rects |
hudakz | 0:1be8870a132e | 92 | |
hudakz | 0:1be8870a132e | 93 | wait(5.0); |
hudakz | 0:1be8870a132e | 94 | lcd.clear(); |
hudakz | 0:1be8870a132e | 95 | } |
hudakz | 0:1be8870a132e | 96 | } |