/*
Project: A very basic clap switch
Description : Clap switch using PIC1fF877A
Oscillator: Internal 4.0000 MHz, MCLR Disabled, PWRT ON enabled
Written by: Rajendra Bhatt (www.embedded-lab.com)
Date: SEPT 23, 2013
*/
#define Output_LED PORTd.F2;
unsigned short i, TIME_UP;
void interrupt(void){
if(PIR1.TMR1IF) {
i ++;
if(i == 3) TIME_UP = 1; // Time Up in 1.5 sec
PIR1.TMR1IF = 0;
}
}
void main() {
//TRISIO = 0b00000011 ;
TRISA= 0b00000011 ;
//ANSEL = 0x00;
INTCON = 0b11000000 ; // Enable GIE and PEIE for Timer1 overflow interrpt
PIE1 = 0b00000001 ; // Enable TMR1IE
//CMCON0 = 0b00010100;
//CMCON = 0b00010100; changed on my own
CMCON = 0b00010110;
//CVRCON = 0b10100011;
CVRCON = 0b10100010;
//Output_LED = 0;
PORTd.F2=0;
do{
TMR1H = 0x00;
TMR1L = 0x00;
TIME_UP = 0;
i = 0;
T1CON = 0b00110000; // Configure Timer 1
if(CMCON.C2OUT) { // First clap detected
Delay_ms(100);
T1CON.TMR1ON = 1; // Start Timer1
while(!CMCON.C2OUT && !TIME_UP); // Wait until second clap is
T1CON.TMR1ON = 0; // detected or Timer1 overflows
if(CMCON.C2OUT && !TIME_UP)
//Output_LED = ~Output_LED;
PORTc.F2=~PORTc.F2;
Delay_ms(100);
}
}
while(1);
}