Skip to content

Instantly share code, notes, and snippets.

@derfaq
Created January 18, 2018 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derfaq/02d22d32524d48a8db3488100cd21998 to your computer and use it in GitHub Desktop.
Save derfaq/02d22d32524d48a8db3488100cd21998 to your computer and use it in GitHub Desktop.
/*
frequencyAC_AVR
***************
*/
volatile uint32_t count = 0;
volatile uint8_t semiCicle = 0;
void initInputCaptureInterrupt(void){
TCCR1A = 0; // Timer/Counter1 Control Register A, reset (Normal operation)
TCCR1B = 0; // Timer/Counter1 Control Register B, reset
// Not Noise Canceler, Capture Event on falling
TCCR1B |= (1<<CS11); // Clock Select Bit, prescaler 8 >> 1/2 uS per count
// 20.000 counts for 10ms
TIMSK1 |= (1<<ICIE1); // Timer/Counter1 Interrupt Mask Register
// Input Capture Interrupt Enable
sei(); // Set (global) interrupt enable bit
}
ISR(TIMER1_CAPT_vect){
count += ICR1; // Input Capture Register 1
//* resetear contador
GTCCR |= (1<<TSM)|(1<<PSRSYNC); // halt timer1 and 0
TCNT1 = 0; // Timer/Counter1
GTCCR = 0; // release all timers
//* activar flag de nuevo dato
semiCicle++;
}
void setup(){
Serial.begin(9600);
initInputCaptureInterrupt();
}
void loop(){
if(semiCicle>99){
Serial.println(count);
count=0;
semiCicle = 0;
}//Data sent every 1s, 250 samples per second
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment