Help to set up a 1ms time base interrupt Mega2560

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
pirvcb
Posts: 12
Joined: Wed Mar 06, 2019 9:20 am
Has thanked: 3 times
Been thanked: 1 time
Contact:

Help to set up a 1ms time base interrupt Mega2560

Post by pirvcb »

Hello to everyone!
The platform is an Arduino Mega. But i program it directly by ICSP external programmer. So, no bootloader reserved memory etc.
I need to set up a 1ms time base interrupt in an AVR ATMEGA 2560 at 16MHz. Does someone can help me in doing this? For PIC, i seen, the problem inside Flowcode is not real because prescaler, postscaler and rollover are all presents. But for AVR side i'm stuck to combact with internal register and, because i'm a beginner, this is a little over my skills. Thanks in advance to anyone...

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Help to set up a 1ms time base interrupt Mega2560

Post by Benj »

Hello,

Is the 1:64 prescaler an option for you, this generates 976.563 Hz which is fairly close to 1000Hz.

Are you trying to achieve very precise timings?

pirvcb
Posts: 12
Joined: Wed Mar 06, 2019 9:20 am
Has thanked: 3 times
Been thanked: 1 time
Contact:

Re: Help to set up a 1ms time base interrupt Mega2560

Post by pirvcb »

Yes, i would like have exact timing of 1ms to simplify wheel rpm calculation. Was only a facility, i can go over wuthout it.
The other and bigger problem is that putting in sleep mode the micro ATMEGA2560, without any other absobtion than itself, i can't obtain current under 8ma that's quite high for my objective. Maybe i have to try with PIC, more efficient in sleep state.
Thanks for you fast reply, anyway!

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Help to set up a 1ms time base interrupt Mega2560

Post by Benj »

Hello,

Inside your timer service macro add this code into a C icon and it should give you pretty much bang on 1000hz interrupt assuming your using timer 0 with a 1:64 prescaler.

Code: Select all

TCNT0 += 6;
Based on this calculator.
https://eleccelerator.com/avr-timer-calculator/

Post Reply