NUCLEO-F446RE ENCODER TIMER3 Y TIMER 2 HAL AND PWM. PID PARA UN PENDULO INVERTIDO CON 2 ENCODER DE CUADRATURA

Dependencies:   mbed-dev

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EncoderMspInitF4.cpp Source File

EncoderMspInitF4.cpp

00001 #include "mbed.h"
00002 /*
00003  * HAL_TIM_Encoder_MspInit()
00004  * Overrides the __weak function stub in stm32f4xx_hal_tim.h
00005  *
00006  * Edit the below for your preferred pin wiring & pullup/down
00007  * I have encoder common at 3V3, using GPIO_PULLDOWN on inputs.
00008  * Encoder A&B outputs connected directly to GPIOs.
00009  *
00010  * www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00102166.pdf
00011  * www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00141306.pdf
00012  *
00013  * TIM1_CH1: AF1 @ PA_8, PE_9 
00014  * TIM1_CH2: AF1 @ PA_9, PE_11 
00015  *
00016  * TIM2_CH1: AF1 @ PA_0, PA_5, PA_15, PB_8*     *F446 only
00017  * TIM2_CH2: AF1 @ PA_1, PB_3, PB_9*            *F446 only
00018  *
00019  * TIM3_CH1: AF2 @ PA_6, PB_4, PC_6
00020  * TIM3_CH2: AF2 @ PA_7, PB_5, PC_7
00021  *
00022  * TIM4_CH1: AF2 @ PB_6, PD_12
00023  * TIM4_CH2: AF2 @ PB_7, PD_13
00024  *
00025  * TIM5_CH1: AF2 @ PA_0*    *TIM5 used by mbed system ticker so unavailable
00026  * TIM5_CH2: AF2 @ PA_1*
00027  *
00028  *
00029  * Agrego el TIMER 8 tambien funciona 
00030  * TIM8_CH1: AF3 @ PC_6
00031  * TIM8_CH2: AF3 @ PC_7 
00032  *
00033  *
00034  *
00035  *
00036  */
00037 
00038 #ifdef TARGET_STM32F4
00039 void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim)
00040 {
00041     GPIO_InitTypeDef GPIO_InitStruct;
00042 
00043     if (htim->Instance == TIM1) { //PA8 PA9 = Nucleo D7 D8
00044         __TIM1_CLK_ENABLE();
00045         __GPIOA_CLK_ENABLE();
00046         GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
00047         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00048         GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00049         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00050         GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
00051         HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
00052     }
00053     else if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1 //USANDO ALTERNO PB8 Y PB 9 para el pololulu funcione
00054         __TIM2_CLK_ENABLE();
00055         __GPIOB_CLK_ENABLE();
00056         GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
00057         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00058         GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00059         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00060         GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
00061         HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
00062     }
00063     else if (htim->Instance == TIM3) { //PB4 PB5 = Nucleo D5 D4
00064         __TIM3_CLK_ENABLE();
00065         __GPIOB_CLK_ENABLE();
00066         GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5;
00067         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00068         GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00069         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00070         GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
00071         HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
00072     }
00073     else if (htim->Instance == TIM4) { // PB6 PB7 = Nucleo D10 MORPHO_PB7
00074         __TIM4_CLK_ENABLE();
00075         __GPIOB_CLK_ENABLE();
00076         GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
00077         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00078         GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00079         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00080         GPIO_InitStruct.Alternate = GPIO_AF2_TIM4;
00081         HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
00082     }
00083      
00084           
00085      else if (htim->Instance == TIM8) { // PC7 PC8 =  D9  PC6
00086         __TIM8_CLK_ENABLE();
00087         __GPIOC_CLK_ENABLE();
00088         GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
00089         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
00090         GPIO_InitStruct.Pull = GPIO_PULLDOWN;
00091         GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
00092         GPIO_InitStruct.Alternate = GPIO_AF3_TIM8;
00093         HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
00094     }    
00095     
00096     
00097     
00098     
00099     
00100     
00101 }
00102 #endif