4 Rotary encoders with 5110 LCD display. For Nucleo boards

Dependencies:   N5110 mbed

Committer:
triochi
Date:
Sun Oct 23 15:48:58 2016 +0000
Revision:
7:62c2c1382d86
Parent:
6:920bfbc7a7a1
Child:
8:67732769ecdd
Changed encoder library;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
triochi 7:62c2c1382d86 1 /*
triochi 7:62c2c1382d86 2 * Using STM32's counter peripherals to interface rotary encoders.
triochi 7:62c2c1382d86 3 * Encoders are supported on F4xx's TIM1,2,3,4,5. TIM2 & TIM5 have 32bit count, others 16bit.
triochi 7:62c2c1382d86 4 * Beware mbed uses TIM5 for system timer, SPI needs TIM1, others used for PWM.
triochi 7:62c2c1382d86 5 * Check your platform's PeripheralPins.c & PeripheralNames.h if you need both PWM & encoders.
triochi 7:62c2c1382d86 6 *
triochi 7:62c2c1382d86 7 * Edit HAL_TIM_Encoder_MspInitFx.cpp to suit your mcu & board's available pinouts & pullups/downs.
triochi 7:62c2c1382d86 8 *
triochi 7:62c2c1382d86 9 * Thanks to:
triochi 7:62c2c1382d86 10 * http://petoknm.wordpress.com/2015/01/05/rotary-encoder-and-stm32/
triochi 7:62c2c1382d86 11 *
triochi 7:62c2c1382d86 12 * References:
triochi 7:62c2c1382d86 13 * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/user_manual/DM00122015.pdf
triochi 7:62c2c1382d86 14 * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/reference_manual/DM00096844.pdf
triochi 7:62c2c1382d86 15 * http://www.st.com/web/en/resource/technical/document/application_note/DM00042534.pdf
triochi 7:62c2c1382d86 16 * http://www.st.com/web/en/resource/technical/document/datasheet/DM00102166.pdf
triochi 7:62c2c1382d86 17 *
triochi 7:62c2c1382d86 18 * David Lowe Jan 2015
triochi 7:62c2c1382d86 19 */
triochi 7:62c2c1382d86 20
triochi 7:62c2c1382d86 21
triochi 0:b7e471386653 22 #include "mbed.h"
triochi 0:b7e471386653 23 #include "N5110.h"
triochi 7:62c2c1382d86 24 #include "Encoder.h"
triochi 7:62c2c1382d86 25
triochi 7:62c2c1382d86 26 //STM mbed bug: these macros are MISSING from stm32f3xx_hal_tim.h
triochi 7:62c2c1382d86 27 #ifdef TARGET_STM32F3
triochi 7:62c2c1382d86 28 #define __HAL_TIM_GET_COUNTER(__HANDLE__) ((__HANDLE__)->Instance->CNT)
triochi 7:62c2c1382d86 29 #define __HAL_TIM_IS_TIM_COUNTING_DOWN(__HANDLE__) (((__HANDLE__)->Instance->CR1 &(TIM_CR1_DIR)) == (TIM_CR1_DIR))
triochi 7:62c2c1382d86 30 #endif
triochi 7:62c2c1382d86 31
triochi 7:62c2c1382d86 32 TIM_Encoder_InitTypeDef encoder1, encoder2, encoder3, encoder4;
triochi 7:62c2c1382d86 33 TIM_HandleTypeDef timer1, timer2, timer3, timer4;
triochi 5:0daf19d95cc7 34
triochi 0:b7e471386653 35
triochi 0:b7e471386653 36 //DigitalOut red(LED1);
triochi 0:b7e471386653 37 //DigitalOut blue(LED2);
triochi 0:b7e471386653 38 //DigitalOut green(LED3);
triochi 0:b7e471386653 39 int i;
triochi 0:b7e471386653 40
triochi 0:b7e471386653 41 // VCC,SCE, RST, D/C, MOSI,SCLK,LED
triochi 0:b7e471386653 42 N5110 lcd(PB_8,PA_4,PA_0,PA_1,PA_7,PA_5,PB_9); //PA_4 and PA_6 not used
triochi 7:62c2c1382d86 43
triochi 1:d7d729404013 44 int temperature = 50;
triochi 1:d7d729404013 45
triochi 7:62c2c1382d86 46 int main() {
triochi 7:62c2c1382d86 47 uint16_t count1=0, count3=0, count4=0;
triochi 7:62c2c1382d86 48 uint32_t count2=0;
triochi 7:62c2c1382d86 49 int8_t dir1, dir2, dir3, dir4;
triochi 7:62c2c1382d86 50
triochi 7:62c2c1382d86 51 // first need to initialise display
triochi 7:62c2c1382d86 52 lcd.init();
triochi 7:62c2c1382d86 53 //examples
triochi 1:d7d729404013 54
triochi 7:62c2c1382d86 55 //counting on A-input only, 2 ticks per cycle, rolls over at 100
triochi 7:62c2c1382d86 56 EncoderInit(&encoder1, &timer1, TIM1, 99, TIM_ENCODERMODE_TI1);
triochi 7:62c2c1382d86 57
triochi 7:62c2c1382d86 58 //counting on both A&B inputs, 4 ticks per cycle, full 32-bit count
triochi 7:62c2c1382d86 59 EncoderInit(&encoder2, &timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12);
triochi 0:b7e471386653 60
triochi 7:62c2c1382d86 61 //counting on B-input only, 2 ticks per cycle, full 16-bit count
triochi 7:62c2c1382d86 62 EncoderInit(&encoder3, &timer3, TIM3, 0xffff, TIM_ENCODERMODE_TI2);
triochi 7:62c2c1382d86 63
triochi 7:62c2c1382d86 64 //counting on both A&B inputs, 4 ticks per cycle, full 16-bit count
triochi 7:62c2c1382d86 65 EncoderInit(&encoder4, &timer4, TIM4, 0xffff, TIM_ENCODERMODE_TI12);
triochi 7:62c2c1382d86 66
triochi 7:62c2c1382d86 67 //TIM5 is used by mbed for systick
triochi 7:62c2c1382d86 68 //EncoderInit(encoder2, timer2, TIM5, 0xffffffff, TIM_ENCODERMODE_TI12);
triochi 7:62c2c1382d86 69
triochi 7:62c2c1382d86 70 printf("STM HAL encoder demo\n\r");
triochi 0:b7e471386653 71
triochi 0:b7e471386653 72 // while(1) {
triochi 0:b7e471386653 73 // for (i=1; i<7; i++) {
triochi 0:b7e471386653 74 // red = i & 1;
triochi 0:b7e471386653 75 // blue = i & 2;
triochi 0:b7e471386653 76 // green = i & 4;
triochi 0:b7e471386653 77 // wait(0.2);
triochi 0:b7e471386653 78 // }
triochi 0:b7e471386653 79 // }
triochi 7:62c2c1382d86 80 while(1)
triochi 7:62c2c1382d86 81 {
triochi 7:62c2c1382d86 82
triochi 7:62c2c1382d86 83 //OK 401 411 446 TICKER 030
triochi 7:62c2c1382d86 84 //count1=TIM1->CNT;
triochi 7:62c2c1382d86 85 //dir1=TIM1->CR1&TIM_CR1_DIR;
triochi 7:62c2c1382d86 86 count1=__HAL_TIM_GET_COUNTER(&timer1);
triochi 7:62c2c1382d86 87 dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer1);
triochi 0:b7e471386653 88
triochi 7:62c2c1382d86 89 //OK 401 411 446 NOK 030
triochi 7:62c2c1382d86 90 //count2=TIM2->CNT;
triochi 7:62c2c1382d86 91 //dir2=TIM2->CR1&TIM_CR1_DIR;
triochi 7:62c2c1382d86 92 count2=__HAL_TIM_GET_COUNTER(&timer2);
triochi 7:62c2c1382d86 93 dir2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
triochi 0:b7e471386653 94
triochi 7:62c2c1382d86 95 //OK 401 411 446 030
triochi 7:62c2c1382d86 96 //count3=TIM3->CNT;
triochi 7:62c2c1382d86 97 //dir3=TIM3->CR1&TIM_CR1_DIR;
triochi 7:62c2c1382d86 98 count3=__HAL_TIM_GET_COUNTER(&timer3);
triochi 7:62c2c1382d86 99 dir3 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer3);
triochi 7:62c2c1382d86 100
triochi 7:62c2c1382d86 101 //OK 401 411 446 N/A 030
triochi 7:62c2c1382d86 102 //count4=TIM4->CNT;
triochi 7:62c2c1382d86 103 //dir4=TIM4->CR1&TIM_CR1_DIR;
triochi 7:62c2c1382d86 104 count4=__HAL_TIM_GET_COUNTER(&timer4);
triochi 7:62c2c1382d86 105 dir4 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer4);
triochi 0:b7e471386653 106
triochi 7:62c2c1382d86 107 //TICKER 401 411 446 N/A 030
triochi 7:62c2c1382d86 108 // count5=__HAL_TIM_GET_COUNTER(&timer5);
triochi 7:62c2c1382d86 109 // dir5 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer5);
triochi 7:62c2c1382d86 110 // printf("%d%s %d%s %d%s %d%s\r\n", count1, dir1==0 ? "+":"-",
triochi 7:62c2c1382d86 111 // count2, dir2==0 ? "+":"-",
triochi 7:62c2c1382d86 112 // count3, dir3==0 ? "+":"-",
triochi 7:62c2c1382d86 113 // count4, dir4==0 ? "+":"-" );
triochi 7:62c2c1382d86 114
triochi 7:62c2c1382d86 115 // these are default settings so not strictly needed
triochi 7:62c2c1382d86 116 lcd.normalMode(); // normal colour mode
triochi 7:62c2c1382d86 117 lcd.setBrightness(0.5); // put LED backlight on 50%
triochi 0:b7e471386653 118
triochi 7:62c2c1382d86 119 // can directly print strings at specified co-ordinates
triochi 7:62c2c1382d86 120 lcd.printString("Hello, World!",0,0);
triochi 0:b7e471386653 121
triochi 7:62c2c1382d86 122 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
triochi 7:62c2c1382d86 123 // so can display a string of a maximum 14 characters in length
triochi 7:62c2c1382d86 124 // or create formatted strings - ensure they aren't more than 14 characters long
triochi 7:62c2c1382d86 125 // int temperature = encoder.getVal();
triochi 7:62c2c1382d86 126 int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer
triochi 7:62c2c1382d86 127 // it is important the format specifier ensures the length will fit in the buffer
triochi 7:62c2c1382d86 128 if (length <= 14) // if string will fit on display
triochi 7:62c2c1382d86 129 {
triochi 7:62c2c1382d86 130 lcd.printString(buffer,0,1); // display on screen
triochi 7:62c2c1382d86 131 }
triochi 7:62c2c1382d86 132 float pressure = 1012.3; // same idea with floats
triochi 7:62c2c1382d86 133 length = sprintf(buffer,"P = %.2f mb",pressure);
triochi 7:62c2c1382d86 134 if (length <= 14)
triochi 7:62c2c1382d86 135 {
triochi 7:62c2c1382d86 136 lcd.printString(buffer,0,2);
triochi 7:62c2c1382d86 137 }
triochi 7:62c2c1382d86 138 // can also print individual characters at specified place
triochi 7:62c2c1382d86 139 lcd.printChar('X',5,3);
triochi 0:b7e471386653 140
triochi 7:62c2c1382d86 141 // draw a line across the display at y = 40 pixels (origin top-left)
triochi 7:62c2c1382d86 142 for (int i = 0; i < WIDTH; i++)
triochi 7:62c2c1382d86 143 {
triochi 7:62c2c1382d86 144 lcd.setPixel(i,40);
triochi 7:62c2c1382d86 145 }
triochi 7:62c2c1382d86 146 // need to refresh display after setting pixels
triochi 7:62c2c1382d86 147 lcd.refresh();
triochi 0:b7e471386653 148
triochi 7:62c2c1382d86 149 // can also check status of pixels using getPixel(x,y)
triochi 7:62c2c1382d86 150 while(1)
triochi 7:62c2c1382d86 151 {
triochi 7:62c2c1382d86 152 wait(.5);
triochi 7:62c2c1382d86 153 length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer
triochi 7:62c2c1382d86 154 if (length <= 14) // if string will fit on display
triochi 7:62c2c1382d86 155 {
triochi 7:62c2c1382d86 156 lcd.printString(buffer,0,1); // display on screen
triochi 7:62c2c1382d86 157 }
triochi 7:62c2c1382d86 158 }
triochi 7:62c2c1382d86 159 lcd.clear();
triochi 7:62c2c1382d86 160 }
triochi 0:b7e471386653 161 }