Interesting article How to Program an Arduino Based Metal Detector
Announcement
Collapse
No announcement yet.
How to Program an Arduino Based Metal Detector
Collapse
X
-
It's better to use one PWM pin (OCR1A) for driving the MOSFET without servicing any interrupts.
Pages and table references for Atmega328P datasheet at https://www.sparkfun.com/datasheets/.../ATMega328.pdf
PHP Code:/* Pages and table references for Atmega328P datasheet at https://www.sparkfun.com/datasheets/Components/SMD/ATMega328.pdf Clock assumed to be 16MHz. */ #define F_CPU 1600000L uint sampleDelay = 16 * 10; // 10 us sample delay; void init(void) { /* Timer 1 in Fast PWM mode 14 (page 136, Table 13-4) */ TCCR1B |= (1 << WGM13) | (1 << WGM12); TCCR1A |= (1 << WGM11); /* Compare output mode in Fast PWM: set OC1A at BOTTOM (0 count), reset OC1A on compare match. page 135, Table 13-2 */ TCCR1A |= (1 << COM1A1); /* Compare match registers for timer 1 */ OCR1A = 4000; // 250us pulse OCR1B = 4000 + sampleDelay; // 16 = 1us /* The TOP value of timer 1 for Fast PWM 14 is set by the ICR1 register (page 139) */ ICR1 = 16000; // 1ms period, 1000 Hz pulse rate. /* ADC configured to sample on timer 1 compare match B and generate interrupts on conversion complete */ ADCSRB |= (1 << ADTS2) | (1 << ADTS0); // ADC trigger source: compare match B in timer 1 ADCSRA |= (1 << ADATE) | (1 << ADEN) | (1 << ADIE); // Autotrigger, enable interrupt, enable ADC ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Prescaler 1/128; 125 KHz /* Start pulsing */ TCCR1B |= (1 << CS10); // start counter at maximum clock speed } int main (void) { cli(); init(); sei(); while (1){ // do your thing } } ISR(ADC_vect){ // do your thing }Attached Files
-
I built one just for understanding purposes and it's working: https://www.youtube.com/watch?v=LwxB16ynQGs
Thanks!
Comment
Comment