所以我用的是一台PIC 18F。
如果设置了Alarm_Status.bits.b3 (本质上只是一个开关),就会创建一个警报。第一段代码按其应有的方式工作。
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);但是,我只想在开关设置一定时间时才报警,而不是在设置开关时,每秒钟直接调用away.The函数。有人能告诉我我哪里出错了吗?
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); 发布于 2014-12-21 07:30:26
您可以将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);
}https://stackoverflow.com/questions/27529561
复制相似问题