Hello I,m trying to build a chronograph just Using AVR 16 bit timer and interrupts .im using an ATmega8A running at 8Mhz with External crystal. I was using an Overflow interrupt to count seconds that should be 122 overflows per second at /1 prescaler . well look and see if you can see my problems it,s not working as expected ..
//------------------------ATmega8-16-bit-timer
#define F_CPU 8000000
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <Wire.h>//MUST include for LCD to work
#include <LiquidCrystal_I2C.h>//MUST include for LCD to work
LiquidCrystal_I2C lcd(0x27,16,2); //MUST have for LCD to work with I2C on ATmega
volatile uint16_t overflows = 0;
volatile uint8_t Seconds = 0;
volatile uint16_t Time0 = 0;
volatile uint16_t Time1 = 0;
volatile uint16_t result = 0;
void setup(void)
{
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
GICR = 0b11000000;//-General Interrupt Control Register turn on external INT0 and INT1
MCUCR = 0b00000110;//--INTERRUPT-SENSE-CONTROL-SET-TO-FALLING-EDGE on both inerrupts
DDRB = 0b00000001; //enable PORTB0 as OUTPUT
// TIMSK = 0b00000100 ;//--OVERFLOW INTERRUPT
sei(); // enable global interrupts
lcd.setCursor(0,0);
lcd.print(" it,s alive ");
delay(2000);
}//end setup
void loop(void)
{
result = Time1 - Time0 ;
lcd.setCursor(0,0);
lcd.print("Result ");
lcd.print(result);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Seconds ");
lcd.print(Seconds);
lcd.print(" ");
delay(500);
}//end loop
//-------------------------------------
ISR (INT0_vect)
{
TCCR1B = 0b00000001;//--START-timer-no prescaling
Time0 = TCNT1;
}
//-------------------------------------
ISR (INT1_vect)
{
Time1 = TCNT1;
// TCCR1B = 0b00000000;//--CLEAR-Timer
//TIMSK = 0x00 ;//--OVERFLOW INTERRUPT RESET
}
//---------------------------------------
ISR(TIMER1_OVF_vect)
{
overflows++;
if (overflows >= 122) // NOTE: '>=' used instead of '=='
{
Seconds++;
overflows = 0; // reset overflow counter
}
if(Seconds >= 6){Seconds = 0;PORTB ^= 0b00000001; }
}
I built a very simple input capture chronograph as a flash trigger, using not much more than a couple of photodiodes and a resistor (plus illuminator LEDs and optotriac).
well I have photodiodes and LDRs just for testing i used LDRs but i'm sure photodiodes are maybe faster ..maybe not don't know ..and I'm guessing the lm393 is faster than the lm358 dual op amp ..should I connect both negative inputs together and just use one POT to adjust sensitivity ? the lm393 will only sink so it will
have to trigger on negative edge if i use the comparator
well I have several options i guess i'm going for the simple yet most accurate approach I want to build a chronograph for my uncle who would be using a 22 cal or BOW but maybe slower or faster
bandmwhitt2013:
well I have photodiodes and LDRs just for testing i used LDRs but i'm sure photodiodes are maybe faster ..maybe not don't know ..and I'm guessing the lm393 is faster than the lm358 dual op amp ..should I connect both negative inputs together and just use one POT to adjust sensitivity ? the lm393 will only sink so it will
have to trigger on negative edge if i use the comparator
Why don't you stop guessing and do some research? Please post your schematic so we aren't discussing vague generalities.