timer1 always reads 8 bit value?

Hello All

Not sure what i am doing wrong but when i read timer1 using the atomic safe code in the spec sheet for the atmega i never get a value above 8 bits?

I read this constantly in my loop and store the max value after 100000 loops (also into an unsigned int). It is never more than 255.

I have no prescaler (therefore read that ckdiv8 is not set), the arduino is an atmega328p revision 3 with 16mhz crystal.

Atomic read as below.

unsigned int TIM16_ReadTCNT1()
{
unsigned char sreg;
unsigned int i;
/* Save global interrupt flag /
sreg = SREG;
/
Disable interrupts /
cli();
/
Read TCNT1 into i /
i = TCNT1;
/
Restore global interrupt flag */
SREG = sreg;
return i;
}

Any ideas?

Cheers

J

Look at the init() function that runs before any of your code. It puts Timer1 into 8 bit mode so the PWM on those pins will work the same as the others.

If you want 16bit mode, you'll have to set the timer up yourself.

Thanks for the reply Delta_G

Not sure where the init function is that you mention, it's not in the code i wrote that is for sure. Could you provide a filename for where this init function resides?

Thanks

J

It is in wiring.c if I remember right.

Behind the scenes the IDE hides the main function. It is worth taking a look at. I believe it is in Main.cpp. Search the folder where you installed the IDE.