4 Rotary encoders with 5110 LCD display. For Nucleo boards

Dependencies:   N5110 mbed

Committer:
triochi
Date:
Wed Sep 21 06:57:27 2016 +0000
Revision:
3:bf5e17e09fe3
Parent:
2:7d10aa2795c5
Child:
4:cd50b7dfaf27
test for pwm

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 3:bf5e17e09fe3 4 //https://developer.mbed.org/users/mbed_official/code/mbed-src/file/c9b73cd93427/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_NUCLEO_F411RE/PeripheralPins.c
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 3:bf5e17e09fe3 9
triochi 0:b7e471386653 10 int i;
triochi 0:b7e471386653 11
triochi 0:b7e471386653 12 // VCC,SCE, RST, D/C, MOSI,SCLK,LED
triochi 0:b7e471386653 13 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 14 REnc encoder(PC_3, PC_2);
triochi 3:bf5e17e09fe3 15 //PwmOut led(PA_3);
triochi 3:bf5e17e09fe3 16 PwmOut green(LED1);
triochi 1:d7d729404013 17 int temperature = 50;
triochi 1:d7d729404013 18
triochi 1:d7d729404013 19 void CCW_Handle(void)
triochi 1:d7d729404013 20 {
triochi 1:d7d729404013 21 temperature--;
triochi 1:d7d729404013 22 }
triochi 1:d7d729404013 23
triochi 1:d7d729404013 24 void CW_Handle(void)
triochi 1:d7d729404013 25 {
triochi 1:d7d729404013 26 temperature++;
triochi 1:d7d729404013 27 }
triochi 1:d7d729404013 28
triochi 0:b7e471386653 29
triochi 0:b7e471386653 30 int main() {
triochi 0:b7e471386653 31 // first need to initialise display
triochi 0:b7e471386653 32 lcd.init();
triochi 1:d7d729404013 33 encoder.setHandleCCW(CCW_Handle);
triochi 2:7d10aa2795c5 34 encoder.setHandleCC(CW_Handle);
triochi 3:bf5e17e09fe3 35 // led.period_us(100);
triochi 3:bf5e17e09fe3 36 // led = 20;
triochi 3:bf5e17e09fe3 37 green = 0.5;
triochi 3:bf5e17e09fe3 38 green.period_ms(10);
triochi 3:bf5e17e09fe3 39 while(1) {
triochi 3:bf5e17e09fe3 40 green = green + 0.01;
triochi 3:bf5e17e09fe3 41 wait(0.2);
triochi 3:bf5e17e09fe3 42 if(green == 1.0) {
triochi 3:bf5e17e09fe3 43 green = 0;
triochi 3:bf5e17e09fe3 44 }
triochi 3:bf5e17e09fe3 45 }
triochi 0:b7e471386653 46
triochi 0:b7e471386653 47 // while(1) {
triochi 0:b7e471386653 48 // for (i=1; i<7; i++) {
triochi 0:b7e471386653 49 // red = i & 1;
triochi 0:b7e471386653 50 // blue = i & 2;
triochi 0:b7e471386653 51 // green = i & 4;
triochi 0:b7e471386653 52 // wait(0.2);
triochi 0:b7e471386653 53 // }
triochi 0:b7e471386653 54 // }
triochi 0:b7e471386653 55 while(1) {
triochi 0:b7e471386653 56
triochi 0:b7e471386653 57 // these are default settings so not strictly needed
triochi 0:b7e471386653 58 lcd.normalMode(); // normal colour mode
triochi 0:b7e471386653 59 lcd.setBrightness(0.5); // put LED backlight on 50%
triochi 0:b7e471386653 60
triochi 0:b7e471386653 61 // can directly print strings at specified co-ordinates
triochi 0:b7e471386653 62 lcd.printString("Hello, World!",0,0);
triochi 0:b7e471386653 63
triochi 0:b7e471386653 64 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
triochi 0:b7e471386653 65 // so can display a string of a maximum 14 characters in length
triochi 0:b7e471386653 66 // or create formatted strings - ensure they aren't more than 14 characters long
triochi 1:d7d729404013 67 // int temperature = encoder.getVal();
triochi 0:b7e471386653 68 int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer
triochi 0:b7e471386653 69 // it is important the format specifier ensures the length will fit in the buffer
triochi 0:b7e471386653 70 if (length <= 14) // if string will fit on display
triochi 0:b7e471386653 71 lcd.printString(buffer,0,1); // display on screen
triochi 0:b7e471386653 72
triochi 0:b7e471386653 73 float pressure = 1012.3; // same idea with floats
triochi 0:b7e471386653 74 length = sprintf(buffer,"P = %.2f mb",pressure);
triochi 0:b7e471386653 75 if (length <= 14)
triochi 0:b7e471386653 76 lcd.printString(buffer,0,2);
triochi 0:b7e471386653 77
triochi 0:b7e471386653 78 // can also print individual characters at specified place
triochi 0:b7e471386653 79 lcd.printChar('X',5,3);
triochi 0:b7e471386653 80
triochi 0:b7e471386653 81 // draw a line across the display at y = 40 pixels (origin top-left)
triochi 0:b7e471386653 82 for (int i = 0; i < WIDTH; i++) {
triochi 0:b7e471386653 83 lcd.setPixel(i,40);
triochi 0:b7e471386653 84 }
triochi 0:b7e471386653 85 // need to refresh display after setting pixels
triochi 0:b7e471386653 86 lcd.refresh();
triochi 0:b7e471386653 87
triochi 0:b7e471386653 88 // can also check status of pixels using getPixel(x,y)
triochi 1:d7d729404013 89 while(1)
triochi 1:d7d729404013 90 {
triochi 1:d7d729404013 91 wait(.5);
triochi 1:d7d729404013 92
triochi 1:d7d729404013 93 length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer
triochi 1:d7d729404013 94
triochi 1:d7d729404013 95 if (length <= 14) // if string will fit on display
triochi 1:d7d729404013 96 lcd.printString(buffer,0,1); // display on screen
triochi 1:d7d729404013 97 }
triochi 0:b7e471386653 98 lcd.clear();
triochi 0:b7e471386653 99
triochi 0:b7e471386653 100 }
triochi 0:b7e471386653 101 }