4 Rotary encoders with 5110 LCD display. For Nucleo boards

Dependencies:   N5110 mbed

Committer:
triochi
Date:
Sun Oct 23 16:16:23 2016 +0000
Revision:
8:67732769ecdd
Parent:
7:62c2c1382d86
Child:
9:37975a517fb0
changed pins for nokia lcd to release pins for timers

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 8:67732769ecdd 42 N5110 lcd(PB_8,PA_4,PA_3,PA_2,PA_10,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 8:67732769ecdd 50
triochi 8:67732769ecdd 51 char buffer[16]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
triochi 8:67732769ecdd 52 // so can display a string of a maximum 14 characters in length
triochi 8:67732769ecdd 53 // or create formatted strings - ensure they aren't more than 14 characters long
triochi 8:67732769ecdd 54 // first need to initialise display
triochi 7:62c2c1382d86 55 lcd.init();
triochi 8:67732769ecdd 56
triochi 8:67732769ecdd 57 //examples
triochi 1:d7d729404013 58
triochi 7:62c2c1382d86 59 //counting on A-input only, 2 ticks per cycle, rolls over at 100
triochi 7:62c2c1382d86 60 EncoderInit(&encoder1, &timer1, TIM1, 99, TIM_ENCODERMODE_TI1);
triochi 7:62c2c1382d86 61
triochi 7:62c2c1382d86 62 //counting on both A&B inputs, 4 ticks per cycle, full 32-bit count
triochi 7:62c2c1382d86 63 EncoderInit(&encoder2, &timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12);
triochi 0:b7e471386653 64
triochi 7:62c2c1382d86 65 //counting on B-input only, 2 ticks per cycle, full 16-bit count
triochi 7:62c2c1382d86 66 EncoderInit(&encoder3, &timer3, TIM3, 0xffff, TIM_ENCODERMODE_TI2);
triochi 7:62c2c1382d86 67
triochi 7:62c2c1382d86 68 //counting on both A&B inputs, 4 ticks per cycle, full 16-bit count
triochi 7:62c2c1382d86 69 EncoderInit(&encoder4, &timer4, TIM4, 0xffff, TIM_ENCODERMODE_TI12);
triochi 7:62c2c1382d86 70
triochi 7:62c2c1382d86 71 //TIM5 is used by mbed for systick
triochi 7:62c2c1382d86 72 //EncoderInit(encoder2, timer2, TIM5, 0xffffffff, TIM_ENCODERMODE_TI12);
triochi 7:62c2c1382d86 73
triochi 8:67732769ecdd 74 // printf("STM HAL encoder demo\n\r");
triochi 0:b7e471386653 75
triochi 8:67732769ecdd 76 // these are default settings so not strictly needed
triochi 8:67732769ecdd 77 lcd.normalMode(); // normal colour mode
triochi 8:67732769ecdd 78 lcd.setBrightness(0.5); // put LED backlight on 50%
triochi 8:67732769ecdd 79
triochi 8:67732769ecdd 80 // can directly print strings at specified co-ordinates
triochi 8:67732769ecdd 81 lcd.printString("**STM Nucleo**",0,0);
triochi 7:62c2c1382d86 82 while(1)
triochi 8:67732769ecdd 83 {
triochi 7:62c2c1382d86 84 //OK 401 411 446 TICKER 030
triochi 7:62c2c1382d86 85 //count1=TIM1->CNT;
triochi 7:62c2c1382d86 86 //dir1=TIM1->CR1&TIM_CR1_DIR;
triochi 7:62c2c1382d86 87 count1=__HAL_TIM_GET_COUNTER(&timer1);
triochi 7:62c2c1382d86 88 dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer1);
triochi 0:b7e471386653 89
triochi 7:62c2c1382d86 90 //OK 401 411 446 NOK 030
triochi 7:62c2c1382d86 91 //count2=TIM2->CNT;
triochi 7:62c2c1382d86 92 //dir2=TIM2->CR1&TIM_CR1_DIR;
triochi 7:62c2c1382d86 93 count2=__HAL_TIM_GET_COUNTER(&timer2);
triochi 7:62c2c1382d86 94 dir2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
triochi 0:b7e471386653 95
triochi 7:62c2c1382d86 96 //OK 401 411 446 030
triochi 7:62c2c1382d86 97 //count3=TIM3->CNT;
triochi 7:62c2c1382d86 98 //dir3=TIM3->CR1&TIM_CR1_DIR;
triochi 7:62c2c1382d86 99 count3=__HAL_TIM_GET_COUNTER(&timer3);
triochi 7:62c2c1382d86 100 dir3 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer3);
triochi 7:62c2c1382d86 101
triochi 7:62c2c1382d86 102 //OK 401 411 446 N/A 030
triochi 7:62c2c1382d86 103 //count4=TIM4->CNT;
triochi 7:62c2c1382d86 104 //dir4=TIM4->CR1&TIM_CR1_DIR;
triochi 7:62c2c1382d86 105 count4=__HAL_TIM_GET_COUNTER(&timer4);
triochi 7:62c2c1382d86 106 dir4 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer4);
triochi 0:b7e471386653 107
triochi 7:62c2c1382d86 108 //TICKER 401 411 446 N/A 030
triochi 7:62c2c1382d86 109 // count5=__HAL_TIM_GET_COUNTER(&timer5);
triochi 7:62c2c1382d86 110 // dir5 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer5);
triochi 7:62c2c1382d86 111 // printf("%d%s %d%s %d%s %d%s\r\n", count1, dir1==0 ? "+":"-",
triochi 7:62c2c1382d86 112 // count2, dir2==0 ? "+":"-",
triochi 7:62c2c1382d86 113 // count3, dir3==0 ? "+":"-",
triochi 7:62c2c1382d86 114 // count4, dir4==0 ? "+":"-" );
triochi 7:62c2c1382d86 115
triochi 7:62c2c1382d86 116 // int temperature = encoder.getVal();
triochi 8:67732769ecdd 117 int length = sprintf(buffer,"C1 = %2d ",count1); // print formatted data to buffer
triochi 7:62c2c1382d86 118 // it is important the format specifier ensures the length will fit in the buffer
triochi 8:67732769ecdd 119
triochi 8:67732769ecdd 120 lcd.printString(buffer,0,1); // display on screen
triochi 8:67732769ecdd 121 sprintf(buffer,"C2 = %2d ",count2); // print formatted data to buffer
triochi 8:67732769ecdd 122 lcd.printString(buffer,0,1); // display on screen
triochi 8:67732769ecdd 123 sprintf(buffer,"C3 = %2d ",count3); // print formatted data to buffer
triochi 8:67732769ecdd 124 lcd.printString(buffer,0,1); // display on screen
triochi 8:67732769ecdd 125 sprintf(buffer,"C4 = %2d ",count4); // print formatted data to buffer
triochi 8:67732769ecdd 126 lcd.printString(buffer,0,1); // display on screen
triochi 8:67732769ecdd 127
triochi 7:62c2c1382d86 128 // can also print individual characters at specified place
triochi 7:62c2c1382d86 129 lcd.printChar('X',5,3);
triochi 0:b7e471386653 130
triochi 7:62c2c1382d86 131 // draw a line across the display at y = 40 pixels (origin top-left)
triochi 7:62c2c1382d86 132 for (int i = 0; i < WIDTH; i++)
triochi 7:62c2c1382d86 133 {
triochi 7:62c2c1382d86 134 lcd.setPixel(i,40);
triochi 7:62c2c1382d86 135 }
triochi 7:62c2c1382d86 136 // need to refresh display after setting pixels
triochi 7:62c2c1382d86 137 lcd.refresh();
triochi 0:b7e471386653 138
triochi 7:62c2c1382d86 139 // can also check status of pixels using getPixel(x,y)
triochi 8:67732769ecdd 140
triochi 8:67732769ecdd 141 // lcd.clear();
triochi 7:62c2c1382d86 142 }
triochi 0:b7e471386653 143 }