16 bit Timer counter count direction

I am using counter 4 but it only seems to count down. I have the Wave generation mode set to 0 with WGM4 0 to 3 set to zero. I have read the atmel documentation multiple times but there only seems brief mention of "direction" for the control unit.

How do you set the count direction please.

It is the Mega 2560

Please post the code that demonstrates this. Use code tags.

The Arduino core may have initialized timers for "Phase Correct" PWM. If you're going to use a timer for purposes other than analogWrite(), make sure you initialize ALL of the control bits.

Here is the code, I suspect I am doing something daft, it's definitely counting down, it's not the end of the world just really annoying not knowing whats wrong. Ignore the ICR as I was going on the use that further in the project.

void setup()
{
Serial.begin(9600);
noInterrupts();
TCCR4A=0;
TCCR4B=0;
TCNT4=0;

TCCR4A |= (0 << WGM40);
TCCR4A |= (0 << WGM41);

TCCR4B |= (1 << ICNC4); // enable noice canceler
TCCR4B |= (1 << CS42);//128 prescaler
TCCR4B |= (0 << WGM42);
TCCR4B |= (0 << WGM43);

interrupts();
}

void loop()
{
Serial.print("counter =");Serial.println(TCNT4);
Serial.print("ICR =");Serial.println(ICR4);
Serial.print("TCCR4A =");Serial.println(TCCR4A);
Serial.print("TCCR4B =");Serial.println(TCCR4B);
Serial.println("");

delay(1000);
}

ORing with zero accomplishes nothing. What was your intent here?

TCCR4A |= (0 << WGM40);
TCCR4A |= (0 << WGM41);

The problem is that you are reading the counter every second, but it overflows within that time, so you see steadily decreasing counts.

Change the serial Baud rate to a modern rate, like 115200, and remove the delay to see the expected behavior.

1 Like

This is a classic case illustrating the Sampling Theorem, and the expected aliasing that results from sampling at a frequency less than the highest frequency in the input stream.

Like wagon wheels that appear to be rotating backwards in a cowboy Western movie.

1 Like

Well done guys, obvious when you point that out. Lesson learnt, thanks.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.