简体   繁体   English

反发行(C程序)

[英]Counter issue ( C Program)

I only want my count_float to reset back to 0 when Alarm_Status.bits.b3 is not set and when leakDetected=0 . 我只希望在未设置Alarm_Status.bits.b3我count_float重置回为0,当leakDetected = 0。 However it never seems to reset for some reason. 但是,由于某种原因,它似乎从未重置过。 When alarm goes off I put leakDetected to 1 so the counter cant be reset. 当警报响起时,我将LeakDetected设置为1,因此无法重置计数器。 But if alarm goes off for 2 seconds and is disabled again it holds this count of 2. It should reset to 0. 但是,如果警报响起2秒钟并再次被禁用,它将保持此计数2。应将其重置为0。

{   
    int count_float     
    int fixedCount = 50;
    short  leakDetected=0;
    BS(TRISB,7);    // Bund sw port=input.
    DelayMs(2); // will rise is bund SW open
    if(RB7){                                
        if(Control.bits.BUND_ENABLE){       // if bund alarm enabled
            if(Alarm_Status.bits.b3){   // if  already set
                count_float +=10;       //count increased by 10
                if(count_float == fixedCount) { 
                    leakDetected=1;
                    DU_Reason.bits.EmergencyDialIn=1;// alarm!
                }
            }
            if((!Alarm_Status.bits.b3)&&(!leakDetected)){
                count_float=0;
            }                           
        }   
    } else {
        Alarm_Status.bits.b3=1;     // Bund Sw Closed
        BC(TRISB,7);
    }
}

You are using count_float uninitialized. 您正在使用未初始化的count_float Accessing a variable without initializing it leads to undefined behavior. 在不初始化变量的情况下访问变量会导致未定义的行为。

To fix it, simply initialize as: 要修复它,只需将其初始化为:

int count_float = 0;

About your updated question, you initialize count_float everytime you come to this loop. 关于更新的问题,每次进入此循环时count_float初始化count_float If you want it to maintain the previous count(as it is looping every second), please make it static as shown below. 如果您希望它保持先前的计数(因为它每秒循环一次),请使其保持static ,如下所示。

static int count_float = 0;  /* '= 0' is optional but recommended */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM