Процедура инициализации таймера для работы с энкодером
InitTIM2(it2Mode: Byte; it2Edge1, it2Edge2: Byte; it2ARV : Word);
Параметры, которые необходимо передать при вызове процедуры:
- it2Mode
- 0 - прямой счет, используется первый канал
- 1 - прямой счет, используется второй канал
- 2 - режим енкодера, счет по обоим каналам
- 3 - режим энкодера, счет по первому каналу, направление по второму каналу
- 4 - режим энкодера, счет по второму каналу, направление по первому каналу
- it2Edge1 - выбор фронта для первого канала. 0 - передний фронт, 1 - задний фронт
- it2Edge2 - выбор фронта для второго канала. 0 - передний фронт, 1 - задний фронт
- it2AVR - значение переполнения счетчика, 0 - 65535
Сама процедура для копипасты.
Procedure InitTIM2(it2Mode: Byte; it2Edge1, it2Edge2: Byte; it2ARV : Word);
Begin
{ Mode 0 - Encoder mode, use input 1 only
Mode 1 - Encoder mode, use input 2 only
Mode 2 - Encoder mode, use both channels
Mode 3 - Direct counting, use input 1
Mode 4 - Direct counting, use input 2
see below TIM2_CR1.DIR_ for count direction
Edge1 - Rising (0) or falling edge (1) detection for input 1
Edge2 - Rising (0) or falling edge (1) detection for input 2
ARV - Auto reload value for the Timer }
GPIO_Alternate_Function_Enable(@_GPIO_MODULE_TIM2_CH1_PA15);
GPIO_Alternate_Function_Enable(@_GPIO_MODULE_TIM2_CH2_PB3);
GPIO_Set_Pin_Mode(@GPIOA_BASE, _GPIO_PIN_15, _GPIO_CFG_MODE_ALT_FUNCTION);
GPIO_Set_Pin_Mode(@GPIOB_BASE, _GPIO_PIN_3, _GPIO_CFG_MODE_ALT_FUNCTION);
RCC_APB1ENR.TIM2EN := 1; {Enable switching the Timer2}
TIM2_CR1.CEN := 0; {TIM2 disable}
TIM2_CCER.CC1E := 0; {Capture 1 disable}
TIM2_CCER.CC2E := 0; {Capture 2 disable}
{
TIM2_CCER.CC1P := 1; //Rising Edge (0) Falling Edge (1)
TIM2_CCER.CC2P := 1; //Rising Edge (0) Falling Edge (1)
}
If it2Edge1 > 0 then TIM2_CCER.CC1P:= 1 else TIM2_CCER.CC1P:= 0;
If it2Edge2 > 0 then TIM2_CCER.CC2P:= 1 else TIM2_CCER.CC2P:= 0;
TIM2_CR1.DIR_ := 0; {Direction 0 - up, 1- down}
TIM2_CCMR1_Input.CC1S0 := 1; {01: CC1 channel is configured as input, IC1 is mapped on TI1}
TIM2_CCMR1_Input.CC1S1 := 0; {01: CC1 channel is configured as input, IC1 is mapped on TI1}
TIM2_CCMR1_Input.CC2S0 := 1; {01: CC2 channel is configured as input, IC2 is mapped on TI2}
TIM2_CCMR1_Input.CC2S0 := 0; {01: CC2 channel is configured as input, IC2 is mapped on TI2}
{
TIM2_SMCR.TS0 := 0; // Trigger selection (If SMS = %111) - external clock
TIM2_SMCR.TS1 := 1; // 101: Filtered Timer Input 1 (TI1FP1)
TIM2_SMCR.TS2 := 1; // 110: Filtered Timer Input 2 (TI2FP2)
TIM2_SMCR.SMS0 := 1; // 001: Encoder mode 1 - Counter counts up/down on TI2FP1 edge depending on TI1FP2l evel.
TIM2_SMCR.SMS1 := 1; // 010: Encoder mode 2 - Counter counts up/down on TI1FP2 edge depending on TI2FP1 level.
TIM2_SMCR.SMS2 := 0; // 011: Encoder mode 3 - Counter counts up/down on both TI1FP1 and TI2FP2 edges
// depending on the level of the other input.
}
Case it2Mode of
0 : begin
TIM2_SMCR.SMS0:= 0;
TIM2_SMCR.SMS1:= 1;
TIM2_SMCR.SMS2:= 0;
end;
1 : begin
TIM2_SMCR.SMS0:= 1;
TIM2_SMCR.SMS1:= 0;
TIM2_SMCR.SMS2:= 0;
end;
2 : begin
TIM2_SMCR.SMS0:= 1;
TIM2_SMCR.SMS1:= 1;
TIM2_SMCR.SMS2:= 0;
end;
3 : begin
TIM2_SMCR.SMS0:= 1;
TIM2_SMCR.SMS1:= 1;
TIM2_SMCR.SMS2:= 1;
TIM2_SMCR.TS0 := 1;
TIM2_SMCR.TS1 := 0;
TIM2_SMCR.TS2 := 1;
end;
4 : begin
TIM2_SMCR.SMS0:= 1;
TIM2_SMCR.SMS1:= 1;
TIM2_SMCR.SMS2:= 1;
TIM2_SMCR.TS0 := 0;
TIM2_SMCR.TS1 := 1;
TIM2_SMCR.TS2 := 1;
end;
end; {Case}
TIM2_ARR := it2ARV; {Value for auto reload the counter}
TIM2_CCMR1_Input.IC1F0 := 1; {Digital filter 00 - %1111}
TIM2_CCMR1_Input.IC1F1 := 1; {Digital filter 00 - %1111}
TIM2_CCMR1_Input.IC1F2 := 1; {Digital filter 00 - %1111}
TIM2_CCMR1_Input.IC1F3 := 1; {Digital filter 00 - %1111}
TIM2_CCMR1_Input.IC2F0 := 1; {Digital filter 00 - %1111}
TIM2_CCMR1_Input.IC2F1 := 1; {Digital filter 00 - %1111}
TIM2_CCMR1_Input.IC2F2 := 1; {Digital filter 00 - %1111}
TIM2_CCMR1_Input.IC2F3 := 1; {Digital filter 00 - %1111}
TIM2_CCER.CC1E := 1; {Capture enable}
TIM2_CCER.CC2E := 1; {Capture enable}
TIM2_CR1.CEN := 1; {TIM2 enable}
End;
Комментарии
Отправить комментарий