[C] 纯文本查看 复制代码 /*
*********************************************************************************************************
*
* module name : Encoder driver
* file name : bsp_encoder.c
* version : V1.0
* Copyright (C), [email]tianmaduxingliulei@outlook.com[/email]
* [url]https://www.chuandong.com/tech/tech30155.html[/url] FYI
*********************************************************************************************************
*/
#include "bsp.h"
#define GET_STATE_A_1 GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7)
#define GET_STATE_A_2 GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8)
#define GET_STATE_B_1 GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_15)
#define GET_STATE_B_2 GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_3)
static uint32_t throttleA = 0;
static boolean preStateA = FALSE;
static boolean gotAthrottleState = TRUE;
static uint32_t limitThrottleA;
static uint32_t throttleB = 0;
static boolean preStateB = FALSE;
void encoderInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOB, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOC, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void encoderLoopA(void)
{
if (preStateA && (!GET_STATE_A_1))
{
gotAthrottleState = FALSE;
if (GET_STATE_A_2)
{
if (throttleA >= 1)
throttleA -= 1;
}
else {
if (throttleA <= 200 && throttleA < limitThrottleA)
throttleA += 1;
}
}
preStateA = GET_STATE_A_1;
}
void encoderLoopB(void)
{
if (preStateB && (!GET_STATE_B_1))
{
if (GET_STATE_B_2)
{
if (throttleB >= 1)
throttleB -= 1;
}
else
{
if (throttleB <= 200)
throttleB += 1;
}
}
preStateB = !!GET_STATE_B_1;
}
boolean encoderGetA(uint32_t * getValue)
{
boolean ret;
*getValue = throttleA;
ret = gotAthrottleState;
gotAthrottleState = TRUE;
return ret;
}
void encoderClear(void)
{
throttleA = 0;
}
void encoderSetLimitA(uint32_t value)
{
limitThrottleA = value;
} |