Serial.print (TCCR1B, BIN); gives me nothing

Thank you in advance

In my sketch (based in the Nick Gammon’s “Freq. Timer; Input Pin D2) I want to check the real value of some registers to check if its bits have the value I expect.

I started with:
CS10 in TCCR1B – Timer/Counter1 Control Register B, and also check the complete byte value of TCCR1B.

So , I write:,
Serial.print (" TCCR1B: ");
Serial.print (TCCR1B, BIN);

Serial.print (" CS10: ");
Serial.println (CS10, BIN);

Result were:
TCCR1B: 1
CS10: 0

Because I expected something like:
TCCR1B = 00000001
CS10: 1
I tried with another register:
TOV1 in TIFR1 – Timer/Counter1 Interrupt Flag Register, and TIFR1 itself, by writing:
Serial.print ("TIFR1: ");
Serial.print (TIFR1, BIN);

Serial.print ("TOV1: ");
Serial.print (TOV1);

Result are (closer to ones I expected):
TIFR1: 100110
TOV1: 0.

Q.- Why I cannot see TCCR1B (which includes CS10 at bit 0)?.

Serial.print (" TCCR1B: ");
Serial.print (TCCR1B, BIN);

Serial.print (" CS10: ");
Serial.println (CS10, BIN);

Result were:
TCCR1B: 1
CS10: 0

Q.- Why I cannot see TCCR1B (which includes CS10 at bit 0)?.

?

CS10 and its ilk are bit numbers. Not bit masks. Are you "converting" when you change the registers (and printing)?

TCCR1B = CS10;

...versus...

TCCR1B = 1 << CS10;

I would save the register to a variable and then print the variable

byte aaa = TCCR1B;
Serial.print(aaa, BIN);

Who knows when the Serial.print function gets around to actually reading the register.

...R

Robin2:
I would save the register to a variable and then print the variable

byte aaa = TCCR1B;

Serial.print(aaa, BIN);




Who knows when the Serial.print function gets around to actually reading the register.

...R

Thank you Robin2

Sorry for the delay.

I have tried your recomendation. Still the same result.: TIFR1: 100110 TOV1: 0 TCCR1B: 1 CS10: 0

Because in the sketch I put TCCR1B |= (1 << CS10), I expected TCCR1B: 0000001 and CS10: 1

(The Atmel manual page 134 sais for TCCR1B:
Bit 7 6 5 4 3 2 1 0
(0x81) ICNC1 ICES1 – WGM13 WGM12 CS12 CS11 CS10
Read/Write R/W R/W R R/W R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0

Why can I see TIFR1 which is a similar register as TCCR1B, but I cannot see this one?

Atmel pag 137 for TIFR1:
Bit 7 6 5 4 3 2 1 0
0x16 (0x36) – – ICF1 – – OCF1B OCF1A TOV1
Read/Write R R R/W R R R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0

Thank you again in advance

Hi iunnius

I have tried your recomendation. Still the same result.: TIFR1: 100110 TOV1: 0 TCCR1B: 1 CS10: 0
Because in the sketch I put TCCR1B |= (1 << CS10), I expected TCCR1B: 0000001 and CS10: 1

The print() method does not display leading 0s. So "1" is what I would expect in this case.

For CS10, try the following, which bitwise ANDs the register value with the bit mask shifted by CS10 (which in fact means no shift).

byte aaa = TCCR1B;
Serial.println(aaa, BIN);
Serial.println(aaa & (1 << CS10), BIN);

Regards

Ray

Here is a little routine to add to Serial.print to display the leading zeros of a binary number b.

  for (int i = 0; i < 8; i++)  //routine for printing full 8 bits, leading zeros
{
      if (b < pow(2, i))
       Serial.print(B0);
}
  Serial.println (b, BIN);

  }
 if (b < pow(2, i))

Using "pow" is a sledgehammer to miss a nut.

AWOL:

 if (b < pow(2, i))

Using "pow" is a sledgehammer to miss a nut.

Thank you AWOL, I appreciate you very much, but ...

can you say is a sledgehammer to miss a nut in other words I can understand?

(Sorry, I am just a 'foreigner')

iunnius:

AWOL:

 if (b < pow(2, i))

Using "pow" is a sledgehammer to miss a nut.

Thank you AWOL, I appreciate you very much, but ...

can you say is a sledgehammer to miss a nut in other words I can understand?

(Sorry, I am just a 'foreigner')

The more usual cliche is "take a sledgehammer to crack a nut", meaning to use an inappropriately heavy-handed method to solve a trivial problem.
However here, using "pow()" to calculate powers of two is the sledgehammer that will not hit the nut, as I shall now demonstrate :

void setup ()
{
  Serial.begin (115200);
  for (int i = 0; i < 32; i++)
  {
    uint32_t x = pow (2, i);
    uint32_t y = 1UL << i;
    if (x != y)
    {
      Serial.print ("Oops ! i = ");
      Serial.print (i);
      Serial.print (", x = ");
      Serial.print (x);
      Serial.print (", y = ");
      Serial.println (y);
    }  
  }
}

void loop ()
{}
Oops ! i = 2, x = 3, y = 4
Oops ! i = 3, x = 7, y = 8
Oops ! i = 4, x = 15, y = 16
Oops ! i = 5, x = 31, y = 32
Oops ! i = 6, x = 63, y = 64
Oops ! i = 7, x = 127, y = 128
Oops ! i = 8, x = 255, y = 256
Oops ! i = 9, x = 511, y = 512
Oops ! i = 10, x = 1023, y = 1024
Oops ! i = 11, x = 2047, y = 2048
Oops ! i = 12, x = 4095, y = 4096
Oops ! i = 14, x = 16383, y = 16384
Oops ! i = 15, x = 32767, y = 32768
Oops ! i = 16, x = 65535, y = 65536
Oops ! i = 18, x = 262143, y = 262144
Oops ! i = 19, x = 524287, y = 524288
Oops ! i = 20, x = 1048574, y = 1048576
Oops ! i = 22, x = 4194298, y = 4194304
Oops ! i = 23, x = 8388597, y = 8388608
Oops ! i = 24, x = 16777194, y = 16777216
Oops ! i = 27, x = 134217552, y = 134217728
Oops ! i = 28, x = 268435104, y = 268435456
Oops ! i = 30, x = 1073740416, y = 1073741824
Oops ! i = 31, x = 2147480832, y = 2147483648

(Sorry, I am just a 'foreigner')

Hey! Me too!

Thanks AWOL for pointing out this error.

Bitten in the butt again by Float.

I've changed my 8 bit register display print out routines to

 for (int i = 7; i > -1; i-- )
    {
       Serial.print((b >> i) & 0X01);
    }

AWOL:

iunnius:

AWOL:

 if (b < pow(2, i))

Using "pow" is a sledgehammer to miss a nut.

Thank you AWOL, I appreciate you very much, but ...

can you say is a sledgehammer to miss a nut in other words I can understand?

(Sorry, I am just a 'foreigner')

The more usual cliche is "take a sledgehammer to crack a nut", meaning to use an inappropriately heavy-handed method to solve a trivial problem.
However here, using "pow()" to calculate powers of two is the sledgehammer that will not hit the nut, as I shall now demonstrate :

void setup ()

{
 Serial.begin (115200);
 for (int i = 0; i < 32; i++)
 {
   uint32_t x = pow (2, i);
   uint32_t y = 1UL << i;
   if (x != y)
   {
     Serial.print ("Oops ! i = ");
     Serial.print (i);
     Serial.print (", x = ");
     Serial.print (x);
     Serial.print (", y = ");
     Serial.println (y);
   }  
 }
}

void loop ()
{}






Oops ! i = 2, x = 3, y = 4
Oops ! i = 3, x = 7, y = 8
Oops ! i = 4, x = 15, y = 16
Oops ! i = 5, x = 31, y = 32
Oops ! i = 6, x = 63, y = 64
Oops ! i = 7, x = 127, y = 128
Oops ! i = 8, x = 255, y = 256
Oops ! i = 9, x = 511, y = 512
Oops ! i = 10, x = 1023, y = 1024
Oops ! i = 11, x = 2047, y = 2048
Oops ! i = 12, x = 4095, y = 4096
Oops ! i = 14, x = 16383, y = 16384
Oops ! i = 15, x = 32767, y = 32768
Oops ! i = 16, x = 65535, y = 65536
Oops ! i = 18, x = 262143, y = 262144
Oops ! i = 19, x = 524287, y = 524288
Oops ! i = 20, x = 1048574, y = 1048576
Oops ! i = 22, x = 4194298, y = 4194304
Oops ! i = 23, x = 8388597, y = 8388608
Oops ! i = 24, x = 16777194, y = 16777216
Oops ! i = 27, x = 134217552, y = 134217728
Oops ! i = 28, x = 268435104, y = 268435456
Oops ! i = 30, x = 1073740416, y = 1073741824
Oops ! i = 31, x = 2147480832, y = 2147483648






> (Sorry, I am just a 'foreigner')


Hey! Me too!

Yes, I appreciate you.

I'll try to solve.

Thank you again

iunnius:
Solved: The print() method does not display leading 0s
So when I read TCCR1B = 1 is correct (00000001).

Another qestion is why I cannot read directly CS10 (the first bit of TCCR1B). I'll investigate and result will be post in other placed,

Thank you everybody


Thank you in advance

In my sketch (based in the Nick Gammon’s “Freq. Timer; Input Pin D2) I want to check the real value of some registers to check if its bits have the value I expect.

I started with:
CS10 in TCCR1B – Timer/Counter1 Control Register B, and also check the complete byte value of TCCR1B.

So , I write:,
Serial.print (" TCCR1B: ");
Serial.print (TCCR1B, BIN);

Serial.print (" CS10: ");
Serial.println (CS10, BIN);

Result were:
TCCR1B: 1
CS10: 0

Because I expected something like:
TCCR1B = 00000001
CS10: 1
I tried with another register:
TOV1 in TIFR1 – Timer/Counter1 Interrupt Flag Register, and TIFR1 itself, by writing:
Serial.print ("TIFR1: ");
Serial.print (TIFR1, BIN);

Serial.print ("TOV1: ");
Serial.print (TOV1);

Result are (closer to ones I expected):
TIFR1: 100110
TOV1: 0.

Q.- Why I cannot see TCCR1B (which includes CS10 at bit 0)?.