Timer1 appears truncated to 8 bits

Hello,

I'm trying to make a transmission happen ~ every 200mS. I'd previously had this working just fine using a 'delay (200);' in my loop, but I'd like to have more flexibility here so I decided to use timer1. My plan was to set timer1 to a 1024 prescaler and then have a line in my loop that checks if TCNT1 >= 3125 and if so, send the transmission.

When I tried to run the sketch my data was not sent. I played around a bit and it seems like if I have an event that's conditional of TCNT1 being >= 255, nothing happens. Is this timer truncated to 8 bits in the arduino environment? Is so, is there any way around it?

Any advice appreciated! :-)

When I tried to run the sketch ...

What sketch?

Read this before posting a programming question

Is this timer truncated to 8 bits in the arduino environment?

No, why would they do that?

Sorry Nick, I was thinking this was a general enough question and didn't require a code snippet. I don't have any idea why they would truncate the counter, but it seems to be what's happening here (or maybe I'm just missing something). Here's a code snippet that's simplified to easily demonstrate what's happening here. If the code waits for TCNT1 to reach a value >= 256, nothing happens. If that value gets dropped down to 255, the LED on pin 13 lights up.

void setup(){

pinMode (13,OUTPUT);    //Just setting up the LED on pin 13 to see if timer arrives at target
digitalWrite (13,LOW);
TCCR1B = _BV(CS12) | _BV(CS10);   //Setting mode 0 for timer 1 with a /1024 prescaler which will mean a 64uS period.  When TCNT >= 3125, will call tX routine

}

void loop(){

if (TCNT1 >= 256) {digitalWrite (13, HIGH);}
}

Look at blink without delay.

Mark

[quote author=Nick Gammon link=topic=189167.msg1399468#msg1399468 date=1379737054]

Is this timer truncated to 8 bits in the arduino environment?

No, why would they do that? [/quote]

Yes, it is set to mode 0001 (8 bit phase correct) in init(). This is done partly to allow a PWM frequency faster than 244Hz (16MHz/2^16), but mainly so that the same analogWrite values work with all 3 timers.

Thanks Mark! I cleared TCCR1A and now it's working as I'd initially expected!

Hitting the timer hardware directly is a massive overkill for this problem. You're sacrificing all the abstraction and protection of the Arduino runtime library, for no reason. You don't need any special tricks to get code to run every 200ms, just follow the blink without delay example. It's not a perfect example, but it shows the principle.

MarkT: Yes, it is set to mode 0001 (8 bit phase correct) in init(). This is done partly to allow a PWM frequency faster than 244Hz (16MHz/2^16), but mainly so that the same analogWrite values work with all 3 timers.

Oh, OK. I asssumed that someone who wanted to count to 3125 would not have set the timer up in 8-bit mode. That's why we ask for code. :)

I further assumed that he had set the timer up in the first place, and not relied on whatever-it-is that init does to it.

Ach, goes to show what happens when you assume. Next time I'll ask for code without saying anything extra.

However my answer was correct in that TCNT1 is a 16-bit field.

Is this timer truncated to 8 bits in the arduino environment?

No it isn't truncated.