Advertisement
prjbrook

Arduino Timer1

Jul 23rd, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1.  
  2. HOME
  3. STORE
  4. SOFTWARE
  5. EDU
  6. RESOURCES
  7. COMMUNITY
  8. HELP
  9. SEARCH
  10. Show Cart
  11. SIGN IN
  12. Arduino Forum > Using Arduino > Programming Questions > Reading count value from Timer1
  13. PRINT Go DownPages: [1]
  14. Topic: Reading count value from Timer1 (Read 2938 times) Previous Topic - Next Topic
  15. soulid
  16. avatar_soulid
  17. ***
  18. Full Member
  19. Posts: 162
  20. Karma: 4 [add]
  21.  
  22. Reading count value from Timer1
  23. Nov 25, 2012, 02:17 pm
  24. Hi,
  25. I need to read the count value from the Timer1. How can I read out the 16bit value from Timer1? I cannot find a starting point...
  26. billroy
  27. Guest
  28.  
  29. Re: Reading count value from Timer1
  30. #1
  31. Nov 25, 2012, 02:26 pm
  32. See the 328p data sheet here: http://www.atmel.com/Images/doc8161.pdf, page 117, for a C code example.
  33.  
  34. -br
  35. dhenry
  36. Guest
  37.  
  38. Re: Reading count value from Timer1
  39. #2
  40. Nov 25, 2012, 02:47 pm
  41. If you can stop the timer, it is very simple.
  42.  
  43. If you cannot stop the timer, you have to double read it in case the timer advances in between the low/high byte reads. A double read is a fairly typical technique in this kind of solutions.
  44.  
  45. The fact that those timers don't have a shadow register / read buffer is a shame.
  46. dc42
  47. avatar_dc42
  48. ***
  49. Tesla Member
  50. Posts: 6,665
  51. Karma: 349 [add]
  52. Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
  53. http://www.eschertech.com
  54.  
  55. Re: Reading count value from Timer1
  56. #3
  57. Nov 25, 2012, 07:26 pm
  58. Quote from: dhenry on Nov 25, 2012, 02:47 pm
  59.  
  60. The fact that those timers don't have a shadow register / read buffer is a shame.
  61.  
  62.  
  63. The atmega mcus used in Arduinos do have a shadow register - when the low byte is read, the high byte is latched. See the datasheet.
  64. Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.
  65. soulid
  66. avatar_soulid
  67. ***
  68. Full Member
  69. Posts: 162
  70. Karma: 4 [add]
  71.  
  72. Re: Reading count value from Timer1
  73. #4
  74. Nov 25, 2012, 08:53 pm
  75. Hi,
  76. I read the part of the datasheet. The complete 16-bit set can be accessed by reading " TCNT1 " according to 16.11.4 "The two Timer/Counter I/O locations (TCNT1H and TCNT1L, combined TCNT1) give direct access, both for read and for write operations, to the Timer/Counter unit 16-bit counter."
  77.  
  78. Correct result shown. Thanks for your hint to the right part of the datasheet
  79.  
  80. Code: [Select]
  81.  
  82. #include <avr/io.h>
  83. #include <avr/interrupt.h>
  84. #define LEDPIN 13
  85. int timer;
  86. long counter;
  87.  
  88.  
  89. void setup()
  90. {
  91. Serial.begin(9600);
  92. pinMode(LEDPIN, OUTPUT);
  93. cli(); // disable global interrupts
  94. pinMode(2, INPUT); // Enable Pin2 as input to interrupt
  95. EIMSK |= (1 << INT0); // Enable external interrupt INT0 (see table 13-2-2 in Atmel 328 spec)
  96. EICRA |= (1 << ISC01); // Trigger INT0 on rising edge (see table 13-1 in Atmel 328 spec)
  97. EICRA |= (1 << ISC00); // Trigger INT0 on riding edge (see table 13-1 in Atmel 328 spec)
  98.  
  99. // initialize Timer1
  100. // ZERO Timer1
  101. TCCR1A = 0; // set entire TCCR1A register to 0
  102. TCCR1B = 0; // same for TCCR1B
  103.  
  104. // set compare match register to desired timer count:
  105. OCR1A = 7000; // turn on CTC mode:initial value changed in loop
  106. OCR1B = 14000; // turn on CTC mode:initial value changed in loop
  107. //TCCR1B |= (1 << WGM12); // Set CTC mode. Resets the timer after compA has been reached
  108. TCCR1B |= (1 << CS10); // prescaler 1024
  109. TCCR1B |= (1 << CS12); // prescaler 1024
  110. TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt:
  111. TIMSK1 |= (1 << OCIE1B); // enable timer compare interrupt:
  112. sei(); // Enable global interrupts
  113. }
  114.  
  115. void loop()
  116. {}
  117. ISR(TIMER1_COMPA_vect){
  118. counter= TCNT1;
  119. digitalWrite(LEDPIN, HIGH);
  120. counter= TCNT1;
  121. Serial.print ("counteran:");
  122.  
  123. Serial.println(counter);
  124. }
  125.  
  126. ISR(TIMER1_COMPB_vect){
  127. digitalWrite(LEDPIN, LOW);
  128. Serial.print ("counteraus:");
  129. counter= TCNT1;
  130. Serial.println(counter);
  131.  
  132. TCNT1 = 0; // Reset Timer1 Count
  133. }
  134. PRINT Go UpPages: [1]
  135. Jump to:
  136.  
  137.  
  138. NEWSLETTER
  139. ENTER YOUR EMAIL TO SIGN UP
  140. SUBSCRIBE
  141. Terms Of Service
  142. Privacy Policy
  143. Contact Us
  144. About Us
  145. Distributors
  146. Careers
  147. Security
  148. © 2019 Arduino
  149.  
  150. facebook
  151. twitter
  152. instagram
  153. github
  154. flickr
  155. youtube
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement