HW: Simple rotary encoder debouncing
This commit is contained in:
parent
af0f1b5441
commit
4f35324bc5
@ -7,6 +7,6 @@
|
|||||||
|
|
||||||
// Auto generated header file containing the last git revision
|
// Auto generated header file containing the last git revision
|
||||||
|
|
||||||
#define GIT_HASH "d9957b3"
|
#define GIT_HASH "6c775da"
|
||||||
|
|
||||||
#endif //FLOATPUMP_GIT_REVISION_TEMPLATE_H
|
#endif //FLOATPUMP_GIT_REVISION_TEMPLATE_H
|
@ -4,48 +4,27 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "button_input.h"
|
#include "button_input.h"
|
||||||
|
|
||||||
bool INP_button_state;
|
bool rot_button = false;
|
||||||
|
uint16_t rot_counter = 0;
|
||||||
int8_t last;
|
|
||||||
int8_t enc_delta;
|
|
||||||
int16_t rot_value;
|
|
||||||
int8_t val;
|
|
||||||
|
|
||||||
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
|
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
|
||||||
if(HAL_GPIO_ReadPin(RRSW_GPIO_Port, RRSW_Pin) == GPIO_PIN_SET) {
|
static uint16_t rot_state = 0;
|
||||||
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
|
static uint16_t btn_state = 0;
|
||||||
INP_button_state = false;
|
|
||||||
} else {
|
rot_state = (rot_state << 1) | (HAL_GPIO_ReadPin(RRCLK_GPIO_Port, RRCLK_Pin) == GPIO_PIN_SET ? 0x1 : 0x0) | 0xe000;
|
||||||
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
|
|
||||||
INP_button_state = true;
|
btn_state = (btn_state << 1) | (HAL_GPIO_ReadPin(RRSW_GPIO_Port, RRSW_Pin) == GPIO_PIN_SET ? 0x1 : 0x0) | 0xe000;
|
||||||
|
|
||||||
|
if (rot_state == 0xf000) {
|
||||||
|
rot_state = 0x0000;
|
||||||
|
if (HAL_GPIO_ReadPin(RRDT_GPIO_Port, RRDT_Pin) == GPIO_PIN_SET) {
|
||||||
|
rot_counter++;
|
||||||
|
} else {
|
||||||
|
rot_counter--;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// int8_t new = 0;
|
if (btn_state == 0xf000) {
|
||||||
//
|
rot_button = true;
|
||||||
// if (HAL_GPIO_ReadPin(RRCLK_GPIO_Port, RRCLK_Pin) == GPIO_PIN_RESET)
|
|
||||||
// new = 1;
|
|
||||||
//
|
|
||||||
// if (new != last) {
|
|
||||||
// if(HAL_GPIO_ReadPin(RRDT_GPIO_Port, RRDT_Pin) == GPIO_PIN_RESET) {
|
|
||||||
// val++;
|
|
||||||
// } else {
|
|
||||||
// val --;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// last = new;
|
|
||||||
|
|
||||||
int8_t new = 0;
|
|
||||||
if (HAL_GPIO_ReadPin(RRCLK_GPIO_Port, RRCLK_Pin) == GPIO_PIN_SET) new = 3;
|
|
||||||
if (HAL_GPIO_ReadPin(RRDT_GPIO_Port, RRDT_Pin) == GPIO_PIN_SET) new ^= 1;
|
|
||||||
|
|
||||||
int8_t diff = last - new;
|
|
||||||
if( diff & 1) {
|
|
||||||
last = new;
|
|
||||||
enc_delta += (diff & 2) -1;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
val = enc_delta;
|
|
||||||
enc_delta = val & 3; val >> 2;
|
|
||||||
}
|
|
||||||
|
|
@ -17,16 +17,21 @@ TIM_HandleTypeDef htim2;
|
|||||||
UART_HandleTypeDef huart1;
|
UART_HandleTypeDef huart1;
|
||||||
|
|
||||||
void SystemClock_Config(void);
|
void SystemClock_Config(void);
|
||||||
|
|
||||||
static void MX_GPIO_Init(void);
|
static void MX_GPIO_Init(void);
|
||||||
|
|
||||||
static void MX_RTC_Init(void);
|
static void MX_RTC_Init(void);
|
||||||
|
|
||||||
static void MX_ADC1_Init(void);
|
static void MX_ADC1_Init(void);
|
||||||
|
|
||||||
static void MX_I2C1_Init(void);
|
static void MX_I2C1_Init(void);
|
||||||
|
|
||||||
static void MX_TIM2_Init(void);
|
static void MX_TIM2_Init(void);
|
||||||
|
|
||||||
static void MX_USART1_UART_Init(void);
|
static void MX_USART1_UART_Init(void);
|
||||||
|
|
||||||
extern int8_t enc_delta;
|
extern int16_t rot_counter;
|
||||||
extern int16_t rot_value;
|
extern bool rot_button;
|
||||||
extern int8_t val;
|
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
// Step 1: Initialize HAL
|
// Step 1: Initialize HAL
|
||||||
@ -63,23 +68,33 @@ int main(void) {
|
|||||||
// HAL_Delay(1000);
|
// HAL_Delay(1000);
|
||||||
// display.LCDSetCursor(18, 3);
|
// display.LCDSetCursor(18, 3);
|
||||||
|
|
||||||
floatpump::LCD_I2C_Driver &display = floatpump::LCD_I2C_Driver::getInstance(hi2c1, SLAVE_ADDRESS_LCD);
|
//Disable Interrupt for Debouncing timer during display initalisation (exact timings are necessary)
|
||||||
floatpump::InitSequence initializer(display);
|
HAL_NVIC_DisableIRQ(TIM1_CC_IRQn);
|
||||||
initializer.runInitSequence();
|
floatpump::LCD_I2C_Driver &display = floatpump::LCD_I2C_Driver::getInstance(hi2c1, SLAVE_ADDRESS_LCD);
|
||||||
|
HAL_NVIC_EnableIRQ(TIM1_CC_IRQn);
|
||||||
|
|
||||||
|
floatpump::InitSequence initializer(display);
|
||||||
|
initializer.runInitSequence();
|
||||||
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
display.LCDSetCursor(0, 0);
|
||||||
|
char stri[20];
|
||||||
|
sprintf(stri, "DT: %03d", rot_counter);
|
||||||
|
display.LCDSendCString(stri);
|
||||||
|
|
||||||
rot_value += val;
|
if (rot_button && rot_counter == 1) {
|
||||||
display.LCDSetCursor(0,0);
|
HAL_GPIO_TogglePin(OCHAN0_GPIO_Port, OCHAN0_Pin);
|
||||||
char stri[20];
|
rot_button = false;
|
||||||
sprintf(stri, "DT: %d", val);
|
} else if (rot_button && rot_counter == 2) {
|
||||||
display.LCDSendCString(stri);
|
HAL_GPIO_TogglePin(OCHAN1_GPIO_Port, OCHAN1_Pin);
|
||||||
|
rot_button = false;
|
||||||
|
}
|
||||||
|
|
||||||
HAL_Delay(100);
|
HAL_Delay(100);
|
||||||
|
|
||||||
HAL_GPIO_TogglePin(OCHAN0_GPIO_Port, OCHAN0_Pin);
|
// HAL_GPIO_TogglePin(OCHAN0_GPIO_Port, OCHAN0_Pin);
|
||||||
HAL_Delay(2000);
|
// HAL_Delay(2000);
|
||||||
|
|
||||||
// HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_0);
|
// HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_0);
|
||||||
// HAL_Delay(1000);
|
// HAL_Delay(1000);
|
||||||
@ -88,12 +103,12 @@ int main(void) {
|
|||||||
// HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_2);
|
// HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_2);
|
||||||
// HAL_Delay(1000);
|
// HAL_Delay(1000);
|
||||||
//
|
//
|
||||||
HAL_GPIO_TogglePin(LED3_GPIO_Port, LED3_Pin);
|
// HAL_GPIO_TogglePin(LED3_GPIO_Port, LED3_Pin);
|
||||||
HAL_Delay(1000);
|
// HAL_Delay(1000);
|
||||||
HAL_GPIO_TogglePin(LED4_GPIO_Port, LED4_Pin);
|
// HAL_GPIO_TogglePin(LED4_GPIO_Port, LED4_Pin);
|
||||||
HAL_Delay(1000);
|
// HAL_Delay(1000);
|
||||||
HAL_GPIO_TogglePin(LED5_GPIO_Port, LED5_Pin);
|
// HAL_GPIO_TogglePin(LED5_GPIO_Port, LED5_Pin);
|
||||||
HAL_Delay(1000);
|
// HAL_Delay(1000);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -268,41 +283,41 @@ static void MX_RTC_Init(void) {
|
|||||||
* @param None
|
* @param None
|
||||||
* @retval None
|
* @retval None
|
||||||
*/
|
*/
|
||||||
static void MX_TIM2_Init(void)
|
static void MX_TIM2_Init(void) {
|
||||||
{
|
//Timer has input of 24MHZ
|
||||||
|
//Scale down with prescaler to 10kHz
|
||||||
|
//Auto reload each ms
|
||||||
|
|
||||||
/* USER CODE BEGIN TIM2_Init 0 */
|
/* USER CODE BEGIN TIM2_Init 0 */
|
||||||
|
|
||||||
/* USER CODE END TIM2_Init 0 */
|
/* USER CODE END TIM2_Init 0 */
|
||||||
|
|
||||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||||
|
|
||||||
/* USER CODE BEGIN TIM2_Init 1 */
|
/* USER CODE BEGIN TIM2_Init 1 */
|
||||||
|
|
||||||
/* USER CODE END TIM2_Init 1 */
|
/* USER CODE END TIM2_Init 1 */
|
||||||
htim2.Instance = TIM2;
|
htim2.Instance = TIM2;
|
||||||
htim2.Init.Prescaler = 2399;
|
htim2.Init.Prescaler = 23;
|
||||||
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
|
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||||
htim2.Init.Period = 9;
|
htim2.Init.Period = 99;
|
||||||
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||||
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
|
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
|
||||||
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
|
if (HAL_TIM_Base_Init(&htim2) != HAL_OK) {
|
||||||
{
|
Error_Handler();
|
||||||
Error_Handler();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||||
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
|
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) {
|
||||||
{
|
Error_Handler();
|
||||||
Error_Handler();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
//HAL_TIM_Base_Start(&htim2);
|
//HAL_TIM_Base_Start(&htim2);
|
||||||
//Directly start timer base generation in interrupt mode
|
//Directly start timer base generation in interrupt mode
|
||||||
HAL_TIM_Base_Start_IT(&htim2);
|
HAL_TIM_Base_Start_IT(&htim2);
|
||||||
/* USER CODE BEGIN TIM2_Init 2 */
|
/* USER CODE BEGIN TIM2_Init 2 */
|
||||||
|
|
||||||
/* USER CODE END TIM2_Init 2 */
|
/* USER CODE END TIM2_Init 2 */
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -311,8 +326,7 @@ static void MX_TIM2_Init(void)
|
|||||||
* @param None
|
* @param None
|
||||||
* @retval None
|
* @retval None
|
||||||
*/
|
*/
|
||||||
static void MX_USART1_UART_Init(void)
|
static void MX_USART1_UART_Init(void) {
|
||||||
{
|
|
||||||
|
|
||||||
/* USER CODE BEGIN USART1_Init 0 */
|
/* USER CODE BEGIN USART1_Init 0 */
|
||||||
|
|
||||||
@ -329,8 +343,7 @@ static void MX_USART1_UART_Init(void)
|
|||||||
huart1.Init.Mode = UART_MODE_TX_RX;
|
huart1.Init.Mode = UART_MODE_TX_RX;
|
||||||
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
||||||
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
||||||
if (HAL_UART_Init(&huart1) != HAL_OK)
|
if (HAL_UART_Init(&huart1) != HAL_OK) {
|
||||||
{
|
|
||||||
Error_Handler();
|
Error_Handler();
|
||||||
}
|
}
|
||||||
/* USER CODE BEGIN USART1_Init 2 */
|
/* USER CODE BEGIN USART1_Init 2 */
|
||||||
@ -362,7 +375,7 @@ static void MX_GPIO_Init(void) {
|
|||||||
| LED1_Pin, GPIO_PIN_SET);
|
| LED1_Pin, GPIO_PIN_SET);
|
||||||
|
|
||||||
HAL_GPIO_WritePin(GPIOB,
|
HAL_GPIO_WritePin(GPIOB,
|
||||||
MPWR0_Pin | MPWR1_Pin | MPWR2_Pin, GPIO_PIN_RESET);
|
MPWR0_Pin | MPWR1_Pin | MPWR2_Pin, GPIO_PIN_RESET);
|
||||||
|
|
||||||
/*Configure GPIO pin : XXUNUSED_Pin */
|
/*Configure GPIO pin : XXUNUSED_Pin */
|
||||||
GPIO_InitStruct.Pin = XXUNUSED_Pin;
|
GPIO_InitStruct.Pin = XXUNUSED_Pin;
|
||||||
|
Loading…
Reference in New Issue
Block a user