PWM resolution

Hi,

I found here code for increasing PWM resolution to 10bit. It works. But I have one problem with it.

#define potPin 0
#define ledPin 9
#define delayTime 500

int potVal;
int i;

void setup()
{
/**********************************************************************************/
// Set pwm clock divider
/**********************************************************************************/ 
 TCCR1B &= ~(1 << CS12);
 TCCR1B  |=   (1 << CS11);
 TCCR1B &= ~(1 << CS10);  
                             

/**********************************************************************************/
// Set pwm resolution  to mode 7 (10 bit)
/**********************************************************************************/ 

TCCR1B &= ~(1 << WGM13);    // Timer B clear bit 4
TCCR1B |=  (1 << WGM12);    // set bit 3

TCCR1A |= (1 << WGM11);    //  Timer A set bit 1
TCCR1A |= (1 << WGM10);    //  set bit 0


pinMode(ledPin, OUTPUT); 
Serial.begin(9600);

}


void loop()
{
// potVal = analogRead(potPin);
for( int i = 250 ; i < 260 ; i++ ){
    analogWrite( ledPin, i );
    Serial.println(i); 
    delay( delayTime );
  }
}

I have LED connected on ledPin. Everything works fine, but when there is value 255 in i LED is fully on. So if I will connect also pot and turn it from 0 to max on position 255 LED will blink to full brightness.

How to solve this without solution like inserting this:
if i=255 than i=256(or254) ...
So without skiping this step.

Thanks.

if you look at the code of analogWrite() you can see what happens when you write 255 to the PWM pin.

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\wiring_analog.c

void analogWrite(uint8_t pin, int val)
{
	// We need to make sure the PWM output is enabled for those pins
	// that support it, as we turn it off when digitally reading or
	// writing with them.  Also, make sure the pin is in output mode
	// for consistenty with Wiring, which doesn't require a pinMode
	// call for the analog output pins.
	pinMode(pin, OUTPUT);
	if (val == 0)
	{
		digitalWrite(pin, LOW);
	}
	else if (val == 255)
	{
		digitalWrite(pin, HIGH);
	}
	else
...

Yes. I thought.

But How to solve it ? It is pointless 10bit DAC if it outputs 100% on this one step/spot.
Can I replace analogwrite somehow ?

Can I replace analogwrite somehow ?

Yes, of course you can

Thanks. You helped me alot.

But if you tell me also some ideas how to do it, it will help me much more.

Did you read reply #1?
It gave you a pathname, and the original source.

OK.

So I just need to comment this 4 lines in wiring_analog.c ?:

//	else if (val == 255)
//	{
//		digitalWrite(pin, HIGH);
//	}

So I just need to comment this 4 lines in wiring_analog.c ?:

What happened when you did? Can YOU really discern 1024 different brightnesses of an LED? I seriously doubt it.

Jerry19:
OK.

So I just need to comment this 4 lines in wiring_analog.c ?:

//	else if (val == 255)

// {
// digitalWrite(pin, HIGH);
// }

No. Look at the source again.

I did it and now LED do what I expect. And also full range sweep works fine without flash on 255 like before. 0 to 1023 no problem. It was no problem also before, problem was only 255 value, because on this value LED was on full brightness. Now 250, 255, 260 looks like cca 25% brightness. Like it should if 1023 is full.

so what did you do in the end? comment out the four lines? or change it it to 1023?