This program is for controlling the velocity of 2 motors.
EncoderMspInitF4.cpp@0:e3e886021c90, 2020-07-15 (annotated)
- Committer:
- r08522622
- Date:
- Wed Jul 15 11:18:13 2020 +0000
- Revision:
- 0:e3e886021c90
edit parameters
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
r08522622 | 0:e3e886021c90 | 1 | #include "mbed.h" |
r08522622 | 0:e3e886021c90 | 2 | /* |
r08522622 | 0:e3e886021c90 | 3 | * HAL_TIM_Encoder_MspInit() |
r08522622 | 0:e3e886021c90 | 4 | * Overrides the __weak function stub in stm32f4xx_hal_tim.h |
r08522622 | 0:e3e886021c90 | 5 | * |
r08522622 | 0:e3e886021c90 | 6 | * Edit the below for your preferred pin wiring & pullup/down |
r08522622 | 0:e3e886021c90 | 7 | * I have encoder common at 3V3, using GPIO_PULLDOWN on inputs. |
r08522622 | 0:e3e886021c90 | 8 | * Encoder A&B outputs connected directly to GPIOs. |
r08522622 | 0:e3e886021c90 | 9 | * |
r08522622 | 0:e3e886021c90 | 10 | * www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00102166.pdf |
r08522622 | 0:e3e886021c90 | 11 | * www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00141306.pdf |
r08522622 | 0:e3e886021c90 | 12 | * |
r08522622 | 0:e3e886021c90 | 13 | * TIM1_CH1: AF1 @ PA_8, PE_9 |
r08522622 | 0:e3e886021c90 | 14 | * TIM1_CH2: AF1 @ PA_9, PE_11 |
r08522622 | 0:e3e886021c90 | 15 | * |
r08522622 | 0:e3e886021c90 | 16 | * TIM2_CH1: AF1 @ PA_0, PA_5, PA_15, PB_8* *F446 only |
r08522622 | 0:e3e886021c90 | 17 | * TIM2_CH2: AF1 @ PA_1, PB_3, PB_9* *F446 only |
r08522622 | 0:e3e886021c90 | 18 | * |
r08522622 | 0:e3e886021c90 | 19 | * TIM3_CH1: AF2 @ PA_6, PB_4, PC_6 |
r08522622 | 0:e3e886021c90 | 20 | * TIM3_CH2: AF2 @ PA_7, PB_5, PC_7 |
r08522622 | 0:e3e886021c90 | 21 | * |
r08522622 | 0:e3e886021c90 | 22 | * TIM4_CH1: AF2 @ PB_6, PD_12 |
r08522622 | 0:e3e886021c90 | 23 | * TIM4_CH2: AF2 @ PB_7, PD_13 |
r08522622 | 0:e3e886021c90 | 24 | * |
r08522622 | 0:e3e886021c90 | 25 | * TIM5_CH1: AF2 @ PA_0* *TIM5 used by mbed system ticker so unavailable |
r08522622 | 0:e3e886021c90 | 26 | * TIM5_CH2: AF2 @ PA_1* |
r08522622 | 0:e3e886021c90 | 27 | * |
r08522622 | 0:e3e886021c90 | 28 | */ |
r08522622 | 0:e3e886021c90 | 29 | |
r08522622 | 0:e3e886021c90 | 30 | #ifdef TARGET_STM32F4 |
r08522622 | 0:e3e886021c90 | 31 | void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim) |
r08522622 | 0:e3e886021c90 | 32 | { |
r08522622 | 0:e3e886021c90 | 33 | GPIO_InitTypeDef GPIO_InitStruct; |
r08522622 | 0:e3e886021c90 | 34 | |
r08522622 | 0:e3e886021c90 | 35 | if (htim->Instance == TIM1) { //PA8 PA9 = Nucleo D7 D8 |
r08522622 | 0:e3e886021c90 | 36 | __TIM1_CLK_ENABLE(); |
r08522622 | 0:e3e886021c90 | 37 | __GPIOA_CLK_ENABLE(); |
r08522622 | 0:e3e886021c90 | 38 | GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; |
r08522622 | 0:e3e886021c90 | 39 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
r08522622 | 0:e3e886021c90 | 40 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
r08522622 | 0:e3e886021c90 | 41 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
r08522622 | 0:e3e886021c90 | 42 | GPIO_InitStruct.Alternate = GPIO_AF1_TIM1; |
r08522622 | 0:e3e886021c90 | 43 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
r08522622 | 0:e3e886021c90 | 44 | } |
r08522622 | 0:e3e886021c90 | 45 | else if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1 |
r08522622 | 0:e3e886021c90 | 46 | __TIM2_CLK_ENABLE(); |
r08522622 | 0:e3e886021c90 | 47 | __GPIOA_CLK_ENABLE(); |
r08522622 | 0:e3e886021c90 | 48 | GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; |
r08522622 | 0:e3e886021c90 | 49 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
r08522622 | 0:e3e886021c90 | 50 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
r08522622 | 0:e3e886021c90 | 51 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
r08522622 | 0:e3e886021c90 | 52 | GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; |
r08522622 | 0:e3e886021c90 | 53 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
r08522622 | 0:e3e886021c90 | 54 | } |
r08522622 | 0:e3e886021c90 | 55 | else if (htim->Instance == TIM3) { //PA6 PA7 = Nucleo D12 D11 |
r08522622 | 0:e3e886021c90 | 56 | __TIM3_CLK_ENABLE(); |
r08522622 | 0:e3e886021c90 | 57 | __GPIOB_CLK_ENABLE(); |
r08522622 | 0:e3e886021c90 | 58 | GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; |
r08522622 | 0:e3e886021c90 | 59 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
r08522622 | 0:e3e886021c90 | 60 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
r08522622 | 0:e3e886021c90 | 61 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
r08522622 | 0:e3e886021c90 | 62 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; |
r08522622 | 0:e3e886021c90 | 63 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
r08522622 | 0:e3e886021c90 | 64 | } |
r08522622 | 0:e3e886021c90 | 65 | else if (htim->Instance == TIM4) { // PB6 PB7 = Nucleo D10 MORPHO_PB7 |
r08522622 | 0:e3e886021c90 | 66 | __TIM4_CLK_ENABLE(); |
r08522622 | 0:e3e886021c90 | 67 | __GPIOB_CLK_ENABLE(); |
r08522622 | 0:e3e886021c90 | 68 | GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; |
r08522622 | 0:e3e886021c90 | 69 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
r08522622 | 0:e3e886021c90 | 70 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
r08522622 | 0:e3e886021c90 | 71 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
r08522622 | 0:e3e886021c90 | 72 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM4; |
r08522622 | 0:e3e886021c90 | 73 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
r08522622 | 0:e3e886021c90 | 74 | } |
r08522622 | 0:e3e886021c90 | 75 | } |
r08522622 | 0:e3e886021c90 | 76 | #endif |