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.
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.
Selecting a duty cycle 30% would produce 0.99V signal:
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.
To start using the PWM Interface, you need to configure and activate the PWM port:
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.
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.
Enable the PWM port. When the PWM port is enabled, you can change configuration for each channel but you cannot activate or release channels.
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.
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.
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.
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.
#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.
This section describes the PWM Interface functions. They are used to control and monitor the PWM module of a DLN-series adapter.
Retrieves the number of ports that can be assigned to the PWM module.
Assigns a port to the PWM module.
Unassigns a port to the PWM module.
Retrieves whether a port is assigned to the PWM module.
Retrieves the number of channels available to the PWM port.
Activates a channel of the PWM port.
Releases a channel of the PWM port.
Retrieves whether a channel is activated.
Configures the duty cycle value for the specified PWM port.
Retrieves the current duty cycle value for the specified PWM port.
Configures the frequency value for the specified PWM port.
Retrieves the current frequency value for the specified PWM port.
Retrieves the maximum frequency value for the specified PWM port.
Retrieves the minimum frequency value for the specified PWM port.
The dln_pwm.h
file declares the PWM Interface functions.
The DlnPwmGetPortCount()
function retrieves the number of PWM ports available in your DLN-series adapter.
The DlnPwmEnable()
function activates the corresponding PWM port of your DLN-series adapter.
The DlnPwmDisable()
function releases the specified PWM port of your DLN-series adapter.
The DlnPwmIsEnabled()
function retrieves information, whether the specified port is assigned to the PWM module.
The DlnPwmGetChannelCount()
function retrieves the number of PWM channels available in the specified PWM-port of your DLN-series adapter.
The DlnPwmChannelEnable()
function activates the specified channel from the corresponding PWM port of your DLN-series adapter.
The DlnPwmChannelDisable()
function releases the specified channel from the corresponding PWM port of your DLN-series adapter.
The DlnPwmChannelIsEnabled()
retrieves information, whether the specified PWM channel is activated.
The DlnPwmSetDutyCycle()
function defines a PWM duty cycle value, which is the ratio of the high time to the PWM period.
The DlnPwmGetDutyCycle()
function retrieves the current PWM duty cycle value.
The DlnPwmSetFrequency()
function defines the PWM frequency.
The DlnPwmGetFrequency()
function retrieves the current PWM frequency.
The DlnPwmGetMaxFrequency()
function retrieves the maximum possible value of the PWM frequency.
The DlnPwmGetMinFrequency()
function retrieves the minimum possible value of the PWM frequency.