PWM (Pulse Width Modulation) Interface

Pulse-width modulation (PWM) is the technique that allows to produce variable analog signals using digital means. The average value of the output signal is controlled by switching the digital signal between HIGH (1) and LOW (0) states at a fast rate. The longer the digital signal is in the HIGH state compared to LOW state periods, the higher the output analog signal voltage.

PWM signals are used in a wide variety of control applications. You can use them to control the power supplied to electrical devices like DC motors, valves, pumps, hydraulics and other mechanical parts.

Rating: 
Средняя: 5 (2 оценок)

How PWM Works

A DLN-series adapter generates digital pulses with a certain frequency. Each cycle includes the signal in a HIGH (1) and the following LOW (0) states.

The amount of time the signal is in a HIGH state as a percentage of the total time of one cycle describes the duty cycle.

A duty cycle and a frequency are two main parameters that define a PWM signal.

By cycling a digital signal HIGH and LOW at a fast rate and with a certain duty cycle, the PWM output behaves like a constant voltage analog signal when providing power to devices.

Having a DLN adapter that can generate a digital signal either HIGH (3.3V) or LOW (0V), you can create a 2.3V signal with a PWM module specifying a duty cycle as 61%. The PWM module outputs 3.3V 70% of the time. If the PWM frequency is fast enough, then the voltage seen at the output appears to be average voltage and can be calculated by taking the digital high voltage multiplied by the duty cycle: 3.3V x 0.7 = 2.31V.

PWM 61%

Selecting a duty cycle 30% would produce 0.99V signal:

PWM 30%

The PWM Interface is present in all DLN-series adapters. Some DLN adapters can have more than one PWM ports. Each PWM port includes several channels (Read PWM Channels). The number of ports and channels depends on the type of your DLN adapter.

Rating: 
Средняя: 4 (1 оценка)

Configuring PWM Interface

To start using the PWM Interface, you need to configure and activate the PWM port:

  1. For each channel that you suppose to use:

    • Configure PWM frequency. The higher frequency, the smoother the output signal becomes. Read PWM Frequency.

    • Configure the duty cycle. The level of the output signal depends on the duty cycle value. Read PWM Duty Cycle.

  2. Enable all channels that you suppose to use. All channels are equal, but every channel can have specific parameters (frequency and duty cycle). Read PWM Channels.

  3. Enable the PWM port. When the PWM port is enabled, you can change configuration for each channel but you cannot activate or release channels.

Rating: 
Средняя: 5 (1 оценка)

PWM Frequency

Your DLN adapter outputs a PWM signal that must be accepted by the device receiving it. The device connected to the DLN adapter requires a particular frequency of the PWM signal.

You can specify the frequency of the PWM calling the DlnPwmSetFrequency() function. The frequency value should be within the range supported by your DLN adapter. To check the maximum and minimum PWM frequency values possible for DLN adapters, use the DlnPwmGetMaxFrequency() and DlnPwmGetMinFrequency() functions. If you enter an incompatible value, the DLN adapter approximates the frequency to the closest lower value supported by the DLN adapter.

The default frequency value is set to 1000Hz. To check the current frequency, use the DlnPwmGetFrequency() function.

You can specify individual frequency for each PWM channel.

Rating: 
Голосов пока нет

PWM Duty Cycle

The duty cycle describes the pulse width as a percentage of the period. By switching the signal level with the specified duty cycle, the output signal will be approximated to the desired level. The 100% duty cycle means that the output level is HIGH. The 0% duty cycle means that the output level is LOW. The 50% duty cycle means that the output level is in the middle between HIGH and LOW.

You can define the duty cycle of the PWM calling the DlnPwmSetDutyCycle() function. To check the current duty cycle, use the DlnPwmGetDutyCycle() functions.

The default duty cycle value depends on the DLN adapter.

Rating: 
Голосов пока нет

PWM Channels

Each DLN adapter has several PWM channels. All channels are independent from each other and have similar functionality. You can define frequency and duty cycle for each channel separately.

To know the number of available PWM channels for a specified PWM port, use the DlnPwmGetChannelCount() function.

To activate the specified channel, call the DlnPwmChannelEnable() function. You can change frequency and duty cycle values regardless whether the channel is activated or not.

To release the channel, use the DlnPwmChannelDisable() function.

You cannot activate or release the channel if the appropriate PWM port is enabled. To disable the active port, use the DlnPwmDisable() function. To enable the PWM port, use the DlnPwmEnable() function.

Rating: 
Голосов пока нет

Simple PWM Module Example

The following application shows how to perform an output analog signal using the PWM module. For brevity, this application does not include error detection. You can find the complete example in the “..\Program Files\Diolan\DLN\examples\c_cpp\examples\simple” folder after DLN setup package installation.

C/C++
#include "..\..\..\common\dln_generic.h"
#include "..\..\..\common\dln_pwm.h"
#pragma comment(lib, "..\\..\\..\\bin\\dln.lib")

int _tmain(int argc, _TCHAR* argv[])
{
	// Open device
	HDLN device;
	DlnOpenUsbDevice(&device);

	// Set PWM duty cycle
	double duty;
	DlnPwmSetDutyCycle(device, 0, 0, 50.0, &duty);

	// Set PWM frequency
	uint32_t frequency;
	DlnPwmSetFrequency(device, 0, 0, 1000, &frequency);

	// Enable PWM channel
	DlnPwmChannelEnable(device, 0, 0);
	// Enable PWM port
	uint16_t conflict;
	DlnPwmEnable(device, 0, &conflict);

	// Wait
	getchar();

	// Disable PWM port
	DlnPwmDisable(device, 0);
	// Disable PWM channel
	DlnPwmChannelDisable(device, 0, 0);
	// Close device
	DlnCloseHandle(device);
	return 0;
}

  • Line 1:#include "..\..\..\common\dln_generic.h"

    The dln_generic..h header file declares functions and data structures for the generic interface. In current example this header is used to call DlnOpenUsbDevice() and DlnCloseHandle() functions.

  • Line 2:#include "..\..\..\common\dln_pwm.h"

    The dln_pwm.h header file declares functions and data structures for the PWM interface. In current example this header is used to call DlnPwmSetDutyCycle(), DlnPwmSetFrequency(), DlnPwmChannelEnable(), DlnPwmEnable(), DlnPwmDisable(), DlnPwmChannelDisable() functions.

  • Line 3:#pragma comment(lib, "..\\..\\..\\bin\\dln.lib")

    Use dln.lib library while project linking.

  • Line 9:DlnOpenUsbDevice(&device);

    The application establishes the connection with the DLN adapter. This application uses the USB connectivity of the adapter. For additional options, refer to the Device Opening & Identification section

  • Line 13: DlnPwmSetDutyCycle(device, 0, 0, 50.0, &duty);

    The application configures the duty cycle of the PWM port 0 channel 0. A duty cycle is the percentage of one period in which a signal is active. You can read more about duty cycle parameter at PWM Duty Cycle section.

  • Line 17: DlnPwmSetFrequency(device, 0, 0, 1000, &frequency);

    The application configures the frequency of the PWM port 0 channel 0. The frequency influences the smoothness of the output signal.

  • Line 20:DlnPwmChannelEnable(device, 0, 0);

    The application enables the channel 0 of the PWM port 0. PWM ports can have several channels, each channel can have different configuration. See PWM Channels for details.

  • Line 23:DlnPwmEnable(device, 0, &conflict);

    The application enables the PWM port 0. The DlnPwmEnable() function assigns the corresponding pins to the SPI master module and configures them. If some other module uses a pin required for the SPI bus interface, the DlnPwmEnable() function returns the DLN_RES_PIN_IN_USE error code. The conflictPin parameter receives the pin’s number.

  • Line 26:getchar();

    Wait for pressing “Enter” button. The PWM signal will be active before you press “Enter” button.

  • Line 29: DlnPwmDisable(device, 0);

    The application releases (disables) the PWM port.

  • Line 31: DlnPwmChannelDisable(device, 0, 0);

    The application releases the PWM channel 0 of PWM port 0. The channel cannot be released while the PWM port is enabled.

  • Line 33: DlnCloseHandle(device);

    The application closes handle to the DLN adapter.

Rating: 
Голосов пока нет

PWM Functions

This section describes the PWM Interface functions. They are used to control and monitor the PWM module of a DLN-series adapter.

General information:

DlnPwmGetPortCount()

Retrieves the number of ports that can be assigned to the PWM module.

DlnPwmEnable()

Assigns a port to the PWM module.

DlnPwmDisable()

Unassigns a port to the PWM module.

DlnPwmIsEnabled()

Retrieves whether a port is assigned to the PWM module.

DlnPwmGetChannelCount()

Retrieves the number of channels available to the PWM port.

DlnPwmChannelEnable()

Activates a channel of the PWM port.

DlnPwmChannelDisable()

Releases a channel of the PWM port.

DlnPwmChannelIsEnabled()

Retrieves whether a channel is activated.

Configuration functions:

DlnPwmSetDutyCycle()

Configures the duty cycle value for the specified PWM port.

DlnPwmGetDutyCycle()

Retrieves the current duty cycle value for the specified PWM port.

DlnPwmSetFrequency()

Configures the frequency value for the specified PWM port.

DlnPwmGetFrequency()

Retrieves the current frequency value for the specified PWM port.

DlnPwmGetMaxFrequency()

Retrieves the maximum frequency value for the specified PWM port.

DlnPwmGetMinFrequency()

Retrieves the minimum frequency value for the specified PWM port.

The dln_pwm.h file declares the PWM Interface functions.

Rating: 
Голосов пока нет

DlnPwmGetPortCount() Function

The DlnPwmGetPortCount() function retrieves the number of PWM ports available in your DLN-series adapter.

Syntax
C/C++
DLN_RESULT DlnPwmGetPortCount(
    HDLN handle,
    uint8_t* count
);
Parameters
handle

A handle to the DLN-series adapter.

count

A pointer to an unsigned 8-bit integer that receives the number of available PWM ports.

Return Value
DLN_RES_SUCCESS (0x00)

The function successfully retrieved the number of PWM ports.

Remarks

The DlnPwmGetPortCount() function is defined in the dln_pwm.h file.

DlnPwmEnable() Function

The DlnPwmEnable() function activates the corresponding PWM port of your DLN-series adapter.

Syntax
C/C++
DLN_RESULT DlnPwmEnable(
    HDLN handle, 
    uint8_t port,
    uint16_t* conflict
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the PWM port.

conflict

A pointer to an unsigned 16-bit integer that receives the number of the conflicted pin, if any.A conflict arises if a pin is already assigned to another module of the DLN-series adapter and cannot be used for the PWM module. To fix this, check which module uses the pin (call the DlnGetPinCfg()function), disconnect the pin from that module and call the DlnPwmEnable() function once again. In case there still are conflicted pins, only the number of the next one will be returned.

Return Value
DLN_RES_SUCCESS (0x00)

The function successfully activated the PWM port.

DLN_RES_INVALID_PORT_NUMBER (0xA8)

The port number is not valid. Use the DlnPwmGetPortCount() function to find the maximum possible port number.

DLN_RES_PIN_IN_USE (0xA5)

The port cannot be activated as the PWM port because one or more pins of the port are assigned to another module. The conflict parameter contains the number of a conflicting pin.

Remarks

The DlnPwmEnable() function is defined in the dln_pwm.h file.

DlnPwmDisable() Function

The DlnPwmDisable() function releases the specified PWM port of your DLN-series adapter.

Syntax
C/C++
DLN_RESULT DlnPwmDisable(
    HDLN handle,
    uint8_t port
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the PWM port.

Return Value
DLN_RES_SUCCESS (0x00)

The function successfully released the PWM port.

DLN_RES_INVALID_PORT_NUMBER (0xA8)

The port number is not valid. Use the DlnPwmGetPortCount() function to find the maximum possible port number.

Remarks

The DlnPwmDisable() function is defined in the dln_pwm.h file.

DlnPwmIsEnabled() Function

The DlnPwmIsEnabled() function retrieves information, whether the specified port is assigned to the PWM module.

Syntax
C/C++
DLN_RESULT DlnPwmIsEnabled(
    HDLN handle,
    uint8_t port,
    uint8_t* enabled
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the PWM port.

enabled

A pointer to an unsigned 8-bit integer. The integer will be filled with the information whether the specified PWM port is activated. There are two possible values:

  • 0 or DLN_PWM_DISABLED - PWM port is deactivated.

  • 1 or DLN_PWM_ENABLED - PWM port is activated.

Return Value
DLN_RES_SUCCESS (0x00)

The function successfully retrieved the PWM port state.

DLN_RES_INVALID_PORT_NUMBER (0xA8)

The port number is not valid. Use the DlnPwmGetPortCount() function to find the maximum possible port number.

Remarks

The DlnPwmIsEnabled() function is defined in the dln_pwm.h file.

DlnPwmGetChannelCount() Function

The DlnPwmGetChannelCount() function retrieves the number of PWM channels available in the specified PWM-port of your DLN-series adapter.

Syntax
C/C++
DLN_RESULT DlnPwmGetChannelCount(
    HDLN handle,
    uint8_t port,
    uint8_t* count
);
Parameters
handle

A handle to the DLN-series adapter.

port

A PWM port to retrieve the number of channels from.

count

A pointer to an unsigned 8-bit integer that receives the available number of channels in the specified PWM port of the DLN-series adapter.

Return Value
DLN_RES_SUCCESS (0x00)

The function successfully retrieved the number of channels.

DLN_RES_INVALID_PORT_NUMBER (0xA8)

The port number is not valid. Use the DlnPwmGetPortCount() function to find the maximum possible port number.

Remarks

The DlnPwmGetChannelCount() function is defined in the dln_pwm.h file.

DlnPwmChannelEnable() Function

The DlnPwmChannelEnable() function activates the specified channel from the corresponding PWM port of your DLN-series adapter.

Syntax
C/C++
DLN_RESULT DlnPwmChannelEnable(
    HDLN handle,
    uint8_t port,
    uint8_t channel
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the PWM port.

channel

A number of the channel.

Return Value
DLN_RES_SUCCESS (0x00)

The function successfully activated the channel.

DLN_RES_INVALID_PORT_NUMBER (0xA8)

The port number is not valid. Use the DlnPwmGetPortCount() function to find the maximum possible port number.

DLN_RES_INVALID_CHANNEL_NUMBER (0xC0)

The channel number is not valid. Use the DlnPwmGetChannelCount() function to find the maximum possible port number.

Remarks

The DlnPwmChannelEnable() function is defined in the dln_pwm.h file.

DlnPwmChannelDisable() Function

The DlnPwmChannelDisable() function releases the specified channel from the corresponding PWM port of your DLN-series adapter.

Syntax
C/C++
DLN_RESULT DlnPwmChannelDisable(
    HDLN handle,
    uint8_t port,
    uint8_t channel
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the PWM port.

channel

A number of the channel.

Return Value
DLN_RES_SUCCESS (0x00)

The function successfully released the channel.

DLN_RES_INVALID_PORT_NUMBER (0xA8)

The port number is not valid. Use the DlnPwmGetPortCount() function to find the maximum possible port number.

DLN_RES_INVALID_CHANNEL_NUMBER (0xC0)

The channel number is not valid. Use the DlnPwmGetChannelCount() function to find the maximum possible port number.

Remarks

The DlnPwmChannelDisable() function is defined in the dln_pwm.h file.

DlnPwmChannelIsEnabled() Function

The DlnPwmChannelIsEnabled() retrieves information, whether the specified PWM channel is activated.

Syntax
C/C++
DLN_RESULT DlnPwmChannelIsEnabled(
    HDLN handle, 
    uint8_t port,
    uint8_t channel,
    uint8_t* enabled
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the PWM port.

channel

A number of the PWM channel.

enabled

A pointer to an unsigned 8-bit integer that receives the information about the specified PWM channel. There are two possible values:

  • 0 or DLN_PWM_DISABLED - The PWM channel is released.

  • 1 or DLN_PWM_ENABLED - The PWM channel is activated.

Return Value
DLN_RES_SUCCESS (0x00)

The function successfully retrieved information about the channel.

DLN_RES_INVALID_PORT_NUMBER (0xA8)

The port number is not valid. Use the DlnPwmGetPortCount() function to find the maximum possible port number.

DLN_RES_INVALID_CHANNEL_NUMBER (0xC0)

The channel number is not valid. Use the DlnPwmGetChannelCount() function to find the maximum possible port number.

Remarks

The DlnPwmChannelIsEnabled() function is defined in the dln_pwm.h file.

DlnPwmSetDutyCycle() Function

The DlnPwmSetDutyCycle() function defines a PWM duty cycle value, which is the ratio of the high time to the PWM period.

Syntax
C/C++
DLN_RESULT DlnPwmSetDutyCycle(
   HDLN handle, 
   uint8_t port, 
   uint8_t channel,
   double dutyCycle,
   double* actualDutyCycle
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the PWM port.

channel

A number of the PWM channel.

dutyCycle

A double precision floating point number. Must contain a duty cycle that should to be set. The value is specified in percents.

actualDutyCycle

A pointer to a double precision floating point number that receives the actual duty cycle value.

Return Value
DLN_RES_SUCCESS (0x00)

The function successfully defined the duty cycle value.

DLN_RES_INVALID_PORT_NUMBER (0xA8)

The port number is not valid. Use the DlnPwmGetPortCount() function to find the maximum possible port number.

DLN_RES_INVALID_CHANNEL_NUMBER (0xC0)

The channel number is not valid. Use the DlnPwmGetChannelCount() function to find the maximum possible port number.

DLN_RES_PWM_INVALID_DUTY_CYCLE (0xC4)

The provided value is not valid.

Remarks

The DlnPwmSetDutyCycle() function is defined in the dln_pwm.h file.

DlnPwmGetDutyCycle() Function

The DlnPwmGetDutyCycle() function retrieves the current PWM duty cycle value.

Syntax
C/C++
DLN_RESULT DlnPwmGetDutyCycle(
   HDLN handle, 
   uint8_t port,
   uint8_t channel,
   double* dutyCycle
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the PWM port.

channel

A number of the PWM channel.

dutyCycle

A pointer to the double precision floating point number that receives the current duty cycle.

Return Value
DLN_RES_SUCCESS (0x00)

The function successfully retrieved the current duty cycle value.

DLN_RES_INVALID_PORT_NUMBER (0xA8)

The port number is not valid. Use the DlnPwmGetPortCount() function to find the maximum possible port number.

DLN_RES_INVALID_CHANNEL_NUMBER (0xC0)

The channel number is not valid. Use the DlnPwmGetChannelCount() function to find the maximum possible port number.

Remarks

The DlnPwmGetDutyCycle() function is defined in the dln_pwm.h file.

DlnPwmSetFrequency() Function

The DlnPwmSetFrequency() function defines the PWM frequency.

Syntax
C/C++
DLN_RESULT DlnPwmSetFrequency(
   HDLN handle, 
   uint8_t port,
   uint8_t channel,
   uint32_t frequency,
   uint32_t* actualFrequency
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the PWM port.

channel

A number of the PWM channel.

frequency

The frequency value, specified in Hz. You may specify any value within the range, supported by the DLN-series adapter. This range can be retrieved using the respective function. If you enter an incompatible value, it will be approximated as the closest lower frequency value, supported by the adapter.

actualFrequency

A pointer to an unsigned 32-bit integer that receives the actually set frequency approximated as the closest to user-defined lower value.

Return Value
DLN_RES_SUCCESS (0x00)

The function successfully defined the frequency value.

DLN_RES_INVALID_PORT_NUMBER (0xA8)

The port number is not valid. Use the DlnPwmGetPortCount() function to find the maximum possible port number.

DLN_RES_INVALID_CHANNEL_NUMBER (0xC0)

The channel number is not valid. Use the DlnPwmGetChannelCount() function to find the maximum possible port number.

DLN_RES_VALUE_ROUNDED (0x21)

The function approximated the frequency value to the closest supported value.

Remarks

The DlnPwmSetFrequency() function is defined in the dln_pwm.h file.

DlnPwmGetFrequency() Function

The DlnPwmGetFrequency() function retrieves the current PWM frequency.

Syntax
C/C++
DLN_RESULT DlnPwmGetFrequency(
  HDLN handle, 
  uint8_t port,
  uint8_t channel,
  uint32_t* frequency,
);	
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the PWM port.

channel

A number of the PWM channel.

frequency

A pointer to an unsigned 32-bit integer that receives the current frequency setting for the DLN-series adapter.

Return Value
DLN_RES_SUCCESS (0x00)

The function successfully retrieved the current frequency value.

DLN_RES_INVALID_PORT_NUMBER (0xA8)

The port number is not valid. Use the DlnPwmGetPortCount() function to find the maximum possible port number.

DLN_RES_INVALID_CHANNEL_NUMBER (0xC0)

The channel number is not valid. Use the DlnPwmGetChannelCount() function to find the maximum possible port number.

Remarks

The DlnPwmGetFrequency() function is defined in the dln_pwm.h file.

DlnPwmGetMaxFrequency() Function

The DlnPwmGetMaxFrequency() function retrieves the maximum possible value of the PWM frequency.

Syntax
C/C++
DLN_RESULT DlnPwmGetMaxFrequency(
    HDLN handle, 
    uint8_t port, 
    uint32_t *frequency
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the PWM port.

frequency

A pointer to an unsigned 32-bit integer that receives the maximum frequency value for the DLN-series adapter.

Return Value
DLN_RES_SUCCESS (0x00)

The function successfully retrieved the maximum possible frequency value.

DLN_RES_INVALID_PORT_NUMBER (0xA8)

The port number is not valid. Use the DlnPwmGetPortCount() function to find the maximum possible port number.

DLN_RES_INVALID_CHANNEL_NUMBER (0xC0)

The channel number is not valid. Use the DlnPwmGetChannelCount() function to find the maximum possible port number.

Remarks

The DlnPwmGetMaxFrequency() function is defined in the dln_pwm.h file.

DlnPwmGetMinFrequency() Function

The DlnPwmGetMinFrequency() function retrieves the minimum possible value of the PWM frequency.

Syntax
C/C++
DLN_RESULT DlnPwmGetMinFrequency(
    HDLN handle, 
    uint8_t port, 
    uint32_t *frequency
);
Parameters
handle

A handle to the DLN-series adapter.

port

A number of the PWM port.

frequency

A pointer to an unsigned 32-bit integer that receives the minimum frequency value for the DLN-series adapter.

Return Value
DLN_RES_SUCCESS (0x00)

The function successfully retrieved the minimum possible frequency value.

DLN_RES_INVALID_PORT_NUMBER (0xA8)

The port number is not valid. Use the DlnPwmGetPortCount() function to find the maximum possible port number.

DLN_RES_INVALID_CHANNEL_NUMBER (0xC0)

The channel number is not valid. Use the DlnPwmGetChannelCount() function to find the maximum possible port number.

Remarks

The DlnPwmGetMinFrequency() function is defined in the dln_pwm.h file.