Elegoo Nano timer prescaler problem
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I am trying to use Timer2 on an Elegoo Nano to produce a "tick" interrupt every 10uS. My plan was to set Timer2 to CTC mode with a /8 prescaler (to get a timer clock of 2MHz from the 16MHz chip clock) and a compare value of 19 (to reset the timer every 20 counts) which should trigger the compareA ISR at 100KHz (every 10us). The problem is that only prescale values of 64 or greater work as expected. Setting the prescaler to any value lower than 64 actually results in the "tick" interval getting much longer (10 times the interval or more). I have carefully studied the ATmega628 datasheet to see if I am configuring every register correctly, but I don't see anything wrong with what I am doing. Here is my code...
void initTimer() _BV( CS21 );
// ---Enable Timer2 CTC interrupts for compare A match
TIMSK2
SIGNAL( TIMER2_COMPA_vect )
/*
** This should execute every 10uS, but for prescaler values
** below 64 the interval is many times longer than expected.
*/
Can anyone point out my mistake?
arduino-nano interrupt timers
New contributor
add a comment |Â
up vote
1
down vote
favorite
I am trying to use Timer2 on an Elegoo Nano to produce a "tick" interrupt every 10uS. My plan was to set Timer2 to CTC mode with a /8 prescaler (to get a timer clock of 2MHz from the 16MHz chip clock) and a compare value of 19 (to reset the timer every 20 counts) which should trigger the compareA ISR at 100KHz (every 10us). The problem is that only prescale values of 64 or greater work as expected. Setting the prescaler to any value lower than 64 actually results in the "tick" interval getting much longer (10 times the interval or more). I have carefully studied the ATmega628 datasheet to see if I am configuring every register correctly, but I don't see anything wrong with what I am doing. Here is my code...
void initTimer() _BV( CS21 );
// ---Enable Timer2 CTC interrupts for compare A match
TIMSK2
SIGNAL( TIMER2_COMPA_vect )
/*
** This should execute every 10uS, but for prescaler values
** below 64 the interval is many times longer than expected.
*/
Can anyone point out my mistake?
arduino-nano interrupt timers
New contributor
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to use Timer2 on an Elegoo Nano to produce a "tick" interrupt every 10uS. My plan was to set Timer2 to CTC mode with a /8 prescaler (to get a timer clock of 2MHz from the 16MHz chip clock) and a compare value of 19 (to reset the timer every 20 counts) which should trigger the compareA ISR at 100KHz (every 10us). The problem is that only prescale values of 64 or greater work as expected. Setting the prescaler to any value lower than 64 actually results in the "tick" interval getting much longer (10 times the interval or more). I have carefully studied the ATmega628 datasheet to see if I am configuring every register correctly, but I don't see anything wrong with what I am doing. Here is my code...
void initTimer() _BV( CS21 );
// ---Enable Timer2 CTC interrupts for compare A match
TIMSK2
SIGNAL( TIMER2_COMPA_vect )
/*
** This should execute every 10uS, but for prescaler values
** below 64 the interval is many times longer than expected.
*/
Can anyone point out my mistake?
arduino-nano interrupt timers
New contributor
I am trying to use Timer2 on an Elegoo Nano to produce a "tick" interrupt every 10uS. My plan was to set Timer2 to CTC mode with a /8 prescaler (to get a timer clock of 2MHz from the 16MHz chip clock) and a compare value of 19 (to reset the timer every 20 counts) which should trigger the compareA ISR at 100KHz (every 10us). The problem is that only prescale values of 64 or greater work as expected. Setting the prescaler to any value lower than 64 actually results in the "tick" interval getting much longer (10 times the interval or more). I have carefully studied the ATmega628 datasheet to see if I am configuring every register correctly, but I don't see anything wrong with what I am doing. Here is my code...
void initTimer() _BV( CS21 );
// ---Enable Timer2 CTC interrupts for compare A match
TIMSK2
SIGNAL( TIMER2_COMPA_vect )
/*
** This should execute every 10uS, but for prescaler values
** below 64 the interval is many times longer than expected.
*/
Can anyone point out my mistake?
arduino-nano interrupt timers
arduino-nano interrupt timers
New contributor
New contributor
New contributor
asked 2 hours ago
PhoenixRevealed
61
61
New contributor
New contributor
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
Can anyone point out my mistake?
Your mistake is in thinking that it's possible to do more than you can within a 10uS period.
At 16MHz each instruction takes 63ns (for single-clock instructions). 10nS gives you a maximum of (10,000 / 63) 156 assembly instructions. Into that you have to fit the preamble and postamble to store the registers to the stack and retrieve them again.
The remaining clock cycles (maybe no more than 100) won't give you much time to do complex things. And given that many of the Arduino API calls are quite heavyweight, even just something as "simple" as a digitalWrite()
can overflow that many times over.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Can anyone point out my mistake?
Your mistake is in thinking that it's possible to do more than you can within a 10uS period.
At 16MHz each instruction takes 63ns (for single-clock instructions). 10nS gives you a maximum of (10,000 / 63) 156 assembly instructions. Into that you have to fit the preamble and postamble to store the registers to the stack and retrieve them again.
The remaining clock cycles (maybe no more than 100) won't give you much time to do complex things. And given that many of the Arduino API calls are quite heavyweight, even just something as "simple" as a digitalWrite()
can overflow that many times over.
add a comment |Â
up vote
2
down vote
Can anyone point out my mistake?
Your mistake is in thinking that it's possible to do more than you can within a 10uS period.
At 16MHz each instruction takes 63ns (for single-clock instructions). 10nS gives you a maximum of (10,000 / 63) 156 assembly instructions. Into that you have to fit the preamble and postamble to store the registers to the stack and retrieve them again.
The remaining clock cycles (maybe no more than 100) won't give you much time to do complex things. And given that many of the Arduino API calls are quite heavyweight, even just something as "simple" as a digitalWrite()
can overflow that many times over.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Can anyone point out my mistake?
Your mistake is in thinking that it's possible to do more than you can within a 10uS period.
At 16MHz each instruction takes 63ns (for single-clock instructions). 10nS gives you a maximum of (10,000 / 63) 156 assembly instructions. Into that you have to fit the preamble and postamble to store the registers to the stack and retrieve them again.
The remaining clock cycles (maybe no more than 100) won't give you much time to do complex things. And given that many of the Arduino API calls are quite heavyweight, even just something as "simple" as a digitalWrite()
can overflow that many times over.
Can anyone point out my mistake?
Your mistake is in thinking that it's possible to do more than you can within a 10uS period.
At 16MHz each instruction takes 63ns (for single-clock instructions). 10nS gives you a maximum of (10,000 / 63) 156 assembly instructions. Into that you have to fit the preamble and postamble to store the registers to the stack and retrieve them again.
The remaining clock cycles (maybe no more than 100) won't give you much time to do complex things. And given that many of the Arduino API calls are quite heavyweight, even just something as "simple" as a digitalWrite()
can overflow that many times over.
answered 1 hour ago
Majenkoâ¦
62.5k42873
62.5k42873
add a comment |Â
add a comment |Â
PhoenixRevealed is a new contributor. Be nice, and check out our Code of Conduct.
PhoenixRevealed is a new contributor. Be nice, and check out our Code of Conduct.
PhoenixRevealed is a new contributor. Be nice, and check out our Code of Conduct.
PhoenixRevealed is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f56499%2felegoo-nano-timer-prescaler-problem%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password