Pages: [1]   Go Down
Author Topic: how to access "Input Capture Interrupt", Atmega328, USB Boarduino  (Read 1043 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pretty advanced question here:

I'm trying to detect a signal on the ICP1 (input capture pin D8 of the Atmega328 chip, USB Boarduino). I have Timer 1 set up to count with a prescaler of 1024 and if there is a rising edge on the ICP1 pin then the chip interrupts and in the ISR copies TCNT1 (timer counter1) to the ICR (Input Capture Register)... Then in the regular loop I'm just outputting the values serially.

I'm sending in a 60Hz 2% TTL signal to pin D8 (ICP1).

And so far all I get is a bunch of 0000.

Any tips, ideas, suggestions?

Thank you

Code:
#include "wiring_private.h" //need this to use sbi ,cbi commands
#include "Wire.h"         


// variable here

byte inputCapture = 8;
volatile word period = 0;

//ISR here

ISR (TIMER1_CAPT_vect) //Timer1 capture vector
{
   cli();                    // deactivate other interrupts
   period = ICR1;      //copy Time Stamp
  TCNT1 = 0x0000;  //reset Timer1 counter.

   sei();               // reactivate other interrupts
}


void setup()
{
  sbi(TCCR1B,CS12); //set prescaler to 1024 on Timer1
  cbi(TCCR1B,CS11);
  sbi(TCCR1B,CS10);
 
  cbi(TCCR1B,WGM13); // "waveform generation mode bit description"
  cbi(TCCR1B,WGM12); // normal counter mode
  cbi(TCCR1A,WGM11);
  cbi(TCCR1A,WGM10); 
 
  sbi(TCCR1B,ICES1);      //trigger Input Capture on rising edge
  //sbi(TCCR1B,ICNC1);  //enable Capture Noise Canceler
 
  pinMode(inputCapture, INPUT);
 
  Serial.begin(9600);

  sbi(TIMSK1,ICIE1);  // Input Capture interrupt enable
  sbi(PRR,PRTIM1);    //enable Timer/Counter1 (extra redundancy)
  sei();                     //activate global interrupts (redundancy)
}

void loop()
{
  Serial.print("period= ");
  Serial.print(period);
  Serial.print("\tTCNT1= ");
  Serial.println(TCNT1);
  delay(500);
}
Logged

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

Silly ME!! instead of redundantly Enabling the timer I disabled it!!

here's the fix

Code:
  sbi(TIMSK1,ICIE1);  // Input Capture interrupt enable
  cbi(PRR,PRTIM1);    //enable Timer/Counter1 (extra redundancy) <<<<<<<----------
  sei();                     //activate global interrupts (redundancy)
Logged

Pages: [1]   Go Up
Jump to: