diff --git a/TeensyPwm.cpp b/TeensyPwm.cpp index 9884bde..670269c 100644 --- a/TeensyPwm.cpp +++ b/TeensyPwm.cpp @@ -29,9 +29,10 @@ const uint8_t LCD_D7 = 7; const uint8_t LCD_ROWS = 2; const uint8_t LCD_COLS = 16; -const uint32_t CYCLE_TIME = 500; // microseconds +const uint32_t CYCLE_TIME = 250; // microseconds const uint32_t DISPLAY_UPDATE_TIME = 5e5; // microseconds + const float Ctrl_P = 5.0; const float Ctrl_I = 2.0; const float Ctrl_D = 0.0; @@ -64,10 +65,15 @@ void loop() { static float u_curr = 0; static uint16_t newPwm = 0; static uint32_t cycleDelay = 0; + static uint32_t maxCycleDelay = 0; + static uint32_t cycleCnt = 0; uint32_t currentTime = micros(); if ((lastCycle + CYCLE_TIME <= currentTime) || (lastCycle > currentTime)) { cycleDelay = currentTime - lastCycle; + if (cycleDelay > maxCycleDelay) { + maxCycleDelay = cycleDelay; + } lastCycle = currentTime; uint16_t adcIn = analogRead(ADC_IN); float u_adc = ((float)adcIn) * U_ref / ((float)ADC_MAX); @@ -83,13 +89,17 @@ void loop() { currentTime = micros(); if ((lastDisplayCycle + DISPLAY_UPDATE_TIME <= currentTime) || (lastDisplayCycle > currentTime)) { lastDisplayCycle = currentTime; + cycleCnt++; + if (cycleCnt == 60) { + maxCycleDelay = 0; + } lcd.clear(); lcd.setCursor(0, 0); lcd.print(U_des); lcd.setCursor(8, 0); lcd.print(u_curr); lcd.setCursor(0, 1); - lcd.print(cycleDelay); + lcd.print(maxCycleDelay); float dutyCycle = ((float)newPwm) / ((float)PWM_MAX) * 100.0; lcd.setCursor(8, 1); lcd.print(dutyCycle);