S
S
Slavka2016-08-08 21:02:40
Arduino
Slavka, 2016-08-08 21:02:40

Calculating timer time on Atmega?

static int getCounter(int  seconds) {

  double frc = ((double)1 / seconds) * 1000;
  int cnt  =  65536 -  (15625 / frc) ;
  return cnt;

}

Here is a function that takes milliseconds as input and sets the timer register TCNTx , but the problem is that the oscilloscope comes out with nonsense.
Atmega2560 controller, Arduino Mega board. The prescaler is set to 1024, i.e. 16kk / 1024 = 15625
Inside the interrupt, the usual pin kickdigitalWrite(port, !digitalRead(port));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Ocelot, 2016-08-09
@Ocelot

Try like this:

const unsigned long F_cpu = 16000000UL;
const unsigned long Prescaler = 1024UL;
unsigned int cnt = seconds * F_cpu / Prescaler;
if (cnt > 65535)   cnt = 65535;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question