From 56f96e5f26d8b4365945094385458551a36858b6 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Wed, 31 May 2017 17:45:45 +0200 Subject: [PATCH] rotary and logic integrated, rotary not working good --- cube/output/ttt2/Inc/mxconstants.h | 6 ++ cube/output/ttt2/Inc/stm32f1xx_it.h | 3 + cube/output/ttt2/Src/main.c | 16 ++++ cube/output/ttt2/Src/stm32f1xx_it.c | 42 +++++++++ cube/output/ttt2/ttt2.ioc | 40 +++++++-- include/mxconstants.h | 6 ++ include/stm32f1xx_it.h | 3 + my_src/display.c | 134 ++++++++++++++++++++++++++++ my_src/display.h | 35 ++++++++ my_src/main2.c | 85 ++---------------- my_src/switches.c | 119 ++++++++++++++++++++++++ my_src/switches.h | 18 ++++ my_src/timer.c | 38 ++++++++ my_src/timer.h | 21 +++++ src/main.c | 16 ++++ src/main.c-bak | 16 ++++ src/stm32f1xx_it.c | 42 +++++++++ 17 files changed, 555 insertions(+), 85 deletions(-) create mode 100644 my_src/display.c create mode 100644 my_src/display.h create mode 100644 my_src/switches.c create mode 100644 my_src/switches.h create mode 100644 my_src/timer.c create mode 100644 my_src/timer.h diff --git a/cube/output/ttt2/Inc/mxconstants.h b/cube/output/ttt2/Inc/mxconstants.h index f7be39c..2a3a188 100644 --- a/cube/output/ttt2/Inc/mxconstants.h +++ b/cube/output/ttt2/Inc/mxconstants.h @@ -45,6 +45,12 @@ #define LED_GPIO_Port GPIOC #define ERROR_Pin GPIO_PIN_14 #define ERROR_GPIO_Port GPIOC +#define ROTARY_B_Pin GPIO_PIN_3 +#define ROTARY_B_GPIO_Port GPIOB +#define ROTARY_A_Pin GPIO_PIN_4 +#define ROTARY_A_GPIO_Port GPIOB +#define SWITCH_Pin GPIO_PIN_5 +#define SWITCH_GPIO_Port GPIOB #define OLED_CS_Pin GPIO_PIN_7 #define OLED_CS_GPIO_Port GPIOB #define OLED_DC_Pin GPIO_PIN_8 diff --git a/cube/output/ttt2/Inc/stm32f1xx_it.h b/cube/output/ttt2/Inc/stm32f1xx_it.h index a3cbc9d..616d284 100644 --- a/cube/output/ttt2/Inc/stm32f1xx_it.h +++ b/cube/output/ttt2/Inc/stm32f1xx_it.h @@ -54,6 +54,9 @@ void SVC_Handler(void); void DebugMon_Handler(void); void PendSV_Handler(void); void SysTick_Handler(void); +void EXTI3_IRQHandler(void); +void EXTI4_IRQHandler(void); +void EXTI9_5_IRQHandler(void); #ifdef __cplusplus } diff --git a/cube/output/ttt2/Src/main.c b/cube/output/ttt2/Src/main.c index 7dae870..30e84df 100644 --- a/cube/output/ttt2/Src/main.c +++ b/cube/output/ttt2/Src/main.c @@ -231,12 +231,28 @@ static void MX_GPIO_Init(void) GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + /*Configure GPIO pins : ROTARY_B_Pin ROTARY_A_Pin SWITCH_Pin */ + GPIO_InitStruct.Pin = ROTARY_B_Pin|ROTARY_A_Pin|SWITCH_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING; + GPIO_InitStruct.Pull = GPIO_PULLUP; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + /*Configure GPIO pins : OLED_CS_Pin OLED_DC_Pin OLED_RST_Pin */ GPIO_InitStruct.Pin = OLED_CS_Pin|OLED_DC_Pin|OLED_RST_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + /* EXTI interrupt init*/ + HAL_NVIC_SetPriority(EXTI3_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(EXTI3_IRQn); + + HAL_NVIC_SetPriority(EXTI4_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(EXTI4_IRQn); + + HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); + } /* USER CODE BEGIN 4 */ diff --git a/cube/output/ttt2/Src/stm32f1xx_it.c b/cube/output/ttt2/Src/stm32f1xx_it.c index aadd73d..2d8315c 100644 --- a/cube/output/ttt2/Src/stm32f1xx_it.c +++ b/cube/output/ttt2/Src/stm32f1xx_it.c @@ -183,6 +183,48 @@ void SysTick_Handler(void) /* please refer to the startup file (startup_stm32f1xx.s). */ /******************************************************************************/ +/** +* @brief This function handles EXTI line3 interrupt. +*/ +void EXTI3_IRQHandler(void) +{ + /* USER CODE BEGIN EXTI3_IRQn 0 */ + + /* USER CODE END EXTI3_IRQn 0 */ + HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_3); + /* USER CODE BEGIN EXTI3_IRQn 1 */ + + /* USER CODE END EXTI3_IRQn 1 */ +} + +/** +* @brief This function handles EXTI line4 interrupt. +*/ +void EXTI4_IRQHandler(void) +{ + /* USER CODE BEGIN EXTI4_IRQn 0 */ + + /* USER CODE END EXTI4_IRQn 0 */ + HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_4); + /* USER CODE BEGIN EXTI4_IRQn 1 */ + + /* USER CODE END EXTI4_IRQn 1 */ +} + +/** +* @brief This function handles EXTI line[9:5] interrupts. +*/ +void EXTI9_5_IRQHandler(void) +{ + /* USER CODE BEGIN EXTI9_5_IRQn 0 */ + + /* USER CODE END EXTI9_5_IRQn 0 */ + HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_5); + /* USER CODE BEGIN EXTI9_5_IRQn 1 */ + + /* USER CODE END EXTI9_5_IRQn 1 */ +} + /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ diff --git a/cube/output/ttt2/ttt2.ioc b/cube/output/ttt2/ttt2.ioc index af1995d..3ef28b0 100644 --- a/cube/output/ttt2/ttt2.ioc +++ b/cube/output/ttt2/ttt2.ioc @@ -27,9 +27,12 @@ Mcu.Name=STM32F103C(8-B)Tx Mcu.Package=LQFP48 Mcu.Pin0=PC13-TAMPER-RTC Mcu.Pin1=PC14-OSC32_IN -Mcu.Pin10=PB8 -Mcu.Pin11=PB9 -Mcu.Pin12=VP_SYS_VS_Systick +Mcu.Pin10=PB4 +Mcu.Pin11=PB5 +Mcu.Pin12=PB7 +Mcu.Pin13=PB8 +Mcu.Pin14=PB9 +Mcu.Pin15=VP_SYS_VS_Systick Mcu.Pin2=PD0-OSC_IN Mcu.Pin3=PD1-OSC_OUT Mcu.Pin4=PA0-WKUP @@ -37,14 +40,17 @@ Mcu.Pin5=PA5 Mcu.Pin6=PA7 Mcu.Pin7=PA13 Mcu.Pin8=PA14 -Mcu.Pin9=PB7 -Mcu.PinsNb=13 +Mcu.Pin9=PB3 +Mcu.PinsNb=16 Mcu.UserConstants= Mcu.UserName=STM32F103C8Tx MxCube.Version=4.16.1 MxDb.Version=DB.4.0.161 NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true +NVIC.EXTI3_IRQn=true\:0\:0\:false\:false\:true +NVIC.EXTI4_IRQn=true\:0\:0\:false\:false\:true +NVIC.EXTI9_5_IRQn=true\:0\:0\:false\:false\:true NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true @@ -62,6 +68,24 @@ PA5.Mode=TX_Only_Simplex_Unidirect_Master PA5.Signal=SPI1_SCK PA7.Mode=TX_Only_Simplex_Unidirect_Master PA7.Signal=SPI1_MOSI +PB3.GPIOParameters=GPIO_PuPd,GPIO_Label,GPIO_ModeDefaultEXTI +PB3.GPIO_Label=ROTARY_B +PB3.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_RISING_FALLING +PB3.GPIO_PuPd=GPIO_PULLUP +PB3.Locked=true +PB3.Signal=GPXTI3 +PB4.GPIOParameters=GPIO_PuPd,GPIO_Label,GPIO_ModeDefaultEXTI +PB4.GPIO_Label=ROTARY_A +PB4.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_RISING_FALLING +PB4.GPIO_PuPd=GPIO_PULLUP +PB4.Locked=true +PB4.Signal=GPXTI4 +PB5.GPIOParameters=GPIO_PuPd,GPIO_Label,GPIO_ModeDefaultEXTI +PB5.GPIO_Label=SWITCH +PB5.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_RISING_FALLING +PB5.GPIO_PuPd=GPIO_PULLUP +PB5.Locked=true +PB5.Signal=GPXTI5 PB7.GPIOParameters=GPIO_Label PB7.GPIO_Label=OLED_CS PB7.Locked=true @@ -143,6 +167,12 @@ RCC.USBFreq_Value=72000000 RCC.VCOOutput2Freq_Value=8000000 SH.ADCx_IN0.0=ADC1_IN0,IN0 SH.ADCx_IN0.ConfNb=1 +SH.GPXTI3.0=GPIO_EXTI3 +SH.GPXTI3.ConfNb=1 +SH.GPXTI4.0=GPIO_EXTI4 +SH.GPXTI4.ConfNb=1 +SH.GPXTI5.0=GPIO_EXTI5 +SH.GPXTI5.ConfNb=1 SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_16 SPI1.CLKPhase=SPI_PHASE_2EDGE SPI1.CLKPolarity=SPI_POLARITY_HIGH diff --git a/include/mxconstants.h b/include/mxconstants.h index f7be39c..2a3a188 100644 --- a/include/mxconstants.h +++ b/include/mxconstants.h @@ -45,6 +45,12 @@ #define LED_GPIO_Port GPIOC #define ERROR_Pin GPIO_PIN_14 #define ERROR_GPIO_Port GPIOC +#define ROTARY_B_Pin GPIO_PIN_3 +#define ROTARY_B_GPIO_Port GPIOB +#define ROTARY_A_Pin GPIO_PIN_4 +#define ROTARY_A_GPIO_Port GPIOB +#define SWITCH_Pin GPIO_PIN_5 +#define SWITCH_GPIO_Port GPIOB #define OLED_CS_Pin GPIO_PIN_7 #define OLED_CS_GPIO_Port GPIOB #define OLED_DC_Pin GPIO_PIN_8 diff --git a/include/stm32f1xx_it.h b/include/stm32f1xx_it.h index a3cbc9d..616d284 100644 --- a/include/stm32f1xx_it.h +++ b/include/stm32f1xx_it.h @@ -54,6 +54,9 @@ void SVC_Handler(void); void DebugMon_Handler(void); void PendSV_Handler(void); void SysTick_Handler(void); +void EXTI3_IRQHandler(void); +void EXTI4_IRQHandler(void); +void EXTI9_5_IRQHandler(void); #ifdef __cplusplus } diff --git a/my_src/display.c b/my_src/display.c new file mode 100644 index 0000000..72ad892 --- /dev/null +++ b/my_src/display.c @@ -0,0 +1,134 @@ +/* + * display.c + * + * Created on: May 31, 2017 + * Author: wn + */ + + +#include "stm32f1xx_hal.h" +#include "display.h" +#include + + + + +volatile tDisplay display = { + .toggle = 0, .setModeTemperature = 0, .setModeTime = 0, + .timerState = IDLE, + .toggleModeState = 1, + .targetTemperature = 80, .currentTemperature = 75, + .targetTime = 120, .currentTime = 0, + .overrunTime = 0 +}; + + +void updateDisplay(void *handle) { + tDisplay *lDisplay = (tDisplay*) handle; + lDisplay->toggle ^= 0x01; + char buf[32]; + + sprintf(buf, "ThermometerTeaTimer"); + LED_P6x8Str(0, 0, buf); + + if (lDisplay->setModeTemperature && lDisplay->toggle) { + sprintf(buf, " %3d'C", lDisplay->currentTemperature); + } else { + sprintf(buf, "%3d'C %3d'C", lDisplay->targetTemperature, lDisplay->currentTemperature); + } + LED_P8x16Str(0, 2, buf); + + if (lDisplay->setModeTime && lDisplay->toggle) { + sprintf(buf, " %3ds", lDisplay->currentTime); + } else { + sprintf(buf, "%3ds %3ds", lDisplay->targetTime, lDisplay->currentTime); + } + LED_P8x16Str(0, 4, buf); + + sprintf(buf, " %5ds", lDisplay->overrunTime); + LED_P8x16Str(0, 6, buf); +} + +void clearSetMode(void *handle) { + tDisplay *lDisplay = (tDisplay*) handle; + lDisplay->setModeTemperature = 0; + lDisplay->setModeTime = 0; + lDisplay->toggleModeState = 1; +} + +static void disableSetModeTimer() { + schDel(clearSetMode, (void*)&display); +} + +static void reEnableSetModeTimer() { + schDel(clearSetMode, (void*)&display); + schAdd(clearSetMode, (void*)&display, 10000, 0); +} + +static void toggleSetMode() { + switch (display.toggleModeState) { + default: + case 0: + disableSetModeTimer(); + display.setModeTemperature = 0; + display.setModeTime = 0; + display.toggleModeState = 1; + break; + case 1: + reEnableSetModeTimer(); + display.setModeTemperature = 1; + display.setModeTime = 0; + display.toggleModeState = 2; + break; + case 2: + reEnableSetModeTimer(); + display.setModeTemperature = 0; + display.setModeTime = 1; + display.toggleModeState = 0; + break; + } +} + +void displayIncValue() { + if (display.setModeTemperature == 1) { + reEnableSetModeTimer(); + if (display.targetTemperature < 100) { + display.targetTemperature++; + } + } + if (display.setModeTime == 1) { + reEnableSetModeTimer(); + if (display.targetTime < 999) { + display.targetTime += 10; + } + } +} + +void displayDecValue() { + if (display.setModeTemperature == 1) { + reEnableSetModeTimer(); + if (display.targetTemperature > 0) { + display.targetTemperature--; + } + } + if (display.setModeTime == 1) { + reEnableSetModeTimer(); + if (display.targetTime >= 10) { + display.targetTime -= 10; + } + } +} + +void buttonShort() { + if (display.setModeTemperature == 1 || display.setModeTime == 1) { + toggleSetMode(); + } else if (display.timerState == IDLE) { + startTimer(); + } +} + +void buttonLong() { + if (display.timerState == IDLE) { + toggleSetMode(); + } +} diff --git a/my_src/display.h b/my_src/display.h new file mode 100644 index 0000000..450976a --- /dev/null +++ b/my_src/display.h @@ -0,0 +1,35 @@ +/* + * display.h + * + * Created on: May 31, 2017 + * Author: wn + */ + +#ifndef DISPLAY_H_ +#define DISPLAY_H_ + + +#include "timer.h" +#include "stm32f1xx_hal.h" + + +typedef struct { + uint8_t toggle; + uint8_t setModeTemperature; + uint8_t setModeTime; + tTimerState timerState; + uint8_t toggleModeState; + uint32_t targetTemperature; + uint32_t currentTemperature; + uint32_t targetTime; + uint32_t currentTime; + uint32_t overrunTime; +} tDisplay; + +void updateDisplay(void *handle); +void displayToggleSetMode(); +void displayDecValue(); +void displayIncValue(); + + +#endif /* DISPLAY_H_ */ diff --git a/my_src/main2.c b/my_src/main2.c index 39b80a1..7726f8e 100644 --- a/my_src/main2.c +++ b/my_src/main2.c @@ -21,83 +21,10 @@ #include "stm32f1xx_hal.h" #include "oled.h" +#include "timer.h" +#include "display.h" -typedef enum { - IDLE = 0, - STARTED = 1, - RUNNING = 2, - OVERRUN = 3 -} tTimerState; - -typedef struct { - uint8_t toggle; - uint8_t setModeTemperature; - uint8_t setModeTime; - tTimerState timerState; - uint32_t targetTemperature; - uint32_t currentTemperature; - uint32_t targetTime; - uint32_t currentTime; - uint32_t overrunTime; -} tDisplay; - - -tDisplay display = { - .toggle = 0, .setModeTemperature = 0, .setModeTime = 0, - .timerState = IDLE, - .targetTemperature = 80, .currentTemperature = 75, - .targetTime = 180, .currentTime = 175, - .overrunTime = 0 -}; - - -void updateDisplay(void *handle) { - tDisplay *lDisplay = (tDisplay*) handle; - lDisplay->toggle ^= 0x01; - char buf[32]; - - sprintf(buf, "ThermometerTeaTimer"); - LED_P6x8Str(0, 0, buf); - - if (lDisplay->setModeTemperature && lDisplay->toggle) { - sprintf(buf, " %3d'C", lDisplay->currentTemperature); - } else { - sprintf(buf, "%3d'C %3d'C", lDisplay->targetTemperature, lDisplay->currentTemperature); - } - LED_P8x16Str(0, 2, buf); - - if (lDisplay->setModeTime && lDisplay->toggle) { - sprintf(buf, " %3ds", lDisplay->currentTime); - } else { - sprintf(buf, "%3ds %3ds", lDisplay->targetTime, lDisplay->currentTime); - } - LED_P8x16Str(0, 4, buf); - - sprintf(buf, " %5ds", lDisplay->overrunTime); - LED_P8x16Str(0, 6, buf); -} - -void secondTick(void *handle) { - tDisplay *lDisplay = (tDisplay*) handle; - - switch (lDisplay->timerState) { - case STARTED: - lDisplay->currentTime = lDisplay->targetTime; - lDisplay->timerState = RUNNING; - break; - case RUNNING: - lDisplay->currentTime -= 1; - if (lDisplay->currentTime == 0) { - lDisplay->timerState = OVERRUN; - } - break; - case OVERRUN: - lDisplay->overrunTime += 1; - break; - default: - ; - } -} +extern tDisplay display; void blink(void *handle) { HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin); @@ -105,11 +32,9 @@ void blink(void *handle) { void my_setup_1() { schInit(); - schAdd(blink, NULL, 0, 100); +// schAdd(blink, NULL, 0, 100); schAdd(updateDisplay, &display, 0, 250); schAdd(secondTick, &display, 0, 1000); - - display.timerState = STARTED; } void my_loop() { @@ -120,13 +45,13 @@ void HAL_SYSTICK_Callback() { schUpdate(); } + void my_errorHandler() { HAL_GPIO_WritePin(ERROR_GPIO_Port, ERROR_Pin, GPIO_PIN_SET); } void my_setup_2() { LED_Init(); - LED_P6x8Str(0,0,"welcome to"); } diff --git a/my_src/switches.c b/my_src/switches.c new file mode 100644 index 0000000..0e9d881 --- /dev/null +++ b/my_src/switches.c @@ -0,0 +1,119 @@ +/* + * switches.c + * + * Created on: May 31, 2017 + * Author: wn + */ + + +#include "stm32f1xx_hal.h" +#include "switches.h" +#include "display.h" + + + +extern tDisplay display; +volatile uint32_t lastEvent = 0; + + +void switch_interrupt() { + static uint8_t state = 0; + uint32_t currentEvent = HAL_GetTick(); + + uint16_t line = HAL_GPIO_ReadPin(SWITCH_GPIO_Port, SWITCH_Pin); + + switch (state) { + case 0: + if (line == RESET) { + lastEvent = currentEvent; + state = 1; + } + break; + case 1: + if (line != RESET) { + if (lastEvent + LONG_BUTTON_TIME + DEBOUNCING_DEAD_TIME < currentEvent) { + lastEvent = currentEvent; + buttonLong(); + state = 0; + } else if (lastEvent + DEBOUNCING_DEAD_TIME < currentEvent) { + lastEvent = currentEvent; + buttonShort(); + state = 0; + } + } + break; + default: + state = 0; + } +} + +int myDigitalRead(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) { + int16_t r = 0; + for (uint32_t i = 0; i < DEBOUNCING_REPEAT; i++) { + if (HAL_GPIO_ReadPin(GPIOx, GPIO_Pin) != RESET) { + r++; + } else { + r--; + } + } + int res = -1; + if (r >= (((int32_t)DEBOUNCING_REPEAT) / 2)) { + res = 1; + } else if (r <= -1 * (((int32_t)DEBOUNCING_REPEAT) / 2)) { + res = 0; + } + return res; +} + + +void rotary_a_interrupt() { + uint32_t currentEvent = HAL_GetTick(); + + if ((lastEvent == 0) || (lastEvent + DEBOUNCING_DEAD_TIME < currentEvent)) { + lastEvent = currentEvent; + + int a = myDigitalRead(ROTARY_A_GPIO_Port, ROTARY_A_Pin);; + int b = myDigitalRead(ROTARY_B_GPIO_Port, ROTARY_B_Pin);; + + if (((a != -1) && (b != -1))) { + if (a == b) { + displayIncValue(); + } else { + displayDecValue(); + } + } + } +} + +void rotary_b_interrupt() { + uint32_t currentEvent = HAL_GetTick(); + + if ((lastEvent == 0) || (lastEvent + DEBOUNCING_DEAD_TIME < currentEvent)) { + lastEvent = currentEvent; + + int a = myDigitalRead(ROTARY_A_GPIO_Port, ROTARY_A_Pin);; + int b = myDigitalRead(ROTARY_B_GPIO_Port, ROTARY_B_Pin);; + + if (((a != -1) && (b != -1))) { + if (a == b) { + displayDecValue(); + } else { + displayIncValue(); + } + } + } +} + + +void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { + if (GPIO_Pin == GPIO_PIN_5) { + switch_interrupt(); + } + if (GPIO_Pin == GPIO_PIN_3) { + rotary_a_interrupt(); + } + if (GPIO_Pin == GPIO_PIN_4) { + rotary_b_interrupt(); + } +} + diff --git a/my_src/switches.h b/my_src/switches.h new file mode 100644 index 0000000..6327027 --- /dev/null +++ b/my_src/switches.h @@ -0,0 +1,18 @@ +/* + * switches.h + * + * Created on: May 31, 2017 + * Author: wn + */ + +#ifndef SWITCHES_H_ +#define SWITCHES_H_ + +const uint32_t DEBOUNCING_DEAD_TIME = 100; +const uint32_t DEBOUNCING_REPEAT = 20; +const uint32_t LONG_BUTTON_TIME = 1000; + +void buttonShort(); +void buttonLong(); + +#endif /* SWITCHES_H_ */ diff --git a/my_src/timer.c b/my_src/timer.c new file mode 100644 index 0000000..37b3bdd --- /dev/null +++ b/my_src/timer.c @@ -0,0 +1,38 @@ +/* + * timer.c + * + * Created on: May 31, 2017 + * Author: wn + */ + +#include "timer.h" +#include "display.h" + + +extern tDisplay display; + +void startTimer() { + display.timerState = STARTED; +} + +void secondTick(void *handle) { + tDisplay *lDisplay = (tDisplay*) handle; + + switch (lDisplay->timerState) { + case STARTED: + lDisplay->currentTime = lDisplay->targetTime; + lDisplay->timerState = RUNNING; + break; + case RUNNING: + lDisplay->currentTime -= 1; + if (lDisplay->currentTime == 0) { + lDisplay->timerState = OVERRUN; + } + break; + case OVERRUN: + lDisplay->overrunTime += 1; + break; + default: + ; + } +} diff --git a/my_src/timer.h b/my_src/timer.h new file mode 100644 index 0000000..97a1761 --- /dev/null +++ b/my_src/timer.h @@ -0,0 +1,21 @@ +/* + * timer.h + * + * Created on: May 31, 2017 + * Author: wn + */ + +#ifndef TIMER_H_ +#define TIMER_H_ + +typedef enum { + IDLE = 0, + STARTED = 1, + RUNNING = 2, + OVERRUN = 3 +} tTimerState; + +void secondTick(void *handle); +void startTimer(); + +#endif /* TIMER_H_ */ diff --git a/src/main.c b/src/main.c index 1d0144d..791cd8d 100644 --- a/src/main.c +++ b/src/main.c @@ -235,12 +235,28 @@ static void MX_GPIO_Init(void) GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + /*Configure GPIO pins : ROTARY_B_Pin ROTARY_A_Pin SWITCH_Pin */ + GPIO_InitStruct.Pin = ROTARY_B_Pin|ROTARY_A_Pin|SWITCH_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING; + GPIO_InitStruct.Pull = GPIO_PULLUP; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + /*Configure GPIO pins : OLED_CS_Pin OLED_DC_Pin OLED_RST_Pin */ GPIO_InitStruct.Pin = OLED_CS_Pin|OLED_DC_Pin|OLED_RST_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + /* EXTI interrupt init*/ + HAL_NVIC_SetPriority(EXTI3_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(EXTI3_IRQn); + + HAL_NVIC_SetPriority(EXTI4_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(EXTI4_IRQn); + + HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); + } /* USER CODE BEGIN 4 */ diff --git a/src/main.c-bak b/src/main.c-bak index 7dae870..30e84df 100644 --- a/src/main.c-bak +++ b/src/main.c-bak @@ -231,12 +231,28 @@ static void MX_GPIO_Init(void) GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + /*Configure GPIO pins : ROTARY_B_Pin ROTARY_A_Pin SWITCH_Pin */ + GPIO_InitStruct.Pin = ROTARY_B_Pin|ROTARY_A_Pin|SWITCH_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING; + GPIO_InitStruct.Pull = GPIO_PULLUP; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + /*Configure GPIO pins : OLED_CS_Pin OLED_DC_Pin OLED_RST_Pin */ GPIO_InitStruct.Pin = OLED_CS_Pin|OLED_DC_Pin|OLED_RST_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + /* EXTI interrupt init*/ + HAL_NVIC_SetPriority(EXTI3_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(EXTI3_IRQn); + + HAL_NVIC_SetPriority(EXTI4_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(EXTI4_IRQn); + + HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); + } /* USER CODE BEGIN 4 */ diff --git a/src/stm32f1xx_it.c b/src/stm32f1xx_it.c index aadd73d..2d8315c 100644 --- a/src/stm32f1xx_it.c +++ b/src/stm32f1xx_it.c @@ -183,6 +183,48 @@ void SysTick_Handler(void) /* please refer to the startup file (startup_stm32f1xx.s). */ /******************************************************************************/ +/** +* @brief This function handles EXTI line3 interrupt. +*/ +void EXTI3_IRQHandler(void) +{ + /* USER CODE BEGIN EXTI3_IRQn 0 */ + + /* USER CODE END EXTI3_IRQn 0 */ + HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_3); + /* USER CODE BEGIN EXTI3_IRQn 1 */ + + /* USER CODE END EXTI3_IRQn 1 */ +} + +/** +* @brief This function handles EXTI line4 interrupt. +*/ +void EXTI4_IRQHandler(void) +{ + /* USER CODE BEGIN EXTI4_IRQn 0 */ + + /* USER CODE END EXTI4_IRQn 0 */ + HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_4); + /* USER CODE BEGIN EXTI4_IRQn 1 */ + + /* USER CODE END EXTI4_IRQn 1 */ +} + +/** +* @brief This function handles EXTI line[9:5] interrupts. +*/ +void EXTI9_5_IRQHandler(void) +{ + /* USER CODE BEGIN EXTI9_5_IRQn 0 */ + + /* USER CODE END EXTI9_5_IRQn 0 */ + HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_5); + /* USER CODE BEGIN EXTI9_5_IRQn 1 */ + + /* USER CODE END EXTI9_5_IRQn 1 */ +} + /* USER CODE BEGIN 1 */ /* USER CODE END 1 */