Serial.println is not working above 425kHz.......

Hi,
I am trying a calculate the pulse duration using Timer1 Input capture on Arduino Uno.
The Code is

#include <avr/interrupt.h>
#define capturePin 8
volatile int captureValue =0;
ISR(TIMER1_CAPT_vect){
  if(bit_is_set(TCCR1B,ICES1)){
    // if rising Edge is Detected
    TCNT1 = 0; // make the timer count 0
    TCCR1B ^= _BV(ICES1);
  }
  else {
    captureValue = ICR1;
    TCCR1B ^= _BV(ICES1);
  }
}
void setup()
{
  Serial.begin(9600);
  pinMode(capturePin,INPUT);
  cli();
  TCCR1A = 0;//disable the all Interrupts
  TCCR1B = 0;
  
  // Prescalar
  TCCR1B |=(1<<CS10);//Prescalar 1
  // TCCR1B |=(1<<CS10)| (1<<CS11); // Prescalar 8
 TIMSK1 |=(1<<ICIE1);//Input Capture Enable
 TCCR1B |=(1<<ICES1); //Enable Edge Rising Detection 
 sei(); // Enable all Interrupts
 }

void loop()
{
  Serial.println("Duration In Nano Seconds Are....");
  Serial.println(captureValue);
}

the code is working till 425khz…
above that frequency i am getting nothing on the serial Monitor…
How can i resolve this?

And you're using a baud rate of just 9600!

Do the maths.

@KenF the baud has nothing to do with.

However we have a 16Hz processor and that gives you 32 clock cycles at 500KHz, or at best 32 instructions per interrupt I think your just stuck in the ISR.

Mark

the code is working till 425khz..

I find that extremely hard to believe.

You are expecting to send a 44-character message, at 9600 baud every 2µS?

I think not.

However as holmes4 said, by the time you get to 2 µS you will be spending all your time in the ISR.

What are you really trying to achieve here?

You are expecting to send a 44-character message, at 9600 baud every 2µS?

I think not.

If you look at the code for loop() that is NOT what the code does. It just sends serial data as fast as it can. The OP would get the same problem (serial stops) at any baud rate.

He ends up stuck in the ISR as it has a higher priority than the interrupt used by Serial Tx.

Mark

Thankyou all for your Reply's. I am a Newbie In Arduino World. I hope You all Experts Here Wouldn't Mind For Silly Mistakes....

I am using a ultrasonic sensor to measure a tank level. it have a resolution of 1mm and send 1us pulse for every 1mm range.
so I need to measure a pulse width within us range. i want to send the duration to the serial monitor.

Like holmes said it has nothing to with baud rate. i changed to max 115200 and still getting the same.

by the time you get to 2 µS you will be spending all your time in the ISR.

if i send only number can i get the desired output?

The OP would get the same problem (serial stops) at any baud rate.

He ends up stuck in the ISR as it has a higher priority than the interrupt used by Serial Tx.

Yes. I am getting the same Problem. How to remove it.

holmes4:
If you look at the code for loop() that is NOT what the code does. It just sends serial data as fast as it can. The OP would get the same problem (serial stops) at any baud rate.

He ends up stuck in the ISR as it has a higher priority than the interrupt used by Serial Tx.

Yes, OK, but the strategy of trying to send that much data is still flawed in the way I described.

I am using a ultrasonic sensor to measure a tank level.

OK, back to this sensor. I estimate it would take a sound wave around 3 mS to travel 1 metre, and a period of 3 mS equates to a frequency of 333 Hz.

So if someone could explain where this requirement for measuring 425 kHz comes from, I would be grateful.

Surely you aren’t going to measure how deep the tank is 425,000 times per second? It must be raining pretty hard for that to be necessary.

I am using a ultrasonic sensor to measure a tank level. it have a resolution of 1mm and send 1us pulse for every 1mm range.
so I need to measure a pulse width within us range. i want to send the duration to the serial monitor.

Well, measure the tank once a second. Problem solved.

See here for a ping library, this is basically what you are doing, right?

http://playground.arduino.cc/Code/NewPing

Surely you aren't going to measure how deep the tank is 425,000 times per second?

Yes, I don't want to measure the tank depth at 425,000 times per second.
I just wanna make sure code will able to read pulse width of 1us. so i am giving a signal at that frequency.

See here for a ping library, this is basically what you are doing, right?

How can I measure the length of the pulse using ping library without describing the echo pin and trigger pin?

holmes4:
@KenF the baud has nothing to do with.

However we have a 16Hz processor and that gives you 32 clock cycles at 500KHz, or at best 32 instructions per interrupt I think your just stuck in the ISR.

Mark

Yes you’re right. He’s sending on EVERY itteration of the loop function. So that’s about every 14 microseconds (at a rough guess). That’ll fill the UART’s buffer PDQ.