I've noticed that when using the USI interface on the ATTiny to send I2C via the methods from "Wire.h" with the ATTiny Arduino implementation included from the library manager (ATTinyCore), while analogWrite
PWM on PB4 (Arduino pin 4, physical pin 3) works OK, it seems attempting to use it on PB1 (Arduino pin 1, physical pin 6) behaves erratically or hangs the processor.
Is this expected behavior, and is there a workaround? With the SDA
line on PWM pin 0, physical pin 5 if that's the case it means when using the hardware USI to do I2C communications, I'm down to a single hardware PWM output, which is kind of a bummer.
1 Answer 1
This is normal.
Timer0 is used as the clock source for the USI. It's the only internal clock source that can be used. The only other option is to use "software polled" mode where it's up to the sketch (or support library) to manually create the clock signal (less than desirable) by setting a bit in a register at the right time.
So you can only use Timer1 for your own (PWM) purposes while using the USI.
-
In a situation where two Tiny devices were using i2c to communicate, and at least one of them required more than a single hardware PWM channel, would it be preferable to use software-polling on the slave and the USI on the master, or vice versa?MattyZ– MattyZ2018年01月08日 23:03:47 +00:00Commented Jan 8, 2018 at 23:03
-
1It only relates to master mode. In slave mode the clock is provided by the master, not Timer0. The slave can have all the PWM running.Majenko– Majenko2018年01月08日 23:05:12 +00:00Commented Jan 8, 2018 at 23:05
-
Ah, I didn't know if in acting as a slave it also required it for some purpose, that works out fine then.MattyZ– MattyZ2018年01月08日 23:10:29 +00:00Commented Jan 8, 2018 at 23:10
-
1The only time you need a timer both ends of a connection is for an asynchronous protocol like UART.Majenko– Majenko2018年01月08日 23:15:02 +00:00Commented Jan 8, 2018 at 23:15
millis()
which I believe does something with an interrupt on one of the two hardware timers, so I wonder if that might be part of the problem as well, though I was under the impression that firing a timer overflow interrupt wouldn't interfere with the PWM channels. I plan on breaking the situation down into bare-minimum sketches today to try and isolate the problem