SpenceKonde / megaTinyCore

Arduino core for the tinyAVR 0/1/2-series - Ones's digit 2,4,5,7 (pincount, 8,14,20,24), tens digit 0, 1, or 2 (featureset), preceded by flash in kb. Library maintainers: porting help available!
Other
542 stars 140 forks source link

Only first call to analogWrite() working on ATtiny817 #1109

Open caternuson opened 3 weeks ago

caternuson commented 3 weeks ago

This may affect other variants, but was discovered and tested using an ATtiny817. megaTinyCore release 2.6.10

Example sketch to demonstrate issue:

// attiny817 PWM pins: 0, 1, 9, 10, 11, 12, 13, 20
// works: 0, 1, 9, 10, 20
// issue: 12, 13

#define PWM_PIN 13

void setup() {
  pinMode(PWM_PIN, OUTPUT);
}

void loop() {
  analogWrite(PWM_PIN, 255);
  delay(2000);
  analogWrite(PWM_PIN, 0);
  delay(2000);
}

and an LED attached to the output pin.

For pins 0, 1, 9, 10, and 20, the sketch works as expected. The LED blinks. For pins 12 and 13, only the first call to analogWrite() works. Changing 255 to 127 has the expected effect on brightnesls. But the LED never turns off.

hmeijdam commented 3 weeks ago

I can confirm your findings for pin 12 and 13 (PC0 and PC1). With a 3217 it also seems that analogWrite(PWM_PIN, 0) blocks further analogwrite to that pin. Changing it into analogWrite(PWM_PIN,1) gives no problems, so it seems that only setting it to "0" breaks the function. 13 (PC1) can only have PWM from TimerD (WOD), so I suspect that timer is used for analogWrite on these two pins. TimerD is also notoriously difficult to set up,