interrupt driven cycles
This commit is contained in:
parent
6981c40506
commit
5dc7bb9379
@ -37,11 +37,11 @@ const float Ctrl_P = 5.0;
|
|||||||
const float Ctrl_I = 2.0;
|
const float Ctrl_I = 2.0;
|
||||||
const float Ctrl_D = 0.0;
|
const float Ctrl_D = 0.0;
|
||||||
|
|
||||||
const float U_des = 12.0;
|
volatile float U_des = 12.0;
|
||||||
|
|
||||||
|
|
||||||
LiquidCrystal lcd(LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
|
LiquidCrystal lcd(LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
|
||||||
Control ctrl((float)PWM_MIN, (float)PWM_MAX, Ctrl_P, Ctrl_I, Ctrl_D);
|
volatile Control ctrl((float)PWM_MIN, (float)PWM_MAX, Ctrl_P, Ctrl_I, Ctrl_D);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -49,27 +49,27 @@ void setup() {
|
|||||||
lcd.begin(LCD_COLS, LCD_ROWS);
|
lcd.begin(LCD_COLS, LCD_ROWS);
|
||||||
lcd.print("Teensy SMPS");
|
lcd.print("Teensy SMPS");
|
||||||
pinMode(PWM_PIN, OUTPUT);
|
pinMode(PWM_PIN, OUTPUT);
|
||||||
|
analogWrite(PWM_PIN, 0);
|
||||||
analogWriteFrequency(PWM_PIN, PWM_FREQ);
|
analogWriteFrequency(PWM_PIN, PWM_FREQ);
|
||||||
analogWriteResolution(PWM_RES);
|
analogWriteResolution(PWM_RES);
|
||||||
|
attachInterrupt(PWM_PIN, cycle, RISING);
|
||||||
|
|
||||||
analogReadResolution(ADC_RES);
|
analogReadResolution(ADC_RES);
|
||||||
analogReference(DEFAULT);
|
analogReference(DEFAULT);
|
||||||
// analogReadAveraging(16);
|
// analogReadAveraging(16);
|
||||||
pinMode(ADC_IN, INPUT);
|
pinMode(ADC_IN, INPUT);
|
||||||
|
|
||||||
analogWrite(PWM_PIN, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
|
||||||
static uint32_t lastCycle = 0;
|
|
||||||
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;
|
|
||||||
|
|
||||||
|
volatile uint32_t lastCycle = 0;
|
||||||
|
volatile float u_curr = 0;
|
||||||
|
volatile uint16_t newPwm = 0;
|
||||||
|
volatile uint32_t cycleDelay = 0;
|
||||||
|
volatile uint32_t maxCycleDelay = 0;
|
||||||
|
|
||||||
|
void cycle() {
|
||||||
uint32_t currentTime = micros();
|
uint32_t currentTime = micros();
|
||||||
if ((lastCycle + CYCLE_TIME <= currentTime) || (lastCycle > currentTime)) {
|
|
||||||
cycleDelay = currentTime - lastCycle;
|
cycleDelay = currentTime - lastCycle;
|
||||||
if (cycleDelay > maxCycleDelay) {
|
if (cycleDelay > maxCycleDelay) {
|
||||||
maxCycleDelay = cycleDelay;
|
maxCycleDelay = cycleDelay;
|
||||||
@ -85,7 +85,11 @@ void loop() {
|
|||||||
analogWrite(PWM_PIN, newPwm);
|
analogWrite(PWM_PIN, newPwm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
|
||||||
|
|
||||||
static uint32_t lastDisplayCycle = 0;
|
static uint32_t lastDisplayCycle = 0;
|
||||||
|
static uint32_t cycleCnt = 0;
|
||||||
currentTime = micros();
|
currentTime = micros();
|
||||||
if ((lastDisplayCycle + DISPLAY_UPDATE_TIME <= currentTime) || (lastDisplayCycle > currentTime)) {
|
if ((lastDisplayCycle + DISPLAY_UPDATE_TIME <= currentTime) || (lastDisplayCycle > currentTime)) {
|
||||||
lastDisplayCycle = currentTime;
|
lastDisplayCycle = currentTime;
|
||||||
@ -102,8 +106,9 @@ void loop() {
|
|||||||
lcd.print(maxCycleDelay);
|
lcd.print(maxCycleDelay);
|
||||||
float dutyCycle = ((float)newPwm) / ((float)PWM_MAX) * 100.0;
|
float dutyCycle = ((float)newPwm) / ((float)PWM_MAX) * 100.0;
|
||||||
lcd.setCursor(8, 1);
|
lcd.setCursor(8, 1);
|
||||||
lcd.print(dutyCycle);
|
// lcd.print(dutyCycle);
|
||||||
lcd.print("%");
|
// lcd.print("%");
|
||||||
|
lcd.print(cycleDelay);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user