Hi Guys,
I posted a question a while ago, I wanted to know was there a way to create a better LED fade, because the current method of decreasing analogWrite(255) by 1 created noticeable steps in brightness, especially as the LED got dim.
I was told that it couldn't be done, the Arduino was an 8bit microcontroller and that the only way to achieve it was with a higher resolution board/microcontroller.
The other night I couldnt sleep, so went outside for a smoke, and while I was out there, started thinking of the problem again, I ran lots of things through my head, and decided I would do a Google search in the morning to look at software PWM, to see if I could get a better resolution that way.
Anyway, to cut a long story short, during my search the next day, there on the first page of results was this site - Arduino Slovakia - 16-bit PWM resolution for Arduino
I nearly fell off my chair, the Arduino CAN have 16bit PWM channels. I couldnt believe it, all those people on the Arduino forum lied to me!!!
I know there are forces on the Arduino Forum, who want this secret to remain hidden, they are strong and powerful and will do anything to stop this getting out, but I will not be stopped... FREEDOM.
Now that the truth is out there!! my question is, now that Pin 9 has been changed to 16bit, will I still be able to convert this to Fade Without delay() or will the change to 16bit have undesired consequences.
void setup() {
Serial.begin(9600);
setupPWM16();
}
uint16_t icr = 0xffff;
void loop() {
Serial.println("*");
//for (uint16_t i = 0; i < 8000; i++)
for (uint16_t i = 65535; i > 0; i=i-1)
{
analogWrite16(9, i);
//delay(1);
delayMicroseconds(300);
}
}
void setupPWM16() {
DDRB |= _BV(PB1) | _BV(PB2); /* set pins as outputs */
TCCR1A = _BV(COM1A1) | _BV(COM1B1) /* non-inverting PWM */
| _BV(WGM11); /* mode 14: fast PWM, TOP=ICR1 */
TCCR1B = _BV(WGM13) | _BV(WGM12)
| _BV(CS10); /* prescaler 1 */
ICR1 = icr; /* TOP counter value (freeing OCR1A*/
}
/* 16-bit version of analogWrite(). Works only on pins 9 and 10. */
void analogWrite16(uint8_t pin, uint16_t val)
{
switch (pin) {
case 9: OCR1A = val; break;
case 10: OCR1B = val; break;
}
}