EXP: configure timers for rotary encoder input

This commit is contained in:
Robin Dietzel 2022-12-16 16:51:54 +01:00
parent 370da128e9
commit 97191eef00
9 changed files with 195 additions and 58 deletions

10
Core/Inc/button_input.h Normal file
View File

@ -0,0 +1,10 @@
//
// Created by robtor on 16.12.22.
//
#ifndef FLOATPUMP_BUTTON_INPUT_H
#define FLOATPUMP_BUTTON_INPUT_H
#include <stdbool.h>
#endif //FLOATPUMP_BUTTON_INPUT_H

View File

@ -7,6 +7,6 @@
// Auto generated header file containing the last git revision
#define GIT_HASH "4f8cf4c"
#define GIT_HASH "3acf911"
#endif //FLOATPUMP_GIT_REVISION_TEMPLATE_H

View File

@ -27,6 +27,7 @@ extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include <stdbool.h>
#include "stm32f4xx_hal.h"
/* Private includes ----------------------------------------------------------*/

View File

@ -63,7 +63,7 @@
/* #define HAL_SD_MODULE_ENABLED */
/* #define HAL_MMC_MODULE_ENABLED */
/* #define HAL_SPI_MODULE_ENABLED */
/* #define HAL_TIM_MODULE_ENABLED */
#define HAL_TIM_MODULE_ENABLED
#define HAL_UART_MODULE_ENABLED
/* #define HAL_USART_MODULE_ENABLED */
/* #define HAL_IRDA_MODULE_ENABLED */

View File

@ -55,7 +55,7 @@ void SVC_Handler(void);
void DebugMon_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
void EXTI4_IRQHandler(void);
void TIM2_IRQHandler(void);
void OTG_FS_IRQHandler(void);
/* USER CODE BEGIN EFP */

51
Core/Src/button_input.c Normal file
View File

@ -0,0 +1,51 @@
//
// Created by robtor on 16.12.22.
//
#include "main.h"
#include "button_input.h"
bool INP_button_state;
int8_t last;
int8_t enc_delta;
int16_t rot_value;
int8_t val;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if(HAL_GPIO_ReadPin(RRSW_GPIO_Port, RRSW_Pin) == GPIO_PIN_SET) {
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
INP_button_state = false;
} else {
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
INP_button_state = true;
}
// int8_t new = 0;
//
// 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;
}

View File

@ -5,12 +5,15 @@
#include "LCD_I2C_Driver.h"
#include "InitSequence.h"
#include "button_input.h"
#define SLAVE_ADDRESS_LCD 0x4e
ADC_HandleTypeDef hadc1;
I2C_HandleTypeDef hi2c1;
RTC_HandleTypeDef hrtc;
TIM_HandleTypeDef htim2;
UART_HandleTypeDef huart1;
void SystemClock_Config(void);
@ -18,8 +21,13 @@ static void MX_GPIO_Init(void);
static void MX_RTC_Init(void);
static void MX_ADC1_Init(void);
static void MX_I2C1_Init(void);
static void MX_TIM2_Init(void);
static void MX_USART1_UART_Init(void);
extern int8_t enc_delta;
extern int16_t rot_value;
extern int8_t val;
int main(void) {
// Step 1: Initialize HAL
HAL_Init();
@ -30,13 +38,12 @@ int main(void) {
//Step 3: Configure Peripherals
MX_GPIO_Init();
MX_RTC_Init();
MX_TIM2_Init();
MX_USB_DEVICE_Init();
MX_ADC1_Init();
MX_I2C1_Init();
MX_USART1_UART_Init();
//Simple dummy lcd test
// char buf[] = "Hallo THM -- es funktioniert";
//
@ -63,42 +70,27 @@ int main(void) {
while (1) {
// if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5) == GPIO_PIN_SET) {
// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
// } else {
// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
// }
// HAL_Delay(100);
rot_value += val;
display.LCDSetCursor(0,0);
char stri[20];
sprintf(stri, "DT: %d", val);
display.LCDSendCString(stri);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_0);
HAL_Delay(1000);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_1);
HAL_Delay(1000);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_2);
HAL_Delay(1000);
HAL_Delay(100);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
HAL_Delay(1000);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_4);
HAL_Delay(1000);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_6);
HAL_Delay(1000);
//HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
//HAL_Delay(2);
//HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
// HAL_ADC_Start(&hadc1);
// HAL_ADC_PollForConversion(&hadc1, 1);
// value = HAL_ADC_GetValue(&hadc1);
// HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_0);
// HAL_Delay(1000);
// HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_1);
// HAL_Delay(1000);
// HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_2);
// HAL_Delay(1000);
//
uint8_t buf[250] = "asdf";
// sprintf(buf, "Value: %d \r\n", value);
CDC_Transmit_FS(buf, sizeof(buf));
// //display.LCDSendCString(buf);
// HAL_Delay(1000);
// HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
// HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
// HAL_Delay(1000);
// HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_4);
// HAL_Delay(1000);
// HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_6);
// HAL_Delay(1000);
}
}
@ -268,6 +260,49 @@ static void MX_RTC_Init(void) {
}
/**
* @brief TIM2 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM2_Init(void)
{
/* USER CODE BEGIN TIM2_Init 0 */
/* USER CODE END TIM2_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 2399;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 9;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
//HAL_TIM_Base_Start(&htim2);
//Directly start timer base generation in interrupt mode
HAL_TIM_Base_Start_IT(&htim2);
/* USER CODE BEGIN TIM2_Init 2 */
/* USER CODE END TIM2_Init 2 */
}
/**
* @brief USART1 Initialization Function
* @param None
@ -339,18 +374,12 @@ static void MX_GPIO_Init(void) {
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pins : RRDT_Pin RRCLK_Pin */
GPIO_InitStruct.Pin = RRDT_Pin | RRCLK_Pin;
/*Configure GPIO pins : RRDT_Pin RRSW_pin RRCLK_Pin */
GPIO_InitStruct.Pin = RRDT_Pin | RRSW_Pin | RRCLK_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin : RRSW_Pin */
GPIO_InitStruct.Pin = RRSW_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(RRSW_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : ADC1_2_Pin */
GPIO_InitStruct.Pin = ADC1_2_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
@ -371,10 +400,6 @@ static void MX_GPIO_Init(void) {
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI4_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI4_IRQn);
}
/**

View File

@ -261,6 +261,56 @@ void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
}
/**
* @brief TIM_Base MSP Initialization
* This function configures the hardware resources used in this example
* @param htim_base: TIM_Base handle pointer
* @retval None
*/
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM2)
{
/* USER CODE BEGIN TIM2_MspInit 0 */
/* USER CODE END TIM2_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_TIM2_CLK_ENABLE();
/* TIM2 interrupt Init */
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
/* USER CODE BEGIN TIM2_MspInit 1 */
/* USER CODE END TIM2_MspInit 1 */
}
}
/**
* @brief TIM_Base MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param htim_base: TIM_Base handle pointer
* @retval None
*/
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM2)
{
/* USER CODE BEGIN TIM2_MspDeInit 0 */
/* USER CODE END TIM2_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM2_CLK_DISABLE();
/* TIM2 interrupt DeInit */
HAL_NVIC_DisableIRQ(TIM2_IRQn);
/* USER CODE BEGIN TIM2_MspDeInit 1 */
/* USER CODE END TIM2_MspDeInit 1 */
}
}
/**
* @brief UART MSP Initialization
* This function configures the hardware resources used in this example

View File

@ -56,6 +56,7 @@
/* External variables --------------------------------------------------------*/
extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
extern TIM_HandleTypeDef htim2;
/* USER CODE BEGIN EV */
/* USER CODE END EV */
@ -199,18 +200,17 @@ void SysTick_Handler(void)
/******************************************************************************/
/**
* @brief This function handles EXTI line4 interrupt.
* @brief This function handles TIM2 global interrupt.
*/
void EXTI4_IRQHandler(void)
void TIM2_IRQHandler(void)
{
/* USER CODE BEGIN EXTI4_IRQn 0 */
/* USER CODE BEGIN TIM2_IRQn 0 */
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
/* USER CODE END TIM2_IRQn 0 */
HAL_TIM_IRQHandler(&htim2);
/* USER CODE BEGIN TIM2_IRQn 1 */
/* USER CODE END EXTI4_IRQn 0 */
HAL_GPIO_EXTI_IRQHandler(RRSW_Pin);
/* USER CODE BEGIN EXTI4_IRQn 1 */
/* USER CODE END EXTI4_IRQn 1 */
/* USER CODE END TIM2_IRQn 1 */
}
/**