Pages: 1 2 3 [4]   Go Down
Author Topic: Measuring Pulse Width  (Read 20172 times)
0 Members and 2 Guests are viewing this topic.
Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 221
Posts: 13848
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is strange. Seems like it is triggered around the time the timer overflows. Can't explain it yet

Can you tell more about how you connected things?
which function generator you use ?
Did you use a pull down/up resistor?
Connected gnd's?
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@ Rob

I am using a simple function generator and giving the pulses from it and yes I have connected both the grounds of the arduino board. I am applying the pulses to the BNC connector and from there I am giving the pulses to the digital pin 3 (D3) and I am not using pull down/up resistor. But did you also check the code output? Can you too check the output and then make the comparison with mine?

Thanks
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 221
Posts: 13848
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Not using a pull down resistor can cause that the line keeps high for a longer period than the pulselength (RC effect ?) giving you results you would not expect.

No time to dive into it as one of my own experiments is not working now smiley-sad
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@ Rob

I have made few changes in the code and now the code is working fine smiley check it out but it only misses out 1 count I don't know how we can improve that? I mean for 1 microsecond pulse it is doing 15 counts (theoretically 16) can we give a external high clock to the timer of the arduino? The code is given below

Code:
#include <avr/io.h>
#include <avr/interrupt.h>

volatile uint16_t cnt = 0;
unsigned int pulse_counts = 0;

void setup()
{
  Serial.begin(115200);
  Serial.println("PulseWidthMeter (timer1) 0.1");
  pinMode(3, INPUT);
}

void loop()
{
  // initialize Timer1
  cli();
  // reset counters
  cnt = 0;
  TCNT1 = 0;
  // reset registers
  TCCR1A = 0;
  TCCR1B = 0;

  pulse_counts++;
  // wait for HIGH
  while ((PIND & B00001000) == B00000000);
  // Set CS10 bit so timer runs at clock speed: 16 MHz
  TCCR1B |= (1 << CS10);
  // enable Timer1 overflow interrupt:
  TIMSK1 = (1 << TOIE1);

  // enable global interrupts:
//  sei();

  // keep counting until LOW
  while ((PIND & B00001000) == B00001000);
  // stop IRQ's


  TIMSK1 = 0;
  TCCR1B = 0;
 // cli();

    // Read the counters and convert to long
  uint16_t x = TCNT1;  // work copy
  uint32_t total = (uint32_t)cnt  + (uint32_t)x;
  float usec = (1.0 * total) / 16;

  // Display values
  Serial.print(x, DEC);
  Serial.print(" \t ");
  Serial.print(usec, 2);
  Serial.print(" \t ");
  Serial.print("Count = ");
  Serial.println(pulse_counts);

  // Wait a while
//  delay(1000);
}

// count the overflows in IRQ
ISR(TIMER1_OVF_vect)
{
  cnt++;
}
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 221
Posts: 13848
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good to hear that it works, see you removed the cli()/sei() flags. They are not needed as the TCCR1B starts/stops the timer1. They were a leftovers of my experiments, sorry.

In the first microsecond Timer1 misses a few cpu ticks before it is started as the starting of Timer1 itself takes at least one instruction. That can be added in the formula later. Furthermore the 65535L should be 65536L. This results in the following code:

Code:
//
//    FILE: PulseWidthMeter.pde
//  AUTHOR: Rob Tillaart
//    DATE: 2012-apr-01
//
//    LINK: http://arduino.cc/forum/index.php?action=post;topic=96971.0
//

#include <avr/io.h>
#include <avr/interrupt.h>

volatile unsigned int count = 0;
unsigned int pulseCounter = 0;

void setup()
{
  Serial.begin(38400);
  Serial.println("PulseWidthMeter (timer1) 0.2");
  pinMode(3, INPUT);
}

void loop()
{
  // reset Timer1 registers and the counters (timer must be stopped before reset of counters!.
  TCCR1A = 0;
  TCCR1B = 0;
  count = 0;
  TCNT1 = 0;
  TIMSK1 = (1 << TOIE1);                      // enable Timer1 overflow interrupt:

  while ((PIND & B00001000) == B00000000);    // wait for HIGH
  TCCR1B |= (1 << CS10);                      // Set CS10 bit so timer runs at clock speed: 16 MHz
  while ((PIND & B00001000) == B00001000);    // wait for low
  TCCR1B = 0;                                 // stop counting
  pulseCounter ++;

  // Read the counters and convert to long
  unsigned long total = count * 65536L + TCNT1;  // Might need a +1 to correct start/stop calibration.
  float usec = (1.0 * total) / 16;

  // Display values
  Serial.print(total, DEC);
  Serial.print(" \t ");
  Serial.println(usec, 1);
  Serial.print("pulses: ");
  Serial.println(pulseCounter);
}

// count the overflows in IRQ
ISR(TIMER1_OVF_vect)
{
  count++;
}
Maybe move these statements from loop to setup, to make it absolutely minimal
  TCCR1A = 0;
  TCCR1B = 0;
  TIMSK1 = (1 << TOIE1);                      // enable Timer1 overflow interrupt:
« Last Edit: April 01, 2012, 12:23:31 pm by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it possible to use this sketch to measure two signals at this same time?
Both signals start in the same moment but they have different period.
Logged

Pages: 1 2 3 [4]   Go Up
Jump to: