Non mi è chiaro come interpretare i valori che leggo da ICR1.
Grazie
- Codice: Seleziona tutto
volatile unsigned char current_edge = 0;
volatile uint16_t starting_cnt;
volatile uint16_t ending_cnt;
uint16_t time_diff;
//ISR(TIMER1_OVF_vect) {
//
// tick ++;
//
// if(tick == 1) {
//
// Led_On();
// }
// else if(tick > 2 & tick <= 20) {
//
// Led_Off();
// }
// else {
//
// Led_On();
// delay(100);
// Led_Off();
// delay(100);
// }
//}
ISR(TIMER1_CAPT_vect) {
if(current_edge == 0) {
// Save timestamp
starting_cnt = ICR1;
//Serial.println("starting_cnt");
//Serial.println(starting_cnt);
//Switch to rising edge
TCCR1B |= (1 << ICES1);
current_edge = 1;
}
else if(current_edge == 1) {
//Save timestamp
ending_cnt = ICR1;
//Serial.println("ending_cnt");
//Serial.println(ending_cnt);
//Switch to falling edge
TCCR1B &= ~(1 << ICES1);
current_edge = 2;
}
TIFR1 |= (1 << ICF1);
}
void setup() {
Serial.begin(9600);
/*** Timer 1 overflow ***/
// DDRB |= 1 << 5;
//
// PORTB &= ~ (1 << 5);
//
// TCCR1A = 0x0;
//
// TCCR1B = (1 << CS11) | (1 << CS10); // Prescaler a 64
//
// TIMSK1 |= 1 << TOIE1; // Abilito l'interrupt Overflow
//
// sei();
/*** Timer 1 PWM ***/
// TCCR1B = (1 << CS11); // Prescaler a 8
//
// TCCR1A = (1 << WGM10) | (1 << WGM11); // Scelgo il tipo di onda PWM
//
// TCCR1A |= (1 << COM1A1); // Scelgo il pin di uscita
//
// OCR1A = 127 ; // Duty cycle al 50 %
/*** Timer 1 Input Capture ***/
DDRB = B00000000; // Pin 8 come ingresso
TCCR1A = 0;
TCCR1B |= (1 << CS10); // Prescaleer a 1
TIMSK1 |= (1 << ICIE1); // Abilito l'input capture interrupt
// Non settando il bit ICES1 di TCCR1B esso avrà valore 0 e quindi
// la lettura dell'impulso avverrà sul FALLING
sei(); // abilito i glob interrupt
}
void loop() {
if(current_edge == 2) {
if(starting_cnt < ending_cnt) {
time_diff = ending_cnt - starting_cnt;
Serial.println("time_diff");
Serial.println(time_diff);
}
else {
//wrap around
//(0xffff + A) - B, done without requiring signed
time_diff = starting_cnt - ending_cnt;
time_diff = 0xffff - time_diff;
//Serial.println("PLUTO");
}
current_edge = 0;
}
}

Elettrotecnica e non solo (admin)
Un gatto tra gli elettroni (IsidoroKZ)
Esperienza e simulazioni (g.schgor)
Moleskine di un idraulico (RenzoDF)
Il Blog di ElectroYou (webmaster)
Idee microcontrollate (TardoFreak)
PICcoli grandi PICMicro (Paolino)
Il blog elettrico di carloc (carloc)
DirtEYblooog (dirtydeeds)
Di tutto... un po' (jordan20)
AK47 (lillo)
Esperienze elettroniche (marco438)
Telecomunicazioni musicali (clavicordo)
Automazione ed Elettronica (gustavo)
Direttive per la sicurezza (ErnestoCappelletti)
EYnfo dall'Alaska (mir)
Apriamo il quadro! (attilio)
H7-25 (asdf)
Passione Elettrica (massimob)
Elettroni a spasso (guidob)
Bloguerra (guerra)




