how do i replace millis() in arduino when not using arduino?

Go To Last Post
17 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK so I'm not using Arduino anymore and almost all my programs used millis() function is there a substitute i can use? if not can someone point me in the right direction on how to have some kind of timer for my programs 

This topic has a solution.
Last Edited: Fri. Aug 18, 2017 - 06:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Most of us setup a timer, usually in ctc mode for something like 10ms.  In the ISR, incr a global long int and use that for your timing.  As always that long int must be volatile, and you access it atomically in main.

 

Jim

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

millis() in Arduino works by setting timer0 to run at 250kHz (prescaler=64) and letting it interrupt every overflow (256 cycles: 1.024ms)

Then it does some math so that it can keep a global counter in milliseconds (and also a simpler tick count.)  millis() simply returns the counter value.

 

You can duplicate that logic, or simply extract the code from the Arduino source.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I do the following

#ifndef ARDUINO_DELAY_H
#define ARDUINO_DELAY_H

#include "fcpu.h"  //MUST be before delay.h - does #define F_CPU
#include <util/delay.h>

#ifdef __cplusplus
extern "C" {
#endif

#define delay(X) _delay_ms(X)
#define delayMicroseconds(X) _delay_us(X)

#ifdef __cplusplus
}
#endif

Not exactly the same functionality since this is blocking and it uses waste cycles rather than a timer. But, MOST of the time, it does what is needed. It gives you drop in way of executing Arduino code with no change in the source (for delay, anyway). Have other parts coming.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Thu. Aug 17, 2017 - 08:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks this is what i was looking for but one more thing could i use the Watchdog Timer if Im using the other timers

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jeffd88 wrote:
could i use the Watchdog Timer if Im using the other timers

Yes (at least on new'ish AVRs the watchdog is capable of generating an interrupt), but

 

1) It is not as accurate (see the data sheet for the details), and

 

2) Tell us how you're using the other timers. It is often the case that one timer can be used for several things at one time, e,g, both generating a PWM signal and generating a "time base". Example: A timer that generates a PWM signal with frequency of 1 KHz (i.e. period of 1 ms) can at the same time generate an overflow interrupt so that the ISR gets called every 1 ms.

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The delay generators from avr-libc do not use timers. They tend to be moderately accurate but not sufficiently so to base an hour:min:sec clock on. They are great for LED flashes.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:

jeffd88 wrote:
could i use the Watchdog Timer if Im using the other timers

Yes (at least on new'ish AVRs the watchdog is capable of generating an interrupt), but

 

1) It is not as accurate (see the data sheet for the details), and

 

2) Tell us how you're using the other timers. It is often the case that one timer can be used for several things at one time, e,g, both generating a PWM signal and generating a "time base". Example: A timer that generates a PWM signal with frequency of 1 KHz (i.e. period of 1 ms) can at the same time generate an overflow interrupt so that the ISR gets called every 1 ms.

 

well in this particular instance I'm making an led driver/boost converter with under/over voltage protection on a attiny45. timer1 is using pll so i can get about 60khz pwm and timer0 is probably going to run a pwm at a much low frequency 500hz or so. i wish i could find a 16bit pwm with 60khz because 8bit sucks at fine tuning the current to the led    

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You do realise that 16bit pwm at 60kHz would require a clock of nearly 4GHz? Not technically impossible, but not something you'll find cheaply or easily.
You'll only get 10 bits at 60KHz with a 64MHz clock.
The equation is: 2^numofbits times pwm frequency.

Last Edited: Fri. Aug 18, 2017 - 12:23 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
You do realise that 16bit pwm at 60kHz would require a clock of nearly 4GHz? Not technically impossible, but not something you'll find cheaply or easily. You'll only get 10 bits at 60KHz with a 64MHz clock. The equation is: 2^numofbits times pwm frequency.

 

i see what your saying but i could live with 10bit that would be a big improvement 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can also use a technique called 'dithering' to gain a few extra bits of resolution. This is basically cycling between different pwm values values to average out a fractional number. Eg: if we can only control the pwm by 10% but wanted 1% resolution, we would cycle through the values - 60,50,50,50,50,50,50,50,50,50 the average gives us 51%,  60,60,50,50,50,50,50,50,50,50 the average gives us 52% and so on. If you wanted 4 extra bits of resolution, then you'd cycle through 16 values. We've traded response time for resolution. If you want high pwm frequency for smaller magnetics, then this technique is a good tradeoff. 

Another technique is to do it in analog - use a pwm controller chip and a 16 bit DAC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jeffd88 wrote:
timer0 is probably going to run a pwm at a much low frequency 500hz or so

If the PWM frequency isn't going to change, then there you have have a time base with a resolution of 2 ms, if that is good enough.

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:

jeffd88 wrote:
timer0 is probably going to run a pwm at a much low frequency 500hz or so

If the PWM frequency isn't going to change, then there you have have a time base with a resolution of 2 ms, if that is good enough.

 

 i did not know i could use the timer for both pwm and interrupt at the some time 

 

I am looking at the AT90PWM81 the PSC looks like it would work great and i could debug/program with it running in circuit as well i have spent so much time trying to find bugs with no communication.   

 

oh no the AT90PWM81 has a PLL that when under 4.5v it can only multiply 6 so 48mhz don't know if that would be high enough would need to try it. 

 

 

Last Edited: Fri. Aug 18, 2017 - 03:35 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jeffd88 wrote:
i did not know i could use the timer for both pwm and interrupt at the some time
In Windows and Linux one timer is used for about 10,000 different things at the same time! It's quite normal in an MCU app to have one timer doing a handful of different things as long as the period is the lowest common denominator of all the tasks to be performed.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jeffd88 wrote:
 i did not know i could use the timer for both pwm and interrupt at the some time 

But I told you..

JohanEkdahl wrote:
It is often the case that one timer can be used for several things at one time, e,g, both generating a PWM signal and generating a "time base". Example: A timer that generates a PWM signal with frequency of 1 KHz (i.e. period of 1 ms) can at the same time generate an overflow interrupt so that the ISR gets called every 1 ms.
 

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:

jeffd88 wrote:
 i did not know i could use the timer for both pwm and interrupt at the some time 

But I told you..

JohanEkdahl wrote:
It is often the case that one timer can be used for several things at one time, e,g, both generating a PWM signal and generating a "time base". Example: A timer that generates a PWM signal with frequency of 1 KHz (i.e. period of 1 ms) can at the same time generate an overflow interrupt so that the ISR gets called every 1 ms.
 

 

you did I'm sorry i got brain exhaustion i think 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
as long as the period is the lowest common denominator of all the tasks to be performed

Or highest..

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]