Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HOME
- STORE
- SOFTWARE
- EDU
- RESOURCES
- COMMUNITY
- HELP
- SEARCH
- Show Cart
- SIGN IN
- Arduino Forum > Using Arduino > Programming Questions > Reading count value from Timer1
- PRINT Go DownPages: [1]
- Topic: Reading count value from Timer1 (Read 2938 times) Previous Topic - Next Topic
- soulid
- avatar_soulid
- ***
- Full Member
- Posts: 162
- Karma: 4 [add]
- Reading count value from Timer1
- Nov 25, 2012, 02:17 pm
- Hi,
- 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...
- billroy
- Guest
- Re: Reading count value from Timer1
- #1
- Nov 25, 2012, 02:26 pm
- See the 328p data sheet here: http://www.atmel.com/Images/doc8161.pdf, page 117, for a C code example.
- -br
- dhenry
- Guest
- Re: Reading count value from Timer1
- #2
- Nov 25, 2012, 02:47 pm
- If you can stop the timer, it is very simple.
- 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.
- The fact that those timers don't have a shadow register / read buffer is a shame.
- dc42
- avatar_dc42
- ***
- Tesla Member
- Posts: 6,665
- Karma: 349 [add]
- Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
- http://www.eschertech.com
- Re: Reading count value from Timer1
- #3
- Nov 25, 2012, 07:26 pm
- Quote from: dhenry on Nov 25, 2012, 02:47 pm
- The fact that those timers don't have a shadow register / read buffer is a shame.
- 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.
- 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.
- soulid
- avatar_soulid
- ***
- Full Member
- Posts: 162
- Karma: 4 [add]
- Re: Reading count value from Timer1
- #4
- Nov 25, 2012, 08:53 pm
- Hi,
- 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."
- Correct result shown. Thanks for your hint to the right part of the datasheet
- Code: [Select]
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #define LEDPIN 13
- int timer;
- long counter;
- void setup()
- {
- Serial.begin(9600);
- pinMode(LEDPIN, OUTPUT);
- cli(); // disable global interrupts
- pinMode(2, INPUT); // Enable Pin2 as input to interrupt
- EIMSK |= (1 << INT0); // Enable external interrupt INT0 (see table 13-2-2 in Atmel 328 spec)
- EICRA |= (1 << ISC01); // Trigger INT0 on rising edge (see table 13-1 in Atmel 328 spec)
- EICRA |= (1 << ISC00); // Trigger INT0 on riding edge (see table 13-1 in Atmel 328 spec)
- // initialize Timer1
- // ZERO Timer1
- TCCR1A = 0; // set entire TCCR1A register to 0
- TCCR1B = 0; // same for TCCR1B
- // set compare match register to desired timer count:
- OCR1A = 7000; // turn on CTC mode:initial value changed in loop
- OCR1B = 14000; // turn on CTC mode:initial value changed in loop
- //TCCR1B |= (1 << WGM12); // Set CTC mode. Resets the timer after compA has been reached
- TCCR1B |= (1 << CS10); // prescaler 1024
- TCCR1B |= (1 << CS12); // prescaler 1024
- TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt:
- TIMSK1 |= (1 << OCIE1B); // enable timer compare interrupt:
- sei(); // Enable global interrupts
- }
- void loop()
- {}
- ISR(TIMER1_COMPA_vect){
- counter= TCNT1;
- digitalWrite(LEDPIN, HIGH);
- counter= TCNT1;
- Serial.print ("counteran:");
- Serial.println(counter);
- }
- ISR(TIMER1_COMPB_vect){
- digitalWrite(LEDPIN, LOW);
- Serial.print ("counteraus:");
- counter= TCNT1;
- Serial.println(counter);
- TCNT1 = 0; // Reset Timer1 Count
- }
- PRINT Go UpPages: [1]
- Jump to:
- NEWSLETTER
- ENTER YOUR EMAIL TO SIGN UP
- SUBSCRIBE
- Terms Of Service
- Privacy Policy
- Contact Us
- About Us
- Distributors
- Careers
- Security
- © 2019 Arduino
- facebook
- twitter
- instagram
- github
- flickr
- youtube
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement