now detecting sound

This commit is contained in:
bigalex 2012-10-05 22:43:38 +02:00
parent 43a7f3f89f
commit f1690c6cd1
2 changed files with 46 additions and 15 deletions

View File

@ -58,12 +58,18 @@ void init_buzzr(void){
void init_mic(void){
//its on B2 and C5, for reasons
//
DDRC |= (1 << PORTC5);
DDRB |= (1 << PORTB2);
//switch it off
buzzr_off();
//buzzer is on B2 and C5, for reasons
// we use it as a mic
DDRC &= ~(1 << PORTC5); //switch C5 to input
DDRB |= (1 << PORTB2); //B2 as output
PORTB &= ~(1 << PORTB2);//and to GND
ADMUX = (1<<REFS1) | (1<<REFS0); //use internal 1.1V as reference
ADCSRA = (1<<ADPS1) | (1<<ADPS0);// prescaler F_CPU/8
ADCSRA |= (1<<ADEN); // ADC enable - turn it on
// do one conversion
ADCSRA |= (1<<ADSC);
while (ADCSRA & (1<<ADSC) ) {} //wait for conversion to end
uint16_t dummy = ADCW; //read once
return;
}

View File

@ -44,26 +44,51 @@ void modeswitch_poll(void){
void do_mode0(void){
static timer_t mytimer;
static uint16_t maxval;
uint16_t curval;
if (ModeChanged){
timer_set(&mytimer, 10);
maxval=0;
ModeChanged = false;
buzzr_up();
ModeChanged = false;
init_mic();
maxval=0;
};
if(timer_expired(&mytimer)){
//USART0_put_uint16(maxval);
//USART0_crlf();
timer_set(&mytimer, 10);
USART0_put_uint16(maxval);
USART0_crlf();
timer_set(&mytimer, 1);
if (maxval>0) {led_on(LED_R|LED_L);} else {led_off(LED_R|LED_L);};
maxval=0;
buzzr_inv();
}; //end if timer_expired
maxval++;
}; //end if timer_expired
// single measurement
ADMUX = (ADMUX & ~(0x1F)) | 5; // select channel 5
ADCSRA |= (1<<ADSC); // start single conversion
while (ADCSRA & (1<<ADSC) ) {}; // wait for conversion to end
curval =ADCW; // read result
maxval = (curval>maxval) ? curval : maxval;
}; //end do_mode0
void do_mode1(void){
static uint16_t tone;
if (ModeChanged){