Added encoder operation via timer
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#ifndef DS_ENCODER_H
|
||||
#define DS_ENCODER_H
|
||||
#ifndef ENCODER_H
|
||||
#define ENCODER_H
|
||||
|
||||
#ifdef STM32G030xx
|
||||
#include "stm32g0xx_hal.h"
|
||||
@@ -18,8 +18,10 @@ typedef struct DS_Encoder
|
||||
uint32_t PrevTick;
|
||||
|
||||
uint8_t ValueSignal1, ValueSignal2;
|
||||
TIM_HandleTypeDef *hTim;
|
||||
}DS_Encoder;
|
||||
|
||||
void DS_EncoderInitTim(DS_Encoder *Encoder, TIM_HandleTypeDef *hTim);
|
||||
|
||||
void DS_EncoderInit(DS_Encoder *Encoder, GPIO_TypeDef* PortEncSignal1, uint16_t PinEncSignal1, GPIO_TypeDef* PortEncSignal2, uint16_t PinEncSignal2);
|
||||
|
||||
@@ -27,4 +29,4 @@ void DS_EncoderUpdate(DS_Encoder *Encoder);
|
||||
|
||||
int8_t DS_EncoderGetState(DS_Encoder *Encoder);
|
||||
|
||||
#endif //DS_ENCODER_H
|
||||
#endif //ENCODER_H
|
||||
@@ -1,7 +1,16 @@
|
||||
#include "Encoder.h"
|
||||
|
||||
void DS_EncoderInitTim(DS_Encoder *Encoder, TIM_HandleTypeDef *hTim)
|
||||
{
|
||||
Encoder->hTim = hTim;
|
||||
Encoder->State = 0;
|
||||
HAL_TIM_Base_Start(hTim);
|
||||
|
||||
}
|
||||
|
||||
void DS_EncoderInit(DS_Encoder *Encoder, GPIO_TypeDef *PortEncSignal1, uint16_t PinEncSignal1, GPIO_TypeDef *PortEncSignal2, uint16_t PinEncSignal2)
|
||||
{
|
||||
Encoder->hTim = 0;
|
||||
Encoder->Direction = 0;
|
||||
Encoder->State = 0;
|
||||
Encoder->PrevTick = HAL_GetTick();
|
||||
@@ -69,7 +78,21 @@ void DS_EncoderUpdate(DS_Encoder *Encoder)
|
||||
|
||||
int8_t DS_EncoderGetState(DS_Encoder *Encoder)
|
||||
{
|
||||
int8_t ValueToReturn = Encoder->State;
|
||||
Encoder->State = 0;
|
||||
return ValueToReturn;
|
||||
if (Encoder->hTim != 0)
|
||||
{
|
||||
int8_t CurrentCounter = Encoder->hTim->Instance->CNT / 4;
|
||||
int8_t ValueToReturn = CurrentCounter - Encoder->State;
|
||||
Encoder->State = CurrentCounter;
|
||||
|
||||
return ValueToReturn;
|
||||
}
|
||||
else
|
||||
{
|
||||
int8_t ValueToReturn = Encoder->State;
|
||||
Encoder->State = 0;
|
||||
return ValueToReturn;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user