/
SetPwmOutput()

SetPwmOutput()

Purpose

Sets a PWM Pin to a desired value.

When to use

Every time you want the value of a PWM pin to be modified.

Where to use

Most probably in the void loop() part of your .ino file. Can also be used at the beginning of your code execution, in the void setup() part of your .ino file, so that the pin is in a known state on startup.

Click here to learn more on the PWM outputs of the 9880 board. It explains in particular why PWM port #1 to #4 can only be used with power motor controller.

This function requires the use of the following functions at some point of your .ino file in order to work properly:

Returns

This function does not return a value once it has completed its tasks.

Main prototype and parameters

static void CrcLib::SetPwmOutput(unsigned char pin, char value)

The following parameters must be passed to the function for it to work properly:

  • pin: The name of the PWM pin you want to set. Must be of the type unsigned char. Must have been initialized using InitializePwmOutput() beforehand.

  • value: The value you want to set the pin to. Must be of the type signed char, so in the range  [-128, 127].

    • Setting a pin to -128 will have the pin generating a pulse equal to the minimum pulse width previously set by InitializePwmOutput().

    • Setting a pin to 127 will have the pin generating a pulse equal to the maximum pulse width previously set by InitializePwmOutput().

    • Setting a pin to other values will have the pin generating a pulse mapped between the minimum and maximum pulse width previously set by InitializePwmOutput().

Overloads

This function does not have any overloads. It can only be used as described by the main prototype.

Examples

#include <CrcLib.h> void setup() { CrcLib::Initialize(); CrcLib::InitializePwmOutput(CRC_PWM_5); CrcLib::SetPwmOutput(CRC_PWM_5,127); /* The rest of your setup code ... */ } void loop() { CrcLib::Update(); /* The rest of your looping code ... */ }
#include <CrcLib.h> signed char joystickValue; void setup() { CrcLib::Initialize(); CrcLib::InitializePwmOutput(CRC_PWM_12, 500, 2500); /* The rest of your setup code ... */ } void loop() { CrcLib::Update(); joystickValue = CrcLib::ReadAnalogChannel(ANALOG::JOYSTICK1_X); CrcLib::SetPwmOutput(CRC_PWM_12, joystickValue ); /* The rest of your looping code ... */ }

 

More on this function

Can I use the native Arduino function analogWrite() to control the PWM outputs ?

You could, but we strongly recommend against it if you don’t know what you are doing and don’t have a precise reason to do so. The analogWrite() native Arduino function controls PWM pins using the “normal” definition of PWM. Although PWM pins on the CrcDuino are PWM capable pins, they are used as Servo PWM pins by CrcLib, since the CRC Robotics Competition robots mostly use hobby type standard servos, continuous servo and PWM compatible power motor controllers. For more info on this, read this.

Related articles

Related content

InitializePwmOutput()
InitializePwmOutput()
More like this
GetAnalogInput()
GetAnalogInput()
More like this
CrcDuino Hardware Details
CrcDuino Hardware Details
Read with this
SetPwmOutput() {FR}
SetPwmOutput() {FR}
More like this
CrcConnect Module Details
CrcConnect Module Details
Read with this
SetDigitalOutput()
SetDigitalOutput()
More like this