riccardo
Member
Hello,
My code has developed a weird glitch that I've been unable to properly fix. The symptom is that the code runs to a point then the micro resets and the whole thing goes in a loop. This is very typical of what happens when data is written to an array beyond its size, but I can not find any such array issue. I've narrowed down the possible code areas where I can change something and the glitch goes away, however, I can not make sense of why, so I suspect I am missing something.
The area of code is a switch(), and I've found two ways to stop the reset happening....
1. Comment out the switch code
2. Change the vale of one of the cases from 6, to 66. Yes really! The cases are all #define numbers 1,2,3,4,5,6,7,8,9. I've tried replacing the defines with the numbers directly and it makes no difference. I can't understand why changing the case 6 to another value would stop the issue.
I've also noticed something odd when commenting out the section of code.. If I select all the code (Microchip Studio), and press slash, it comments out all the code except for one curly brace. I can't see why it singles out that one, but it does the same one regardless of how much of the code around it is selected.
P.S. I can comment out one of the switch cases and the bug is gone. However, the case is not even executed as the value is that of another case. I don't understand at all how that can happen!
I've attached a GIF showing what happens when commenting out that code. Maybe I've been staring at it too long and can't see something obvious. Any suggestions?
My code has developed a weird glitch that I've been unable to properly fix. The symptom is that the code runs to a point then the micro resets and the whole thing goes in a loop. This is very typical of what happens when data is written to an array beyond its size, but I can not find any such array issue. I've narrowed down the possible code areas where I can change something and the glitch goes away, however, I can not make sense of why, so I suspect I am missing something.
The area of code is a switch(), and I've found two ways to stop the reset happening....
1. Comment out the switch code
2. Change the vale of one of the cases from 6, to 66. Yes really! The cases are all #define numbers 1,2,3,4,5,6,7,8,9. I've tried replacing the defines with the numbers directly and it makes no difference. I can't understand why changing the case 6 to another value would stop the issue.
I've also noticed something odd when commenting out the section of code.. If I select all the code (Microchip Studio), and press slash, it comments out all the code except for one curly brace. I can't see why it singles out that one, but it does the same one regardless of how much of the code around it is selected.
P.S. I can comment out one of the switch cases and the bug is gone. However, the case is not even executed as the value is that of another case. I don't understand at all how that can happen!
I've attached a GIF showing what happens when commenting out that code. Maybe I've been staring at it too long and can't see something obvious. Any suggestions?
C++:
void PWM_4808::ModeSwitch() { // Set outputs according to mode
// Set outputs according to mode
switch (modeNow) {
// FAULT: Everything stops, fault info on screen
case MODE_FAULT:
StopOutputs(); // OFF
break; // BREAK MODE_FAULT
// NORMAL: Continuous Mode - Toggle On/Off with adjustable time
case MODE_NORMAL:
if (pulseLenCont) {
StartOutputs(DISMODE_DC); // Just on at full power
} else {
if (isRunningP == 0) { // If just starting
timeStampOut = millis(); // Save current time
timeStampOutP = timeStampOut; // Save start time
}
SinglePulseOut(pulseLen*10); // Do a single pulse in ms (isRunning will set to zero when the pulse is ended)
}
isRunningP = 1; // Set the runningP flag
break; // BREAK MODE_NORMAL
// AUTO: Ping coil to detect load. User set detection current. (hysteresis in settings)
case MODE_AUTO:
timeStampOut = millis(); // Save current time
if (isRunningP == 0) { // If just starting
timeStampOutP = timeStampOut; // Save start time
PingCoil(100); // Activate coil and read current after XXms
} // END IF isRunningP == 0
if (ampsVal < holdCurrent) {
if (timeStampOut >= timeStampOutP+1000) { // If time between pings has passed
timeStampOutP = timeStampOut;
PingCoil(100); // Activate coil and read current after 100ms
} // END IF time passed
if (ampsVal < holdCurrent-hysteresis) isLatched = 0; // If below target and hysteresis. Unlatch
} // END IF ampsVal < holdCurrent
if (ampsVal >= holdCurrent) isLatched = 1; // If current is above hold value, Set Latch
if (isLatched) {
StartOutputs(DISMODE_DC); // On at full power
} else {
StopOutputs(); // OFF
} // END IF isLatched
isRunningP = 1;
break; // BREAK MODE_AUTO
// THERMO: Regulate temp using thermocouple input. TODO: Adjustable heat time
case MODE_THERMO:
// NB: aux ADC reading needs to be calibrated for Celsius
if (thermocoupleVal < targetTemp) {
StartOutputs(DISMODE_DC); // On at full power
} else {
StopOutputs(); // OFF
}
isRunningP = 1;
break; // BREAK MODE_THERMO
// AUX: Regulate temp using 0-5V AUX input TODO: Adjustable heat time
case MODE_AUX:
// NB: aux ADC reading needs to be calibrated for Celsius
if (auxVal < targetTemp) { // TODO: auxVal needs a setting to calibrate for temperature
StartOutputs(DISMODE_DC); // On at full power
} else {
StopOutputs(); // OFF
}
isRunningP = 1;
break; // BREAK MODE_AUX
// PWM_RES: Toggle On/Off, Adjustable duty, fixed frequency, selectable Duty or current mode
case MODE_PWM_RES: // TODO: Change this to CC mode?
if (isRunningP == 0) { // If just starting
StartOutputs(DISMODE_PWM);
}
TCApwm(modFrq, opDuty); // Set/Update PWM Output
isRunningP = 1; // Set the runningP flag
break; // BREAK MODE_PWM_RES
// PWM_PWM_DUAL: Resonance disabled, Selectable A/B PWM, Adjustable duty & frequency
case MODE_PWM_DUAL:
// TODO: Disable input capture and select MOSFET
if (isRunningP == 0) { // If just starting
StartOutputs(DISMODE_PWM);
}
TCApwm(modFrq, opDuty); // Set/Update PWM Output
isRunningP = 1;
break; // BREAK MODE_PWM_DUAL
// SERIAL: Just shows serial comms
case MODE_SERIAL:
break; // BREAK MODE_SERIAL
// DIAG: Toggle On/Off, Adjustable duty & frequency
case MODE_DIAG:
break; // BREAK MODE_DIAG
// SETUP:
case MODE_SETUP:
break; // BREAK MODE_SETUP
} // END SWITCH MODE_NOW
}
Attachments
Last edited: