Input Capture mode 2560

Hello! First of all, sorry my bad english. I have a projekt, where i need to use the input capture mode for measure the input signal. (it’s a square signal 1-40Hz) In the program i set the timer registers, but the ISR(TIMER3_CAPT_vect) doesnt run. Any idea?

#include  <TimerOne.h> 
#include  <TimerThree.h> 
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h> 


#define ICP PINB8
//#define icpPin 14;
volatile int inputPin = 8;
volatile int outputPin = 11;
volatile int ignitionPulse = 0;
volatile int timeold;
volatile int time;
volatile int Period;
volatile int rpmCount =0;
volatile int state=LOW;

void setup() {
  pinMode(inputPin, INPUT); // from ECU
  pinMode(outputPin, OUTPUT);
  
  Serial.begin(9600);
  
   // Initialize Timer
  cli();          // disable global interrupts
  TCCR3A = 0;     // set entire TCCR3A register to 0
  TCCR3B = 0;  // same for TCCR3B
  TCNT3 = 0;

  // set compare match register to desired timer count: 800 Hz
  OCR3B = 15000; // 800Hz 5; // 3 Hz
  OCR3A = 15001;
  // turn on CTC mode:
  TCCR3B |= (1 << WGM12);
  // turn on IC mode:
  TCCR3B |= (0<<ICNC1) | (1 << ICES3);
  // Set CS10 and CS12 bits for 1024 prescaler:
  TCCR3B |= (1 << CS30) | (1 << CS32);
  // enable timer compare interrupt:
  TIMSK3 |= (1 << OCIE3A);
  TIMSK3 |= (1 << OCIE3B);
  TIMSK3 |= (1 << ICIE3);
 
  // enable global interrupts:
  sei();
  
  //TCCR1B|=(1<<ICES1);
  //input capture megszakitas engedelyezese
  //TIMSK|=(1<<TICIE1);
  
  
  
  //attachInterrupt(0, inputPulse, CHANGE); 
  
  Timer1.initialize(3000);       // 
  Timer1.attachInterrupt(pwmMode);  // 

}

ISR(TIMER3_CAPT_vect)        // interrupt service routine
{ 
  timeold = time;
 time = (ICR3H << 8)| ICR3L;
 digitalWrite(10, state);
 Serial.println("icp isr tick:");
  Serial.print("\t"); 
  Serial.println( time);
  Serial.println( Period);
  TCNT3 = 0;
}

ISR(TIMER3_COMPA_vect)        // interrupt service routine 
{
   
}

ISR(TIMER3_COMPB_vect)        // interrupt service routine 
{
 
}
 Serial.println("icp isr tick:");
  Serial.print("\t"); 
  Serial.println( time);
  Serial.println( Period);

In an ISR? NO!

I see. Which pin i have to use for it (input), and how i can define it, in program code. (for example: #define icp pin....)

PaulS:

 Serial.println("icp isr tick:");

Serial.print("\t");
  Serial.println( time);
  Serial.println( Period);



In an ISR? NO!

Serial.print is verry slow - that's why you cannot have it in ISR routine. Use ISR just to change a flag and then use this flag i regular loop() to print whatever you want and change pin status.

Serial.print is verry slow - that's why you cannot have it in ISR routine.

While Serial.print() is relatively slow, that is NOT why you can't use it in an ISR. You can't use it in an ISR because the HardwareSerial class uses interrupts to send data. Those interrupts don't happen while an ISR is running, so if the output buffer gets full, the print() method will block waiting for room to become available in the buffer. Room will never become available, because the interrupts needed to make room are not happening.

I replaced the part of code for serial communication, but the ISR doesnt sense the input signal, and it doesnt run....which pin pin is the input capture input?

#include  <TimerOne.h> 
#include  <TimerThree.h> 
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h> 


#define ICP PINB0
//#define icpPin 14;
volatile int inputPin = 3;
volatile int outputPin = 11;
volatile int ignitionPulse = 0;
volatile int timeold;
volatile int time;
volatile int Period;
volatile int rpmCount =0;
volatile int state=LOW;
volatile int shift =0;

void setup() {
  pinMode(inputPin, INPUT); // from ECU
  pinMode(outputPin, OUTPUT);
  
  Serial.begin(9600);
  
   // Initialize Timer
  cli();          // disable global interrupts
  TCCR3A = 0;     // set entire TCCR3A register to 0
  TCCR3B = 0;  // same for TCCR3B
  TCNT3 = 0;

  // set compare match register to desired timer count: 800 Hz
  OCR3B = 15000; // 800Hz 5; // 3 Hz
  OCR3A = 15001;
  // turn on CTC mode:
  TCCR3B |= (1 << WGM12);
  // turn on IC mode:
  TCCR3B |= (0<<ICNC1) | (1 << ICES3);
  // Set CS10 and CS12 bits for 1024 prescaler:
  TCCR3B |= (1 << CS30) | (1 << CS32);
  // enable timer compare interrupt:
  TIMSK3 |= (1 << OCIE3A);
  TIMSK3 |= (1 << OCIE3B);
  TIMSK3 |= (1 << ICIE3);
 
  // enable global interrupts:
  sei();
  
    Timer1.initialize(300000);       // 
  Timer1.attachInterrupt(pwmMode);  

}

ISR(TIMER3_CAPT_vect)        // interrupt service routine
{ 
  shift=1;
  timeold = time;
 time = (ICR3H << 8)| ICR3L;

}




void loop() {
  digitalWrite(53, state);
  shift =0;
  while (shift==1)
  {
 Period = time-timeold;
 digitalWrite(9, state);
 digitalWrite(11, state);
 digitalWrite(53, state);
 Serial.println("icp isr tick:");
 Serial.print("\t"); 
  Serial.println( timeold);
 Serial.println( time);
 Serial.println( Period);

}
}

 void pwmMode()
{
  state=!state;  //
                
  }

I suggest you get rid of all that stuff that you obviously don't understand, and write a very simple program which just implements the interrupt, and get that to work, first.