Counter option in arduino Uno

hi all,

I am pretty new to Arduino Uno and have been using it for last two weeks. I am sending a scanned pulse wave to pin 7 from a scanner and trying to read the timings whenever there is a transition in the pulse.(from high to low and from low to high) as I want to regenerate the pulse through its timings in matlab so that source of glitches can be find out.

Before I was using micros and millis function to read the system clock as soon as there is transition. But I found out that system clock is not giving the consistent result as it is a real time clock and doesn't execute the instruction as soon as it is requested (wrote a program and put delay(100); for 10 times and read the system clock. But it is giving delay of 100, 104, 108 & 112 microseconds randomly)

So, I need to use some other thing as counter and since, I need to measure microseconds, I think I need its precision to measure nanoseconds. Can anybody tell me that is there any inbuilt counter for such purpose on the arduino uno board or I have a design a separate counter and interface it with the board? I would really appreciate if anybody can light up the interfacing part too.

PS: It has Atmega328P-PU micro-controller.


Thanks Neha :)

I am sending a scanned pulse wave to pin 7 from a scanner

Nope, I failed to understand right there.

You need to tell us what sort of signal you're trying to process, what sort of constraints there are, and what you expect to find (and how often)

Thanks...let me try to explain it in another way.

I have a bar pattern of three lines(varying width) placed at a distance of 50cm from a laser scanner and scanned signal is a pulse with '1' corresponding to the black line and '0' to white(spaces between black bars). The width of the pulse is proportional to time (in microsecond) scanner takes to scan.

Scanner does 104 scans/sec. one from left to right and second from right to left but the considered significant scan is from left to right.

Now that scanned signal has been input to digital pin 7 and I want to analyse that incoming scanned signal. So, I need the time whenever there is transition from '0'to'1' or '1'to'0' as the scanned signal has lot of gliches (like instead of three pulses of '1' in three line pattern it is giving me 3,5,8,..upto 15 pulse) so, I am trying to remove those glitches but for that I am trying to observe the pattern in which they repeat themselves so that i can remove them.

I hope I am able to present in clearly now. Please tell me if you have any other questions.


Neha

So, each scan takes about 9.6ms, but you’re only interested in every other scan, so your acquisition rate is only about once every 19.2ms.
Within your 9.6ms there are three or more pulses, and you need to time the edges of the pulses…to what sort of accuracy?

Yes, you are right. I think I need a counter measuring in nanoseconds. My accuracy is already lost in the execution of the command as I was doing it like:
for (i=0; i<20; i++){ //for one scan considering 15 pulses in case of random glitches
while(digitalRead(scan_signal==LOW)){ }
start*=micros(); //But this is not accurate as micros takes time of real time clock*
while(digitalRead(scan_signal==HIGH)){ }
stop*=micros();}*
So, I want it to be as accurate as it can be by substituting an alternative for micors();
-------------------------------------
neha

You probably will gain more by using direct port access.

You probably will gain more by using direct port access.

Can you please explain a little more about this direct port access or direct me to the relevant link?


Thanks Neha

I am trying to remove those glitches but for that I am trying to observe the pattern in which they repeat themselves so that i can remove them.

...is the only goal you have provided. Is that the only thing your application does? Remove glitches from a stream of pulses?

I strongly suggest you separate the two goals. First, collect raw high-speed timing data. Second, analyze the data for glitches. I suspect the more separation you can achieve the better the solution.

As for collecting high-speed timing measurements, the "Input Capture" unit on timer 1 will do what you want.

Sir,

for collecting high-speed timing measurements, the "Input Capture" unit on timer 1 will do what you want.

can you please elaborate a little on this or give me any sample code which uses the "Input Capture" unit so that I can understand it in a better way.


Thanks Neha

arduino_ques: can you please elaborate a little on this

Not as well as the stuff you can find on your own... http://www.google.com/search?q=%22input+capture%22+site:arduino.cc

or give me any sample code which uses the "Input Capture" unit so that I can understand it in a better way

I believe either or both of these libraries makes use of "Input Capture"...

http://www.pjrc.com/teensy/td_libs_FreqMeasure.html http://www.pjrc.com/teensy/td_libs_FreqCount.html

You may have to modify the libraries for use on your processor (or buy a Teensy).

Hi all,

for last few days, I have been trying to collect high speed timing data using Input capture unit on arduino uno. But I am loosing some of my timing events during the execution of program. Basically, I have two signals "scan & data" frequency of scan signal is 50hz but I am reading the data only when scan signal is LOW and ignoring when it is HIGH. my data signal can have upto 15 pulses thus 30 edge transitions in about 10ms. I am using Timer1 with prescalar of 8. I am doing it something like this:

ISR(TIMER1_CAPT_vect){
  iTransitionEdge = ICR1;
    TCCR1B ^= _BV(ICES1);                 // toggle bit value to trigger on the other edge    
}
do{
 cli();
 rise = iTransitionEdge;
 sei();
  Serial.println( rise, DEC); 
  } while(digitalRead(scan)==LOW);

while my data is coming on ICP pin but here I am loosing events as same number is printed again & again on serial port . I think during the execution clock cycles are getting used thus resulting in loosing events. according to me, a solution can be to store data in memory location and read it later on instead of printing it in while loop. But I am not sure how to do that. I will appreciate any kind of suggestions.

Thanks Neha :)

please help me.... :~

same number is printed again & again on serial port

As long as the scan pin is LOW, that's what will happen (your do..while loop). At a minimum, you will need a "got fresh value" flag shared between the interrupt service routine and loop. The flag is set in the ISR, read and cleared in loop (with interrupts disabled).

a high speed counter is required as I need to measure time in micro even nano seconds and I want it to be more software controlled so that I can read the value of counter whenever I want to.

nano seconds timeing is out of the Arduino range. You noticed (or not) that the micros() return multiples of 4 micros all the time. That is about the smallest unit of time to measure in practice.

At those speeds you can't use Serial.print() as that uses way too much time causing missing pulses. You should capture the data into an array and when capture is done, there is time to print.

// wrt performance, maybe the mbed processor is a better choice.

We are able to achieve it b/w +/- 1 degree but our aim is reduce it to one milliradian.

What is the purpose of being accurate in the milliradians area 1 degree = 6.28../360 = 17.45 milliradians So you want to improve say 20-fold. (1 milliradian ~ 3.5 arcseconds?)

Can you stabelize the scandevice to the same degree (eh milliradian?) If so how do you do that? IIRC temperature fluxes can disturb your experiment at that level of precision...

wrt performance, maybe the mbed processor is a better choice.

can you tell me which embedded processor will be better?

Can you stabelize the scandevice to the same degree (eh milliradian?)

not yet sir, but will let you know if I am able to do that.

Thanks Neha

HI Neha,

the mbed processor can be found at http://mbed.org/handbook/mbed-Microcontroller it is a 100MHz 32 bitter. There are other ones that would be sufficient too.
Measuring in the nanoseconds means a processor that is minimal around 100 Mhz, and more is better here.

Can you stabilize the scandevice to the same degree (eh milliradian?)
not yet sir, but will let you know if I am able to do that.

I love to hear how you did it …

the mbed processor

sorry for my earlier post. shows how much ignorant I am about mbed processor :zipper_mouth_face: