nambah buat eksternal
Dependencies: mbed encoderKRAI Motor_new
encoderHAL/EncoderMspInitF4.cpp@1:bbe0769f00e9, 2021-06-21 (annotated)
- Committer:
- Yolandataniaa
- Date:
- Mon Jun 21 07:48:27 2021 +0000
- Revision:
- 1:bbe0769f00e9
- Parent:
- 0:49e87dcad299
revisi
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yolandataniaa | 0:49e87dcad299 | 1 | #include "mbed.h" |
Yolandataniaa | 0:49e87dcad299 | 2 | /* |
Yolandataniaa | 0:49e87dcad299 | 3 | * HAL_TIM_Encoder_MspInit() |
Yolandataniaa | 0:49e87dcad299 | 4 | * Overrides the __weak function stub in stm32f4xx_hal_tim.h |
Yolandataniaa | 0:49e87dcad299 | 5 | * |
Yolandataniaa | 0:49e87dcad299 | 6 | * Edit the below for your preferred pin wiring & pullup/down |
Yolandataniaa | 0:49e87dcad299 | 7 | * I have encoder common at 3V3, using GPIO_PULLDOWN on inputs. |
Yolandataniaa | 0:49e87dcad299 | 8 | * Encoder A&B outputs connected directly to GPIOs. |
Yolandataniaa | 0:49e87dcad299 | 9 | * |
Yolandataniaa | 0:49e87dcad299 | 10 | * www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00102166.pdf |
Yolandataniaa | 0:49e87dcad299 | 11 | * www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00141306.pdf |
Yolandataniaa | 0:49e87dcad299 | 12 | * |
Yolandataniaa | 0:49e87dcad299 | 13 | * TIM1_CH1: AF1 @ PA_8, PE_9 |
Yolandataniaa | 0:49e87dcad299 | 14 | * TIM1_CH2: AF1 @ PA_9, PE_11 |
Yolandataniaa | 0:49e87dcad299 | 15 | * |
Yolandataniaa | 0:49e87dcad299 | 16 | * TIM2_CH1: AF1 @ PA_0, PA_5, PA_15, PB_8* *F446 only |
Yolandataniaa | 0:49e87dcad299 | 17 | * TIM2_CH2: AF1 @ PA_1, PB_3, PB_9* *F446 only |
Yolandataniaa | 0:49e87dcad299 | 18 | * |
Yolandataniaa | 0:49e87dcad299 | 19 | * TIM3_CH1: AF2 @ PA_6, PB_4, PC_6 |
Yolandataniaa | 0:49e87dcad299 | 20 | * TIM3_CH2: AF2 @ PA_7, PB_5, PC_7 |
Yolandataniaa | 0:49e87dcad299 | 21 | * |
Yolandataniaa | 0:49e87dcad299 | 22 | * TIM4_CH1: AF2 @ PB_6, PD_12 |
Yolandataniaa | 0:49e87dcad299 | 23 | * TIM4_CH2: AF2 @ PB_7, PD_13 |
Yolandataniaa | 0:49e87dcad299 | 24 | * |
Yolandataniaa | 0:49e87dcad299 | 25 | * TIM5_CH1: AF2 @ PA_0* *TIM5 used by mbed system ticker so unavailable |
Yolandataniaa | 0:49e87dcad299 | 26 | * TIM5_CH2: AF2 @ PA_1* |
Yolandataniaa | 0:49e87dcad299 | 27 | * |
Yolandataniaa | 0:49e87dcad299 | 28 | * TIM8_CH1: AF3 @ PC_6 |
Yolandataniaa | 0:49e87dcad299 | 29 | * TIM8_CH2: AF3 @ PC_7 |
Yolandataniaa | 0:49e87dcad299 | 30 | */ |
Yolandataniaa | 0:49e87dcad299 | 31 | |
Yolandataniaa | 0:49e87dcad299 | 32 | #ifdef TARGET_STM32F4 |
Yolandataniaa | 0:49e87dcad299 | 33 | void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim) |
Yolandataniaa | 0:49e87dcad299 | 34 | { |
Yolandataniaa | 0:49e87dcad299 | 35 | GPIO_InitTypeDef GPIO_InitStruct; |
Yolandataniaa | 0:49e87dcad299 | 36 | |
Yolandataniaa | 0:49e87dcad299 | 37 | if (htim->Instance == TIM1) { //PA8 PA9 = Nucleo D7 D8 |
Yolandataniaa | 0:49e87dcad299 | 38 | __TIM1_CLK_ENABLE(); |
Yolandataniaa | 0:49e87dcad299 | 39 | __GPIOA_CLK_ENABLE(); |
Yolandataniaa | 0:49e87dcad299 | 40 | GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; |
Yolandataniaa | 0:49e87dcad299 | 41 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
Yolandataniaa | 0:49e87dcad299 | 42 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
Yolandataniaa | 0:49e87dcad299 | 43 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
Yolandataniaa | 0:49e87dcad299 | 44 | GPIO_InitStruct.Alternate = GPIO_AF1_TIM1; |
Yolandataniaa | 0:49e87dcad299 | 45 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
Yolandataniaa | 0:49e87dcad299 | 46 | } |
Yolandataniaa | 0:49e87dcad299 | 47 | else if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1 //USANDO ALTERNO PB8 Y PB 9 para el pololulu funcione |
Yolandataniaa | 0:49e87dcad299 | 48 | __TIM2_CLK_ENABLE(); |
Yolandataniaa | 0:49e87dcad299 | 49 | __GPIOB_CLK_ENABLE(); |
Yolandataniaa | 0:49e87dcad299 | 50 | GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; |
Yolandataniaa | 0:49e87dcad299 | 51 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
Yolandataniaa | 0:49e87dcad299 | 52 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
Yolandataniaa | 0:49e87dcad299 | 53 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
Yolandataniaa | 0:49e87dcad299 | 54 | GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; |
Yolandataniaa | 0:49e87dcad299 | 55 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
Yolandataniaa | 0:49e87dcad299 | 56 | } |
Yolandataniaa | 0:49e87dcad299 | 57 | else if (htim->Instance == TIM3) { //PB4 PB5 = Nucleo D5 D4 |
Yolandataniaa | 0:49e87dcad299 | 58 | __TIM3_CLK_ENABLE(); |
Yolandataniaa | 0:49e87dcad299 | 59 | __GPIOB_CLK_ENABLE(); |
Yolandataniaa | 0:49e87dcad299 | 60 | GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5; |
Yolandataniaa | 0:49e87dcad299 | 61 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
Yolandataniaa | 0:49e87dcad299 | 62 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
Yolandataniaa | 0:49e87dcad299 | 63 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
Yolandataniaa | 0:49e87dcad299 | 64 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; |
Yolandataniaa | 0:49e87dcad299 | 65 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
Yolandataniaa | 0:49e87dcad299 | 66 | } |
Yolandataniaa | 0:49e87dcad299 | 67 | else if (htim->Instance == TIM4) { // PB6 PB7 = Nucleo D10 MORPHO_PB7 |
Yolandataniaa | 0:49e87dcad299 | 68 | __TIM4_CLK_ENABLE(); |
Yolandataniaa | 0:49e87dcad299 | 69 | __GPIOB_CLK_ENABLE(); |
Yolandataniaa | 0:49e87dcad299 | 70 | GPIO_InitStruct.Pin = GPIO_PIN_7 | GPIO_PIN_6; |
Yolandataniaa | 0:49e87dcad299 | 71 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
Yolandataniaa | 0:49e87dcad299 | 72 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
Yolandataniaa | 0:49e87dcad299 | 73 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
Yolandataniaa | 0:49e87dcad299 | 74 | GPIO_InitStruct.Alternate = GPIO_AF2_TIM4; |
Yolandataniaa | 0:49e87dcad299 | 75 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
Yolandataniaa | 0:49e87dcad299 | 76 | } |
Yolandataniaa | 0:49e87dcad299 | 77 | |
Yolandataniaa | 0:49e87dcad299 | 78 | |
Yolandataniaa | 0:49e87dcad299 | 79 | else if (htim->Instance == TIM8) { // PC7 PC8 = D9 PC6 |
Yolandataniaa | 0:49e87dcad299 | 80 | __TIM8_CLK_ENABLE(); |
Yolandataniaa | 0:49e87dcad299 | 81 | __GPIOC_CLK_ENABLE(); |
Yolandataniaa | 0:49e87dcad299 | 82 | GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; |
Yolandataniaa | 0:49e87dcad299 | 83 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
Yolandataniaa | 0:49e87dcad299 | 84 | GPIO_InitStruct.Pull = GPIO_PULLDOWN; |
Yolandataniaa | 0:49e87dcad299 | 85 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
Yolandataniaa | 0:49e87dcad299 | 86 | GPIO_InitStruct.Alternate = GPIO_AF3_TIM8; |
Yolandataniaa | 0:49e87dcad299 | 87 | HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); |
Yolandataniaa | 0:49e87dcad299 | 88 | } |
Yolandataniaa | 0:49e87dcad299 | 89 | |
Yolandataniaa | 0:49e87dcad299 | 90 | |
Yolandataniaa | 0:49e87dcad299 | 91 | |
Yolandataniaa | 0:49e87dcad299 | 92 | |
Yolandataniaa | 0:49e87dcad299 | 93 | |
Yolandataniaa | 0:49e87dcad299 | 94 | |
Yolandataniaa | 0:49e87dcad299 | 95 | } |
Yolandataniaa | 0:49e87dcad299 | 96 | #endif |