【问题标题】:C-Pic Counter issueC-Pic 计数器问题
【发布时间】:2014-12-17 15:53:25
【问题描述】:

所以我使用的微型是 PIC 18F。

如果 Alarm_Status.bits.b3 设置为本质上只是一个开关,则会创建警报。第一个 sn-p 代码可以正常工作

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
                DU_Reason.bits.EmergencyDialIn=1;   // alarm!
            }
        }
        Alarm_Status.bits.b3=0;                 // Bund Sw Open
    }
    else Alarm_Status.bits.b3=1;                // Bund Sw Closed
    BC(TRISB,7);

但是我只想在开关设置一段时间而不是直接设置开关时报警。该函数每秒调用一次。谁能指出我出错的方向。

        int count = 0;
        int fixedCount = 20;
        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 +=10;                 //count increased by 10
                }
                    if(count == fixedCount) {
                        DU_Reason.bits.EmergencyDialIn=1;// alarm!
                        count = 0;
                    }
            }
            Alarm_Status.bits.b3=0;             // Bund Sw Open
        }
        else
            count = 0;
            Alarm_Status.bits.b3=1;     // Bund Sw Closed
            BC(TRISB,7);    

【问题讨论】:

  • count 变量应该是全局变量。
  • “Bund”不是英文的吧?似乎是一个重要的词。
  • 显示你所知道的外滩是英文 你听说过外滩传感器吗,显然不是。无论如何,感谢亚历克斯宣布它在全球范围内工作得很好,干杯。
  • @Alex Farber 或 NewLook。建议发布答案并接受它以关闭此帖子。
  • C 不是 Python - 缩进无关紧要。第二个块中的最后三行缩进,好像它们应该在else 块中执行,但它们没有括在大括号中({ })。因此,只有 count = 0; 特定于 else 案例。其他两行每次都运行。

标签: c counter microcontroller pic


【解决方案1】:

您可以将 count 设为静态。当前代码的问题,在每个实例之后,count 被重新初始化为 0,因此,它永远不会达到 fixedCount

如果您将代码修改为:

#define FIXED_COUNT 20    //Why to waste memory??

void PollSwitch()
{
    static int count = 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 +=10;                 //count increased by 10
             }
            if(count == FIXED_COUNT ) {
                 DU_Reason.bits.EmergencyDialIn=1;// alarm!
                 count = 0;
                 Alarm_Status.bits.b3=0;             //Alarm is raised, Open switch
              }
        }
               // Alarm_Status.bits.b3=0;          // Bund Sw Open <<< NOT NEEDED, IMO
     }
    else
     {
         count = 0;
         Alarm_Status.bits.b3=1;     // Bund Sw Closed
     }
        BC(TRISB,7);    
}

【讨论】:

    猜你喜欢
    • 2021-12-17
    • 2019-09-09
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多