Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. ///////////////////////////////////////
  2. // Lab 5 - Microcontrollers, Triggers
  3. // M K Gharzai
  4. // Oct 08 2017
  5. ///////////////////////////////////////
  6.  
  7. uint16_t captureTime, captureTimePrev, diff;
  8.  
  9. void setup()
  10. {
  11. Serial.begin(115200);
  12.  
  13. // consult the AVR manual and set the following registers,
  14.  
  15. // you can use binary or hex, e.g:
  16. // 0b00001000 == 0x04
  17.  
  18. // A = 0x80; // to set a value (A = 0b10000000)
  19. // A |= 0x2A; // to pull bits high (A = 0b10101010)
  20. // A &= 0x08; // to pull bits low (A = 0b00001000)
  21.  
  22. DDRB = 0x00; // all pins on PORTB should be defined as inputs
  23. TCCR1A &= 0b11111100; //should only affect bits 0 and 1 to activate normal counter operation
  24. TCCR1B |= 0b11000010; //Bit 7 activates noise cancellation, 6 sets rising edge trigger, bit 1 is part of clock scaling
  25. TCCR1B &= 0b11100010; //These bits are set to only affect the WGM and other pins in the line above
  26. TIMSK1 |= 0b00100000; //This turns on the ICIE1 Interrupt Enable
  27. SREG |= 0x80; // enable global interrupts
  28. }
  29.  
  30. void loop()
  31. {
  32. diff = captureTime - captureTimePrev;
  33. Serial.print("difference: "); Serial.print(diff);
  34. Serial.print("\tfrequency: "); Serial.println(2e6/diff);
  35. }
  36.  
  37. ISR(TIMER1_CAPT_vect) {
  38. captureTimePrev = captureTime;
  39. unsigned short *inputCaptureRegister1;
  40. inputCaptureRegister1 = (unsigned short *) 0x86;
  41. captureTime = *inputCaptureRegister1;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement