Hi westfw,
I have one more question. Say, I want to the mcu to capture values via the ICP, Do I have to set both ICR and OCR values? Like if you take CTC mode for example - it had two variants where it uses both ICR and OCR. Could you please check the code below?
/******* Sketch to test Input Capture interrupt handling *********************************
functionality: measure length of pulses on the ICP pin with precision of 0.5 microseconds
Show the min and max pulse widths in microseconds on the serial port
**************************************************************************************************************/
#include <stdio.h> // for verbose debugging using sprintf
//#include "TimerOne.h"
#include "Timer.h"
#define icpPin 8 // ICP input pin on arduino
#define outPin 2 // output pin that will shadow the input, can be monitored with a scope
// some variables to help see that something is happening in the interrupt handlers
volatile unsigned int Value; // this stores the current ICR1 value
volatile unsigned int MinValue;
volatile unsigned int MaxValue;
volatile unsigned int Overflows;
volatile unsigned int PulseCount;
char buffer[800];
short i;
Timer t;
/* Overflow interrupt vector */
ISR(TIMER1_OVF_vect){ // here if no input pulse detected
Overflows++; // incriment overflow count
}
/* ICR interrupt vector */
ISR(TIMER1_CAPT_vect){
TCNT1 = 0; // reset the counter
if( bit_is_set(TCCR1B ,ICES1)){ // was rising edge detected ?
digitalWrite(outPin,HIGH ); // yes, set our output pin high to mirror the input
}
else { // falling edge was detected
Value = ICR1; // save the input capture value
digitalWrite(outPin,LOW ); // set our output pin low to mirror the input
PulseCount++;
}/*if(Value < MinValue) // update min or max values as appropriate
MinValue = Value;
if (Value > MaxValue)
MaxValue = Value;
} */
TCCR1B ^= _BV(ICES1); // toggle bit value to trigger on the other edge
}
/*ISR(TIMER0_COMPA_vect){
if(i<800){
buffer[i] = digitalRead(icpPin);
}i++;
}*/
int val = 0;
int ledPin = 11;
int analogPin = 3;
int pwmPin = 9;
void setup() {
Serial.begin(9600);
pinMode(outPin, OUTPUT); // declare the ledPin as an OUTPUT
//pinMode(icpPin, INPUT); // ICP pin (digital pin 8 on arduino) as input
//Timer1.initialize(8);
//Timer1.pwm(9,512);
//MinValue = 30000; // Initialise with extreme values
//MaxValue = 0;
//CTC Mode
TCCR1A |= (1<<COM1A1)|(1<<COM1A0) ; // this register set to 0!
TCCR1B |= (1<<WGM12)|(1<<CS12)|(1<<CS10); //prescaler 1024 //SET TO CTC MODE // NORMAL MODE!!, prescaler 8, rising edge ICP1 - this works
TCCR1B |= (1<<ICNC1)|(1<<ICES1); // enable input capture
OCR1A = 127;
TIMSK1 |= (1<<ICIE1)|(1<<TOIE1); // enable input capture interrupt for timer// enable overflow interrupt to detect missing input pulses
Serial.print("Finished setup\r\n");
//t.start(8);
//t.pwm(9,512);
//t.pulse(9, 0.008, LOW);
//t.oscillate(9,0.008,LOW);
//t.every(4, takeReading);
//t.oscillate(9,8, LOW);
//delayMicroseconds(8);
//t.oscillate(9,8, HIGH);
//delayMicroseconds(8);
//t.every(4000000, takeReading);
}
// this loop prints the number of pulses in the last second, showing min and max pulse widths
void loop() {
/* //char buffer[800];
// show the captured data, divide pulse widths by 2 to display time in microseconds
sprintf(buffer,"Got %d pulses: min=%d, max=%d (%d timer overflows)\r\n", PulseCount, MinValue / 2, MaxValue / 2, Overflows);
Serial.println(Value);
val = analogRead(pwmPin);
//analogWrite(ledPin, val/4);
//Serial.println(val);
Serial.print(buffer); // send the info to the serial port
/* reset variables ready for the next reading */
/*PulseCount = Overflows = 0; // reset counts
MinValue = 30000; // set values to extremes
MaxValue = 0;
delay(800); */ // wait 1 second for next update [/font]
}
/*
void takeReading(){
Serial.println(analogRead(9));
}*/
Best,
A