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
Comments
After a few hours of pulling my hair, I've managed to figure this out. /*
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 :) |
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 |
@mrakuaku the GTCCR activates PWM on the pin, OCR1B = value; is basicly analogWrite(ledB, value); |
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).
I fixed this for Arduino 1.5.x with 4284860 |
@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 Thanks |
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? |
@mrakuaku First off, let me appologize for my late reply. I should also note that I have not worked on this since my initial experiments a year ago. |
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).
Fixed for 1.0.6 too. |
I couldn't get
analogWrite(3, x)
to work properly, but settingGTCCR[6:4]
to 0x5 andOCR1B
tox
let it work.The text was updated successfully, but these errors were encountered: