PWM generate

 1'''
 2PWM - PWM_generate.py with synchronous mode.
 3
 4This example demonstrates how to generate PWM with USBDAQF1AOD.
 5
 6First, you should set frequency and duty cycle so that it can generate proper signal.
 7By the way, if you want to check PWM signal, you could connect DI pin with PWM pin.
 8
 9-------------------------------------------------------------------------------------
10Please change correct serial number or IP and port number BEFORE you run example code.
11
12For other examples please check:
13    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
14See README.md file to get detailed usage of this example.
15
16Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
17'''
18
19## WPC
20from wpcsys import pywpc
21
22## Python
23import time
24
25
26def main():
27    ## Get Python driver version
28    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
29
30    ## Create device handle
31    dev = pywpc.USBDAQF1AOD()
32
33    ## Connect to device
34    try:
35        dev.connect("default")  ## Depend on your device
36    except Exception as err:
37        pywpc.printGenericError(err)
38        ## Release device handle
39        dev.close()
40        return
41
42    try:
43        ## Parameters setting
44        channel = 0  ## Depend on your device
45        frequency = 100
46        duty_cycle = 50
47        timeout = 3  ## [sec]
48
49        ## Get firmware model & version
50        driver_info = dev.Sys_getDriverInfo(timeout)
51        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
52
53        ## Open PWM
54        err = dev.PWM_open(channel, timeout)
55        print(f"PWM_open in channel {channel}, status: {err}")
56
57        ## Set frequency
58        err = dev.PWM_setFrequency(channel, frequency, timeout)
59        print(f"PWM_setFrequency in channel {channel}, status: {err}")
60
61        ## Set duty cycle
62        err = dev.PWM_setDutyCycle(channel, duty_cycle, timeout)
63        print(f"PWM_setDutyCycle in channel {channel}, status: {err}")
64
65        ## Start PWM
66        err = dev.PWM_start(channel, timeout)
67        print(f"PWM_start in channel {channel}, status: {err}")
68
69        ## Wait for seconds for generating signal
70        time.sleep(5)  ## delay [sec]
71
72        ## Stop PWM
73        err = dev.PWM_stop(channel, timeout)
74        print(f"PWM_stop in channel {channel}, status: {err}")
75
76        ## Close PWM
77        err = dev.PWM_close(channel, timeout)
78        print(f"PWM_close in channel {channel}, status: {err}")
79    except Exception as err:
80        pywpc.printGenericError(err)
81
82    finally:
83        ## Disconnect device
84        dev.disconnect()
85
86        ## Release device handle
87        dev.close()
88
89
90if __name__ == '__main__':
91    main()