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

pirvcb
Posts: 2
Joined: Wed Mar 06, 2019 9:20 am
Has thanked: 2 times
Contact:

Help to set up a 1ms time base interrupt Mega2560

Postby pirvcb » Mon Apr 15, 2019 11:00 am

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: 14151
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4330 times
Been thanked: 4080 times
Contact:

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

Postby Benj » Mon Apr 15, 2019 5:35 pm

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?
These users thanked the author Benj for the post:
pirvcb (Tue Apr 16, 2019 2:29 am)
Rating: 5.56%
 

pirvcb
Posts: 2
Joined: Wed Mar 06, 2019 9:20 am
Has thanked: 2 times
Contact:

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

Postby pirvcb » Mon Apr 15, 2019 7:05 pm

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: 14151
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4330 times
Been thanked: 4080 times
Contact:

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

Postby Benj » Tue Apr 16, 2019 11:27 am

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/
These users thanked the author Benj for the post:
pirvcb (Tue Apr 16, 2019 12:50 pm)
Rating: 5.56%