Hi. I am stuck in my project. May someone more experienced as me amateur take a look at this? :-[
This is my intend:
- Use external trigger by interrupt and reset timer1 (prescaler finally will be 8 )
- Delay by COMPA to activate OUTPUT to a HIGH state
- Delay by COMPB to de-active OUTPUT to a HIGH state
- Wait for next external trigger (can take in my project about 250mS)
Currently I GND pin 2 to as external trigger
This is my sourcecode:
#include <avr/io.h>
#include <avr/interrupt.h>
#define LEDPIN 13
const byte interruptPin = 2;
long micros1;
long micros2;
void setup(){
Serial.begin(9600); // open the serial port at 9600 bps:
pinMode(LEDPIN, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
cli(); // disable global interrupts
TCCR1A = 0; // Clear TCCR1A
TCCR1B = 0; // Clear TCCR1B
TCCR1C = 0; // Clear TCCR1C
TCNT1 = 0; // Clear the timer
TIMSK1 = 0; // Clear the interrupt mask
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt channel A:
TIMSK1 |= (1 << OCIE1B); // enable timer compare interrupt channel B:
TCCR1A = B11110000; // Set OC1A/OC1B on Compare Match, clear OC1A/OC1B at BOTTOM
TCCR1B |= (1 << CS12) | (1 << CS10); // Set CS12 and CS10 bits for 1024 prescaler
OCR1A = 65; // COMPA
OCR1B = 40000; // COMPB
sei(); // Enable global interrupts
//attachInterrupt(0, start, RISING);
attachInterrupt(digitalPinToInterrupt(interruptPin), start, RISING);
}
void loop(){
}
void start(){
//void for external triger here
TCNT1 = 0; // Clear the timer
TCCR1B |= (1 << CS10); // prescaler=1 16 MHz clock
}
ISR(TIMER1_COMPA_vect){
digitalWrite(LEDPIN, HIGH);
micros1= micros();
Serial.print("TCC= ");
Serial.println(OCR1A);
}
ISR(TIMER1_COMPB_vect){
digitalWrite(LEDPIN, LOW);
TCCR1B = 0; //stop timer here
micros2=micros;
Serial.print("micros1=");
Serial.println(micros1);
Serial.print("micros2=");
Serial.println(micros2);
Serial.println(micros2-micros1);
}