[solved] Hall in D2 + Timer0 sketch to count frequency = bizarre result
adding 1o kohm
object: measure frequency
sensor: hall attached pin d2
arduino uno
sketch (below)
// frequency timer; reply 1
// author: nick gammon
// date: 10th february 2012
// input: pin d2
data , questions
1)
with hall in d2:
example of 1 display:
" took: 319905 counts. frequency: 50.01 hz. "
hz moves every 500ms between 50 +/- 2hz approx.
q).- 50 +/- 1 hz coherent result?
2)
without sensor in d2: readings same in case 1).
i think no signal in d2 attachinterrupt(0, isr, rising) doesn't fire reading should have stopped.
q).- wrong?
thank in advance
____________________________________
sketch
volatile boolean first;
volatile boolean triggered;
volatile unsigned long overflowcount;
volatile unsigned long starttime;
volatile unsigned long finishtime;
// here on rising edge
void isr ()
{
unsigned int counter = tcnt1; // save it
// wait until noticed last one
if (triggered)
return;
if (first)
{
starttime = (overflowcount << 16) + counter;
first = false;
return;
}
finishtime = (overflowcount << 16) + counter;
triggered = true;
detachinterrupt(0);
} // end of isr
// timer overflows (every 65536 counts)
isr (timer1_ovf_vect)
{
overflowcount++;
} // end of timer1_ovf_vect
void prepareforinterrupts ()
{
// ready next time
eifr = bit (intf0); // clear flag interrupt 0
first = true;
triggered = false; // re-arm next time
attachinterrupt(0, isr, rising);
} // end of prepareforinterrupts
void setup ()
{
serial.begin(115200);
serial.println("frequency counter");
// reset timer 1
tccr1a = 0;
tccr1b = 0;
// timer 1 - interrupt on overflow
timsk1 = bit (toie1); // enable timer1 interrupt
// 0 it
tcnt1 = 0;
overflowcount = 0;
// start timer 1
tccr1b = bit (cs20); // no prescaling
// set interrupts
prepareforinterrupts ();
} // end of setup
void loop ()
{
if (!triggered)
return;
unsigned long elapsedtime = finishtime - starttime;
float freq = f_cpu / float (elapsedtime); // each tick 62.5 ns @ 16 mhz
serial.print ("took: ");
serial.print (elapsedtime);
serial.print (" counts. ");
serial.print ("frequency: ");
serial.print (freq);
serial.println (" hz. ");
// can read
delay (500);
prepareforinterrupts ();
} // end of loop
object: measure frequency
sensor: hall attached pin d2
arduino uno
sketch (below)
// frequency timer; reply 1
// author: nick gammon
// date: 10th february 2012
// input: pin d2
data , questions
1)
with hall in d2:
example of 1 display:
" took: 319905 counts. frequency: 50.01 hz. "
hz moves every 500ms between 50 +/- 2hz approx.
q).- 50 +/- 1 hz coherent result?
2)
without sensor in d2: readings same in case 1).
i think no signal in d2 attachinterrupt(0, isr, rising) doesn't fire reading should have stopped.
q).- wrong?
thank in advance
____________________________________
sketch
volatile boolean first;
volatile boolean triggered;
volatile unsigned long overflowcount;
volatile unsigned long starttime;
volatile unsigned long finishtime;
// here on rising edge
void isr ()
{
unsigned int counter = tcnt1; // save it
// wait until noticed last one
if (triggered)
return;
if (first)
{
starttime = (overflowcount << 16) + counter;
first = false;
return;
}
finishtime = (overflowcount << 16) + counter;
triggered = true;
detachinterrupt(0);
} // end of isr
// timer overflows (every 65536 counts)
isr (timer1_ovf_vect)
{
overflowcount++;
} // end of timer1_ovf_vect
void prepareforinterrupts ()
{
// ready next time
eifr = bit (intf0); // clear flag interrupt 0
first = true;
triggered = false; // re-arm next time
attachinterrupt(0, isr, rising);
} // end of prepareforinterrupts
void setup ()
{
serial.begin(115200);
serial.println("frequency counter");
// reset timer 1
tccr1a = 0;
tccr1b = 0;
// timer 1 - interrupt on overflow
timsk1 = bit (toie1); // enable timer1 interrupt
// 0 it
tcnt1 = 0;
overflowcount = 0;
// start timer 1
tccr1b = bit (cs20); // no prescaling
// set interrupts
prepareforinterrupts ();
} // end of setup
void loop ()
{
if (!triggered)
return;
unsigned long elapsedtime = finishtime - starttime;
float freq = f_cpu / float (elapsedtime); // each tick 62.5 ns @ 16 mhz
serial.print ("took: ");
serial.print (elapsedtime);
serial.print (" counts. ");
serial.print ("frequency: ");
serial.print (freq);
serial.println (" hz. ");
// can read
delay (500);
prepareforinterrupts ();
} // end of loop
hi, can post copy of circuit please including how powering project, cad or picture of hand drawn circuit in jpg, png or pdf.
it looks though there no connection input of arduino @ all, 50hz radiation mains power, assume mains power 50hz.
check connections hall effect device , spec number?
hope help.
tom......
it looks though there no connection input of arduino @ all, 50hz radiation mains power, assume mains power 50hz.
check connections hall effect device , spec number?
hope help.
tom......

Arduino Forum > Using Arduino > Programming Questions > [solved] Hall in D2 + Timer0 sketch to count frequency = bizarre result
arduino
Comments
Post a Comment