Advertisement
Guest User

Untitled

a guest
Sep 5th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.63 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2.  
  3. //lcd and timer objects declaration
  4. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  5.  
  6. uint8_t inputCapturePin = 13;
  7.  
  8. volatile uint16_t startingEdge = 0;
  9. volatile uint16_t endingEdge = 0;
  10. volatile uint16_t currentOverflowCounter = 0;
  11. volatile uint16_t startOverflowCounter = 0;
  12. volatile uint32_t timer3Clocks = 0;
  13.  
  14.  
  15. void setup() {
  16.   noInterrupts();
  17.   Serial.begin(9600);
  18.  
  19.   lcd.setCursor(1,0);
  20.   lcd.print("IMPULSE LENGTH");
  21.   lcd.setCursor(3,1);
  22.   lcd.print("MEASURMENT");
  23.  
  24.   pinMode(13, INPUT);
  25.   pinMode(3, OUTPUT);
  26.   digitalWrite(3, HIGH);
  27.  
  28.   //T3 registers configuration
  29.   TCCR3A = 0;
  30.  
  31.   TCCR3B |= (1 << CS30); // prescaler = 0
  32.   TCCR3B &= ~(1 << CS31);
  33.   TCCR3B &= ~(1 << CS32);
  34.  
  35.   TCCR3B &= ~(1 << ICES3); //input capture on falling edge
  36.  
  37.  
  38.   TCNT3 = 0; // initialize counter  
  39.  
  40.   TIMSK3 |= (1 << ICIE3)|(1 << TOIE3); //enable input capture interrupt, enable timer ovf interrupt
  41.   interrupts();
  42. }
  43.  
  44.  
  45. //loop method
  46. void loop() {
  47.   int result = timer3Clocks / 16;
  48.  
  49. }
  50.  
  51. ISR (TIMER3_OVF_vect) {
  52.   currentOverflowCounter++;
  53. }
  54.  
  55. ISR (TIMER3_CAPT_vect) {
  56.   if(((TCCR3B & (1 << ICES3)) >> ICES3) == 0) //checking state of ICES3bit (ICES3 == 1 input capture on rising edge)
  57.   {                                           //(ICES3 == 0 input capture on falling edge)
  58.     startingEdge = ICR3;
  59.     startOverflowCounter = currentOverflowCounter;
  60.   } else {
  61.     endingEdge = ICR3;
  62.     timer3Clocks = (uint16_t)endingEdge + ((uint16_t)currentOverflowCounter * 65536) - ((uint16_t)startingEdge + (startOverflowCounter * 65536));
  63.     currentOverflowCounter = 0;
  64.   }
  65.   TCCR3B ^= (1 << ICES3);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement