PWM

This section refers to the following sample application(s):

  • 02-pwm

This application is new to OCSDK 1.2.0

The 

LTC5800

 features a pulse width modulation (PWM) output that is available to your application. The PWM signal is characterized by two parameters: the period, specified in ns, and the duty cycle, expressed as the asserted (high) portion of the period in ns.   Once open, your application can enable and disable the PWM output as desired, and modify the duty cycle.  To change the period, the device must be closed and re-opened.

The PWM module keeps the CPU running when enabled, so it will draw ~ 500 uA in addition to any other circuit power.

The 02-pwm Sample Application

The application provides five CLI functions (available by typing help) for setting the state of pin PWM0 (GPIO 16):

  • open <period> <duty> - opens the DN_PWM_DEV_ID device (PWM0,  LTC5800 pin 49) with an initial period and duty cycle in % of period. The period can be selected from one of the defined glitch-free periods (1-11) or specified directly in units of ns. See dn_pwm.h for the list of glitch-free periods. The device must be opened before enabling/disabling the PWM. 
  • enable - enables the PWM signal output on PWM0.
  • disable - disables the PWM signal output on PWM0
  • set - sets the current duty cycle in %. 
  • close - closes the PWM device and disable output. The PWM must be closed before changing the period.

Each CLI command prints a confirmation or an error:

PWM app, ver 1.2.0.1
SmartMeshIP stack, ver 1.4.1.6

> open 5 50
Opening PWM with period=8888889 ns and duty cycle = 50% (4444444 ns) 

> enable
Enabling PWM

> set 40
Setting duty cycle to 40% (3555555 ns)

> open 4 50
Opening PWM failed RC=-8

> disable
Disabling PWM

> close
Closing PWM

Important points:

  • Opening the PWM more than once will generate an error
  • Closing the PWM more than once will generate an error
  • Setting the duty cycle >= the period will generate an error
  • Disabling the PWM will leave it in an arbitrary state depending on where the PWM was in the cycle. Thus it is possible to disable a PWM and close it, and still leave the pin asserted (high).  To ensure that the PWM output is de-asserted, first set the duty cycle to 0, then disable and close it. 
  • You can change the duty cycle after the PWM has been opened and can change it whether it is currently enabled or disabled.  

Implementation Details

The 02-pwm sample application uses the Helper Modules to initialize the CLI Module and for interaction with the local interface. It passes JOIN_NO in the fJoin flag when initializing the local module in the loc_task_init() to prevent the local module from attempting to join. It does not start any application tasks - it relies solely on CLI commands to drive the PWM to demonstrate its features.

While the sample expresses the duty cycle as % of period, the underlying driver expects the value to be expressed as the on portion of the period, in ns.

While the period can be set to any value, there is an enumerated list of guaranteed glitch-free periods. When using other periods, enabling the PWM may produce a single cycle of the pin being asserted for an arbitrary portion of the period. For this reason, it is recommended to use one of the periods defined in dnm_pwm.h.

An Exercise for the Reader

  • Add a task to the sample that opens the PWM device following the example in the open handler.
  • In the task's while(1) loop, slowly ramp the duty cycle up and down.  
  • Jumper PWM0 to DP3 and check if the blue INDICATOR_0 LED behaves as you'd expect it to.