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: 
Голосов пока нет