#include #include #include #include #define PIC_CLK 20000000 #include "delay_alternative_enhanced_precision.h" //__CONFIG(WDTDIS & XT & UNPROTECT & LVPDIS); // freq. clock 4MHz //__CONFIG(WDTDIS & HS & UNPROTECT & LVPDIS); // freq. clock 20MHz __CONFIG(WDTDIS & PWRTEN & HS & UNPROTECT & DEBUGDIS & BORDIS & LVPDIS); #define PORTBIT(adr, bit) ((unsigned)(&adr)*8+(bit)) static bit MEM @ PORTBIT(PORTA, 3); static bit RDY @ PORTBIT(PORTA, 2); static bit LGT @ PORTBIT(PORTA, 1); static bit ALR @ PORTBIT(PORTA, 0); static bit IN1 @ PORTBIT(PORTB, 0); static bit IN2 @ PORTBIT(PORTB, 1); static bit PUD @ PORTBIT(PORTB, 2); static bit PUC @ PORTBIT(PORTB, 3); static bit PUB @ PORTBIT(PORTB, 4); static bit PUA @ PORTBIT(PORTB, 5); //****************************************************************************** //* function declarationis & global variables //****************************************************************************** #define MIN3 (3*60*4) #define SEC5 (5*4) void DelayMs(unsigned char); void Alarm_long(void); void Alarm_prog(void); int Progressive = 1; void main() { int vol; int vdb; int volume_up = 0; int volume_down = 0; TRISA = 0b00000000; // All port A are output TRISB = 0b11111111; // All port B are input TRISC = 0b00000000; // All post C are output ADCON1 = 0x06; // All posts AN4-7 to digital PORTA = 0b00000000; PORTC = 0b00000000; RDY = 0; ALR = 0; LGT = 0; MEM = 0; DelayMs(250); //################################################## //# main loop //################################################## while(1) { //******************************************** //* A button //* Enable or disable manual alarm //******************************************** if (PUA == 1) { if (ALR == 0) { ALR = 1; LGT = 1; } else { ALR = 0; LGT = 0; } DelayMs(250); DelayMs(250); DelayMs(250); DelayMs(250); } //******************************************** //* B button //* Enable Alarm system //******************************************** if (PUB == 1) { Progressive = 1; RDY = 1; ALR = 0; MEM = 0; LGT = 1; DelayMs(250); DelayMs(250); DelayMs(250); DelayMs(250); DelayMs(250); DelayMs(250); DelayMs(250); DelayMs(250); LGT = 0; } //******************************************** //* D button //* Disable Alarm system //******************************************** if (PUD == 1) { Progressive = 1; RDY = 0; ALR = 0; LGT = 1; DelayMs(250); DelayMs(250); DelayMs(250); DelayMs(250); DelayMs(250); DelayMs(250); DelayMs(250); DelayMs(250); LGT = 0; } //******************************************** //* Only if Alarm system is enable //******************************************** if (RDY == 1) { //******************************************** //* Input signal received on vibration senson //* Start alarm with progressive incr. time //******************************************** if (IN1 == 1) { ALR = 1; LGT = 1; MEM = 1; Alarm_prog(); ALR = 0; LGT = 0; Progressive++; } //******************************************** //* Input signal received on magnetic senson //* Start alarm with long time //******************************************** if (IN2 == 1) { ALR = 1; LGT = 1; MEM = 1; Alarm_long(); ALR = 0; LGT = 0; } } } } void Alarm_long(void) { int i; //******************************************* //* create 3min loop //******************************************* for (i = 0; i < MIN3; i++) { DelayMs(250); if (PUA == 1 || PUB == 1 || PUC == 1 || PUD == 1) break; } } void Alarm_prog(void) { int i; //******************************************* //* max 3min for progressive alarm //******************************************* if ((SEC5 * Progressive) >= MIN3) Progressive = 1; //******************************************* //* create dinamic loop //******************************************* for (i = 0; i < (SEC5 * Progressive); i++) { DelayMs(250); if (PUA == 1 || PUB == 1 || PUC == 1 || PUD == 1) break; } } void DelayMs(unsigned char cnt) { unsigned char i; do { i = 4; do { DelayUs(250); } while(--i); } while(--cnt); }