jayanth.devarayanadurga
Banned
- Joined
- Dec 4, 2012
- Messages
- 4,280
- Helped
- 822
- Reputation
- 1,654
- Reaction score
- 791
- Trophy points
- 1,393
- Location
- Bangalore, India
- Activity points
- 0
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 #include <16f877a.h> #device adc=10 #fuses HS,NOWDT,NOLVP,NOCPD,NODEBUG #use delay(clock=4M) #define useTimer1 //#define useTimer2 float volt, oldval, volt1; int count = 0; unsigned int8 time = 0, clap = 0; #ifdef useTimer1 #INT_TIMER1 void TIMER1_isr(void) { if(interrupt_active(int_timer1)) { time++; } set_timer1(0x0BDB); clear_interrupt(int_timer1); } #elif useTimer2 #int_timer2 void timer2_isr(void) { ; } #endif void clear(){ clap = 0; time = 0; diaable_interrupts(INT_TIMER1); } void main() { set_tris_a(0xff); set_tris_b(0x00); set_tris_c(0x00); set_tris_d(0x00); //Analog module configuration for reading the voltage from the PIN_AN0 setup_comparator(NC_NC_NC_NC); setup_adc(ADC_CLOCK_INTERNAL); setup_adc_ports(AN0); set_adc_channel(0); setup_vref(FALSE); //Timer Configuration #ifdef useTimer1 //Timer1 //Prescaler 1:8; TMR1 Preload = 3035; Actual Interrupt Time : 500 ms setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); set_timer1(0x0BDB); #elif //Code for Timer2 configuration #endif enable_interrupts(GLOBAL); while(1) { delay_us(20); read_adc(adc_start_only); delay_us(100); volt = read_adc(adc_read_only); volt = volt * 30.0/ 1023.0; if((volt > 5) && (clap == 0){ clap++; enable_interrupts(INT_TIMER1); } if((volt > 5) && (clap == 1){ if(time < 4){ clap++; output_low(PIN_C2); clear(); } } if((clap == 1) && (time > 4)){ output_high(PIN_C2); clear(); } } }
if((volt > 5) && (clap == 0))
if((clap == 1) && (time > 4))
volt = volt * 5.0/ 1023.0; //I think 30.0 has to be replaced by 5.0
Code C - [expand] 1 2 3 4 5 f((volt > 5) && (clap == 0){ clap++; enable_interrupts(INT_TIMER1); time = 0 // to clear and start the time variable }
if((volt > 5) && (clap == 0){
if((clap ==1) && (time > 4)){
output_high(PIN_D3);
clear();
}
if((volt>1) && (time > 4)){
output_high(PIN_D3);
clear();
}
where may I put those codes,on which line?Why are you usingwith volt >5 and this volt can not exceeds 1.4 volts?PHP:if((volt > 5) && (clap == 0){
Thanks.
time is multiples of 0.5 sec at the same time volt is multiple of (5*30/1024).. so you can change it according to your clap magnitude....
I have added the time = 0 line in the if statement which already exists.....
Use an amplifier and comparator and INT0 pin to detect clap. It will be better than using ADC. After the first clap Timer will be running and timer interrupt will be occuring every 0.5 sec (500 ms) and if you make second clap and the uC is servicing timer interrupt at that moment then adc will not read. So the second clap will not be detected properly.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 #include <16f876a.h> #device adc=10 #fuses HS,NOWDT,NOLVP,NOCPD,NODEBUG #use delay(clock=4M) //#define useTimer1 //#define useTimer2 float volt, oldval, volt1; int count = 0; unsigned int8 time = 0, clap = 0; //#ifdef useTimer1 #INT_TIMER1 void TIMER1_isr(void) { if(interrupt_active(int_timer1)) { time++; } set_timer1(0x0BDB); clear_interrupt(int_timer1); } //#elif useTimer2 //#int_timer2 //void timer2_isr(void) //{ // ; //} //#endif void clear(){ clap = 0; time = 0; disable_interrupts(INT_TIMER1); } void main() { set_tris_a(0xff); set_tris_b(0x00); set_tris_c(0x00); //Analog module configuration for reading the voltage from the PIN_AN0 setup_comparator(NC_NC_NC_NC); setup_adc(ADC_CLOCK_INTERNAL); setup_adc_ports(AN0); set_adc_channel(0); setup_vref(FALSE); //Timer Configuration //#ifdef useTimer1 //Timer1 //Prescaler 1:8; TMR1 Preload = 3035; Actual Interrupt Time : 500 ms setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); set_timer1(0x0BDB); //#elif //Code for Timer2 configuration //#endif enable_interrupts(GLOBAL); while(1) { delay_us(20); read_adc(adc_start_only); delay_us(100); volt = read_adc(adc_read_only); //volt = volt * 10.0/ 1023.0; if((volt > 10) && (clap == 1)){ if(time < 4){ clap++; output_low(PIN_C2); clear(); } } if((volt > 10) && (clap == 0)){ clap++; enable_interrupts(INT_TIMER1); } if((clap == 1) && (time > 4)){ output_high(PIN_C2); clear(); } volt = 0; } }
yeah its ok because you are stopping the timer, I told that for continuously running timer.......Yes. I know that but ADC reading and conversion will take so time in ms and even during this process there will be timer interrupt occuring and if time value becomes more than 4 (2 sec) then the if() condition will not be executed. He can check that be using
in the isr code.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?