Pages: [1]   Go Down
Author Topic: >8bit pwm on leonardo  (Read 695 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I'm trying to archive a pwm resolution >8bit (10bit or 16bit) on a Leonardo.
I read that the leonardo Timer 1 has 16bit resolution and is used for pine 9 and 10.

After reading through the http://arduino.cc/en/Tutorial/SecretsOfArduinoPWM i know i should set
OCR1A to the desired level (1023 for 10bit resolution).

But wont this affect the frequency aswell?
The timer has to count till 1023 now instead of 255, so the pwm frequency will be 1/4 of the original value, correct?

So what do i have to change to only change the pwm resolution?
On the secrets of pwm page, they provide some code examples.
Adapted the 'Fade' example to this
Code:

int led = 9;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 1;    // how many points to fade the LED by

//Pin 13 , 8, 3
//Timer 1, 3, 4

void setup(void)
{
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11) | _BV(WGM10);
  TCCR1B = _BV(CS12);
  OCR1A = 1023;

}

void loop()
{
 // set the brightness of pin 9:
  analogWrite(9, brightness);   

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 1023) {
    fadeAmount = -fadeAmount ;
  }     
  // wait for 30 milliseconds to see the dimming effect   
  delay(100);     
}

The fading seems to work, but the LED on pin 9.
But, the LED flikkers while fading. It seems there are some duty cycles missing in the pwm so the led turns off in between.
(or the frequency is so low it can be seen).

Any help on what needs to be changed?

Thanks!
Logged

Phillipsburg, NJ
Offline Offline
Full Member
***
Karma: 6
Posts: 184
Author: Matrix Keypad Library
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But wont this affect the frequency aswell?
The timer has to count till 1023 now instead of 255, so the pwm frequency will be 1/4 of the original value, correct?
I'll admit to not doing my howework on setting up Arduino's pwm hardware but this http://playground.arduino.cc/Code/PwmFrequency looks like it explains about resetting the pwm frequency.  There seems to be side effects with delay(), millis(), and micros() so be sure this is what you want to do.

Logged

Montreal
Offline Offline
Faraday Member
**
Karma: 30
Posts: 2602
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 Time to visit atmel.com, than dig into datasheet  smiley.
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your answer,
consulting the PWM frequency link you provided, i put
Code:
TCCR1B = _BV(CS11);
which solves the flickering by increasing the pwm frequency
(in case anyone else has this issue).

I stumbled upon another issue, it seems that the led is turned fully on at value 255.
I read somewhere that the analogwrite function cheats a little by turning a value of 0 into a digitalOut(LOW).
So i guess this is the same for a value of 255 becoming a HIGH?
And i bet the only way to remove this is editing the .h file where analogWrite is in, correct?

example:
Code:
void setup(void)
{
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11) | _BV(WGM10);
  TCCR1B = _BV(CS11);
  OCR1A = 1023;

}

void loop()
{
 // set the brightness of pin 9:
  analogWrite(9, 254);   
  delay(2000);     
  analogWrite(9, 255);   
  delay(2000);     
  analogWrite(9, 256);   
  delay(2000);     
}
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

solved: editing

Code:
pinMode(pin, OUTPUT);
if (val == 0)
{
digitalWrite(pin, LOW);
}
else if (val == 255)
{
digitalWrite(pin, HIGH);
}

in wiring_analog.c is needed.
Logged

Pages: [1]   Go Up
Jump to: