The current code is :
What does this code do ?
If the val is 0 it switches the line to LOW.
If the val is max it switches the line to HIGH.
But the calulation of max is wrong.
max is 256 for 8 bit and 4096 for 12 bit resolution.
Correct would be:
because the highest value for 8 bit is 255 and for 12 bit it is 4095.
______________________
If you study the equivalent Arduino code you see that it is implemented correctly in wiring_analog.c
Code:
void analogWrite(uint8_t pin, int val)
{
.....
.....
max = 1 << analog_write_res;
if (val <= 0) {
digitalWrite(pin, LOW);
pinMode(pin, OUTPUT); // TODO: implement OUTPUT_LOW
return;
} else if (val >= max) {
digitalWrite(pin, HIGH);
pinMode(pin, OUTPUT); // TODO: implement OUTPUT_HIGH
return;
}
....
}
What does this code do ?
If the val is 0 it switches the line to LOW.
If the val is max it switches the line to HIGH.
But the calulation of max is wrong.
max is 256 for 8 bit and 4096 for 12 bit resolution.
Correct would be:
Code:
max = (1 << analog_write_res) - 1;
because the highest value for 8 bit is 255 and for 12 bit it is 4095.
______________________
If you study the equivalent Arduino code you see that it is implemented correctly in wiring_analog.c
Code:
if (val == 0)
{
digitalWrite(pin, LOW);
}
else if (val == 255)
{
digitalWrite(pin, HIGH);
}
else
{
......
Last edited: