void output(char portval, char duration);
void main()
{
int i;
unsigned char state, oldstate;
TRISC=255; // set port as a input
TRISB=0; //set port b as output
while(1)
{
state = (PORTB & 0x2C) >> 2; // we need 2,3,4,5 so put them in low nibble
oldstate <<= 4; // added to oldstate
oldstate += state; // gives us 8 possible states needed
// other states can be implemented as you need them
switch(oldstate)
{
case 0x01: // switch on f2
output(1,5);
break;
case 0x02: // switch on f3
output(1,5);
break;
case 0x04: // switch on f4
output(1,5);
break;
case 0x08: // switch on f5
output(255,5);
break;
case 0x11: // switch on f2 ( again )
output(1,10);
break;
case 0x22: // switch on f3 ( again )
output(1,10);
break;
case 0x44: // switch on f4 ( again )
output(1,10);
break;
case 0x88: // switch on f5 ( again )
output(255,10);
break;
}
}
}
void output(char portval, char duration)
{
int i;
DelayMs(2000); //two second delay for next operation
for(i=0;i <= duration;i++) //use for to read funtion for duration ex;if led it will blink duration
{
PORTB = portval; //port bo on -siren or buzzer is on or LED's
DelayMs(500); //half second delay
PORTB = 0; //all of port b is off-alarm or buzzer is stop after sound for ten time.
DelayMs(500); //half secon delay
}
}