4 Rotary encoders with 5110 LCD display. For Nucleo boards

Dependencies:   N5110 mbed

Committer:
triochi
Date:
Mon Oct 24 17:21:58 2016 +0000
Revision:
10:65e7ceb96b39
Parent:
9:37975a517fb0
finally working 4 encoders

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 9:37975a517fb0 21 // Port definitions:
triochi 9:37975a517fb0 22 // https://developer.mbed.org/users/mbed_official/code/mbed-src/file/c9b73cd93427/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_NUCLEO_F411RE/PeripheralPins.c
triochi 7:62c2c1382d86 23
triochi 0:b7e471386653 24 #include "mbed.h"
triochi 0:b7e471386653 25 #include "N5110.h"
triochi 7:62c2c1382d86 26 #include "Encoder.h"
triochi 7:62c2c1382d86 27
triochi 7:62c2c1382d86 28 //STM mbed bug: these macros are MISSING from stm32f3xx_hal_tim.h
triochi 7:62c2c1382d86 29 #ifdef TARGET_STM32F3
triochi 7:62c2c1382d86 30 #define __HAL_TIM_GET_COUNTER(__HANDLE__) ((__HANDLE__)->Instance->CNT)
triochi 7:62c2c1382d86 31 #define __HAL_TIM_IS_TIM_COUNTING_DOWN(__HANDLE__) (((__HANDLE__)->Instance->CR1 &(TIM_CR1_DIR)) == (TIM_CR1_DIR))
triochi 7:62c2c1382d86 32 #endif
triochi 7:62c2c1382d86 33
triochi 10:65e7ceb96b39 34 TIM_Encoder_InitTypeDef encoder1, encoder2, encoder3, encoder4, encoder5;
triochi 10:65e7ceb96b39 35 TIM_HandleTypeDef timer1, timer2, timer3, timer4, timer5;
triochi 5:0daf19d95cc7 36
triochi 0:b7e471386653 37
triochi 0:b7e471386653 38 //DigitalOut red(LED1);
triochi 0:b7e471386653 39 //DigitalOut blue(LED2);
triochi 0:b7e471386653 40 //DigitalOut green(LED3);
triochi 0:b7e471386653 41 int i;
triochi 0:b7e471386653 42
triochi 9:37975a517fb0 43 //pwrPin, scePin, rstPin, dcPin, mosiPin, sclkPin, ledPin)
triochi 9:37975a517fb0 44
triochi 9:37975a517fb0 45
triochi 0:b7e471386653 46 // VCC,SCE, RST, D/C, MOSI,SCLK,LED
triochi 10:65e7ceb96b39 47
triochi 10:65e7ceb96b39 48 // N.C.,SCE, RST, D/C, MOSI,SCLK,N.C
triochi 10:65e7ceb96b39 49 N5110 lcd(PA_4,PA_4,PB_14,PB_9,PB_15,PB_13,PA_4); //PA_4 and PA_6 not used
triochi 7:62c2c1382d86 50
triochi 1:d7d729404013 51 int temperature = 50;
triochi 1:d7d729404013 52
triochi 7:62c2c1382d86 53 int main() {
triochi 7:62c2c1382d86 54 uint16_t count1=0, count3=0, count4=0;
triochi 7:62c2c1382d86 55 uint32_t count2=0;
triochi 7:62c2c1382d86 56 int8_t dir1, dir2, dir3, dir4;
triochi 8:67732769ecdd 57
triochi 8:67732769ecdd 58 char buffer[16]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
triochi 8:67732769ecdd 59 // so can display a string of a maximum 14 characters in length
triochi 8:67732769ecdd 60 // or create formatted strings - ensure they aren't more than 14 characters long
triochi 8:67732769ecdd 61 // first need to initialise display
triochi 7:62c2c1382d86 62 lcd.init();
triochi 9:37975a517fb0 63 lcd.printString("**STM Nucleo**",0,0);
triochi 8:67732769ecdd 64 //examples
triochi 1:d7d729404013 65
triochi 7:62c2c1382d86 66 //counting on A-input only, 2 ticks per cycle, rolls over at 100
triochi 9:37975a517fb0 67 //EncoderInit(&encoder1, &timer1, TIM1, 99, TIM_ENCODERMODE_TI1);
triochi 9:37975a517fb0 68 EncoderInit(&encoder1, &timer1, TIM1, 3600, TIM_ENCODERMODE_TI12);
triochi 7:62c2c1382d86 69
triochi 7:62c2c1382d86 70 //counting on both A&B inputs, 4 ticks per cycle, full 32-bit count
triochi 9:37975a517fb0 71 // EncoderInit(&encoder2, &timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12);
triochi 9:37975a517fb0 72 EncoderInit(&encoder2, &timer2, TIM2, 3600, TIM_ENCODERMODE_TI12);
triochi 0:b7e471386653 73
triochi 7:62c2c1382d86 74 //counting on B-input only, 2 ticks per cycle, full 16-bit count
triochi 9:37975a517fb0 75 // EncoderInit(&encoder3, &timer3, TIM3, 0xffff, TIM_ENCODERMODE_TI2);
triochi 9:37975a517fb0 76 EncoderInit(&encoder3, &timer3, TIM3, 3600, TIM_ENCODERMODE_TI12);
triochi 7:62c2c1382d86 77
triochi 7:62c2c1382d86 78 //counting on both A&B inputs, 4 ticks per cycle, full 16-bit count
triochi 9:37975a517fb0 79 //EncoderInit(&encoder4, &timer4, TIM4, 0xffff, TIM_ENCODERMODE_TI12);
triochi 9:37975a517fb0 80 EncoderInit(&encoder4, &timer4, TIM4, 3600, TIM_ENCODERMODE_TI12);
triochi 7:62c2c1382d86 81
triochi 7:62c2c1382d86 82 //TIM5 is used by mbed for systick
triochi 10:65e7ceb96b39 83 // EncoderInit(&encoder5, &timer5, TIM5, 0xffffffff, TIM_ENCODERMODE_TI12);
triochi 7:62c2c1382d86 84
triochi 8:67732769ecdd 85 // printf("STM HAL encoder demo\n\r");
triochi 0:b7e471386653 86
triochi 8:67732769ecdd 87 // these are default settings so not strictly needed
triochi 8:67732769ecdd 88 lcd.normalMode(); // normal colour mode
triochi 10:65e7ceb96b39 89 lcd.setBrightness(0.7); // put LED backlight on 50%
triochi 10:65e7ceb96b39 90 // __HAL_TIM_SET_COUNTER(&timer2,0);
triochi 8:67732769ecdd 91
triochi 8:67732769ecdd 92 // can directly print strings at specified co-ordinates
triochi 9:37975a517fb0 93 // lcd.printString("**STM Nucleo**",0,0);
triochi 7:62c2c1382d86 94 while(1)
triochi 8:67732769ecdd 95 {
triochi 7:62c2c1382d86 96 //OK 401 411 446 TICKER 030
triochi 7:62c2c1382d86 97 //count1=TIM1->CNT;
triochi 7:62c2c1382d86 98 //dir1=TIM1->CR1&TIM_CR1_DIR;
triochi 7:62c2c1382d86 99 count1=__HAL_TIM_GET_COUNTER(&timer1);
triochi 7:62c2c1382d86 100 dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer1);
triochi 0:b7e471386653 101
triochi 7:62c2c1382d86 102 //OK 401 411 446 NOK 030
triochi 7:62c2c1382d86 103 //count2=TIM2->CNT;
triochi 7:62c2c1382d86 104 //dir2=TIM2->CR1&TIM_CR1_DIR;
triochi 7:62c2c1382d86 105 count2=__HAL_TIM_GET_COUNTER(&timer2);
triochi 7:62c2c1382d86 106 dir2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
triochi 0:b7e471386653 107
triochi 7:62c2c1382d86 108 //OK 401 411 446 030
triochi 7:62c2c1382d86 109 //count3=TIM3->CNT;
triochi 7:62c2c1382d86 110 //dir3=TIM3->CR1&TIM_CR1_DIR;
triochi 7:62c2c1382d86 111 count3=__HAL_TIM_GET_COUNTER(&timer3);
triochi 7:62c2c1382d86 112 dir3 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer3);
triochi 7:62c2c1382d86 113
triochi 7:62c2c1382d86 114 //OK 401 411 446 N/A 030
triochi 7:62c2c1382d86 115 //count4=TIM4->CNT;
triochi 7:62c2c1382d86 116 //dir4=TIM4->CR1&TIM_CR1_DIR;
triochi 7:62c2c1382d86 117 count4=__HAL_TIM_GET_COUNTER(&timer4);
triochi 7:62c2c1382d86 118 dir4 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer4);
triochi 0:b7e471386653 119
triochi 7:62c2c1382d86 120 //TICKER 401 411 446 N/A 030
triochi 7:62c2c1382d86 121 // count5=__HAL_TIM_GET_COUNTER(&timer5);
triochi 7:62c2c1382d86 122 // dir5 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer5);
triochi 7:62c2c1382d86 123 // printf("%d%s %d%s %d%s %d%s\r\n", count1, dir1==0 ? "+":"-",
triochi 7:62c2c1382d86 124 // count2, dir2==0 ? "+":"-",
triochi 7:62c2c1382d86 125 // count3, dir3==0 ? "+":"-",
triochi 7:62c2c1382d86 126 // count4, dir4==0 ? "+":"-" );
triochi 7:62c2c1382d86 127
triochi 7:62c2c1382d86 128 // int temperature = encoder.getVal();
triochi 9:37975a517fb0 129 int length = sprintf(buffer,"C1 = %06d ",count1); // print formatted data to buffer
triochi 7:62c2c1382d86 130 // it is important the format specifier ensures the length will fit in the buffer
triochi 8:67732769ecdd 131
triochi 9:37975a517fb0 132 lcd.printString(buffer,1,1); // display on screen
triochi 9:37975a517fb0 133 sprintf(buffer,"C2 = %06d ",count2); // print formatted data to buffer
triochi 9:37975a517fb0 134 lcd.printString(buffer,1,2); // display on screen
triochi 9:37975a517fb0 135 sprintf(buffer,"C3 = %06d ",count3); // print formatted data to buffer
triochi 9:37975a517fb0 136 lcd.printString(buffer,1,3); // display on screen
triochi 9:37975a517fb0 137 sprintf(buffer,"C4 = %06d ",count4); // print formatted data to buffer
triochi 9:37975a517fb0 138 lcd.printString(buffer,1,4); // display on screen
triochi 8:67732769ecdd 139
triochi 7:62c2c1382d86 140 // can also print individual characters at specified place
triochi 9:37975a517fb0 141 //lcd.printChar('X',5,3);
triochi 0:b7e471386653 142
triochi 7:62c2c1382d86 143 // draw a line across the display at y = 40 pixels (origin top-left)
triochi 7:62c2c1382d86 144 for (int i = 0; i < WIDTH; i++)
triochi 7:62c2c1382d86 145 {
triochi 9:37975a517fb0 146 lcd.setPixel(i,50);
triochi 7:62c2c1382d86 147 }
triochi 7:62c2c1382d86 148 // need to refresh display after setting pixels
triochi 7:62c2c1382d86 149 lcd.refresh();
triochi 9:37975a517fb0 150 wait(.5);
triochi 0:b7e471386653 151
triochi 7:62c2c1382d86 152 // can also check status of pixels using getPixel(x,y)
triochi 8:67732769ecdd 153
triochi 8:67732769ecdd 154 // lcd.clear();
triochi 7:62c2c1382d86 155 }
triochi 0:b7e471386653 156 }