Pierwotnie był tam głośniczek ze starej nokii (fotka na elektrodzie:
http://obrazki.elektroda.pl/2946337400_1363807867.jpg) ale był za cichy - dołączę chyba jakieś hałaśliwe piezo.
Program obsługujący jest na razie do bólu prosty - dla zainteresowanych zmieści się w poście:
Kod:
#include <avr/io.h>
#include <avr/interrupt.h>
volatile uint16_t time, wait=50, avg_cnt=20;
volatile uint32_t avg_sum=0, avg;
ISR(TIMER1_CAPT_vect)
{
static int16_t icr1;
static int16_t delta;
time = ICR1-icr1;
icr1 = ICR1;
if(wait) // power on wait
wait--;
else
if(avg_cnt>0) // measure LC frequency on startup
{
avg_cnt--;
avg_sum += time;
}
avg = avg_sum / 20;
delta = time - avg;
// no magnetic metal
if( delta < -10) PORTC = (1<<2);
if( delta < -20) PORTC = (1<<1);
if( delta < -60) PORTC = (1<<0);
// magnetic metal
if( delta > 10) PORTC = (1<<4);
if( delta > 60) PORTC = (1<<5);
// nothing
if (( delta <= 10) && (delta >= -10)) PORTC = (1<<3);
// autotune
if(time > avg)
avg_sum += 5;
if(time < avg)
avg_sum -= 5;
}
int main(void)
{
// Timer0 prescaler div 0xff
TCCR0B = (1<<CS01) | (1<<CS02);
OCR0B = 128;
TCCR0A = (1<<COM0B0);
DDRD = (1<<5); // output from prescaler
// Timer1 as capture
TCCR1B = (1<< CS10) | (1<< WGM13); // clock 20MHz
TCCR1B = (1<< CS10) | (1<< ICNC1);
TIMSK1 = (1<< ICIE1);
PORTD = (1<<4); // pull up for input
DDRC = (1<<0) | (1<<1) |(1<<2) |(1<<3) |(1<<4) |(1<<5); // LEDs
sei();
while(1); //
}
Pozdrawiam