21/3 21/3/2 /201 015 5
Conne Connect ct a rotary rotary enco encode derr to STM32F4 STM32F4.. Needs Needs time time base base initializa initializatio tion n on TIM4 but but not not on TIM8, TIM8, no idea idea why
nraynaud / tim4.c Last active on 5 Mar, 2013
Connect Co nnect a rotary encoder to STM32F4. Needs time base initialization initialization on TIM4 but not on TIM8, no idea why
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
tim4.c
#include "stm32f4_discovery.h" #include "arm_math.h"
/* ‐ TIM4_CH1 pin (PB.06) ‐ TIM4_CH2 pin (PB.07) */ void configureEncoder () { RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM4, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6| GPIO_Pin_7; GPIO_Init(GPIOB, GPIO_Init (GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig (GPIOB, GPIO_PinSource6, GPIO_AF_TIM4) ; GPIO_PinAFConfig (GPIOB, GPIO_PinSource7, GPIO_AF_TIM4) ; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructure.TIM_Period = 0xffff; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_Time _TimeBaseStructure.TIM_ClockDivision BaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit (TIM4, &TIM_TimeBaseStructure); /* Configure the timer */ TIM_EncoderInterfaceConfig (TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); /* TIM4 counter enable */ TIM_Cmd(TIM4, TIM_Cmd (TIM4, ENABLE); } // TIM4 is used for encoder int main main( (void void) ) { //enable FPU SCB‐>CPACR |= ((3UL << 10 10* *2)|( )|(3UL 3UL << 11 11* *2)); STM_EVAL_LEDInit (LED3); STM_EVAL_LEDInit (LED4); STM_EVAL_LEDInit (LED5); STM_EVAL_LEDInit (LED6); STM_EVAL_LEDOff (LED3); STM_EVAL_LEDOff (LED4); STM_EVAL_LEDOff (LED5); STM_EVAL_LEDOff (LED6); configureEncoder (); STM_EVAL_LEDOn (LED3); while (1) { } } int32_t rT2 rT2() () { return TIM_GetCounter (TIM4); }
https://g i st.g i thub.com/nr aynaud/5082298
1/2
21/3/2015
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
Connect a rotary encoder to STM32F4. Needs time base initialization on TIM4 but not on TIM8, no idea why
tim8.c
#include "stm32f4_discovery.h" #include "arm_math.h"
/* ‐ TIM8_CH1 pin (PC.06) ‐ TIM8_CH2 pin (PC.07) */ void configureEncoder () { RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd (RCC_APB2Periph_TIM8, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6| GPIO_Pin_7; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_PinAFConfig (GPIOC, GPIO_PinSource6, GPIO_AF_TIM8); GPIO_PinAFConfig (GPIOC, GPIO_PinSource7, GPIO_AF_TIM8); /* Configure the timer */ TIM_EncoderInterfaceConfig (TIM8, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); /* TIM8 counter enable */ TIM_Cmd(TIM8, ENABLE); } // TIM8 is used for encoder int main(void) { //enable FPU SCB‐>CPACR |= ((3UL << 10*2)|(3UL << 11*2)); STM_EVAL_LEDInit (LED3); STM_EVAL_LEDInit (LED4); STM_EVAL_LEDInit (LED5); STM_EVAL_LEDInit (LED6); STM_EVAL_LEDOff (LED3); STM_EVAL_LEDOff (LED4); STM_EVAL_LEDOff (LED5); STM_EVAL_LEDOff (LED6); configureEncoder (); STM_EVAL_LEDOn (LED3); while (1) { } } int32_t rT2() { return TIM_GetCounter (TIM8); }
Sign up for free (https://github.com/signup?return_to=gist)
to join this conversation on
GitHub. Already have an account? Sign in to comment (https://gist.github.com/login? return_to=%2Fnraynaud%2F5082298)
(/) © 2015 GitHub, Inc.
Status (https://status.github.com/) API (http://developer.github.com ) Terms (https://github.com/site/terms)
https://gist.github.com/nraynaud/5082298
Privacy (https://github.com/s ite/privacy)
Blog (https://github.com/blog) About (https://github.com/about)
Security (https://github.com/security)
Contact (https://github.com/contact)
2/2