Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- //lcd and timer objects declaration
- LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
- uint8_t inputCapturePin = 13;
- volatile uint16_t startingEdge = 0;
- volatile uint16_t endingEdge = 0;
- volatile uint16_t currentOverflowCounter = 0;
- volatile uint16_t startOverflowCounter = 0;
- volatile uint32_t timer3Clocks = 0;
- void setup() {
- noInterrupts();
- Serial.begin(9600);
- lcd.setCursor(1,0);
- lcd.print("IMPULSE LENGTH");
- lcd.setCursor(3,1);
- lcd.print("MEASURMENT");
- pinMode(13, INPUT);
- pinMode(3, OUTPUT);
- digitalWrite(3, HIGH);
- //T3 registers configuration
- TCCR3A = 0;
- TCCR3B |= (1 << CS30); // prescaler = 0
- TCCR3B &= ~(1 << CS31);
- TCCR3B &= ~(1 << CS32);
- TCCR3B &= ~(1 << ICES3); //input capture on falling edge
- TCNT3 = 0; // initialize counter
- TIMSK3 |= (1 << ICIE3)|(1 << TOIE3); //enable input capture interrupt, enable timer ovf interrupt
- interrupts();
- }
- //loop method
- void loop() {
- int result = timer3Clocks / 16;
- }
- ISR (TIMER3_OVF_vect) {
- currentOverflowCounter++;
- }
- ISR (TIMER3_CAPT_vect) {
- if(((TCCR3B & (1 << ICES3)) >> ICES3) == 0) //checking state of ICES3bit (ICES3 == 1 input capture on rising edge)
- { //(ICES3 == 0 input capture on falling edge)
- startingEdge = ICR3;
- startOverflowCounter = currentOverflowCounter;
- } else {
- endingEdge = ICR3;
- timer3Clocks = (uint16_t)endingEdge + ((uint16_t)currentOverflowCounter * 65536) - ((uint16_t)startingEdge + (startOverflowCounter * 65536));
- currentOverflowCounter = 0;
- }
- TCCR3B ^= (1 << ICES3);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement