You are on page 1of 2

21/3/2015

ConnectarotaryencodertoSTM32F4.NeedstimebaseinitializationonTIM4butnotonTIM8,noideawhy

nraynaud/tim4.c
Lastactiveon5Mar,2013

ConnectarotaryencodertoSTM32F4.NeedstimebaseinitializationonTIM4butnotonTIM8,noideawhy
tim4.c
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

#include"stm32f4_discovery.h"
#include"arm_math.h"

/*
TIM4_CH1pin(PB.06)
TIM4_CH2pin(PB.07)
*/
voidconfigureEncoder(){
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);

GPIO_InitTypeDefGPIO_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_TimeBaseInitTypeDefTIM_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);

/*Configurethetimer*/
TIM_EncoderInterfaceConfig(TIM4,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);

/*TIM4counterenable*/
TIM_Cmd(TIM4,ENABLE);
}

//TIM4isusedforencoder
intmain(void)
{
//enableFPU
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_trT2(){
returnTIM_GetCounter(TIM4);
}

https://gist.github.com/nraynaud/5082298

1/2

21/3/2015

ConnectarotaryencodertoSTM32F4.NeedstimebaseinitializationonTIM4butnotonTIM8,noideawhy

tim8.c
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

#include"stm32f4_discovery.h"
#include"arm_math.h"

/*
TIM8_CH1pin(PC.06)
TIM8_CH2pin(PC.07)
*/
voidconfigureEncoder(){
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8,ENABLE);

GPIO_InitTypeDefGPIO_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);

/*Configurethetimer*/
TIM_EncoderInterfaceConfig(TIM8,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);

/*TIM8counterenable*/
TIM_Cmd(TIM8,ENABLE);
}

//TIM8isusedforencoder
intmain(void)
{
//enableFPU
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_trT2(){
returnTIM_GetCounter(TIM8);
}

Signupforfree(https://github.com/signup?return_to=gist)

tojointhisconversationon

GitHub.Alreadyhaveanaccount?Signintocomment(https://gist.github.com/login?
return_to=%2Fnraynaud%2F5082298)

(/)
Status(https://status.github.com/) API(http://developer.github.com) Blog(https://github.com/blog) About(https://github.com/about)
2015GitHub,Inc. Terms(https://github.com/site/terms) Privacy(https://github.com/site/privacy) Security(https://github.com/security) Contact(https://github.com/contact)

https://gist.github.com/nraynaud/5082298

2/2

You might also like