Is there a interrupt which comes at regular intervals, like every 1ms. I use 328 CPU. It should be easy to setup or Arduino way, I am sure there is something if I used assembler or something.
for example It calls an interrupt service routine which you use as you wish.
Or, use millis() That's already ticking off a 1ms interrupt :) That would be the normal (aka, not reinventing the wheel again) way of doing it.
6v6gt: I found a zip-file from the playground link but it was empty.
septillion: How to use millis interrupt? I found this https://www.arduino.cc/en/Reference/Millis but it says nothing about interrupts.
That's because it's already incremented (in the background) by an interrupt. All you have to do is periodically check millis(). See Blink without delay as an example. But really, this is an [/url=http://xyproblem.info/]XY-problem[/url]. What exactly do you want to time? You might need to setup an interrupt yourself but in 99,9% of the cases here that would not be the right approach...
PS The playground is big, if you want specific help you might need to point a bit more specific ;)
LMI1: How to use millis interrupt?
Arduino has 2 functions that tell time since startup. Both return an unsigned long.
unsigned long startTime, elapsedTime;
.... some code
startTime = micros();
.... more code
elapsedTime = micros() - startTime; // good for intervals up to 71.58-some minutes, using millis(), 49.71-some days
Rollover is NOT AN ISSUE with unsigned math as long as you subtract the start time from the end time (here, now - then).
millis() skips some values in the low 8 bits to fit 256 into 250. If you need accurate timing, use micros() not millis(). OTOH millis() can count for almost 2 months and is plenty good for most uses.
septillion: Or, use millis() That's already ticking off a 1ms interrupt :) That would be the normal (aka, not reinventing the wheel again) way of doing it.
IMHO learning to use timers is very good skill. You can say learning to drive is reinventing the wheel: you may go by bus.
To OP: try to find some information about use of timers, also look into ATMega328 datasheet. There is much more timers can do and using them directly is faster than trying to find someone else's implementation that may do what you want.
Smajdalf: IMHO learning to use timers is very good skill. You can say learning to drive is reinventing the wheel: you may go by bus.
Using timers is NOT like riding the bus... ::) Using millis() (and micros()) is like riding the bus, it's nice and easy infrastructure that can get you in a lot of place. Using the timers if like building a car every time, it takes some time to learn but once you can make a decent car it can indeed bring you to more places. But that doesn't mean the bus is useless, it still brings you to a lot of places real easy :)
So I would not suggest to dive deep and look into timers. Just tell us what you want to do exactly and we can tell you it's worth spending your time on timers or just to grab the bus, it's free after all ;)
For me it’s worth to learn timers (or anything for that matter). Just started with a Leonardo and I want to write code for 4 16bit PWMs from scratch. Once I’ve mastered it, I can use libraries.
The toughest is to only use the datasheet and don’t cheat by looking at other code.
It’s all about the “after action satisfaction”.
Timer interrupts without the pain (and no gain in the learning department):
I think I understand now the trick in millis. It is not a blocking call or a delay. It is perhaps a little slower than checking if something is 1 or 0, but it is not slow or a delay in any way. Good.
avr_fred: I saw the playground text about Timer1 (and timer3). Timer3 could have been usefull because I plan to use 3 PWM channels and I hear that they use Timers 0 and 1. Timer2? There was a problem downloading the Timer3 example, at first nothing happened. But it is here now.
I partly agree that timers are important and good for many things. That also makes them a bit hard to use. And Arduino has taught me to be a bit lazy.
Thank you all for comments and help. Leif M
PS I have studied some peripheral devices bit by bit. Hardest or most strange was the interrupt controller of Z80. But now I don't care. These are just Cpus or tools and tomorrow there will be a new one.
Had some Cpus years ago when I burnt my hand. Took ages to heal.
LMI1: avr_fred: I saw the playground text about Timer1 (and timer3). Timer3 could have been usefull because I plan to use 3 PWM channels and I hear that they use Timers 0 and 1. Timer2? There was a problem downloading the Timer3 example, at first nothing happened. But it is here now.
The 328p has Timer0 (used for millis), Timer1, and Timer2. No Timer3 on the 328(p) (though the atmega2560 (as used in arduino mega) does have more timers - it's got the same timers as the 328, plus Timer3, Timer4 and Timer5, all of which have the same capabilities as Timer1)
If you want to make an interrupt then use a timer, remember that interrupts have a few us overhead but are the way to go for exactness.
If you can be off by as much as 100-200us then using micros() in a fast loop() can do. How much you print can ruin that though.