4 Rotary encoders with 5110 LCD display. For Nucleo boards

Dependencies:   N5110 mbed

Committer:
triochi
Date:
Sun Sep 18 18:49:37 2016 +0000
Revision:
1:d7d729404013
Parent:
0:b7e471386653
Child:
2:7d10aa2795c5
add rotary encoder

Who changed what in which revision?

UserRevisionLine numberNew contents of line
triochi 0:b7e471386653 1 #include "mbed.h"
triochi 0:b7e471386653 2 #include "N5110.h"
triochi 1:d7d729404013 3 #include "REnc.h"
triochi 0:b7e471386653 4
triochi 0:b7e471386653 5
triochi 0:b7e471386653 6 //DigitalOut red(LED1);
triochi 0:b7e471386653 7 //DigitalOut blue(LED2);
triochi 0:b7e471386653 8 //DigitalOut green(LED3);
triochi 0:b7e471386653 9 int i;
triochi 0:b7e471386653 10
triochi 0:b7e471386653 11 // VCC,SCE, RST, D/C, MOSI,SCLK,LED
triochi 0:b7e471386653 12 N5110 lcd(PB_8,PA_4,PA_0,PA_1,PA_7,PA_5,PB_9); //PA_4 and PA_6 not used
triochi 1:d7d729404013 13 REnc encoder(PC_3, PC_2);
triochi 1:d7d729404013 14 int temperature = 50;
triochi 1:d7d729404013 15
triochi 1:d7d729404013 16 void CCW_Handle(void)
triochi 1:d7d729404013 17 {
triochi 1:d7d729404013 18 temperature--;
triochi 1:d7d729404013 19 }
triochi 1:d7d729404013 20
triochi 1:d7d729404013 21 void CW_Handle(void)
triochi 1:d7d729404013 22 {
triochi 1:d7d729404013 23 temperature++;
triochi 1:d7d729404013 24 }
triochi 1:d7d729404013 25
triochi 0:b7e471386653 26
triochi 0:b7e471386653 27 int main() {
triochi 0:b7e471386653 28 // first need to initialise display
triochi 0:b7e471386653 29 lcd.init();
triochi 1:d7d729404013 30 encoder.setHandleCCW(CCW_Handle);
triochi 1:d7d729404013 31 encoder.setHandleCC(CW_Handle);
triochi 0:b7e471386653 32
triochi 0:b7e471386653 33 // while(1) {
triochi 0:b7e471386653 34 // for (i=1; i<7; i++) {
triochi 0:b7e471386653 35 // red = i & 1;
triochi 0:b7e471386653 36 // blue = i & 2;
triochi 0:b7e471386653 37 // green = i & 4;
triochi 0:b7e471386653 38 // wait(0.2);
triochi 0:b7e471386653 39 // }
triochi 0:b7e471386653 40 // }
triochi 0:b7e471386653 41 while(1) {
triochi 0:b7e471386653 42
triochi 0:b7e471386653 43 // these are default settings so not strictly needed
triochi 0:b7e471386653 44 lcd.normalMode(); // normal colour mode
triochi 0:b7e471386653 45 lcd.setBrightness(0.5); // put LED backlight on 50%
triochi 0:b7e471386653 46
triochi 0:b7e471386653 47 // can directly print strings at specified co-ordinates
triochi 0:b7e471386653 48 lcd.printString("Hello, World!",0,0);
triochi 0:b7e471386653 49
triochi 0:b7e471386653 50 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
triochi 0:b7e471386653 51 // so can display a string of a maximum 14 characters in length
triochi 0:b7e471386653 52 // or create formatted strings - ensure they aren't more than 14 characters long
triochi 1:d7d729404013 53 // int temperature = encoder.getVal();
triochi 0:b7e471386653 54 int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer
triochi 0:b7e471386653 55 // it is important the format specifier ensures the length will fit in the buffer
triochi 0:b7e471386653 56 if (length <= 14) // if string will fit on display
triochi 0:b7e471386653 57 lcd.printString(buffer,0,1); // display on screen
triochi 0:b7e471386653 58
triochi 0:b7e471386653 59 float pressure = 1012.3; // same idea with floats
triochi 0:b7e471386653 60 length = sprintf(buffer,"P = %.2f mb",pressure);
triochi 0:b7e471386653 61 if (length <= 14)
triochi 0:b7e471386653 62 lcd.printString(buffer,0,2);
triochi 0:b7e471386653 63
triochi 0:b7e471386653 64 // can also print individual characters at specified place
triochi 0:b7e471386653 65 lcd.printChar('X',5,3);
triochi 0:b7e471386653 66
triochi 0:b7e471386653 67 // draw a line across the display at y = 40 pixels (origin top-left)
triochi 0:b7e471386653 68 for (int i = 0; i < WIDTH; i++) {
triochi 0:b7e471386653 69 lcd.setPixel(i,40);
triochi 0:b7e471386653 70 }
triochi 0:b7e471386653 71 // need to refresh display after setting pixels
triochi 0:b7e471386653 72 lcd.refresh();
triochi 0:b7e471386653 73
triochi 0:b7e471386653 74 // can also check status of pixels using getPixel(x,y)
triochi 1:d7d729404013 75 while(1)
triochi 1:d7d729404013 76 {
triochi 1:d7d729404013 77 wait(.5);
triochi 1:d7d729404013 78
triochi 1:d7d729404013 79 length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer
triochi 1:d7d729404013 80
triochi 1:d7d729404013 81 if (length <= 14) // if string will fit on display
triochi 1:d7d729404013 82 lcd.printString(buffer,0,1); // display on screen
triochi 1:d7d729404013 83 }
triochi 0:b7e471386653 84 lcd.clear();
triochi 0:b7e471386653 85
triochi 0:b7e471386653 86 }
triochi 0:b7e471386653 87 }