Dimitar Marinov
/
Nucleo_4_encoders_w_Nokia5110
4 Rotary encoders with 5110 LCD display. For Nucleo boards
main.cpp@9:37975a517fb0, 2016-10-23 (annotated)
- Committer:
- triochi
- Date:
- Sun Oct 23 18:44:15 2016 +0000
- Revision:
- 9:37975a517fb0
- Parent:
- 8:67732769ecdd
- Child:
- 10:65e7ceb96b39
Almost working. Encoder 2 has a problem
Who changed what in which revision?
User | Revision | Line number | New 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 | 7:62c2c1382d86 | 34 | TIM_Encoder_InitTypeDef encoder1, encoder2, encoder3, encoder4; |
triochi | 7:62c2c1382d86 | 35 | TIM_HandleTypeDef timer1, timer2, timer3, timer4; |
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 | 9:37975a517fb0 | 47 | N5110 lcd(PB_8,PA_4,PB_14,PB_9,PB_15,PB_13,PB_3); //PA_4 and PA_6 not used |
triochi | 7:62c2c1382d86 | 48 | |
triochi | 1:d7d729404013 | 49 | int temperature = 50; |
triochi | 1:d7d729404013 | 50 | |
triochi | 7:62c2c1382d86 | 51 | int main() { |
triochi | 7:62c2c1382d86 | 52 | uint16_t count1=0, count3=0, count4=0; |
triochi | 7:62c2c1382d86 | 53 | uint32_t count2=0; |
triochi | 7:62c2c1382d86 | 54 | int8_t dir1, dir2, dir3, dir4; |
triochi | 8:67732769ecdd | 55 | |
triochi | 8:67732769ecdd | 56 | char buffer[16]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) |
triochi | 8:67732769ecdd | 57 | // so can display a string of a maximum 14 characters in length |
triochi | 8:67732769ecdd | 58 | // or create formatted strings - ensure they aren't more than 14 characters long |
triochi | 8:67732769ecdd | 59 | // first need to initialise display |
triochi | 7:62c2c1382d86 | 60 | lcd.init(); |
triochi | 9:37975a517fb0 | 61 | lcd.printString("**STM Nucleo**",0,0); |
triochi | 8:67732769ecdd | 62 | //examples |
triochi | 1:d7d729404013 | 63 | |
triochi | 7:62c2c1382d86 | 64 | //counting on A-input only, 2 ticks per cycle, rolls over at 100 |
triochi | 9:37975a517fb0 | 65 | //EncoderInit(&encoder1, &timer1, TIM1, 99, TIM_ENCODERMODE_TI1); |
triochi | 9:37975a517fb0 | 66 | EncoderInit(&encoder1, &timer1, TIM1, 3600, TIM_ENCODERMODE_TI12); |
triochi | 7:62c2c1382d86 | 67 | |
triochi | 7:62c2c1382d86 | 68 | //counting on both A&B inputs, 4 ticks per cycle, full 32-bit count |
triochi | 9:37975a517fb0 | 69 | // EncoderInit(&encoder2, &timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12); |
triochi | 9:37975a517fb0 | 70 | EncoderInit(&encoder2, &timer2, TIM2, 3600, TIM_ENCODERMODE_TI12); |
triochi | 0:b7e471386653 | 71 | |
triochi | 7:62c2c1382d86 | 72 | //counting on B-input only, 2 ticks per cycle, full 16-bit count |
triochi | 9:37975a517fb0 | 73 | // EncoderInit(&encoder3, &timer3, TIM3, 0xffff, TIM_ENCODERMODE_TI2); |
triochi | 9:37975a517fb0 | 74 | EncoderInit(&encoder3, &timer3, TIM3, 3600, TIM_ENCODERMODE_TI12); |
triochi | 7:62c2c1382d86 | 75 | |
triochi | 7:62c2c1382d86 | 76 | //counting on both A&B inputs, 4 ticks per cycle, full 16-bit count |
triochi | 9:37975a517fb0 | 77 | //EncoderInit(&encoder4, &timer4, TIM4, 0xffff, TIM_ENCODERMODE_TI12); |
triochi | 9:37975a517fb0 | 78 | EncoderInit(&encoder4, &timer4, TIM4, 3600, TIM_ENCODERMODE_TI12); |
triochi | 7:62c2c1382d86 | 79 | |
triochi | 7:62c2c1382d86 | 80 | //TIM5 is used by mbed for systick |
triochi | 7:62c2c1382d86 | 81 | //EncoderInit(encoder2, timer2, TIM5, 0xffffffff, TIM_ENCODERMODE_TI12); |
triochi | 7:62c2c1382d86 | 82 | |
triochi | 8:67732769ecdd | 83 | // printf("STM HAL encoder demo\n\r"); |
triochi | 0:b7e471386653 | 84 | |
triochi | 8:67732769ecdd | 85 | // these are default settings so not strictly needed |
triochi | 8:67732769ecdd | 86 | lcd.normalMode(); // normal colour mode |
triochi | 8:67732769ecdd | 87 | lcd.setBrightness(0.5); // put LED backlight on 50% |
triochi | 9:37975a517fb0 | 88 | __HAL_TIM_SET_COUNTER(&timer2,0); |
triochi | 8:67732769ecdd | 89 | |
triochi | 8:67732769ecdd | 90 | // can directly print strings at specified co-ordinates |
triochi | 9:37975a517fb0 | 91 | // lcd.printString("**STM Nucleo**",0,0); |
triochi | 7:62c2c1382d86 | 92 | while(1) |
triochi | 8:67732769ecdd | 93 | { |
triochi | 7:62c2c1382d86 | 94 | //OK 401 411 446 TICKER 030 |
triochi | 7:62c2c1382d86 | 95 | //count1=TIM1->CNT; |
triochi | 7:62c2c1382d86 | 96 | //dir1=TIM1->CR1&TIM_CR1_DIR; |
triochi | 7:62c2c1382d86 | 97 | count1=__HAL_TIM_GET_COUNTER(&timer1); |
triochi | 7:62c2c1382d86 | 98 | dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer1); |
triochi | 0:b7e471386653 | 99 | |
triochi | 7:62c2c1382d86 | 100 | //OK 401 411 446 NOK 030 |
triochi | 7:62c2c1382d86 | 101 | //count2=TIM2->CNT; |
triochi | 7:62c2c1382d86 | 102 | //dir2=TIM2->CR1&TIM_CR1_DIR; |
triochi | 7:62c2c1382d86 | 103 | count2=__HAL_TIM_GET_COUNTER(&timer2); |
triochi | 7:62c2c1382d86 | 104 | dir2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2); |
triochi | 0:b7e471386653 | 105 | |
triochi | 7:62c2c1382d86 | 106 | //OK 401 411 446 030 |
triochi | 7:62c2c1382d86 | 107 | //count3=TIM3->CNT; |
triochi | 7:62c2c1382d86 | 108 | //dir3=TIM3->CR1&TIM_CR1_DIR; |
triochi | 7:62c2c1382d86 | 109 | count3=__HAL_TIM_GET_COUNTER(&timer3); |
triochi | 7:62c2c1382d86 | 110 | dir3 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer3); |
triochi | 7:62c2c1382d86 | 111 | |
triochi | 7:62c2c1382d86 | 112 | //OK 401 411 446 N/A 030 |
triochi | 7:62c2c1382d86 | 113 | //count4=TIM4->CNT; |
triochi | 7:62c2c1382d86 | 114 | //dir4=TIM4->CR1&TIM_CR1_DIR; |
triochi | 7:62c2c1382d86 | 115 | count4=__HAL_TIM_GET_COUNTER(&timer4); |
triochi | 7:62c2c1382d86 | 116 | dir4 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer4); |
triochi | 0:b7e471386653 | 117 | |
triochi | 7:62c2c1382d86 | 118 | //TICKER 401 411 446 N/A 030 |
triochi | 7:62c2c1382d86 | 119 | // count5=__HAL_TIM_GET_COUNTER(&timer5); |
triochi | 7:62c2c1382d86 | 120 | // dir5 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer5); |
triochi | 7:62c2c1382d86 | 121 | // printf("%d%s %d%s %d%s %d%s\r\n", count1, dir1==0 ? "+":"-", |
triochi | 7:62c2c1382d86 | 122 | // count2, dir2==0 ? "+":"-", |
triochi | 7:62c2c1382d86 | 123 | // count3, dir3==0 ? "+":"-", |
triochi | 7:62c2c1382d86 | 124 | // count4, dir4==0 ? "+":"-" ); |
triochi | 7:62c2c1382d86 | 125 | |
triochi | 7:62c2c1382d86 | 126 | // int temperature = encoder.getVal(); |
triochi | 9:37975a517fb0 | 127 | int length = sprintf(buffer,"C1 = %06d ",count1); // print formatted data to buffer |
triochi | 7:62c2c1382d86 | 128 | // it is important the format specifier ensures the length will fit in the buffer |
triochi | 8:67732769ecdd | 129 | |
triochi | 9:37975a517fb0 | 130 | lcd.printString(buffer,1,1); // display on screen |
triochi | 9:37975a517fb0 | 131 | sprintf(buffer,"C2 = %06d ",count2); // print formatted data to buffer |
triochi | 9:37975a517fb0 | 132 | lcd.printString(buffer,1,2); // display on screen |
triochi | 9:37975a517fb0 | 133 | sprintf(buffer,"C3 = %06d ",count3); // print formatted data to buffer |
triochi | 9:37975a517fb0 | 134 | lcd.printString(buffer,1,3); // display on screen |
triochi | 9:37975a517fb0 | 135 | sprintf(buffer,"C4 = %06d ",count4); // print formatted data to buffer |
triochi | 9:37975a517fb0 | 136 | lcd.printString(buffer,1,4); // display on screen |
triochi | 8:67732769ecdd | 137 | |
triochi | 7:62c2c1382d86 | 138 | // can also print individual characters at specified place |
triochi | 9:37975a517fb0 | 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 | 9:37975a517fb0 | 144 | lcd.setPixel(i,50); |
triochi | 7:62c2c1382d86 | 145 | } |
triochi | 7:62c2c1382d86 | 146 | // need to refresh display after setting pixels |
triochi | 7:62c2c1382d86 | 147 | lcd.refresh(); |
triochi | 9:37975a517fb0 | 148 | wait(.5); |
triochi | 0:b7e471386653 | 149 | |
triochi | 7:62c2c1382d86 | 150 | // can also check status of pixels using getPixel(x,y) |
triochi | 8:67732769ecdd | 151 | |
triochi | 8:67732769ecdd | 152 | // lcd.clear(); |
triochi | 7:62c2c1382d86 | 153 | } |
triochi | 0:b7e471386653 | 154 | } |