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:
7:62c2c1382d86
finally working 4 encoders

Who changed what in which revision?

UserRevisionLine numberNew contents of line
triochi 7:62c2c1382d86 1 #include "mbed.h"
triochi 7:62c2c1382d86 2 /*
triochi 7:62c2c1382d86 3 * HAL_TIM_Encoder_MspInit()
triochi 7:62c2c1382d86 4 * Overrides the __weak function stub in stm32f4xx_hal_tim.h
triochi 7:62c2c1382d86 5 *
triochi 7:62c2c1382d86 6 * Edit the below for your preferred pin wiring & pullup/down
triochi 7:62c2c1382d86 7 * I have encoder common at 3V3, using GPIO_PULLDOWN on inputs.
triochi 7:62c2c1382d86 8 * Encoder A&B outputs connected directly to GPIOs.
triochi 7:62c2c1382d86 9 *
triochi 7:62c2c1382d86 10 * www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00102166.pdf
triochi 7:62c2c1382d86 11 * www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00141306.pdf
triochi 7:62c2c1382d86 12 *
triochi 7:62c2c1382d86 13 * TIM1_CH1: AF1 @ PA_8, PE_9
triochi 7:62c2c1382d86 14 * TIM1_CH2: AF1 @ PA_9, PE_11
triochi 7:62c2c1382d86 15 *
triochi 7:62c2c1382d86 16 * TIM2_CH1: AF1 @ PA_0, PA_5, PA_15, PB_8* *F446 only
triochi 7:62c2c1382d86 17 * TIM2_CH2: AF1 @ PA_1, PB_3, PB_9* *F446 only
triochi 7:62c2c1382d86 18 *
triochi 7:62c2c1382d86 19 * TIM3_CH1: AF2 @ PA_6, PB_4, PC_6
triochi 7:62c2c1382d86 20 * TIM3_CH2: AF2 @ PA_7, PB_5, PC_7
triochi 7:62c2c1382d86 21 *
triochi 7:62c2c1382d86 22 * TIM4_CH1: AF2 @ PB_6, PD_12
triochi 7:62c2c1382d86 23 * TIM4_CH2: AF2 @ PB_7, PD_13
triochi 7:62c2c1382d86 24 *
triochi 7:62c2c1382d86 25 * TIM5_CH1: AF2 @ PA_0* *TIM5 used by mbed system ticker so unavailable
triochi 7:62c2c1382d86 26 * TIM5_CH2: AF2 @ PA_1*
triochi 7:62c2c1382d86 27 *
triochi 7:62c2c1382d86 28 */
triochi 7:62c2c1382d86 29
triochi 7:62c2c1382d86 30 #ifdef TARGET_STM32F4
triochi 7:62c2c1382d86 31 void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim)
triochi 7:62c2c1382d86 32 {
triochi 7:62c2c1382d86 33 GPIO_InitTypeDef GPIO_InitStruct;
triochi 7:62c2c1382d86 34
triochi 7:62c2c1382d86 35 if (htim->Instance == TIM1) { //PA8 PA9 = Nucleo D7 D8
triochi 7:62c2c1382d86 36 __TIM1_CLK_ENABLE();
triochi 7:62c2c1382d86 37 __GPIOA_CLK_ENABLE();
triochi 7:62c2c1382d86 38 GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
triochi 7:62c2c1382d86 39 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
triochi 7:62c2c1382d86 40 GPIO_InitStruct.Pull = GPIO_PULLDOWN;
triochi 7:62c2c1382d86 41 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
triochi 7:62c2c1382d86 42 GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
triochi 7:62c2c1382d86 43 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
triochi 7:62c2c1382d86 44 }
triochi 7:62c2c1382d86 45 else if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1
triochi 7:62c2c1382d86 46 __TIM2_CLK_ENABLE();
triochi 7:62c2c1382d86 47 __GPIOA_CLK_ENABLE();
triochi 7:62c2c1382d86 48 GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
triochi 7:62c2c1382d86 49 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
triochi 7:62c2c1382d86 50 GPIO_InitStruct.Pull = GPIO_PULLDOWN;
triochi 7:62c2c1382d86 51 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
triochi 7:62c2c1382d86 52 GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
triochi 7:62c2c1382d86 53 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
triochi 7:62c2c1382d86 54 }
triochi 7:62c2c1382d86 55 else if (htim->Instance == TIM3) { //PB4 PB5 = Nucleo D5 D4
triochi 7:62c2c1382d86 56 __TIM3_CLK_ENABLE();
triochi 7:62c2c1382d86 57 __GPIOB_CLK_ENABLE();
triochi 7:62c2c1382d86 58 GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5;
triochi 7:62c2c1382d86 59 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
triochi 7:62c2c1382d86 60 GPIO_InitStruct.Pull = GPIO_PULLDOWN;
triochi 7:62c2c1382d86 61 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
triochi 7:62c2c1382d86 62 GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
triochi 7:62c2c1382d86 63 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
triochi 7:62c2c1382d86 64 }
triochi 7:62c2c1382d86 65 else if (htim->Instance == TIM4) { // PB6 PB7 = Nucleo D10 MORPHO_PB7
triochi 7:62c2c1382d86 66 __TIM4_CLK_ENABLE();
triochi 7:62c2c1382d86 67 __GPIOB_CLK_ENABLE();
triochi 7:62c2c1382d86 68 GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
triochi 7:62c2c1382d86 69 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
triochi 7:62c2c1382d86 70 GPIO_InitStruct.Pull = GPIO_PULLDOWN;
triochi 7:62c2c1382d86 71 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
triochi 7:62c2c1382d86 72 GPIO_InitStruct.Alternate = GPIO_AF2_TIM4;
triochi 7:62c2c1382d86 73 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
triochi 7:62c2c1382d86 74 }
triochi 10:65e7ceb96b39 75 else if (htim->Instance == TIM5) { // PA2 PA3
triochi 10:65e7ceb96b39 76 __TIM5_CLK_ENABLE();
triochi 10:65e7ceb96b39 77 __GPIOA_CLK_ENABLE();
triochi 10:65e7ceb96b39 78 GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
triochi 10:65e7ceb96b39 79 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
triochi 10:65e7ceb96b39 80 GPIO_InitStruct.Pull = GPIO_PULLDOWN;
triochi 10:65e7ceb96b39 81 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
triochi 10:65e7ceb96b39 82 GPIO_InitStruct.Alternate = GPIO_AF2_TIM5;
triochi 10:65e7ceb96b39 83 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
triochi 10:65e7ceb96b39 84 }
triochi 7:62c2c1382d86 85 }
triochi 10:65e7ceb96b39 86 #endif