0

I am using atmega328pb controller and Arduino Framework with mini-core. The problem is that the microcontroller goes into a never-ending loop while power up. I found this while debugging.

Actually, I am using while(1) loops in the program, and temporarily I have solved this problem by using the watchdog timer. Please check the below code for more details.

Can anyone let me know the reason for this problem?

 bool Fault_ckeck()
{
  if (millis() - FAULT_CNT > FAULT_TMG) return 1; else return 0;
} 

bool WCO_Fault_ckeck()
{
  if (millis() - WCO_FAULT_CNT > FAULT_TMG) return 1; else return 0;
}  

bool WCFR_Fault_ckeck()
{
  if (millis() - WCFR_FAULT_CNT > FAULT_TMG) return 1; else return 0;
}  

bool WCUP_Fault_ckeck()
{
  if (millis() - WCUP_FAULT_CNT > FAULT_TMG) return 1; else return 0;
}  

void WCUP_EMR_STP()
{
    DWGUP.hardStop();
    DWCFR.hardStop();
    DWCO.hardStop();
    while (1) {
      if (STARTUP_FLAG == START_TOTAL) wdt_reset();
      if (millis() - ER_SIG_CNT > WCUP_EMR_STP_TMG) {
        digitalWrite(EMR_STP, !digitalRead(EMR_STP));
        ER_SIG_CNT = millis();
      }
    }
}

void WCFR_EMR_STP()
{
    DWCFR.hardStop();
    DWGUP.hardStop();
    DWCO.hardStop();
    while (1) {
      if (STARTUP_FLAG == START_TOTAL) wdt_reset();
      if (millis() - ER_SIG_CNT > WCFR_EMR_STP_TMG) {
        digitalWrite(EMR_STP, !digitalRead(EMR_STP));
        ER_SIG_CNT = millis();
      }
    }
}

void WCO_EMR_STP()
{
    DWCO.hardStop();
    DWGUP.hardStop();
    DWCFR.hardStop();
    while (1) {
      if (STARTUP_FLAG == START_TOTAL) wdt_reset();
      if (millis() - ER_SIG_CNT > WCO_EMR_STP_TMG) {
        digitalWrite(EMR_STP, !digitalRead(EMR_STP));
        ER_SIG_CNT = millis();
      }
    }
}


void STARTUP_WCUP_EMR_STP()
{
    DWGUP.hardStop();
    DWCFR.hardStop();
    DWCO.hardStop();
    while (1) {
      if (millis() - ER_SIG_CNT > WCUP_EMR_STP_TMG) {
        digitalWrite(EMR_STP, !digitalRead(EMR_STP));
        ER_SIG_CNT = millis();
      }
    }
}

void STARTUP_WCFR_EMR_STP()
{
    DWCFR.hardStop();
    DWGUP.hardStop();
    DWCO.hardStop();
    while (1) {
      if (millis() - ER_SIG_CNT > WCFR_EMR_STP_TMG) {
        digitalWrite(EMR_STP, !digitalRead(EMR_STP));
        ER_SIG_CNT = millis();
      }
    }
}

void STARTUP_WCO_EMR_STP()
{
    DWCO.hardStop();
    DWGUP.hardStop();
    DWCFR.hardStop();
    while (1) {
      if (millis() - ER_SIG_CNT > WCO_EMR_STP_TMG) {
        digitalWrite(EMR_STP, !digitalRead(EMR_STP));
        ER_SIG_CNT = millis();
      }
    }
}
Dave Tweed
  • 168,369
  • 17
  • 228
  • 393
Dinesh
  • 1
  • 2
  • 2
    "I found this while debugging" Then on which line does it hang? Could be anything, stack overflow, integer overflow etc. – Lundin Aug 18 '20 at 06:55
  • 1
    I couldn't check that, it continuously executing the while loop, for example the digital pin blinking at correct timing – Dinesh Aug 18 '20 at 07:12
  • 8
    That's kind of the whole point of using `while(1)`... – Lundin Aug 18 '20 at 07:42
  • yeah, i will try to modify the code – Dinesh Aug 18 '20 at 08:22
  • 1
    so which loop exactly hangs the system? – Ilya Aug 18 '20 at 09:28
  • 2
    What do you think while(1) does? – user253751 Aug 18 '20 at 09:34
  • randomly hangs on while loop (llya) while loop is only reason for program stuck, but i can't avoid this,coz this is an startup logic ( user253751) – Dinesh Aug 18 '20 at 10:23
  • 3
    This isn't even a program, just some disjoint fragments of code. Try reducing to an actual program with a main() from which you've removed everything but what gets it stuck. What would be advancing the millis() count in your system? – Chris Stratton Aug 18 '20 at 10:27
  • 3
    You don't show a program, just a set of functions, many of which contain while(1) loops that will never stop unless wdt_reset() or something else somehow breaks out of them. Please show all the code you are using and tell us what it is supposed to do. – ocrdu Aug 18 '20 at 10:55
  • 2
    Minor coding style suggestion: instead of `if (some condition) return 1; else return 0;`, you could just do `return (some condition);`. makes for cleaner looking code. You don't really even need the `( )` around the condition, but I like to keep them. – brhans Aug 18 '20 at 11:46

0 Answers0