Does the Aiko library eat Timer1 and is therefore incompatible with PWM on pins 9 and 10?

25 views
Skip to first unread message

Marc MERLIN

unread,
Nov 11, 2012, 6:45:22 PM11/11/12
to Andy Gelme, connected-commu...@googlegroups.com
Howdy,

I'm learning more about interrupts and timers, and amont other things read
on http://letsmakerobots.com/node/28278 that for PWN:
Pins 5 and 6: controlled by timer0
Pins 9 and 10: controlled by timer1
Pins 11 and 3: controlled by timer2

Looking at the Aiko source code, it seems that it uses Timer1.

Does this mean that with an arduino Uno, if I use Aiko, I lose the ability
to do PWM on pins 9 and 10?

Thanks,
Marc
--
"A mouse is a device used to point at the xterm you want to type in" - A.S.R.
Microsoft is to operating systems ....
.... what McDonalds is to gourmet cooking
Home page: http://marc.merlins.org/

Zac Faragher

unread,
Nov 11, 2012, 9:59:26 PM11/11/12
to connected-commu...@googlegroups.com
Hi Marc,

You can pretty much only do one thing with the timers at once.

If the Aiko code uses the Timer1, it will stop you from using the
Timer1 PWM functions on pin 9 and 10.

Zac
> --
> You received this message because you are subscribed to the Google Groups "Connected Community HackerSpace" group.
> To post to this group, send an email to connected-commu...@googlegroups.com.
> To unsubscribe from this group, send email to connected-community-h...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Marc MERLIN

unread,
Nov 11, 2012, 10:23:35 PM11/11/12
to connected-commu...@googlegroups.com
On Mon, Nov 12, 2012 at 01:59:26PM +1100, Zac Faragher wrote:
> Hi Marc,
>
> You can pretty much only do one thing with the timers at once.

That was pretty much my understanding.

> If the Aiko code uses the Timer1, it will stop you from using the
> Timer1 PWM functions on pin 9 and 10.

From what I read you can do fancy things on timers like look for overflow
while having a compare interrupt, so technically it looked like you could
maybe have 2 different pieces of code get 2 different calls from the same
interrupt, but unless they were written very carefully to work together, it
didn't seem like it would happen.

Thanks for confirming my understanding.

That means it's probably a good time for me to look into my ATmega and
probably move to it :)

Zac Faragher

unread,
Nov 11, 2012, 10:33:06 PM11/11/12
to connected-commu...@googlegroups.com
Hi Marc,

If the Aiko code sets the timer interrupt registers to something very
specific, you may not be able to get what you're after without
changing them, which of course defeats the entire purpose.

While it is certainly possible to get them to work together, you will
need to be very sure of what you are changing and how it may affect
how other pieces of code run. You may also need to change things like
timer interrupt subroutines in order to add the functionality you're
after, and you may or may not feel comfortable doing this.

Zac

Marc MERLIN

unread,
Nov 11, 2012, 11:15:00 PM11/11/12
to Andy Gelme, Zac Faragher, connected-commu...@googlegroups.com
On Sun, Nov 11, 2012 at 03:45:22PM -0800, Marc MERLIN wrote:
> Howdy,
>
> I'm learning more about interrupts and timers, and amont other things read
> on http://letsmakerobots.com/node/28278 that for PWN:
> Pins 5 and 6: controlled by timer0
> Pins 9 and 10: controlled by timer1
> Pins 11 and 3: controlled by timer2

Actually I had a followup on this.
I noticed that my ardumoto shield uses PWM 3 and 11 which means
Timer2 is gone.

Stupid question: is there a valid reason for a shield to not use
PWM on pins 5 and 6 since this can be done on Timer0 which is generally not
usable for 3rd party code anyway?

I'm thinking about cutting the traces on my ardumoto
https://www.sparkfun.com/products/9815
to redirect the PWM pins to 5 and 6, so that I can at least get Timer2
back, giving me the option of either having 2 extra PWM pins, or a Timer
interrupt for doing something cool like talking to PNG sensors in the
background with http://code.google.com/p/arduino-new-ping/
(it uses Timer2 if you use its interrupt mode).

On Mon, Nov 12, 2012 at 02:33:06PM +1100, Zac Faragher wrote:
> Hi Marc,
>
> If the Aiko code sets the timer interrupt registers to something very
> specific, you may not be able to get what you're after without
> changing them, which of course defeats the entire purpose.

I'm not sure if I was going to change the Aiko Library, but if it stops me
from doing PWM on pins 9 and 10, with an ATMega, I have other PWM pins, so
it's not a big deal.

> While it is certainly possible to get them to work together, you will
> need to be very sure of what you are changing and how it may affect
> how other pieces of code run. You may also need to change things like
> timer interrupt subroutines in order to add the functionality you're
> after, and you may or may not feel comfortable doing this.

If you meant sharing an interrupt (I wsan't quite sure which bit you were
replying to), I agree. That's probably more than what I'd want to chew :)

Thanks for your answers,

Zac Faragher

unread,
Nov 12, 2012, 12:21:13 AM11/12/12
to Marc MERLIN, Andy Gelme, connected-commu...@googlegroups.com
Timer0 and Timer2 function in exactly the same way, with viirtually
identical register setups, so it's pretty easy to configure any
registers/interrupt subroutines to function the same way.
Pins 5 and 6 have the useful function of having almost no other use
than PWM, while pin 11 is also used for MOSI (SPI).

I would see no reason why you can't just reconfigure the code and the
circuit to use Timer0 instead of Timer2.

Keep in mind that you can still do timer interrupts using Timer0, so
you could just as equally change the PING sketch you were looking at
to use Timer0 instead of Timer2. This would probably be easier than
trying to change the code AND hardware for the ardumotor shield.

Zac

Marc MERLIN

unread,
Nov 18, 2012, 5:51:29 PM11/18/12
to Zac Faragher, Andy Gelme, connected-commu...@googlegroups.com
First, can Andy or someone confirm that if you're using Aiko, you do
indeed lose Timer1, and PWM pins 9 and 10, and you can't use the arduino
servo library which also uses Timer1 I believe?
If so, can Aiko easily run on other timers?

On Mon, Nov 12, 2012 at 04:21:13PM +1100, Zac Faragher wrote:
> Timer0 and Timer2 function in exactly the same way, with viirtually
> identical register setups, so it's pretty easy to configure any
> registers/interrupt subroutines to function the same way.

My understanding was that using Timer0 will break things in the arduino
libraries, like delay or millis, so it should be avoided. Not true?

> Pins 5 and 6 have the useful function of having almost no other use
> than PWM, while pin 11 is also used for MOSI (SPI).

Very true.

> I would see no reason why you can't just reconfigure the code and the
> circuit to use Timer0 instead of Timer2.

I can try and see what breaks.

> Keep in mind that you can still do timer interrupts using Timer0, so
> you could just as equally change the PING sketch you were looking at
> to use Timer0 instead of Timer2. This would probably be easier than
> trying to change the code AND hardware for the ardumotor shield.

I'll have to see if it's possible.
Reply all
Reply to author
Forward
0 new messages