Skip to content

Fix for sequential PWM pin setting #229

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

Merged
merged 1 commit into from
Jul 14, 2020
Merged

Conversation

nseidle
Copy link
Member

@nseidle nseidle commented Jul 7, 2020

Fix for issue #192

What was happening in the original code was pin would be set correctly with an analogWrite(). When the user went to set the 2nd pin, the timer # would be the same and the code would wait for a roll over, even though the segments were different. This would never happen and we hit an infinite loop.

This PR changes it so the roll over is only checked if the user is requesting an analogWrite() to a pin who's timer and segment are active. If the segments don't match, don't wait for a rollover.

I've successfully tested on the 30 PWM accessible pins of an ATP. My test sketch is below.

const int numberOfPWMPins = 32;

int pwmPin[numberOfPWMPins] = {
4,  5,  6,  7,  11,
12, 13, 18, 19, 22,
23, 24, 25, 26, 27,
28, 29, 30, 31, 32, //30 is not routed on Artemis module
33, 35, 37, 39, 42,
43, 44, 45, 46, 47, //46 is not routed on Artemis module
48, 49, //Once 48 is set to PWM output, serial output will fail
};

void setup()
{
Serial.begin(115200);

while (!Serial);//Wait for user to open terminal window
Serial.println("SparkFun Arduino Apollo3 PWM Test");

//Step through each PWM pin assigning a different analog voltage
#define ANALOG_OUT_RESOLUTION 8
int pwmMaxValue = pow(2, ANALOG_OUT_RESOLUTION);

float startValue = 0.1;
float endValue = 3.2;
float stepValue = (endValue - startValue) / numberOfPWMPins;

for (int x = 0 ; x < numberOfPWMPins ; x++)
{
    int pinNumber = pwmPin[x];
    float outputVoltage = startValue + (stepValue * x);
    int pwmValue = outputVoltage * pwmMaxValue / 3.3;
    analogWrite(pinNumber, pwmValue);

    Serial.print(pinNumber);
    Serial.print(" now outputting: ");
    Serial.print(outputVoltage, 2);
    Serial.println("V");
    Serial.flush();
}
}

void loop()
{

}

@oclyke oclyke changed the base branch from master to release-candidate July 14, 2020 19:43
@oclyke oclyke self-requested a review July 14, 2020 19:43
Copy link
Contributor

@oclyke oclyke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took me a good squint to sort through those parentheses but this is exactly how I'd have liked to see it done. Thanks Nate!

@oclyke oclyke merged commit 4d4db9c into release-candidate Jul 14, 2020
@oclyke oclyke deleted the pwmSegmentFix branch July 14, 2020 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants