Pages: [1]   Go Down
Author Topic: Reading count value from Timer1  (Read 247 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 1
Posts: 140
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

See the 328p data sheet here: http://www.atmel.com/Images/doc8161.pdf, page 117, for a C code example.

-br
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6593
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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.

0
Offline Offline
Full Member
***
Karma: 1
Posts: 140
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
}
Logged

Pages: [1]   Go Up
Jump to: