Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ATtinyx5: PWM on pin 4 (Arduino 1.0.6) #36

Closed
ivazqueznet opened this issue Sep 19, 2013 · 8 comments
Closed

ATtinyx5: PWM on pin 4 (Arduino 1.0.6) #36

ivazqueznet opened this issue Sep 19, 2013 · 8 comments

Comments

@ivazqueznet
Copy link

I couldn't get analogWrite(3, x) to work properly, but setting GTCCR[6:4] to 0x5 and OCR1B to x let it work.

@oddstr13
Copy link

After a few hours of pulling my hair, I've managed to figure this out.
Finally, three PWM outputs!
Here is a Random fading RGB LED, now finally fading also it's B from the Attiny85.
Based on the fade example sketch that comes with the Arduino.

/*
 Fade

 This example shows how to fade an LED on pin 9
 using the analogWrite() function.

 This example code is in the public domain.
 */

int ledR = 0;           // the pin that the LED is attached to
int ledG = 1;
int ledB = 4;

// the setup routine runs once when you press reset:
void setup()  { 
  // declare pin 9 to be an output:
  pinMode(ledR, OUTPUT);
  pinMode(ledG, OUTPUT);
  pinMode(ledB, OUTPUT);
  randomSeed(analogRead(0));
  //Serial.begin(115200);
  GTCCR |= 0b01010000;
  GTCCR &= 0b11011111;
} 

byte valueR = 0;
byte valueG = 0;
byte valueB = 0;
byte targetR = 0;
byte targetG = 0;
byte targetB = 0;

// the loop routine runs over and over again forever:
void loop()  { 
  // set the brightness of pin 9:
  analogWrite(ledR, valueR);
  analogWrite(ledG, valueG);
  //analogWrite(ledB, valueB);
  OCR1B = valueB;
  if (valueR == targetR && valueG == targetG && valueB == targetB) {
      //Serial.println("Change done.");
      //delay(30);
      targetR = (byte) random(255);
      targetG = (byte) random(255);
      targetB = (byte) random(255);

      /*
      Serial.print("0x");
      char hexR[3];
      char hexG[3];
      char hexB[3];
      sprintf(hexR, "%02x", targetR);
      sprintf(hexG, "%02x", targetG);
      sprintf(hexB, "%02x", targetB);
      Serial.print(hexR);
      Serial.print(hexG);
      Serial.print(hexB);
      Serial.println();
      */
  }
  if (valueR < targetR) {
      valueR += 1;
  } else if (valueR > targetR) {
      valueR -= 1;
  }
  if (valueG < targetG) {
      valueG += 1;
  } else if (valueG > targetG) {
      valueG -= 1;
  }
  if (valueB < targetB) {
      valueB += 1;
  } else if (valueB > targetB) {
      valueB -= 1;
  }

  // Adjust delay to change cycling speed
  delay(10);                            
}

I hope this saves the next poor soul coming along from a few hours of pulling their hair :)
Also please correct me if i missunderstood the GTCCR flags, but it seems to be working for me

@mrakuaku
Copy link

Thanks so much @oddstr13! Works great for me! and yes it saved me from going bald :) Which is the actual line that is activating PB4 as PWM? is it
" GTCCR |= 0b01010000;
GTCCR &= 0b11011111;"
or
"OCR1B = valueB;"
... or another one?

@oddstr13
Copy link

@mrakuaku the GTCCR activates PWM on the pin, OCR1B = value; is basicly analogWrite(ledB, value);

damellis added a commit that referenced this issue Aug 6, 2014
This fixes issue #36 (for Arduino 1.5.x at least). It uses
initVariant() to put Timer1 into PWM mode and a hacky #define alias
(TCCR1A for GTCCR) so that analogWrite() will set the right bit in the
right register to connect the timer PWM output to the pin. Also, the
pre-scale factor and PWM frequency are probably wrong (or, at least,
not the same as on other AVRs).
@damellis
Copy link
Owner

damellis commented Aug 6, 2014

I fixed this for Arduino 1.5.x with 4284860

@damellis damellis changed the title ATtinyx5: PWM on pin 3 ATtinyx5: PWM on pin 3 (Arduino 1.0.5) Aug 6, 2014
@mrakuaku
Copy link

@oddstr13, the red and green LEDs never seem to go on at the same time. Could this be because the PWM's are interfering with each other somehow? I tried another colour fading code, which I modified to include
"GTCCR |= 0b01010000;
GTCCR &= 0b11011111;"
and
"OCR1B = valueB;"
and noticed that they can fade in and out by them selves, and with the blue, but not with each other.
Do you have any suggestions on how to fix this?

Thanks

@damellis damellis changed the title ATtinyx5: PWM on pin 3 (Arduino 1.0.5) ATtinyx5: PWM on pin 3 (Arduino 1.0.6) Sep 30, 2014
@damellis
Copy link
Owner

I believe Arduino 1.0.6 adds support for initVariant(), so it should be possible to get the third PWM working there too (along with 1.5.x). Anyone want to take a shot at it?

@oddstr13
Copy link

oddstr13 commented Oct 1, 2014

@mrakuaku First off, let me appologize for my late reply.
Make sure you use individual resistors for each of the colors, not one on the common lead. Using just one resistor results in erratic behavior. This puzzled me for a while, and I should probably have mentioned that in the original message.

I should also note that I have not worked on this since my initial experiments a year ago.

damellis added a commit that referenced this issue Oct 15, 2014
This fixes issue #36 (for Arduino 1.5.x at least). It uses
initVariant() to put Timer1 into PWM mode and a hacky #define alias
(TCCR1A for GTCCR) so that analogWrite() will set the right bit in the
right register to connect the timer PWM output to the pin. Also, the
pre-scale factor and PWM frequency are probably wrong (or, at least,
not the same as on other AVRs).
@damellis
Copy link
Owner

Fixed for 1.0.6 too.

@damellis damellis changed the title ATtinyx5: PWM on pin 3 (Arduino 1.0.6) ATtinyx5: PWM on pin 4 (Arduino 1.0.6) Oct 15, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants