Reading TCNT value

I amunable to read the value of TCNT1 ( ATMEGA 328P of Arduino Nano )
My sketch is shown below. Can some one help me, tell me what is wrong with my code ?

BlinkPortManipulation_Timer_02.ino


#include <avr/io.h>
#include "BlinkPortManTimer.h"
// bool timeOut;

void setup() {
  Serial.begin(9600);
  DDRB |= 1 << PINB5; // Mapped to LED pin (D13) on Arduino Nano
  PORTB |= 1 << PINB5; // Setting D13 to 1
  TCCR1B |= 1 << CS10 | 1 << CS12 ; // Sets 1024 from pre-scaler. - Arduino Nano

  // Arduino Nano internal clock = 16MHz. 16M / 1024 = 15625. With pre-scaler=1024, the timer will tick 15625 times in a second.

}

void loop()
{
  timer();
}

===============================
BlinkPortManTimer.h

#include <avr/io.h>
bool timeOut;

void timer()
{
  if (TCNT1 > 15625)
  {
    TCNT1 = 0;
    PORTB ^= 1 << PINB5; // Toggling PINB5 : LED pin D13 on Arduino Nano
  }
  if (bit_is_clear(PINB, 5))
  {
    timeOut = false;
  } else {
    timeOut = true;
  }
  Serial.print (TCNT1);
  Serial.print ("::");
  Serial.println (timeOut);
}

Your code looks ok to me, Its your question I can't understand

What do you mean by "unable to read"? What do you see?

Remember that, before your setup() is called, Timer1 is set up for doing PWM. If you want to program it you should probably clear TCCR1A and TCCR1B first.

1 Like

Hi Mr, John,
Thanks for your response. I am using Arduino Nano. So I am referring ATMEGA328P.
The WGM bits are --- TCCR1A bit0(WGM10) and bit1(WGM11) --- TCCR1B bit3(WGM12) and bit4(WGM13) --- which are all initially zero. So, the timer is never set up for PWM.

Here I present my same previous question in a simpler sketch. Please see this sketch below

#include <avr/io.h>
// Arduino Nano internal clock = 16MHz. 16M / 1024 = 15625.
// With pre-scaler=1024, the timer will tick 15625 times in a second.
// I tried three mays of setting prescaler = 1024.Arduino Nano
void setup() {
  TCCR1B |= 1 << CS10 | 1 << CS12 ; // This outputs TCNT1 = 1
  // TCCR1B = 0x05; // This outputs TCNT1 = runs from 0 upto 255 and back to 0 and over again.
  // TCCR1B = 0b00000101; // This outputs TCNT1 = runs from 0 upto 255 and back to 0 and over again.
  Serial.begin(9600);
}

void loop() {
  int tcnt1 = TCNT1;
  Serial.println (tcnt1);
}

Are you absolutely sure about that? Run this code and tell us what is printed:

void setup() {
  Serial.begin(9600);
  Serial.println( TCCR1A);
  Serial.println( TCCR1B);
}
void loop() {
}

I got TCCR1A = 1 and TCCR1B = 3 :: What do these outputs mean ?

Your best source of information about the meaning of Timer1 register bits is the ATmega328 data sheet, Timer1 section.

But for the moment, that output demonstrates that your assumptions were incorrect, and that it would be a good idea to follow johnwasser's advice in reply #3. He knows what he is talking about.

Also, those numbers demonstrate that using the bitwise OR operation in this line is a serious mistake, if you are not absolutely sure what bits are already set.

  TCCR1B |= 1 << CS10 | 1 << CS12 ; // Sets 1024 from pre-scaler. - Arduino Nano

It worked !!!! Thank you so much, Sir.
I never expected I would have to be skeptical about the data sheet. Well, I will follow your ( Mr. John Wasser's ) advice whenever I set any register in future.
My problem is solved.

The data sheet is accurate. It is your assumptions that you should be skeptical of.

Please tell me, how do these bits get "previously" set ? Do the previous uploads affect the current register value in the chip?

The Arduino IDE preloads a lot of data, every time you upload a new program.

For example, it sets up Timer1 for PWM, and Timer0 as the millis() clock. You can think of these operations as part of the "operating system".

1 Like

Ah ! Hm..... Another down side of the Arduino ???? :astonished:

Anyway, my problem is solved. I got my answer, and I got more.
Thank you very much.

No.

1 Like

Sorry for my negative thinking.

Thank you, Mr. John Wasser.

Thanks for your response.
I have mentioned (commented) that in my simplified (revised) sketch.
Anyway, as you can see here, I have already received an answer to my query.

Let us see how TCCR1A and TCCR1B Registers have contained 0x01 (00000000b) and 0x03 (00000011b) when they contain 0x00 and 0x00 during power up.

1. In Arduino IDE, we begin a sketch by putting the following two empty functions:

void setup()
{

}

void loop()
{

}

2. In fact, there is an invisible main() function which is executed first and then the setup() function is called upon and then the loop() function is called upon. That is we, have:

void main(void)
{
    //-- execute many functions
   void init();
}

void init()
{
    // put timer 1 in 8-bit phase correct pwm mode
    sbi(TCCR1A, WGM10);   //TCCR1A = b7 b6 b5 b4 b3 WGM11 WGM10 = 00000001 = 0x01

   //set timer 1 prescale factor to 64
   sbi(TCCR1B, CS11);      //TCCR1B = b7 b6 b5 b4 CS12  CS11  CS10 = 00000001 
   sbi(TCCR1B, CS10);      //TCCR1B = 00000011 = 0x03
}

void setup()
{

}

void loop()
{

}

3. You can see the codes in the init() function, which has manipulated the TCCR1A and TCCR1B Registers before the setup() is being called upon. That's why @johnwasser has advised to clear these two registers in the setup() function before you configure them to suit your requirements. More could be found in this file: wiring.c

4. Bits Layout of the TCCR1A Register
tccr1a

5. Bits Layout of TCCr1B Register

You can do 'bare metal' programming with the Arduino IDE. Make your .ino file empty and put 'int main()' in a .cpp (or .c) file in your sketch folder. Your .cpp (or .c) file will show up as a tab in the IDE.

With an empty .ino file, this .cpp file works to blink the LED as you originally expected:

#include <avr/io.h>

bool timeOut;

void timer()
{
  if (TCNT1 > 15625)
  {
    TCNT1 = 0;
    PORTB ^= 1 << PINB5; // Toggling PINB5 : LED pin D13 on Arduino Nano
  }
  if (bit_is_clear(PINB, 5))
  {
    timeOut = false;
  }
  else
  {
    timeOut = true;
  }
  // Serial.print (TCNT1);
  // Serial.print ("::");
  // Serial.println (timeOut);
}

int main()
{
  // Serial.begin(9600);
  DDRB |= 1 << PINB5; // Mapped to LED pin (D13) on Arduino Nano
  PORTB |= 1 << PINB5; // Setting D13 to 1
  TCCR1B |= 1 << CS10 | 1 << CS12 ; // Sets 1024 from pre-scaler. - Arduino Nano

  // Arduino Nano internal clock = 16MHz. 16M / 1024 = 15625. With pre-scaler=1024, the timer will tick 15625 times in a second.

  while (1)
    timer();
}

Note that you can't use any Arduino library features, like Serial, if you don't first initialize the Arduino runtime by calling 'init()' and 'initVarient()'.

Note that if you make it a '.c' file you can't use the 'bool' type or the values 'true' or 'false'.

@johnwasser
I tried your sketch of Post-19 in MEGA without success. Can you please, present it in the form of tutorial so that I follow the steps and end up blinking the LED?