Last active
December 24, 2019 14:51
-
-
Save nraynaud/5082298 to your computer and use it in GitHub Desktop.
Connect a rotary encoder to STM32F4.
Needs time base initialization on TIM4 but not on TIM8, no idea why
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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_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_TimeBaseStructure.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, ENABLE); | |
} | |
// TIM4 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(TIM4); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for your great efforts.
please i want to ask you about this function
rT2() what is it??